GO:HTTP链接数量对程序性能的影响

总结

对一个HTTP服务进程而言,并不是HTTP(TCP)链接数量越多,程序性能就越好,吞吐量就越高。

具体问题,具体分析,实测为王。


测试环境

go版本:

/root>go version
go version go1.13.6 linux/amd64

服务器版本:

/root>cat /proc/version
Linux version 3.10.0-1062.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Wed Aug 7 18:08:02 UTC 2019

CPU信息:12 Core

cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 63
model name  : Intel(R) Xeon(R) CPU E5-2609 v3 @ 1.90GHz
stepping    : 2
microcode   : 0x43
cpu MHz     : 1238.757
cache size  : 15360 KB
physical id : 0
siblings    : 6
core id     : 0
cpu cores   : 6
apicid      : 0
initial apicid  : 0
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 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm arat pln pts md_clear spec_ctrl intel_stibp flush_l1d
bogomips    : 3791.03
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:……processor    : 11
vendor_id   : GenuineIntel
cpu family  : 6
model       : 63
model name  : Intel(R) Xeon(R) CPU E5-2609 v3 @ 1.90GHz
stepping    : 2
microcode   : 0x43
cpu MHz     : 1268.792
cache size  : 15360 KB
physical id : 1
siblings    : 6
core id     : 5
cpu cores   : 6
apicid      : 26
initial apicid  : 26
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 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm epb ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm xsaveopt cqm_llc cqm_occup_llc dtherm arat pln pts md_clear spec_ctrl intel_stibp flush_l1d
bogomips    : 3795.84
clflush size    : 64
cache_alignment : 64
address sizes   : 46 bits physical, 48 bits virtual
power management:

HTTP服务程序代码:

package mainimport ("net/http"_ "net/http/pprof"
)func main() {http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {/* 设置应答头 */w.Header().Set("Content-Type", "application/json")/* 设置状态码 */w.WriteHeader(201)/* 设置应答体 */w.Write([]byte("{}"))})http.ListenAndServe(":1280", nil)
}

测试工具:ab

/root>ab -V
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

A.链接数量:1

压测命令:

ab -k -n 2000000 -c 1 http://127.0.0.1:1280/

压测结果:

TP50=TP99=0ms,TP100=4ms,QPS=15000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      1
Time taken for tests:   126.080 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    15862.92 [#/sec] (mean)
Time per request:       0.063 [ms] (mean)
Time per request:       0.063 [ms] (mean, across all concurrent requests)
Transfer rate:          2137.78 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.0      0       4
Waiting:        0    0   0.0      0       4
Total:          0    0   0.0      0       4Percentage of the requests served within a certain time (ms)50%      066%      075%      080%      090%      095%      098%      099%      0100%      4 (longest request)

TOP截图:

sy系统占用(中断)6.7,HTTP服务进程(main-127%),ab客户端进程(35%)


B.链接数量:5

压测命令:

ab -k -n 2000000 -c 5 http://127.0.0.1:1280/

压测结果:

TP50=TP99=0ms,TP100=6ms,QPS=65000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      5
Time taken for tests:   30.539 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    65489.12 [#/sec] (mean)
Time per request:       0.076 [ms] (mean)
Time per request:       0.015 [ms] (mean, across all concurrent requests)
Transfer rate:          8825.68 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.0      0       6
Waiting:        0    0   0.0      0       6
Total:          0    0   0.0      0       6Percentage of the requests served within a certain time (ms)50%      066%      075%      080%      090%      095%      098%      099%      0100%      6 (longest request)

TOP截图:

sy系统占用(中断)16.7,HTTP服务进程(main-423%),ab客户端进程(95%)


C.链接数量:10

压测命令:

ab -k -n 2000000 -c 10 http://127.0.0.1:1280/

压测结果:

TP50=TP99=0ms,TP100=13ms,QPS=70000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      10
Time taken for tests:   28.246 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    70807.73 [#/sec] (mean)
Time per request:       0.141 [ms] (mean)
Time per request:       0.014 [ms] (mean, across all concurrent requests)
Transfer rate:          9542.45 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       0
Processing:     0    0   0.1      0      13
Waiting:        0    0   0.1      0      13
Total:          0    0   0.1      0      13Percentage of the requests served within a certain time (ms)50%      066%      075%      080%      090%      095%      098%      099%      0100%     13 (longest request)

TOP截图:

sy系统占用(中断)17.4,HTTP服务进程(main-466%),ab客户端进程(98%)


D.链接数量:50

压测命令:

ab -k -n 2000000 -c 50 http://127.0.0.1:1280/

压测结果:

TP50=1ms,TP99=2ms,TP100=14ms,QPS=72000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      50
Time taken for tests:   27.654 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    72321.58 [#/sec] (mean)
Time per request:       0.691 [ms] (mean)
Time per request:       0.014 [ms] (mean, across all concurrent requests)
Transfer rate:          9746.46 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       3
Processing:     0    1   0.3      1      14
Waiting:        0    1   0.3      1      14
Total:          0    1   0.3      1      14Percentage of the requests served within a certain time (ms)50%      166%      175%      180%      190%      195%      198%      199%      2100%     14 (longest request)

TOP截图:

sy系统占用(中断)18.3,HTTP服务进程(main-496%),ab客户端进程(100%)


E.链接数量:100

压测命令:

ab -k -n 2000000 -c 100 http://127.0.0.1:1280/

压测结果:

TP50=1ms,TP99=3ms,TP100=15ms,QPS=74000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      100
Time taken for tests:   26.892 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    74371.85 [#/sec] (mean)
Time per request:       1.345 [ms] (mean)
Time per request:       0.013 [ms] (mean, across all concurrent requests)
Transfer rate:          10022.77 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       6
Processing:     0    1   0.4      1      15
Waiting:        0    1   0.4      1      15
Total:          0    1   0.4      1      15Percentage of the requests served within a certain time (ms)50%      166%      175%      180%      190%      195%      298%      399%      3100%     15 (longest request)

TOP截图:

sy系统占用(中断)18.9,HTTP服务进程(main-519%),ab客户端进程(100%)


F.链接数量:200

压测命令:

ab -k -n 2000000 -c 200 http://127.0.0.1:1280/

压测结果:

TP50=3ms,TP99=6ms,TP100=19ms,QPS=75000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      200
Time taken for tests:   26.582 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    75239.01 [#/sec] (mean)
Time per request:       2.658 [ms] (mean)
Time per request:       0.013 [ms] (mean, across all concurrent requests)
Transfer rate:          10139.63 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.1      0      11
Processing:     0    3   0.7      3      19
Waiting:        0    3   0.7      3      19
Total:          0    3   0.7      3      19Percentage of the requests served within a certain time (ms)50%      366%      375%      380%      390%      395%      498%      599%      6100%     19 (longest request)

TOP截图:

sy系统占用(中断)18.1,HTTP服务进程(main-534%),ab客户端进程(100%)


G.链接数量:1000

压测命令:

ab -k -n 2000000 -c 1000 http://127.0.0.1:1280/

压测结果:

TP50=14ms,TP99=26ms,TP100=71ms,QPS=70000。

Server Software:
Server Hostname:        127.0.0.1
Server Port:            1280Document Path:          /
Document Length:        2 bytesConcurrency Level:      1000
Time taken for tests:   28.568 seconds
Complete requests:      2000000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    2000000
Total transferred:      276000000 bytes
HTML transferred:       4000000 bytes
Requests per second:    70008.65 [#/sec] (mean)
Time per request:       14.284 [ms] (mean)
Time per request:       0.014 [ms] (mean, across all concurrent requests)
Transfer rate:          9434.76 [Kbytes/sec] receivedConnection Times (ms)min  mean[+/-sd] median   max
Connect:        0    0   0.8      0      50
Processing:     0   14   1.9     14      71
Waiting:        0   14   1.9     14      71
Total:          0   14   2.1     14      71Percentage of the requests served within a certain time (ms)50%     1466%     1475%     1480%     1490%     1595%     1598%     2199%     26100%     71 (longest request)

TOP截图:

sy系统占用(中断)18.3,HTTP服务进程(main-488%),ab客户端进程(100%)

汇总分析

先说明两个机制,再分三类分析。


机制一:ab并发机制

ab -c 指的是并发的TCP/HTTP链接数量,指的是:

任意时刻,ab同时维持 -c 个链接,每个链接上跑一个HTTP请求,即ab同时并发 -c 个HTTP请求。

【实际上,一个HTTP/1链路只能跑一个请求,非异步模式,不考虑pipeline形式下】

ab发请求不是恒定QPS的,是按照并发数量测试的。

这就会有一个现象:

后台程序性能越好,请求处理时间越短,相同的并发链接数,跑的消息将越多,占用CPU将越多。

假设忽略网络传输时间的情况下:

A.一个请求平均处理时长是1ms,则一个连接的QPS是1000
B.一个请求平均处理时长是10ms,则一个连接的QPS是100
C.一个请求平均处理时长是100ms,则一个连接的QPS是10
D.……

程序未过负荷时,若跑的越快,处理的请求将越多,占用的CPU将越高。

通常程序跑的慢,常因为阻塞之类的原因,例如条件变量、锁、IO等,它们占用CPU很小(线程挂起“让出”,让出了CPU资源),但使得进程要等,跑不起来量。


机制二:ab上限

在测试时发现,ab最大似乎只能跑到100%的CPU,为啥呢?

因为……ab是单线程的啊……

ApacheBench (ab) is a single-threaded command line computer program for measuring the performance of HTTP web servers.

参考:https://en.wikipedia.org/wiki/ApacheBench#cite_note-1

一直以为ab是多线程的呢。

所以,ab是单线程工具,当然最多只能占用100%,即一个Core的CPU资源。

因此,ab是存在上限的,其发出的请求速率,受限于客户端自己的单线程发请求机制。

假设,HTTP服务端QPS能力无限大,其处理时延无限小,其实真正通过ab发出的请求仍然不是无限大的,ab本身不可能发出无限多的请求。


分析一:TP时延(ab可见的请求处理时长)

此时延,是ab看见的请求处理时长,包括了HTTP服务端实际处理时长以及网络传输时长。


TP时延,随着链接数增大,时延逐渐增加。

链接数小,后台线程(协程)争抢锁等资源就少,跑的就快,处理时延就低。
链接数多,后台线程(协程)争抢锁等资源就多,跑的就慢,处理时延就高。

一个进程,同时处理1个请求和同时处理1000个请求,明显是有区别的。

后台进程,跑A-1和B-5等对比,在处理不同QPS的开销不同,在管理多TCP链路开销也不同……

(管理链路越少,开销【例如sy软中断】越小,有更多的CPU资源处理请求)

TP100即测试期间最大的时延。

PS:关于链路多少的开销影响,还可以看redis性能。

最开始做的是同步模式,一个redis客户端建立一个TCP链路到redis服务端。

redis服务端耗费大量的CPU在几千个链路管理上(每个链路有数据到,就软中断,系统sy很高),真正用于处理redis指令的CPU反而很少。


分析二:QPS


随着链接数的增加,QPS呈现先增加后减少。

A-1时,QPS小,主要是受限于链接数量。

ab和HTTP服务进程之间只能有一个链路,ab发一个请求就要等到应答回来才能再发下一个请求,是串行的。

B-5之后就是并发执行,可以见到QPS大幅增加。

此时,增加链接数,可以增加少许QPS,但作用有限。

可以认为,在B-5+的测试用例,就是服务端的一个性能数据水准。

其实可以大约估算出来每个请求的时延(理想情况,暂时忽略其他不可忽略的因素):

1.A-1:1s/15865/1链路=6.30e-5s
2.B-5:1s/65489/5链路=3.05e-6s

链路越多,时延越大,虽然QPS上去了……

G-1000时,大量的链路管理,耗费了很多的CPU资源,进程内部也可能在抢、等资源,因此QPS反而下降。


分析三:CPU

随着HTTP链接数增加:

  • ab,HTTP服务端跑的越快,ab发的请求越快,占用CPU越高,直到100%上限
  • sy,链接数越多,QPS越大,软中断越大,sy越高
  • HTTP服务端进程(main),链路越多,QPS越大,占用CPU越高,直到(上限):链路数量增加导致sy增加,忙着切换上下文,QPS降低,CPU也降低。

总结

性能测试涉及到很多方面的考虑,一点不起眼的配置,就会大大影响性能数据。

并不是HTTP链接数越多,程序性能就越好,QPS就越大。

给你10000个链接,HTTP服务器都去做IO、做中断去,业务逻辑都没CPU资源去做,哈哈。

所以,要实际测试,在QPS、CPU占用率等之间做一个权衡,选择适合自己应用场景的模式。

性能测试不仅要考虑到QPS、CPU占用率等指标,还有CPU型号、内存占用等……这里暂未介绍。

PS:可以参见我的另外一篇文章:

https://blog.csdn.net/test1280/article/details/109715926

里面的测试数据,是我笔记本win装vmware虚拟机跑的,相同的代码,相同的测试环境:

虚拟机跑的QPS要比我这篇文章中使用的服务器要多(7W/10W),占用的CPU也更少。

足以可见CPU性能对性能数据的影响。

GO:HTTP链接数量对程序性能的影响相关推荐

  1. 深入理解操作系统(16)第六章:存储器层次结构(2)高速缓存存储器+对程序性能的影响(包括:L1/L2高速缓存历史/缓存写:直写和写回/暂无L4级缓存/缓存命中率/存储器山/高速缓存友好的代码/)

    深入理解操作系统(16)第六章:存储器层次结构(2)高速缓存存储器+对程序性能的影响(包括:L1/L2高速缓存历史/缓存写:直写和写回/暂无L4级缓存/缓存命中率/存储器山/高速缓存友好的代码/) 1 ...

  2. AOT和单文件发布对程序性能的影响

    AOT和单文件发布对程序性能的影响 以前的.NET框架原生并不支持最终编译结果的单文件发布(需要依赖第三方工具) 1. 前言 这里先和大家介绍一下.NET一些发布的历史,以前的.NET框架原生并不支持 ...

  3. 从矩阵乘法来看-O优化和ijk执行顺序对程序性能的影响

    从矩阵乘法来看-O优化和ijk执行顺序对程序性能的影响 根据计算矩阵乘积的c程序,主要想做想做两件事情: 统计采用不同的优化选项编译程序所用的时间,感受-O优化带来的性能提升. 看看矩阵乘法中不同循环 ...

  4. 消息中间件学习总结(11)——Kafka与RocketMQ的Topic数量对单机性能的影响比较分析

    引言 上一期我们对比了三类消息产品(Kafka.RabbitMQ.RocketMQ)单纯发送小消息的性能,受到了程序猿们的广泛关注,其中大家对这种单纯的发送场景感到并不过瘾,因为没有任何一个网站的业务 ...

  5. 以 rte_mempool_ops_table 为例描述 dpdk 程序库链接顺序对程序执行的影响

    dpdk mempool_ops dpdk mempool_ops 是对旧版 mempool 代码的抽象,在 dpdk-16.07 中被引入. 老版本在创建 mempool 时会创建一个内部的 rin ...

  6. 《深入理解计算机系统》之浅析程序性能优化

    此文已由作者余笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文主要是基于我之前学习<深入理解计算机系统>(以下简称CSAPP)这本书第五章优化程序性能内容 ...

  7. C++ 应用程序性能优化

    C++ 应用程序性能优化 eryar@163.com 1. Introduction 对于几何造型内核OpenCASCADE,由于会涉及到大量的数值算法,如矩阵相关计算,微积分,Newton迭代法解方 ...

  8. WP7 App性能优化(12):检测应用程序性能(Ⅴ)

    透视填充率示例 该示例展示了平面投影.透视转换对应用程序性能的影响.设计师创建的XAML经常会包含很多的透视转换用以创建漂亮的视觉效果,但是这会影响应用程序的性能.该示例演示了透视和动画的缓存行为.不 ...

  9. CPU 缓存如何影响你的 Go 程序性能

    小菜刀最近在medium上阅读了一篇高赞文章<Go and CPU Caches>,其地址为https://teivah.medium.com/go-and-cpu-caches-af5d ...

最新文章

  1. Https协议/SSL协议
  2. Visual C++——黄维通《 Visual C++面向对象与可视化程序设计》——习题4-11
  3. React学习笔记2017-12-31
  4. dematel matlab,决策与实验室方法,DEMATEL分析方法介绍
  5. 微型计算机原理与接口技术 考题,微机原理与接口技术试题答案
  6. JavaScript的回调函数及Ajax中的回调函数
  7. 择校秘籍|保研去 北大软微 还是 中科院计算所?
  8. python 只取数值_GitHub又放大招,Python版本的植物大战僵尸还能作弊玩!
  9. java virt res_top命令里内存参数 VIRT, RES 和 SHR 分别是什么意思
  10. java 实现异地登陆_Java实现用户异地登陆踢人操作
  11. Milliman和Akur8结成战略联盟,为财产和意外伤害保险公司、管理型总代理和初创企业提供下一代定价能力
  12. mysql数据库加密存储过程_数据库系统(六)---MySQL语句及存储过程
  13. irq: nobody cared (try booting with the “irqpoll” option)
  14. VIEWGOOD(远古)供应校园网视频点播系统
  15. 黑盒测试用例设计--题目3
  16. PS鼠绘教程:PS鼠绘炫酷红色保时捷跑车
  17. Redis键-值数据库 nosql 数据建模(4)------ 如何存储主从表数据 一对超级多关系
  18. 微博小米鸿蒙,华为确定鸿蒙OS全面开源,小米会... - @V黑猫评测 的微博精选 - 微博国际站...
  19. python:编解码器基类之无状态的编码和解码
  20. 学习笔记—网页设计基础知识(一)

热门文章

  1. 探索贝叶斯-多项式/伯努力/补集朴素贝叶斯
  2. java 产生p10证书_PKCS#10 以及证书颁发过程
  3. 项目主管谈网页游戏:一将功成万骨枯
  4. c#随机生成IP地址
  5. labview 串口通信 modbusRtu
  6. 模拟(婚礼上的小杉vijos1389)
  7. [Python] OpenCV 摄像头黑边问题
  8. 驱动开发中platform设备驱动架构详解
  9. 《Hadoop进阶》利用Hadoop构建豆瓣图书推荐系统
  10. 从网络启动linux调试方法