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

43 篇文章 3 订阅
30 篇文章 1 订阅

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 main

import (
	"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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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       4

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      0
  99%      0
 100%      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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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       6

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      0
  99%      0
 100%      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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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      13

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      0
  95%      0
  98%      0
  99%      0
 100%     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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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      14

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      1
  98%      1
  99%      2
 100%     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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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      15

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      1
  95%      2
  98%      3
  99%      3
 100%     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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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      19

Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      3
  80%      3
  90%      3
  95%      4
  98%      5
  99%      6
 100%     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:            1280

Document Path:          /
Document Length:        2 bytes

Concurrency 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] received

Connection 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      71

Percentage of the requests served within a certain time (ms)
  50%     14
  66%     14
  75%     14
  80%     14
  90%     15
  95%     15
  98%     21
  99%     26
 100%     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性能对性能数据的影响。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值