c++多线程实现反向代理 QPS 达到 Haproxy/Nginx 的 3 倍

查看 53|回复 2
作者:shaoyie   
NiubiX.
项目地址
实验性项目,NiubiX 只提供反向代理功能,大家轻拍有不好的地方可以留言或提 issue/pr. 觉得好就点个 star ,我会持续完善它
与 Nginx/Haproxy 对比测试

Linux 5.19.0-1030-gcp #32~22.04.1-Ubuntu
Instacne 1 GCP cloud VM, 2 cores, 4GB RAM 10.146.0.2 (nginx,haproxy, niubix run at here)
Instacne 2 GCP cloud VM, 2 cores, 4GB RAM 10.146.0.3 (backend, wrk run at here)

nginx version config
nginx version: nginx/1.18.0 (Ubuntu)
server {
    listen       8082 reuseport;
    server_name  localhost;
    access_log  off;
    error_log off;
    location / {
        proxy_pass http://10.146.0.3:8080;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
root         516       1  0 Aug24 ?        00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data  417322     516  0 12:13 ?        00:00:06 nginx: worker process
www-data  417323     516  0 12:13 ?        00:00:08 nginx: worker process
haproxy version config
HAProxy version 2.4.22-0ubuntu0.22.04.2 2023/08/14
listen niubix
    bind 0.0.0.0:8083
    mode http
    option forwardfor
    server s1 10.146.0.3:8080
ps -eLf | grep haproxy
root      449421       1  449421  0    1 15:11 ?        00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
haproxy   449423  449421  449423  0    2 15:11 ?        00:00:05 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
haproxy   449423  449421  449429  0    2 15:11 ?        00:00:05 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /run/haproxy-master.sock
单独测试后端程序处理能力, 确保不存在吞吐量瓶颈
run at 10.146.0.2
wrk -t 2 -c 100 -d 10s  http://10.146.0.3:8080/xxx
Running 10s test @ http://10.146.0.3:8080/xxx
  2 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   520.95us  203.98us   4.09ms   68.03%
    Req/Sec    59.25k     2.68k   63.62k    52.50%
  1179133 requests in 10.00s, 173.17MB read
Requests/sec: 117888.45
Transfer/sec:     17.31MB
为了数据真实性,我只取了 1 次测试结果,连续对 3 个服务测试截图


对于 nginx 的数据声明一下:只有偶尔能跑到 1.7w 的 qps ,如果 proxy_pass http://10.146.0.3:8080; 换到 127.0.0.1:8080 ,qps 能到 9000 qps ,至于局域网内为什么这么低通过 strace 也没看到异常,而且 cpu 也通跑满,不知道它在干嘛


tcpdump tcp port 8080 抓包查看 niubix 实际数据,包含 X-Real-IP, XFF ,并且响应在微秒级
目前具备功能:
  • master/worker 模式,worker 采用多线程,支持配置优雅的 Reload(像 nginx 一样),master 还是守护进程,当 worker 进程异常会马上 fork 一个新的
  • 只支持 Linux (将来也不准备跨平台)
  • 主体逻辑无锁,简单高效,可靠
  • 优雅的 acceptor/connector ,高效实现异步监听+连接

    测试声明
  • niubix 仅提供反向代理功能
  • niubix 支持 X-Real-IP, X-Forwarded-For, 其他 Header 并没有解析
  • http parser 只是简单的解析, 并没有完全实现
  • 只是初步测试, 并没有做冒烟测试和稳定性测试以及多条件下复杂测试
  • niubix 均衡策略使用的是 roundrobin(别的也还没实现呢), nginx/haproxy 也是一样的策略
  • backend 测试程序 code
  • niubix 不解析 response 内容
  • 功能逐步完善中, 基本框架是过硬的, 我相信这是一个好的开始

    接下来开发计划
  • Better HTTP parser
  • TCP protocol + Proxy Protocol
  • Https

    haproxy, Nginx, niubix, run

  • 1423   
    Nginx 1K tps?
    我是秦始皇,信我
    xiaooloong   
    nginx 上 upstream ,开 http 1.1 和 keepalive 试下
    您需要登录后才可以回帖 登录 | 立即注册

    返回顶部