libhv http client vs cpr

libhv http client 和 cpr 的性能对比

libhv

  • test code
static void test_http_async(HttpClient* cli, int seq, int* resp_cnt) 
{
    auto req = std::make_shared<HttpRequest>();
    req->method = HTTP_GET;
    req->url = "www.baidu.com";
    req->timeout = 10;
    cli->sendAsync(req, [seq, resp_cnt](const HttpResponsePtr& resp) {
        std::string path = "async-result-" + std::to_string(seq) + ".log";
        std::ofstream fout(path.c_str());
        fout << "test_http_async_client response thread" << std::endl;
        fout << "tid=" << hv_gettid() << std::endl;
        
        if (resp == NULL) {
            fout << "request failed!" << std::endl;
        } else {
            fout << resp->status_code << "\t" <<  resp->status_message() << std::endl;
            // fout << resp->body << std::endl;
        }
        *resp_cnt += 1;
    });
}

static void test_http_sync(HttpClient* cli, int seq) 
{
    HttpRequest req;
    req.method = HTTP_POST;
    req.url = "www.baidu.com";
    req.timeout = 10;
    HttpResponse resp;
    int ret = cli->send(&req, &resp);
    std::string path = "sync-result-" + std::to_string(seq) + ".log";
    std::ofstream fout(path.c_str());
    fout << "test_http_sync_client response thread" << std::endl;
    if (ret != 0) 
    {
        fout << "request failed!" << std::endl;
    }
    else 
    {
        fout << resp.status_code << "\t" <<  resp.status_message() << std::endl;
        // fout << resp->body << std::endl;
    }
}

int main(int argc, char* argv[]) {
    int req_cnt = 0;
    if (argc > 1) req_cnt = atoi(argv[1]);
    if (req_cnt == 0) req_cnt = 1;

    HttpClient sync_client;
    HttpClient async_client;
    int resp_cnt = 0;

    long long beg = gettimeofday_ms();
    printf("test_http_async_client request thread tid=%ld\n", hv_gettid());
    for (int i = 0; i < req_cnt; ++i) {
        test_http_async(&async_client, i, &resp_cnt);
    }
    // demo wait async finished
    while (resp_cnt < req_cnt) hv_delay(10);
    long long used = gettimeofday_ms() - beg;
    std::cout << "async finished: " << used << std::endl;

    beg = gettimeofday_ms();
    printf("test_http_sync_client request thread tid=%ld\n", hv_gettid());
    for (int i = 0; i < req_cnt; ++i) {
        test_http_sync(&async_client, i);
    }

    used = gettimeofday_ms() - beg;
    std::cout << "sync finished: " << used << std::endl;

    return 0;
}
  • result
test_http_async_client request thread tid=104820
async finished: 3249ms
test_http_sync_client request thread tid=104820
sync finished: 80398ms

[wei@localmachine /home/wei/myworks/libhv]
$cat async-result-*.log | grep "tid=" | sort | uniq -c
   1000 tid=104823

cpr

  • test code

void test_http_async(int seq, int *resp_cnt, vector<std::shared_future<string> >& futures)
{
    auto callback = [seq, resp_cnt](cpr::Response r) 
    {
        std::string path = "async-result-" + std::to_string(seq) + ".log";
        std::ofstream fout(path.c_str());
        fout << "test async response thread" << std::endl;
        fout << "tid=" << std::this_thread::get_id() << std::endl;
        fout << r.status_code << "\t" <<  r.status_line << std::endl;
        if (r.status_code != 200)
        {
            fout << "error:" << r.error.message << endl;
        }
        // else
        // {
        //     fout << r.text << std::endl;
        // }
        *resp_cnt += 1;
        return r.text;
    };
    string url = "http://www.baidu.com";
    int timeout = 10 * 1000;
    futures[seq] = cpr::GetCallback(callback, cpr::Url{url}, cpr::Timeout{timeout});
}

void test_http_sync(int seq)
{
    string url = "http://www.baidu.com";
    int timeout = 10 * 1000;
    auto r = cpr::Get(cpr::Url{url}, cpr::Timeout{timeout});
    std::string path = "sync-result-" + std::to_string(seq) + ".log";
    std::ofstream fout(path.c_str());
    fout << "test sync response thread" << std::endl;
    fout << "tid=" << std::this_thread::get_id() << std::endl;
    fout << r.status_code << "\t" <<  r.status_line << std::endl;
    if (r.status_code != 200)
    {
        fout << "error:" << r.error.message << endl;
    }
    // else
    // {
    //     fout << r.text << std::endl;
    // }
}

void CprRequestTest::AsyncSyncRequestTest()
{
    int req_cnt = 1000;
    int resp_cnt = 0;
    vector<std::shared_future<string>> cprfutures(req_cnt);
    cprfutures.reserve(1000);
    int64_t beg = Timer::GetCurrentTimeInMilliSeconds();
    cout << "test async request thread tid=" << std::this_thread::get_id() << endl;;
    for (int i = 0; i < req_cnt; ++i) {
        test_http_async(i, &resp_cnt, cprfutures);
        // cprfutures.emplace_back(f);
    }
    // demo wait async finished
    while (resp_cnt < req_cnt) usleep(10 * 1000);
    int64_t used = Timer::GetCurrentTimeInMilliSeconds() - beg;
    std::cout << "async finished: " << used << "ms" << std::endl;

    beg = Timer::GetCurrentTimeInMilliSeconds();
    cout << "test sync request thread tid=" << std::this_thread::get_id() << endl;;
    for (int i = 0; i < req_cnt; ++i) {
        test_http_sync(i);
    }
    
    used = Timer::GetCurrentTimeInMilliSeconds() - beg;
    std::cout << "sync finished: " << used << "ms" << std::endl;
}
  • result
test async request thread tid=140052864329472
async finished: 554ms
test sync request thread tid=140052864329472
sync finished: 32382ms

[wei@localmachine /home/wei/myworks/cpr]
cat async-result-*.log | grep "tid=" | sort | uniq -c | wc -l
1000

  • 机器
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 63
model name      : Intel(R) Xeon(R) CPU E5-2640 v3 @ 2.60GHz
stepping        : 2
microcode       : 0x3a
cpu MHz         : 2600.000
cache size      : 20480 KB
physical id     : 0
siblings        : 16
core id         : 0
cpu cores       : 16
apicid          : 10
initial apicid  : 10
fpu             : yes
fpu_exception   : yes
cpuid level     : 15
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 ds_cpl vmx smx est tm2 ssse3 fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm arat epb pln pts dtherm tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc
bogomips        : 5187.90
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:

[wei@localmachine /home/wei]
cat /proc/cpuinfo | grep processor | wc -l
16
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值