当前位置: 首页 > news >正文

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 finishedwhile (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 -c1000 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 finishedwhile (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
http://www.lryc.cn/news/343832.html

相关文章:

  • CTFHub-Web-文件上传
  • 笔记2:cifar10数据集获取及pytorch批量处理
  • FSD自动驾驶泛谈
  • golang获取变量动态类型
  • 外企接受大龄程序员吗?
  • 【DevOps】掌控云端:Google Cloud SDK 快速上手
  • 【Unity 踩坑系列】配置VScode环境Downloading the.NET Runtime Failed to download.NET 7.0.9installation timed out
  • 深入了解 NumPy:深度学习中的数学运算利器
  • 鸿蒙开发接口Ability框架:【@ohos.ability.particleAbility (particleAbility模块)】
  • Flutter笔记:Widgets Easier组件库(8)使用图片
  • 商务分析方法与工具(五):Python的趣味快捷-文件和文件夹操作自动化
  • codeforce#938 (div3) 题解
  • 【Docker】如何注册Hub账号并上传镜像到Hub仓库
  • [初阶数据结构】单链表
  • 项目使用git开发流程
  • Day 28 MySQL的数据备份与恢复
  • PackageKit的使用(三)疑问篇
  • 【Linux】17. 进程间通信 --- 管道
  • 有哪些有效的复习方法可以帮助备考软考?
  • 【MySQL | 第九篇】重新认识MySQL锁
  • 含义:理财风险等级R1、R2、R3、R4、R5
  • ICode国际青少年编程竞赛- Python-2级训练场-列表入门
  • 【设计模式】14、strategy 策略模式
  • C++类和对象(基础篇)
  • Oracle导入数据中文乱码问题处理,修改客户端字符编码跟数据库的一致
  • 【与 Apollo 共创生态:展望自动驾驶全新未来】
  • 【webrtc】MessageHandler 5: 基于线程的消息处理:以PeerConnection信令线程为例
  • 计算机网络 3.2网络体系结构
  • 连接HiveMQ代理器实现MQTT协议传输
  • springcloud报错:Failed to start bean‘webServerStartStop‘