libevent http post json 测试

测试性能才265.47 pps,严重怀疑自己测试有问题,先记录下来

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <event2/event.h>
#include <event2/buffer.h>
#include <event2/http.h>
#include <event2/http_struct.h>
#include <event2/keyvalq_struct.h>
#include <event2/listener.h>
#include <event2/util.h>
#include <event2/bufferevent.h>

static char *post_data = "{\n"
        "    \"dawn_ts0\": 1483468791574000,\n"
        "    \"guid\": \"31\",\n"
        "    \"device_id\": \"79bf7e53-d92f-5cdd-a7c3-3e9e97685c2c\",\n"
        "    \"probe\": {\n"
        "        \"name\": \"cloudsensor\"\n"
        "    },\n"
        "    \"appname\": \"cloudsensor\",\n"
        "    \"type\": \"tcp\",\n"
        "    \"kafka\": {\n"
        "        \"topic\": \"cloudsensor\"\n"
        "    },\n"
        "    \"aggregate_count\": 1,\n"
        "    \"tcp\": {\n"
        "        \"src_isp\": 0,\n"
        "        \"l4_proto\": 6,\n"
        "        \"out_bytes\": 0,\n"
        "        \"dst_port\": 3306,\n"
        "        \"retransmitted_out_fin_pkts\": 0,\n"
        "        \"client_latency_sec\": 0,\n"
        "        \"window_zero_size\": 0,\n"
        "        \"src_ipv4\": 178257969,\n"
        "        \"topic\": \"tcp\",\n"
        "        \"in_pkts\": 0,\n"
        "        \"src_region\": 0,\n"
        "        \"retransmitted_out_payload_pkts\": 0,\n"
        "        \"dst_ipv4\": 178808905,\n"
        "        \"ts\": 1483468791,\n"
        "        \"final_status\": 3,\n"
        "        \"retransmitted_in_syn_pkts\": 0,\n"
        "        \"src_ip\": 178257969,\n"
        "        \"server_latency_sec\": 0,\n"
        "        \"l4_protocol\": 6,\n"
        "        \"bytes_in\": 0,\n"
        "        \"src_port\": 57366,\n"
        "        \"retransmitted_out_syn_pkts\": 0,\n"
        "        \"retransmitted_in_ack_pkts\": 0,\n"
        "        \"out_pkts\": 0,\n"
        "        \"device_id\": \"79bf7e53-d92f-5cdd-a7c3-3e9e97685c2c\",\n"
        "        \"guid\": \"31\",\n"
        "        \"bytes_out\": 0,\n"
        "        \"retransmitted_in_payload_pkts\": 0,\n"
        "        \"dst_ip\": 178808905,\n"
        "        \"in_bytes\": 0,\n"
        "        \"retransmitted_out_ack_pkts\": 0,\n"
        "        \"server_latency_usec\": 3314,\n"
        "        \"client_latency_usec\": 3740006,\n"
        "        \"retransmitted_in_fin_pkts\": 0\n"
        "    },\n"
        "    \"probe_ts\": 1483468791,\n"
        "    \"dawn_ts1\": 1483468791574000,\n"
        "    \"topic\": \"cloudsensor\"\n"
        "}";

void http_request_done(struct evhttp_request *req, void *arg){
    char buf[1024];
    int s = evbuffer_remove(req->input_buffer, &buf, sizeof(buf) - 1);
    buf[s] = '\0';
//    printf("%s\n", buf);
    // terminate event_base_dispatch()
    event_base_loopbreak((struct event_base *)arg);
}

int main(int argc, char **argv)
{
    struct event_base *base;
    struct evhttp_connection *conn;
    struct evhttp_request *req;

    base = event_base_new();
    conn = evhttp_connection_base_new(base, NULL, "192.168.10.212", 9200);

    evhttp_connection_set_timeout(conn, 600);
    evhttp_connection_set_retries(conn, -1);

    int i = 0;
    struct timeval start, end;
    gettimeofday(&start, NULL);
    for (i = 0; i < 10000; i++) {

        req = evhttp_request_new(http_request_done, base);
        evhttp_add_header(req->output_headers, "Host", "192.168.10.212");

        evbuffer_add(req->output_buffer, post_data, strlen(post_data));
        evhttp_make_request(conn, req, EVHTTP_REQ_POST, "/cc-2017.01.17/test");

        event_base_dispatch(base);
    }
    gettimeofday(&end, NULL);

    long time_cost = ((end.tv_sec - start.tv_sec) * 1000000 + \
            end.tv_usec - start.tv_usec);
    printf("cost time: %ld us, %.2f pps\n", time_cost, 10000 / (time_cost * 1.0) * 1000000);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值