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)
{
    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;
    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<cpr::AsyncResponse> cprfutures;
    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.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=140471206545152
async finished: 32586ms
test sync request thread tid=140471206545152
sync finished: 32248ms

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

  • 机器
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.mfbz.cn/a/594366.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

15_Scala面向对象编程_访问权限

文章目录 Scala访问权限1.同类中访问2.同包不同类访问3.不同包访问4.子类权限小结 Scala访问权限 知识点概念 private --同类访问private[包名] --包私有&#xff1b; 同类同包下访问protected --同类&#xff0c;或子类 //同包不能访问(default)(public)默认public --公…

学习大数据,所需要的shell基础(1)

文章目录 Shell概述shell脚本入门变量系统预定义变量自定义变量特殊变量$n$#\$*、$$&#xff1f; 运算符条件判断流程控制&#xff08;重点&#xff09;if判断case语句for循环while循环 Shell概述 shell是一个命令解释器&#xff0c;他接受应用程序/用户命令&#xff0c;然后调…

【Python项目】基于opencv的的【疲劳检测系统】

技术简介&#xff1a;使用Python技术、OpenCV图像处理库、MYSQL数据库等实现。 系统简介&#xff1a;用户可以通过登录系统平台实现实时的人脸照片的拍摄和上传&#xff0c;结合上传图像的内容进行后台的图像预处理和运算分析&#xff0c;用户可以通过照片分析界面查看到当前检…

【iOS】KVO

文章目录 前言一、KVO使用1.基本使用2.context使用3.移除KVO通知的必要性4.KVO观察可变数组 二、代码调试探索1.KVO对属性观察2.中间类3.中间类的方法3.dealloc中移除观察者后&#xff0c;isa指向是谁&#xff0c;以及中间类是否会销毁&#xff1f;总结 三、KVO本质GNUStep窥探…

虚拟化技术 使用Vsphere Client管理ESXi服务器系统

使用Vsphere Client管理ESXi服务器系统 一、实验目的与要求 1.掌握使用vSphere Client管理ESXi主机 2.掌握将CentOS的安装介质ISO上传到ESXi存储 3.掌握在VMware ESXi中创建虚拟机 4.掌握在所创建的虚拟机中安装CentOS6.5操作系统 5.掌握给CentOS6.5安装VMware Tools 6.掌…

HCIP的学习(11)

OSPF的LSA详解 LSA头部信息 ​ [r2]display ospf lsdb router 1.1.1.1----查看OSPF某一条LSA的详细信息&#xff0c;类型以及LS ID参数。 链路状态老化时间 指一条LSA的老化时间&#xff0c;即存在了多长时间。当一条LSA被始发路由器产生时&#xff0c;该参数值被设定为0之后…

RK3568 学习笔记 : u-boot 千兆网络无法 ping 通PC问题的解决方法二

参考 RK3568 学习笔记 : u-boot 千兆网络无法 ping 通PC问题的解决 前言 rk3568 rockchip 提供的 u-boot&#xff0c;默认的设备树需要读取 单独分区 resouce.img 镜像中的 设备树文件&#xff0c;也就是 Linux 内核的设备树 dtb 文件&#xff0c;gmac 网络才能正常的 ping 通…

知识图谱和大语言模型的共存之道

导读 知识图谱和大型语言模型都是用来表示和处理知识的手段。大模型补足了理解语言的能力&#xff0c;知识图谱则丰富了表示知识的方式&#xff0c;两者的深度结合必将为人工智能提供更为全面、可靠、可控的知识处理方法。在这一背景下&#xff0c;OpenKG组织新KG视点系列文章—…

Microsoft Remote Desktop Beta for Mac:远程办公桌面连接工具

Microsoft Remote Desktop Beta for Mac不仅是一款远程桌面连接工具&#xff0c;更是开启远程办公新篇章的利器。 它让Mac用户能够轻松访问和操作远程Windows计算机&#xff0c;实现跨平台办公的无缝衔接。无论是在家中、咖啡店还是旅途中&#xff0c;只要有网络连接&#xff0…

如何使用DEEPL免费翻译PDF

如何使用DEEPL免费翻译PDF 安装DEEPL取消PDF限制 安装DEEPL 安装教程比较多&#xff0c;这里不重复。 把英文pdf拖进去&#xff0c;点翻译&#xff0c;在下面的框中有已经翻译完毕的文档。 但是存在两个问题 问题1&#xff1a;这些文档是加密的。 问题2&#xff1a;带有DeepL标…

Golang | Leetcode Golang题解之第70题爬楼梯

题目&#xff1a; 题解&#xff1a; func climbStairs(n int) int {sqrt5 : math.Sqrt(5)pow1 : math.Pow((1sqrt5)/2, float64(n1))pow2 : math.Pow((1-sqrt5)/2, float64(n1))return int(math.Round((pow1 - pow2) / sqrt5)) }

Java基于Spring Boot框架的课程管理系统(附源码,说明文档)

博主介绍&#xff1a;✌IT徐师兄、7年大厂程序员经历。全网粉丝15W、csdn博客专家、掘金/华为云//InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ &#x1f345;文末获取源码联系&#x1f345; &#x1f447;&#x1f3fb; 精彩专栏推荐订阅&#x1f447;&#x1f3…

【微服务】分布式事务(通过Seata解决分布式事务问题)

分布式事务 分布式事务Seata微服务集成SeataXA模式XA模式使用 AT模式AT模式实现 分布式事务 在分布式系统中&#xff0c;如果一个业务需要多个服务合作完成&#xff0c;而且每一个服务都有事务&#xff0c;多个事务必须同时成功或失败&#xff0c;这样的事务就是分布式事务&am…

解决Maven本地仓库存在依赖包还需要远程下载的问题

背景 公司有自己maven私服&#xff0c;正在在私服可以使用的情况&#xff0c;打包是没问题的。但是这次是由于公司大楼整体因电路检修而停电&#xff0c;所有服务器关机&#xff0c;包括maven私服服务器。然后当天确有一个包需要打&#xff0c;这个时候发现死活打不了&#xf…

自然语言处理(NLP)原理、用法、案例、注意事项

自然语言处理&#xff08;Natural Language Processing&#xff0c;简称NLP&#xff09;是人工智能&#xff08;Artificial Intelligence&#xff0c;简称AI&#xff09;领域的一个重要分支&#xff0c;旨在让计算机能够理解、理解和生成人类语言。 NLP的原理是基于统计建模和机…

list 的模拟实现

目录 1. list 的实现框架 2. push_back 3. 迭代器 4. constructor 4.1. default 4.2. fill 4.3. range 4.4. initializer list 5. insert 6. erase 7. clear 和 destructor 8. copy constructor 9. operator 10. const_iterator 10.1. 普通人的处理方案 10.2. …

物联网小demo

机智云生成代码 具体参考之前的文章 初始化 ADC用来使用光敏电阻 连续采样开启 采样的周期调高 定时器 定时器1用来实现延时 为了只用温湿度模块DHT11 定时器4用来和51进行交互 实现定时的发送和检测心跳信号 IIC 用来使用oled屏幕 USART 串口1和串口2是机智云自己…

Linux —— 信号初识

Linux —— 信号初识 什么是信号测试几个信号signal函数函数原型参数说明返回值注意事项示例 后台程序前台转后台检测输入中断向量表 我们今天来继续学习Linux的内容&#xff0c;今天我们要了解的是Linux操作系统中的信号&#xff1a; 什么是信号 信号是操作系统内核与进程之…

Hash Function(fft)

链接&#xff1a;H-Hash Function_2024牛客五一集训派对day4 (nowcoder.com) 题意&#xff1a;给定一个序列&#xff0c;求使得任意两数的hash值不同的最小模数&#xff1b; 分析&#xff1a;ab(mod seed) |a-b|%seed0; 也就是说seed不能是任意两数差的因子。 如果暴力求解…

【大麦小米学量化】使用Python读写通达信自选股(含代码转换及完整源代码),想要通过通达信自选股实现量化自动关联交易的有福了

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、通达信自选股文件所在位置二、通达信自选股文件数据结构三、使用Python读写通达信自选股文件&#xff08;附完整源代码&#xff09;1. 切换目录路径2. 将li…
最新文章