lesismal / go-net-benchmark

5 stars 0 forks source link

关于高分位时延的疑问 #2

Open irying opened 2 years ago

irying commented 2 years ago

我今天用这个仓库跑了下300跟1000连接的压测,数据如下:

300连接的时候

server nbio running with taskset -c 0-3
client nbio running with taskset -c 4-15
Info: [nbio] start benching [2022-06-05 15:02:46.992575655 +0800 CST m=+0.247217739], concurrent: 300, total: 5000000, sleep: 0
Info: [nbio]: finish benching [2022-06-05 15:02:57.30769415 +0800 CST m=+10.562336223], took 10315 ms for 5000000 requests
Info: [nbio]: requests total: 5000000, failed: 0
Info: [nbio]: TPS: 484725.20, TP99: 4.56ms, TP999: 10.70ms (b=1024 Byte, c=300, n=5000000)
[server@nbio] CPU Usage: MIN: 333.33%, TP50: 346.00%, TP90: 349.00%, TP99: 349.00%, MAX: 349.35%, AVG:345.31%
[server@nbio] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@nbio] CPU Usage: MIN: 341.00%, TP50: 794.00%, TP90: 796.00%, TP99: 797.00%, MAX: 797.00%, AVG:752.86%
[client@nbio] Mem Usage: AVG: 95 MB, MAX: 215 MB

server gnet running with taskset -c 0-3
client gnet running with taskset -c 4-15
Info: [gnet] start benching [2022-06-05 15:03:01.639231322 +0800 CST m=+0.255805367], concurrent: 300, total: 5000000, sleep: 0
Info: [gnet]: finish benching [2022-06-05 15:03:12.122368861 +0800 CST m=+10.738942907], took 10483 ms for 5000000 requests
Info: [gnet]: requests total: 5000000, failed: 0
Info: [gnet]: TPS: 476959.66, TP99: 4.52ms, TP999: 10.61ms (b=1024 Byte, c=300, n=5000000)
[server@gnet] CPU Usage: MIN: 291.26%, TP50: 349.00%, TP90: 352.00%, TP99: 352.00%, MAX: 353.00%, AVG:343.52%
[server@gnet] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@gnet] CPU Usage: MIN: 440.56%, TP50: 787.00%, TP90: 791.00%, TP99: 791.00%, MAX: 792.00%, AVG:750.29%
[client@gnet] Mem Usage: AVG: 104 MB, MAX: 214 MB

server gnetv2 running with taskset -c 0-3
client gnetv2 running with taskset -c 4-15
Info: [gnetv2] start benching [2022-06-05 15:03:16.440693834 +0800 CST m=+0.245694132], concurrent: 300, total: 5000000, sleep: 0
Info: [gnetv2]: finish benching [2022-06-05 15:03:26.760326081 +0800 CST m=+10.565326379], took 10319 ms for 5000000 requests
Info: [gnetv2]: requests total: 5000000, failed: 0
Info: [gnetv2]: TPS: 484513.24, TP99: 4.50ms, TP999: 10.58ms (b=1024 Byte, c=300, n=5000000)
[server@gnetv2] CPU Usage: MIN: 316.83%, TP50: 347.39%, TP90: 348.81%, TP99: 348.81%, MAX: 351.21%, AVG:344.86%
[server@gnetv2] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@gnetv2] CPU Usage: MIN: 336.66%, TP50: 792.00%, TP90: 798.89%, TP99: 799.00%, MAX: 800.00%, AVG:746.69%
[client@gnetv2] Mem Usage: AVG: 103 MB, MAX: 214 MB

server netpoll running with taskset -c 0-3
client netpoll running with taskset -c 4-15
Info: [netpoll] start benching [2022-06-05 15:03:45.804151801 +0800 CST m=+0.352840378], concurrent: 300, total: 5000000, sleep: 0
Info: [netpoll]: finish benching [2022-06-05 15:04:00.214278558 +0800 CST m=+14.762967135], took 14410 ms for 5000000 requests
Info: [netpoll]: requests total: 5000000, failed: 0
Info: [netpoll]: TPS: 346979.89, TP99: 1.46ms, TP999: 2.33ms (b=1024 Byte, c=300, n=5000000)
[server@netpoll] CPU Usage: MIN: 358.89%, TP50: 363.00%, TP90: 366.00%, TP99: 366.00%, MAX: 366.00%, AVG:363.28%
[server@netpoll] Mem Usage: AVG: 2832 MB, MAX: 5445 MB
[client@netpoll] CPU Usage: MIN: 337.66%, TP50: 656.00%, TP90: 661.00%, TP99: 661.00%, MAX: 661.00%, AVG:632.65%
[client@netpoll] Mem Usage: AVG: 83 MB, MAX: 175 MB

server net running with taskset -c 0-3
client net running with taskset -c 4-15
Info: [net] start benching [2022-06-05 15:04:04.434243034 +0800 CST m=+0.250838757], concurrent: 300, total: 5000000, sleep: 0
Info: [net]: finish benching [2022-06-05 15:04:14.502160975 +0800 CST m=+10.318756698], took 10067 ms for 5000000 requests
Info: [net]: requests total: 5000000, failed: 0
Info: [net]: TPS: 496630.11, TP99: 1.42ms, TP999: 1.62ms (b=1024 Byte, c=300, n=5000000)
[server@net] CPU Usage: MIN: 390.00%, TP50: 399.00%, TP90: 400.00%, TP99: 400.00%, MAX: 400.00%, AVG:398.39%
[server@net] Mem Usage: AVG: 10 MB, MAX: 10 MB
[client@net] CPU Usage: MIN: 166.00%, TP50: 811.88%, TP90: 814.00%, TP99: 814.00%, MAX: 814.00%, AVG:752.58%
[client@net] Mem Usage: AVG: 95 MB, MAX: 215 MB

1000连接的时候

server nbio running with taskset -c 0-3
client nbio running with taskset -c 4-15
Info: [nbio] start benching [2022-06-05 15:18:11.884428941 +0800 CST m=+0.294909195], concurrent: 1000, total: 5000000, sleep: 0
Info: [nbio]: finish benching [2022-06-05 15:18:22.900862056 +0800 CST m=+11.311342310], took 11016 ms for 5000000 requests
Info: [nbio]: requests total: 5000000, failed: 0
Info: [nbio]: TPS: 453873.14, TP99: 10.15ms, TP999: 14.74ms (b=1024 Byte, c=1000, n=5000000)
[server@nbio] CPU Usage: MIN: 346.00%, TP50: 348.30%, TP90: 352.35%, TP99: 352.35%, MAX: 355.00%, AVG:349.24%
[server@nbio] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@nbio] CPU Usage: MIN: 125.00%, TP50: 794.00%, TP90: 796.67%, TP99: 797.00%, MAX: 798.00%, AVG:738.47%
[client@nbio] Mem Usage: AVG: 93 MB, MAX: 184 MB

server gnet running with taskset -c 0-3
client gnet running with taskset -c 4-15
Info: [gnet] start benching [2022-06-05 15:18:27.532637923 +0800 CST m=+0.320627610], concurrent: 1000, total: 5000000, sleep: 0
Info: [gnet]: finish benching [2022-06-05 15:18:38.782221053 +0800 CST m=+11.570210740], took 11249 ms for 5000000 requests
Info: [gnet]: requests total: 5000000, failed: 0
Info: [gnet]: TPS: 444460.85, TP99: 10.50ms, TP999: 15.53ms (b=1024 Byte, c=1000, n=5000000)
[server@gnet] CPU Usage: MIN: 336.63%, TP50: 346.00%, TP90: 350.35%, TP99: 350.95%, MAX: 351.35%, AVG:346.38%
[server@gnet] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@gnet] CPU Usage: MIN: 300.80%, TP50: 787.00%, TP90: 789.00%, TP99: 790.00%, MAX: 793.00%, AVG:746.64%
[client@gnet] Mem Usage: AVG: 94 MB, MAX: 184 MB

server gnetv2 running with taskset -c 0-3
client gnetv2 running with taskset -c 4-15
Info: [gnetv2] start benching [2022-06-05 15:18:43.364852527 +0800 CST m=+0.308295470], concurrent: 1000, total: 5000000, sleep: 0
Info: [gnetv2]: finish benching [2022-06-05 15:18:54.391321577 +0800 CST m=+11.334764520], took 11026 ms for 5000000 requests
Info: [gnetv2]: requests total: 5000000, failed: 0
Info: [gnetv2]: TPS: 453457.71, TP99: 10.64ms, TP999: 15.99ms (b=1024 Byte, c=1000, n=5000000)
[server@gnetv2] CPU Usage: MIN: 343.34%, TP50: 347.12%, TP90: 350.84%, TP99: 350.84%, MAX: 351.00%, AVG:347.52%
[server@gnetv2] Mem Usage: AVG: 7 MB, MAX: 7 MB
[client@gnetv2] CPU Usage: MIN: 137.72%, TP50: 793.00%, TP90: 795.00%, TP99: 795.00%, MAX: 797.00%, AVG:738.60%
[client@gnetv2] Mem Usage: AVG: 93 MB, MAX: 184 MB

server netpoll running with taskset -c 0-3
client netpoll running with taskset -c 4-15
Info: [netpoll] start benching [2022-06-05 15:19:14.76135199 +0800 CST m=+0.468968583], concurrent: 1000, total: 5000000, sleep: 0
Info: [netpoll]: finish benching [2022-06-05 15:19:32.929770518 +0800 CST m=+18.637387111], took 18168 ms for 5000000 requests
Info: [netpoll]: requests total: 5000000, failed: 0
Info: [netpoll]: TPS: 275203.73, TP99: 5.55ms, TP999: 7.38ms (b=1024 Byte, c=1000, n=5000000)
[server@netpoll] CPU Usage: MIN: 330.00%, TP50: 345.00%, TP90: 346.00%, TP99: 347.00%, MAX: 348.00%, AVG:343.69%
[server@netpoll] Mem Usage: AVG: 2746 MB, MAX: 5396 MB
[client@netpoll] CPU Usage: MIN: 209.00%, TP50: 599.00%, TP90: 602.00%, TP99: 602.00%, MAX: 603.00%, AVG:576.22%
[client@netpoll] Mem Usage: AVG: 78 MB, MAX: 105 MB

server net running with taskset -c 0-3
client net running with taskset -c 4-15
Info: [net] start benching [2022-06-05 15:19:37.316771629 +0800 CST m=+0.333771790], concurrent: 1000, total: 5000000, sleep: 0
Info: [net]: finish benching [2022-06-05 15:19:49.026585028 +0800 CST m=+12.043585280], took 11709 ms for 5000000 requests
Info: [net]: requests total: 5000000, failed: 0
Info: [net]: TPS: 426995.91, TP99: 3.41ms, TP999: 4.27ms (b=1024 Byte, c=1000, n=5000000)
[server@net] CPU Usage: MIN: 380.00%, TP50: 400.00%, TP90: 400.00%, TP99: 400.00%, MAX: 400.00%, AVG:397.73%
[server@net] Mem Usage: AVG: 23 MB, MAX: 23 MB
[client@net] CPU Usage: MIN: 582.67%, TP50: 774.44%, TP90: 780.00%, TP99: 780.00%, MAX: 781.00%, AVG:759.84%
[client@net] Mem Usage: AVG: 93 MB, MAX: 183 MB

对比发现异步库跟原生Go net,在高分位时延这块,比如TP99跟TP999,,异步库是大大落后于Go net。 分析异步库的事件处理机制,我猜想原因大概是: 异步库处理读写事件循环时,是遍历所有事件,逐个去处理对应的fd,比如读取完当前fd上的数据再处理下一个fd。 这样导致越往后遍历的fd,响应越慢,比如nbio这里

    for _, ev := range events[:n] {
            fd := int(ev.Fd)
            switch fd {
            case p.evtfd:
            default:
                c := p.getConn(fd)

而Go net是获取有读写事件的协程列表,逐个唤醒,让具体协程去处理读写事件,也就是只负责唤醒。

我的疑问这里有没有优化空间来减少高分位的时延,目前我能想到的是利用ringbuffer来实现引用读,减少遍历里边的Read耗时。

lesismal commented 2 years ago

nbio支持EPOLLET,也支持 g.OnRead 设置用户自己处理读事件,这样就相当于只传递事件给应用层、应用层可以自己在事件到来时去协程池取一个协程进行实际的读写。 如果是异步读,应该使用 EPOLLET + g.OnRead,因为如果用 EPOLLLT 并且跨协程读,读还没开始就可能下一轮 epoll loop 了、会有较多的重复触发。

我之前在nbhttp里试过这种,但是压测数据并没有提升。因为除了epoll loop里的事件,还涉及读buffer,现在的是每个epoll loop对应一个buffer,如果是 epoll loop 只传递事件、另外的协成池去处理实际的读,协程池里的每个协程仍然需要对应一个固定的buffer。对于nbhttp,使用的协成池里的协程是: https://github.com/lesismal/nbio/blob/master/taskpool/mixedpool.go 这个pool benchmark似乎比 panjf2000/ants 要好一点,相比直接go也可以控制size,但缺点是不容易每个协程对应一个buffer,其实是可以再对应一个sync.Pool buffer,但还涉及到协程池最大并发时的buffer数量要多,比如协程池size 2w,最大就能对应成2w个buffe,这也会消耗更多内存,因为相比于标准库,nbio主要优势是内存占用、gc之类的,所以nbhttp并没有为了更好点的性能尝试去把这块放开一些内存使用

但是其他自定制协议,是可以这样做的,毕竟协程池的那点数量个buffer相比于海量并发时每个连接一个协程,已经节省了大量的内存了

lesismal commented 2 years ago

EPOLLET每次有新数据到达都会触发,只要当前未读完之前数据,重复触发读可能就会造成浪费,所以可以考虑给Conn如果是 EPOLLET+g.OnRead,g.OnRead还可以为每个Conn.SetSession维护个当前状态,如果是已经可读,就不再触发syacall.Read,只有当前是未读状态并且有读事件到来,才去触发syscall.Read,这样能尽量节约syscall,刚好前两天在这里也提到过这个: https://github.com/xtaci/gaio/issues/17#issuecomment-1143472108

lesismal commented 2 years ago

另外就是标准库的EPOLL loop + runtime调度的亲和性更友好更节约,比用chan或者每次go一个协程应该是要好一点。 标准库相当于空间、runtime换时间了,但这其中各种消耗存在一些阈值,比如连接数对应的标准库协程数量、内存消耗、gc、每个request的耗时等等。超过阈值了,异步库才能有优势,阈值之内应该是很难做到比标准库好。 因为难做到,所以我之前又考虑了一些其他思路,比如自己实现个 Listener ,把连接数分流,一定数量内的连接数交给标准库处理,超过了交给 nbio 处理,具体的之前在这里有交流过、包括了例子: https://github.com/smallnest/gitalk/issues/210#issuecomment-901705290 listener代码在这里: https://github.com/lesismal/arpc/blob/master/extension/listener/listener.go

lesismal commented 2 years ago

还有就是,总吞吐是差不多的,所以P99/999主要还是epoll loop里排在后面的fd的等待久一点造成的,avg应该是差不多的,对于整体服务,还好

lesismal commented 2 years ago

TP99/999是取末尾的请求的耗时最大值,可能把TP50-90范围都统计出来更好些,比如总共1000个请求,取第990和999个对比可能某个框架表现更好,但前面的900个可能是其他框架表现更好。1000连接的数据对比就是这个样子,标准库的TP99/999仍然最佳但整体吞吐已经不如其他几个高,也就是说前面的大部分请求标准库不如其他几个框架快,比如前面大部分请求其他框架2ms而标准库4ms。 这个仓库里的perf部分是用的字节那个benchmark库,只包括了TP99/999,等闲了我完善下取更多区段的值,再对比下整体情况。

lesismal commented 2 years ago

1000连接时标准库不如其他几个异步的吞吐,应该是因为连接数多了调度消耗更多些了,而异步框架只需要poller num个协程(netpoll有些不同),调度切换协程更少。 但异步框架还有另外一个问题,除非是像做nginx这种基础设施,否则实际应用中(比如带数据库操作的业务)不会直接在poller协程中响应,所以需要额外的协程池处理逻辑,在连接数不高时,这个协程池的性能应该是不如runtime直接调度协程的

irying commented 2 years ago

@lesismal 感谢大佬,我最近准备改成ringbuff的方式读写,看TP99会不会改进,netpoll用的ringbuff,它的TP99在echo压测模型下挺不错

lesismal commented 2 years ago

ringbuffer我在一些地方有解释过,可以思考下: https://github.com/Allenxuxu/gev/issues/114 https://github.com/lesismal/nbio/issues/151#issuecomment-1010881695

所以netpoll我估计应该不是ringbuffer的功劳,而是因为它类似标准库,仍然是为每个fd可读时专门用一个协程去处理,但是因为不是runtime调度,所以它的T99响应也并不如标准库,并且吞吐远低于其他所有参赛者。并且如我前面所讲,T99数据好、整体吞吐却不好说明前面更大范围的请求数响应不如其他框架,比如可能95%的请求比别的框架慢,只是尾部部分比别人尾部请求略快,实际的统计需要加更多日志比如TP50/60/70/80/90/95才能得到。 另外一点重要的,你看一下netpoll的内存占用,是别人的几十几百倍。。。所以这注定它解决不了标准库方案面对海量并发时面临的问题。 综合下来,TP和整体吞吐都不如标准库,而它也并不像其他异步框架那样是为了解决一个连接一个协程海量并发场景下消耗太大的问题,反而比标准库消耗更多资源,那么抛开其他异步框架不说,我甚至想不明白为什么用它来替代标准库。或许是因为字节用go比较早,那个年代的go版本标准库比现在慢太多?但不管什么版本,应用层协程池怎么也比标准库runtime的调度亲和性要差一些的。

当然,你可以折腾一波试试看ringbuffer是否能真的带来提升,我只是理论上推测、并没有去实际压测

irying commented 2 years ago

标准库相当于空间、runtime换时间了,但这其中各种消耗存在一些阈值,比如连接数对应的标准库协程数量、内存消耗、gc、每个request的耗时等等。超过阈值了,异步库才能有优势,阈值之内应该是很难做到比标准库好。

赞同标准库是以空间换时间 我用https://github.com/lesismal/nbio-examples/blob/master/http_1m 这个仓库分别压来nbio 的http和标准库的http,8 核的服务器,我发现异步库也只有内存有优势了,从10w到50w的连接压测结果来看,标准库的qps比异步库高很多,也很稳定(20w vs 15w)。

我感觉在http场景,异步库真的还不要用。

这是nbio http, 50w连接时的qps

running for 120 seconds, NumGoroutine: 468, qps: 154593, total: 13105787
running for 121 seconds, NumGoroutine: 620, qps: 105556, total: 13211343
running for 122 seconds, NumGoroutine: 646, qps: 126760, total: 13338103
running for 123 seconds, NumGoroutine: 722, qps: 156347, total: 13494450
running for 124 seconds, NumGoroutine: 545, qps: 118749, total: 13613199
running for 125 seconds, NumGoroutine: 488, qps: 112841, total: 13726040
running for 126 seconds, NumGoroutine: 642, qps: 149489, total: 13875529
running for 127 seconds, NumGoroutine: 75, qps: 192045, total: 14067574
running for 128 seconds, NumGoroutine: 670, qps: 49454, total: 14117028
running for 129 seconds, NumGoroutine: 771, qps: 151241, total: 14268269
running for 130 seconds, NumGoroutine: 563, qps: 158546, total: 14426815
running for 131 seconds, NumGoroutine: 75, qps: 89137, total: 14515952
running for 132 seconds, NumGoroutine: 461, qps: 136277, total: 14652229
running for 133 seconds, NumGoroutine: 620, qps: 153186, total: 14805415
running for 134 seconds, NumGoroutine: 633, qps: 123298, total: 14928713
running for 135 seconds, NumGoroutine: 582, qps: 111193, total: 15039906
running for 136 seconds, NumGoroutine: 1342, qps: 150804, total: 15190710
running for 137 seconds, NumGoroutine: 481, qps: 147168, total: 15337878
running for 138 seconds, NumGoroutine: 702, qps: 86392, total: 15424270
running for 139 seconds, NumGoroutine: 308, qps: 154083, total: 15578353
running for 140 seconds, NumGoroutine: 548, qps: 151090, total: 15729443
running for 141 seconds, NumGoroutine: 2748, qps: 101616, total: 15831059
running for 142 seconds, NumGoroutine: 609, qps: 154029, total: 15985088
running for 143 seconds, NumGoroutine: 721, qps: 152118, total: 16137206
running for 144 seconds, NumGoroutine: 528, qps: 99170, total: 16236376
running for 145 seconds, NumGoroutine: 447, qps: 140547, total: 16376923
running for 146 seconds, NumGoroutine: 466, qps: 156040, total: 16532963
running for 147 seconds, NumGoroutine: 561, qps: 111099, total: 16644062
running for 148 seconds, NumGoroutine: 454, qps: 127853, total: 16771915
running for 149 seconds, NumGoroutine: 387, qps: 153285, total: 16925200
running for 150 seconds, NumGoroutine: 900, qps: 184966, total: 17110166
running for 151 seconds, NumGoroutine: 643, qps: 58760, total: 17168926
running for 152 seconds, NumGoroutine: 650, qps: 150705, total: 17319631
running for 153 seconds, NumGoroutine: 2540, qps: 194278, total: 17513909
running for 154 seconds, NumGoroutine: 555, qps: 44544, total: 17558453
running for 155 seconds, NumGoroutine: 686, qps: 154110, total: 17712563
running for 156 seconds, NumGoroutine: 577, qps: 151758, total: 17864321
running for 157 seconds, NumGoroutine: 499, qps: 94727, total: 17959048
running for 158 seconds, NumGoroutine: 656, qps: 152972, total: 18112020
running for 159 seconds, NumGoroutine: 751, qps: 152721, total: 18264741
running for 160 seconds, NumGoroutine: 736, qps: 93576, total: 18358317
running for 161 seconds, NumGoroutine: 781, qps: 149640, total: 18507957
running for 162 seconds, NumGoroutine: 561, qps: 153979, total: 18661936
running for 163 seconds, NumGoroutine: 628, qps: 102260, total: 18764196
running for 164 seconds, NumGoroutine: 539, qps: 128718, total: 18892914
running for 165 seconds, NumGoroutine: 581, qps: 153712, total: 19046626
running for 166 seconds, NumGoroutine: 75, qps: 150655, total: 19197281
running for 167 seconds, NumGoroutine: 503, qps: 83929, total: 19281210
running for 168 seconds, NumGoroutine: 564, qps: 152566, total: 19433776
running for 169 seconds, NumGoroutine: 616, qps: 151347, total: 19585123
running for 170 seconds, NumGoroutine: 812, qps: 83686, total: 19668809
running for 171 seconds, NumGoroutine: 669, qps: 143269, total: 19812078
running for 172 seconds, NumGoroutine: 958, qps: 153313, total: 19965391
running for 173 seconds, NumGoroutine: 618, qps: 135984, total: 20101375
running for 174 seconds, NumGoroutine: 619, qps: 102283, total: 20203658
running for 175 seconds, NumGoroutine: 1244, qps: 152521, total: 20356179
running for 176 seconds, NumGoroutine: 644, qps: 175668, total: 20531847
running for 177 seconds, NumGoroutine: 403, qps: 60046, total: 20591893
running for 178 seconds, NumGoroutine: 595, qps: 148883, total: 20740776
running for 179 seconds, NumGoroutine: 834, qps: 150356, total: 20891132

这是go http50w连接时的qps

running for 120 seconds, NumGoroutine: 500060, qps: 213319, total: 20052854
running for 121 seconds, NumGoroutine: 500058, qps: 208078, total: 20260932
running for 122 seconds, NumGoroutine: 500057, qps: 212296, total: 20473228
running for 123 seconds, NumGoroutine: 500054, qps: 202710, total: 20675938
running for 124 seconds, NumGoroutine: 500056, qps: 205766, total: 20881704
running for 125 seconds, NumGoroutine: 500059, qps: 97265, total: 20978969
running for 126 seconds, NumGoroutine: 500055, qps: 152350, total: 21131319
running for 127 seconds, NumGoroutine: 500057, qps: 207625, total: 21338944
running for 128 seconds, NumGoroutine: 500055, qps: 214129, total: 21553073
running for 129 seconds, NumGoroutine: 500056, qps: 208930, total: 21762003
running for 130 seconds, NumGoroutine: 500059, qps: 215001, total: 21977004
running for 131 seconds, NumGoroutine: 500061, qps: 210190, total: 22187194
running for 132 seconds, NumGoroutine: 500058, qps: 214351, total: 22401545
running for 133 seconds, NumGoroutine: 500058, qps: 204651, total: 22606196
running for 134 seconds, NumGoroutine: 500062, qps: 212699, total: 22818895
running for 135 seconds, NumGoroutine: 500055, qps: 214027, total: 23032922
running for 136 seconds, NumGoroutine: 500060, qps: 208264, total: 23241186
running for 137 seconds, NumGoroutine: 500058, qps: 52273, total: 23293459
running for 138 seconds, NumGoroutine: 500058, qps: 197298, total: 23490757
running for 139 seconds, NumGoroutine: 500054, qps: 209550, total: 23700307
running for 140 seconds, NumGoroutine: 500060, qps: 211377, total: 23911684
running for 141 seconds, NumGoroutine: 500052, qps: 208713, total: 24120397
running for 142 seconds, NumGoroutine: 500057, qps: 208563, total: 24328960
running for 143 seconds, NumGoroutine: 500056, qps: 213848, total: 24542808
running for 144 seconds, NumGoroutine: 500059, qps: 210882, total: 24753690
running for 145 seconds, NumGoroutine: 500057, qps: 214205, total: 24967895
running for 146 seconds, NumGoroutine: 500062, qps: 209960, total: 25177855
running for 147 seconds, NumGoroutine: 500056, qps: 204744, total: 25382599
running for 148 seconds, NumGoroutine: 500065, qps: 194133, total: 25576732
running for 149 seconds, NumGoroutine: 500057, qps: 61957, total: 25638689
running for 150 seconds, NumGoroutine: 500055, qps: 187952, total: 25826641
running for 151 seconds, NumGoroutine: 500052, qps: 203320, total: 26029961
running for 152 seconds, NumGoroutine: 500058, qps: 197997, total: 26227958
running for 153 seconds, NumGoroutine: 500059, qps: 207761, total: 26435719
running for 154 seconds, NumGoroutine: 500060, qps: 200741, total: 26636460
running for 155 seconds, NumGoroutine: 500056, qps: 208725, total: 26845185
running for 156 seconds, NumGoroutine: 500061, qps: 206463, total: 27051648
running for 157 seconds, NumGoroutine: 500056, qps: 203281, total: 27254929
running for 158 seconds, NumGoroutine: 500062, qps: 207761, total: 27462690
running for 159 seconds, NumGoroutine: 500056, qps: 203394, total: 27666084
running for 160 seconds, NumGoroutine: 500058, qps: 209021, total: 27875105
running for 161 seconds, NumGoroutine: 500054, qps: 37161, total: 27912266
running for 162 seconds, NumGoroutine: 500056, qps: 183496, total: 28095762
running for 163 seconds, NumGoroutine: 500057, qps: 198456, total: 28294218
running for 164 seconds, NumGoroutine: 500057, qps: 195926, total: 28490144
running for 165 seconds, NumGoroutine: 500056, qps: 199699, total: 28689843
running for 166 seconds, NumGoroutine: 500056, qps: 197460, total: 28887303
running for 167 seconds, NumGoroutine: 500056, qps: 198824, total: 29086127
running for 168 seconds, NumGoroutine: 500053, qps: 193927, total: 29280054
running for 169 seconds, NumGoroutine: 500057, qps: 200698, total: 29480752
running for 170 seconds, NumGoroutine: 500060, qps: 199260, total: 29680012
running for 171 seconds, NumGoroutine: 500061, qps: 202681, total: 29882693
running for 172 seconds, NumGoroutine: 500057, qps: 200106, total: 30082799
running for 173 seconds, NumGoroutine: 500058, qps: 92579, total: 30175378
running for 174 seconds, NumGoroutine: 500056, qps: 135552, total: 30310930
running for 175 seconds, NumGoroutine: 500056, qps: 194701, total: 30505631
running for 176 seconds, NumGoroutine: 500056, qps: 199057, total: 30704688
running for 177 seconds, NumGoroutine: 500058, qps: 198744, total: 30903432
running for 178 seconds, NumGoroutine: 500059, qps: 200621, total: 31104053
running for 179 seconds, NumGoroutine: 500059, qps: 198781, total: 31302834
lesismal commented 2 years ago

对,我之前在几个地方也说过,普通连接数普通接口,异步库没有优势。 绝大多数企业的接口类服务,都是没必要用异步库的

但还是有些领域适合,对于50w连接数这种,通常异步库可能会更好。http_1m的示例只是简单请求、小buffer echo,而实际业务场景不会这么简单。随着每个接口的参数、数据长度的不同,在这种大连接数下对runtime的压力是不一样的,你可以把测试接口里面加一点cpu消耗计算进去,再把buffer调大一些看看效果,我这里用1024的buffer、加上10w loop的简单计算sum,8c8g(c/s都在一个虚拟机里)直接OOM了

再说实际的场景,还是有一些的。比如大厂基础设施,中间件、代理之类的,每个节点承载的量都比较大。用标准库方案,硬件成本也是很高的。中小公司省成本也是同样的道理,虽然堆机器也能搞定,但纯依赖机器,成本、环保都不划算的

lesismal commented 2 years ago

1024 buffer、加了点cpu消耗:

标准库:

func onEcho(w http.ResponseWriter, r *http.Request) {
    sum := 0
    for i := 0; i < 100000; i++ {
        sum += i
    }

    data, _ := io.ReadAll(r.Body)
    if len(data) > 0 {
        w.Write(data)
    } else {
        w.Write([]byte(time.Now().Format("20060102 15:04:05")))
    }
    atomic.AddUint64(&qps, 1)
}

client

package main

import (
    "bytes"
    "flag"
    "fmt"
    "net"
    "os"
    "runtime"
    "sync/atomic"
    "time"
)

var (
    connected    uint64 = 0
    success      uint64 = 0
    failed       uint64 = 0
    totalSuccess uint64 = 0
    totalFailed  uint64 = 0

    numClient    = flag.Int("c", 500000, "client num")
    numGoroutine = flag.Int("g", 1000, "goroutine num")

    body = make([]byte, 1024)
)

func main() {
    flag.Parse()

    for i := 0; i < 1024; i++ {
        body[i] = 'a' + byte(i%26)
    }

    connNum := *numClient
    goroutineNum := *numGoroutine

    for i := 0; i < goroutineNum; i++ {
        go loop(addrs[i%len(addrs)], connNum/goroutineNum)
    }

    ticker := time.NewTicker(time.Second)
    for i := 1; true; i++ {
        <-ticker.C
        nSuccess := atomic.SwapUint64(&success, 0)
        nFailed := atomic.SwapUint64(&failed, 0)
        totalSuccess += nSuccess
        totalFailed += nFailed
        fmt.Printf("running for %v seconds, online: %v, NumGoroutine: %v, success: %v, totalSuccess: %v, failed: %v, totalFailed: %v\n", i, connected, runtime.NumGoroutine(), nSuccess, totalSuccess, failed, totalFailed)
    }
}

func loop(addr string, connNum int) {
    conns := make([]net.Conn, connNum)
    for i := 0; i < connNum; i++ {
        for {
            conn, err := net.Dial("tcp", addr)
            if err == nil {
                conns[i] = conn
                atomic.AddUint64(&connected, 1)
                break
            }
            time.Sleep(time.Second / 10)
        }
    }
    reqData := []byte(fmt.Sprintf("POST /echo HTTP/1.1\r\nHost: %v\r\nContent-Length: 1024\r\nAccept-Encoding: gzip\r\n\r\n", addr))
    reqData = append(reqData, body...)
    for {
        for i := 0; i < connNum; i++ {
            post(conns[i], reqData)
        }
    }
}

func post(conn net.Conn, reqData []byte) {
    resData := make([]byte, 1024*2)
    n, err := conn.Write(reqData)
    if err != nil || n < len(reqData) {
        atomic.AddUint64(&failed, 1)
        fmt.Println("write failed:", n, err)
        return
    }
    // time.Sleep(time.Second / 10)
    // n, err = io.ReadFull(conn, resData)
    n, err = conn.Read(resData)
    if err != nil || !bytes.Equal(resData[n-1024:n], body) {
        atomic.AddUint64(&failed, 1)
        fmt.Println("read failed:", n, err, string(resData[n-1024:n]))
        os.Exit(0)
    }
    atomic.AddUint64(&success, 1)
}

var addrs = []string{
    "localhost:28001",
    "localhost:28002",
    "localhost:28003",
    "localhost:28004",
    "localhost:28005",
    "localhost:28006",
    "localhost:28007",
    "localhost:28008",
    "localhost:28009",
    "localhost:28010",

    "localhost:28011",
    "localhost:28012",
    "localhost:28013",
    "localhost:28014",
    "localhost:28015",
    "localhost:28016",
    "localhost:28017",
    "localhost:28018",
    "localhost:28019",
    "localhost:28020",

    "localhost:28021",
    "localhost:28022",
    "localhost:28023",
    "localhost:28024",
    "localhost:28025",
    "localhost:28026",
    "localhost:28027",
    "localhost:28028",
    "localhost:28029",
    "localhost:28030",

    "localhost:28031",
    "localhost:28032",
    "localhost:28033",
    "localhost:28034",
    "localhost:28035",
    "localhost:28036",
    "localhost:28037",
    "localhost:28038",
    "localhost:28039",
    "localhost:28040",

    "localhost:28041",
    "localhost:28042",
    "localhost:28043",
    "localhost:28044",
    "localhost:28045",
    "localhost:28046",
    "localhost:28047",
    "localhost:28048",
    "localhost:28049",
    "localhost:28050",
}

output:

running for 1 seconds, online: 18783, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 2 seconds, online: 40541, NumGoroutine: 1003, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 3 seconds, online: 63772, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 4 seconds, online: 86171, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 5 seconds, online: 108934, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 6 seconds, online: 131222, NumGoroutine: 1002, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 7 seconds, online: 146469, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 8 seconds, online: 161895, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 9 seconds, online: 178536, NumGoroutine: 1003, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 10 seconds, online: 193894, NumGoroutine: 1003, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 11 seconds, online: 206362, NumGoroutine: 1001, success: 2, totalSuccess: 2, failed: 0, totalFailed: 0
running for 12 seconds, online: 223479, NumGoroutine: 1001, success: 3, totalSuccess: 5, failed: 0, totalFailed: 0
running for 13 seconds, online: 235686, NumGoroutine: 1003, success: 1, totalSuccess: 6, failed: 0, totalFailed: 0
running for 14 seconds, online: 252053, NumGoroutine: 1001, success: 9, totalSuccess: 15, failed: 0, totalFailed: 0
running for 15 seconds, online: 264258, NumGoroutine: 1001, success: 0, totalSuccess: 15, failed: 0, totalFailed: 0
running for 16 seconds, online: 277843, NumGoroutine: 1001, success: 12, totalSuccess: 27, failed: 0, totalFailed: 0
running for 17 seconds, online: 291761, NumGoroutine: 1003, success: 3, totalSuccess: 30, failed: 0, totalFailed: 0
running for 18 seconds, online: 308240, NumGoroutine: 1001, success: 6, totalSuccess: 36, failed: 0, totalFailed: 0
running for 19 seconds, online: 321924, NumGoroutine: 1001, success: 8, totalSuccess: 44, failed: 0, totalFailed: 0
running for 20 seconds, online: 335931, NumGoroutine: 1003, success: 4, totalSuccess: 48, failed: 0, totalFailed: 0
running for 21 seconds, online: 348310, NumGoroutine: 1003, success: 12, totalSuccess: 60, failed: 0, totalFailed: 0
running for 22 seconds, online: 363288, NumGoroutine: 1001, success: 28, totalSuccess: 88, failed: 0, totalFailed: 0
running for 23 seconds, online: 380325, NumGoroutine: 1001, success: 19, totalSuccess: 107, failed: 0, totalFailed: 0
running for 24 seconds, online: 388488, NumGoroutine: 1001, success: 35, totalSuccess: 142, failed: 0, totalFailed: 0
running for 25 seconds, online: 398988, NumGoroutine: 1001, success: 46, totalSuccess: 188, failed: 0, totalFailed: 0
running for 26 seconds, online: 417832, NumGoroutine: 1001, success: 13, totalSuccess: 201, failed: 0, totalFailed: 0
running for 27 seconds, online: 431164, NumGoroutine: 1003, success: 45, totalSuccess: 246, failed: 0, totalFailed: 0
running for 28 seconds, online: 442897, NumGoroutine: 1001, success: 94, totalSuccess: 340, failed: 0, totalFailed: 0
running for 29 seconds, online: 452870, NumGoroutine: 1003, success: 60, totalSuccess: 400, failed: 0, totalFailed: 0
running for 30 seconds, online: 464070, NumGoroutine: 1001, success: 29, totalSuccess: 429, failed: 0, totalFailed: 0
running for 31 seconds, online: 476819, NumGoroutine: 1001, success: 81, totalSuccess: 510, failed: 0, totalFailed: 0
running for 32 seconds, online: 486747, NumGoroutine: 1001, success: 33, totalSuccess: 543, failed: 0, totalFailed: 0
running for 33 seconds, online: 491506, NumGoroutine: 1001, success: 787, totalSuccess: 1330, failed: 0, totalFailed: 0
running for 34 seconds, online: 493710, NumGoroutine: 1001, success: 17045, totalSuccess: 18375, failed: 0, totalFailed: 0
running for 35 seconds, online: 495434, NumGoroutine: 1001, success: 26439, totalSuccess: 44814, failed: 0, totalFailed: 0
running for 36 seconds, online: 496015, NumGoroutine: 1001, success: 20157, totalSuccess: 64971, failed: 0, totalFailed: 0
running for 37 seconds, online: 496212, NumGoroutine: 1001, success: 30615, totalSuccess: 95586, failed: 0, totalFailed: 0
running for 38 seconds, online: 498542, NumGoroutine: 1001, success: 58815, totalSuccess: 154401, failed: 0, totalFailed: 0
running for 39 seconds, online: 498568, NumGoroutine: 1001, success: 35348, totalSuccess: 189749, failed: 0, totalFailed: 0
running for 40 seconds, online: 498568, NumGoroutine: 1001, success: 34650, totalSuccess: 224399, failed: 0, totalFailed: 0
running for 41 seconds, online: 498902, NumGoroutine: 1001, success: 34196, totalSuccess: 258595, failed: 0, totalFailed: 0
running for 42 seconds, online: 498939, NumGoroutine: 1001, success: 35723, totalSuccess: 294318, failed: 0, totalFailed: 0
running for 43 seconds, online: 498939, NumGoroutine: 1001, success: 32988, totalSuccess: 327306, failed: 0, totalFailed: 0
running for 44 seconds, online: 498939, NumGoroutine: 1001, success: 36581, totalSuccess: 363887, failed: 0, totalFailed: 0
running for 45 seconds, online: 498939, NumGoroutine: 1001, success: 62607, totalSuccess: 426494, failed: 0, totalFailed: 0
running for 46 seconds, online: 498939, NumGoroutine: 1001, success: 92239, totalSuccess: 518733, failed: 0, totalFailed: 0
running for 47 seconds, online: 498939, NumGoroutine: 1001, success: 91302, totalSuccess: 610035, failed: 0, totalFailed: 0
running for 48 seconds, online: 498939, NumGoroutine: 1001, success: 89452, totalSuccess: 699487, failed: 0, totalFailed: 0
running for 49 seconds, online: 498939, NumGoroutine: 1001, success: 29663, totalSuccess: 729150, failed: 0, totalFailed: 0
running for 50 seconds, online: 498939, NumGoroutine: 1001, success: 37721, totalSuccess: 766871, failed: 0, totalFailed: 0
running for 51 seconds, online: 498939, NumGoroutine: 1001, success: 37214, totalSuccess: 804085, failed: 0, totalFailed: 0
running for 52 seconds, online: 498939, NumGoroutine: 1001, success: 30514, totalSuccess: 834599, failed: 0, totalFailed: 0
running for 53 seconds, online: 498939, NumGoroutine: 1001, success: 41117, totalSuccess: 875716, failed: 0, totalFailed: 0
running for 54 seconds, online: 498939, NumGoroutine: 1001, success: 44506, totalSuccess: 920222, failed: 0, totalFailed: 0
running for 55 seconds, online: 498939, NumGoroutine: 1001, success: 33536, totalSuccess: 953758, failed: 0, totalFailed: 0
running for 56 seconds, online: 498939, NumGoroutine: 1001, success: 28511, totalSuccess: 982269, failed: 0, totalFailed: 0
running for 57 seconds, online: 498939, NumGoroutine: 1001, success: 17312, totalSuccess: 999581, failed: 0, totalFailed: 0
running for 58 seconds, online: 498939, NumGoroutine: 1001, success: 13247, totalSuccess: 1012828, failed: 0, totalFailed: 0
running for 59 seconds, online: 498939, NumGoroutine: 1001, success: 11465, totalSuccess: 1024293, failed: 0, totalFailed: 0
running for 60 seconds, online: 498939, NumGoroutine: 1001, success: 8431, totalSuccess: 1032724, failed: 0, totalFailed: 0
running for 61 seconds, online: 498939, NumGoroutine: 1001, success: 9221, totalSuccess: 1041945, failed: 0, totalFailed: 0
running for 62 seconds, online: 498939, NumGoroutine: 1001, success: 8358, totalSuccess: 1050303, failed: 0, totalFailed: 0
running for 63 seconds, online: 498939, NumGoroutine: 1001, success: 8260, totalSuccess: 1058563, failed: 0, totalFailed: 0
running for 64 seconds, online: 498939, NumGoroutine: 1001, success: 9020, totalSuccess: 1067583, failed: 0, totalFailed: 0
running for 65 seconds, online: 498939, NumGoroutine: 1001, success: 7821, totalSuccess: 1075404, failed: 0, totalFailed: 0
running for 66 seconds, online: 498939, NumGoroutine: 1001, success: 8186, totalSuccess: 1083590, failed: 0, totalFailed: 0
running for 67 seconds, online: 498939, NumGoroutine: 1001, success: 7975, totalSuccess: 1091565, failed: 0, totalFailed: 0
running for 68 seconds, online: 498939, NumGoroutine: 1001, success: 7771, totalSuccess: 1099336, failed: 0, totalFailed: 0
running for 69 seconds, online: 498939, NumGoroutine: 1001, success: 8296, totalSuccess: 1107632, failed: 0, totalFailed: 0
running for 70 seconds, online: 498939, NumGoroutine: 1001, success: 7895, totalSuccess: 1115527, failed: 0, totalFailed: 0
running for 71 seconds, online: 499188, NumGoroutine: 1001, success: 7200, totalSuccess: 1122727, failed: 0, totalFailed: 0
running for 72 seconds, online: 500000, NumGoroutine: 1001, success: 7980, totalSuccess: 1130707, failed: 0, totalFailed: 0
running for 73 seconds, online: 500000, NumGoroutine: 1001, success: 8463, totalSuccess: 1139170, failed: 0, totalFailed: 0
running for 74 seconds, online: 500000, NumGoroutine: 1001, success: 10266, totalSuccess: 1149436, failed: 0, totalFailed: 0
running for 75 seconds, online: 500000, NumGoroutine: 1001, success: 6879, totalSuccess: 1156315, failed: 0, totalFailed: 0
running for 76 seconds, online: 500000, NumGoroutine: 1001, success: 8582, totalSuccess: 1164897, failed: 0, totalFailed: 0
running for 77 seconds, online: 500000, NumGoroutine: 1001, success: 6784, totalSuccess: 1171681, failed: 0, totalFailed: 0
running for 78 seconds, online: 500000, NumGoroutine: 1001, success: 7699, totalSuccess: 1179380, failed: 0, totalFailed: 0
running for 79 seconds, online: 500000, NumGoroutine: 1001, success: 7563, totalSuccess: 1186943, failed: 0, totalFailed: 0
running for 80 seconds, online: 500000, NumGoroutine: 1001, success: 7882, totalSuccess: 1194825, failed: 0, totalFailed: 0
running for 81 seconds, online: 500000, NumGoroutine: 1001, success: 7599, totalSuccess: 1202424, failed: 0, totalFailed: 0
running for 82 seconds, online: 500000, NumGoroutine: 1001, success: 6981, totalSuccess: 1209405, failed: 0, totalFailed: 0
running for 83 seconds, online: 500000, NumGoroutine: 1001, success: 7059, totalSuccess: 1216464, failed: 0, totalFailed: 0
running for 84 seconds, online: 500000, NumGoroutine: 1001, success: 4126, totalSuccess: 1220590, failed: 0, totalFailed: 0
running for 85 seconds, online: 500000, NumGoroutine: 1001, success: 5965, totalSuccess: 1226555, failed: 0, totalFailed: 0
running for 86 seconds, online: 500000, NumGoroutine: 1001, success: 7603, totalSuccess: 1234158, failed: 0, totalFailed: 0
running for 87 seconds, online: 500000, NumGoroutine: 1001, success: 6816, totalSuccess: 1240974, failed: 0, totalFailed: 0
running for 88 seconds, online: 500000, NumGoroutine: 1001, success: 7676, totalSuccess: 1248650, failed: 0, totalFailed: 0
running for 89 seconds, online: 500000, NumGoroutine: 1001, success: 7341, totalSuccess: 1255991, failed: 0, totalFailed: 0
running for 90 seconds, online: 500000, NumGoroutine: 1001, success: 7427, totalSuccess: 1263418, failed: 0, totalFailed: 0
running for 91 seconds, online: 500000, NumGoroutine: 1001, success: 7192, totalSuccess: 1270610, failed: 0, totalFailed: 0
running for 92 seconds, online: 500000, NumGoroutine: 1001, success: 6384, totalSuccess: 1276994, failed: 0, totalFailed: 0
running for 93 seconds, online: 500000, NumGoroutine: 1001, success: 6199, totalSuccess: 1283193, failed: 0, totalFailed: 0
running for 94 seconds, online: 500000, NumGoroutine: 1001, success: 6348, totalSuccess: 1289541, failed: 0, totalFailed: 0
running for 95 seconds, online: 500000, NumGoroutine: 1001, success: 6712, totalSuccess: 1296253, failed: 0, totalFailed: 0

消耗:

top -d 1

image

lesismal commented 2 years ago

可以看到的是,增加buffer size和一点cpu消耗模拟实际业务后,内存占用过高,并且qps也下降了,因为runtime的gc压力大了很多,应该是STW比较多导致cpu都发挥不起来了 golang的gc对小对象、小buffer的回收是比较友好的,但是大点size,就没那么友好了,内存池的分配算法发展了这么多年,从tcmalloc、jemalloc,到c++ stl自带的一些分配器的优化,也都是类似的,小对象因为size小可控,使用策略相对容易优化,size大的buffer,内存池也是不太好去优化的,然后不同size对应的gc的压力的区别就很大了

http_1m只是为了演示内存优势,没有去模拟实际业务,因为实际业务确实难模拟,用户还是应该自行选择。 即使普通连接数即使nbio比标准库略慢,慢的也不多,影响业务的响应性也并不大。 另外就是,如果考虑用户是使用其他框架,比如gin、echo,又有一些区别了,因为这些框架本身的封装也带有一些消耗,我之前跑简单测试好像gin基于标准库略慢于gin基于nbio,但我只是简单测了下,没有做更多测试验证,有兴趣的话你可以跑跑试试

lesismal commented 2 years ago

再看下nbio的1024+cpu消耗:

func onEcho(w http.ResponseWriter, r *http.Request) {
    sum := 0
    for i := 0; i < 100000; i++ {
        sum += i
    }

    data, _ := io.ReadAll(r.Body)
    if len(data) > 0 {
        w.Write(data)
    } else {
        w.Write([]byte(time.Now().Format("20060102 15:04:05")))
    }
    atomic.AddUint64(&qps, 1)
}

client跟上面测标准库的相同

output:

running for 1 seconds, online: 142119, NumGoroutine: 1001, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 2 seconds, online: 328438, NumGoroutine: 1002, success: 0, totalSuccess: 0, failed: 0, totalFailed: 0
running for 3 seconds, online: 499858, NumGoroutine: 1002, success: 8585, totalSuccess: 8585, failed: 0, totalFailed: 0
running for 4 seconds, online: 500000, NumGoroutine: 1001, success: 72042, totalSuccess: 80627, failed: 0, totalFailed: 0
running for 5 seconds, online: 500000, NumGoroutine: 1001, success: 95376, totalSuccess: 176003, failed: 0, totalFailed: 0
running for 6 seconds, online: 500000, NumGoroutine: 1001, success: 96099, totalSuccess: 272102, failed: 0, totalFailed: 0
running for 7 seconds, online: 500000, NumGoroutine: 1001, success: 82225, totalSuccess: 354327, failed: 0, totalFailed: 0
running for 8 seconds, online: 500000, NumGoroutine: 1001, success: 103987, totalSuccess: 458314, failed: 0, totalFailed: 0
running for 9 seconds, online: 500000, NumGoroutine: 1001, success: 100681, totalSuccess: 558995, failed: 0, totalFailed: 0
running for 10 seconds, online: 500000, NumGoroutine: 1001, success: 81808, totalSuccess: 640803, failed: 0, totalFailed: 0
running for 11 seconds, online: 500000, NumGoroutine: 1001, success: 98235, totalSuccess: 739038, failed: 0, totalFailed: 0
running for 12 seconds, online: 500000, NumGoroutine: 1001, success: 103170, totalSuccess: 842208, failed: 0, totalFailed: 0
running for 13 seconds, online: 500000, NumGoroutine: 1001, success: 85211, totalSuccess: 927419, failed: 0, totalFailed: 0
running for 14 seconds, online: 500000, NumGoroutine: 1001, success: 100749, totalSuccess: 1028168, failed: 0, totalFailed: 0
running for 15 seconds, online: 500000, NumGoroutine: 1001, success: 98372, totalSuccess: 1126540, failed: 0, totalFailed: 0
running for 16 seconds, online: 500000, NumGoroutine: 1001, success: 79420, totalSuccess: 1205960, failed: 0, totalFailed: 0
running for 17 seconds, online: 500000, NumGoroutine: 1001, success: 92194, totalSuccess: 1298154, failed: 0, totalFailed: 0
running for 18 seconds, online: 500000, NumGoroutine: 1001, success: 100267, totalSuccess: 1398421, failed: 0, totalFailed: 0
running for 19 seconds, online: 500000, NumGoroutine: 1001, success: 94731, totalSuccess: 1493152, failed: 0, totalFailed: 0
running for 20 seconds, online: 500000, NumGoroutine: 1001, success: 87267, totalSuccess: 1580419, failed: 0, totalFailed: 0
running for 21 seconds, online: 500000, NumGoroutine: 1001, success: 97277, totalSuccess: 1677696, failed: 0, totalFailed: 0
running for 22 seconds, online: 500000, NumGoroutine: 1001, success: 94567, totalSuccess: 1772263, failed: 0, totalFailed: 0
running for 23 seconds, online: 500000, NumGoroutine: 1001, success: 86103, totalSuccess: 1858366, failed: 0, totalFailed: 0
running for 24 seconds, online: 500000, NumGoroutine: 1001, success: 103035, totalSuccess: 1961401, failed: 0, totalFailed: 0
running for 25 seconds, online: 500000, NumGoroutine: 1001, success: 97126, totalSuccess: 2058527, failed: 0, totalFailed: 0
running for 26 seconds, online: 500000, NumGoroutine: 1001, success: 79992, totalSuccess: 2138519, failed: 0, totalFailed: 0
running for 27 seconds, online: 500000, NumGoroutine: 1001, success: 99274, totalSuccess: 2237793, failed: 0, totalFailed: 0
running for 28 seconds, online: 500000, NumGoroutine: 1001, success: 100299, totalSuccess: 2338092, failed: 0, totalFailed: 0
running for 29 seconds, online: 500000, NumGoroutine: 1001, success: 102970, totalSuccess: 2441062, failed: 0, totalFailed: 0
running for 30 seconds, online: 500000, NumGoroutine: 1001, success: 84929, totalSuccess: 2525991, failed: 0, totalFailed: 0
running for 31 seconds, online: 500000, NumGoroutine: 1001, success: 94345, totalSuccess: 2620336, failed: 0, totalFailed: 0
running for 32 seconds, online: 500000, NumGoroutine: 1001, success: 99340, totalSuccess: 2719676, failed: 0, totalFailed: 0
running for 33 seconds, online: 500000, NumGoroutine: 1001, success: 85706, totalSuccess: 2805382, failed: 0, totalFailed: 0
running for 34 seconds, online: 500000, NumGoroutine: 1001, success: 97082, totalSuccess: 2902464, failed: 0, totalFailed: 0
running for 35 seconds, online: 500000, NumGoroutine: 1001, success: 102339, totalSuccess: 3004803, failed: 0, totalFailed: 0
running for 36 seconds, online: 500000, NumGoroutine: 1001, success: 96240, totalSuccess: 3101043, failed: 0, totalFailed: 0
running for 37 seconds, online: 500000, NumGoroutine: 1001, success: 80855, totalSuccess: 3181898, failed: 0, totalFailed: 0
running for 38 seconds, online: 500000, NumGoroutine: 1001, success: 94926, totalSuccess: 3276824, failed: 0, totalFailed: 0
running for 39 seconds, online: 500000, NumGoroutine: 1001, success: 99174, totalSuccess: 3375998, failed: 0, totalFailed: 0
running for 40 seconds, online: 500000, NumGoroutine: 1001, success: 102677, totalSuccess: 3478675, failed: 0, totalFailed: 0
running for 41 seconds, online: 500000, NumGoroutine: 1001, success: 71755, totalSuccess: 3550430, failed: 0, totalFailed: 0
running for 42 seconds, online: 500000, NumGoroutine: 1001, success: 97453, totalSuccess: 3647883, failed: 0, totalFailed: 0
running for 43 seconds, online: 500000, NumGoroutine: 1001, success: 98880, totalSuccess: 3746763, failed: 0, totalFailed: 0
running for 44 seconds, online: 500000, NumGoroutine: 1001, success: 102045, totalSuccess: 3848808, failed: 0, totalFailed: 0
running for 45 seconds, online: 500000, NumGoroutine: 1001, success: 87112, totalSuccess: 3935920, failed: 0, totalFailed: 0
running for 46 seconds, online: 500000, NumGoroutine: 1001, success: 102314, totalSuccess: 4038234, failed: 0, totalFailed: 0
running for 47 seconds, online: 500000, NumGoroutine: 1001, success: 92366, totalSuccess: 4130600, failed: 0, totalFailed: 0
running for 48 seconds, online: 500000, NumGoroutine: 1001, success: 82082, totalSuccess: 4212682, failed: 0, totalFailed: 0
running for 49 seconds, online: 500000, NumGoroutine: 1001, success: 91192, totalSuccess: 4303874, failed: 0, totalFailed: 0
running for 50 seconds, online: 500000, NumGoroutine: 1001, success: 98275, totalSuccess: 4402149, failed: 0, totalFailed: 0
running for 51 seconds, online: 500000, NumGoroutine: 1001, success: 97724, totalSuccess: 4499873, failed: 0, totalFailed: 0
running for 52 seconds, online: 500000, NumGoroutine: 1001, success: 82122, totalSuccess: 4581995, failed: 0, totalFailed: 0
running for 53 seconds, online: 500000, NumGoroutine: 1001, success: 96324, totalSuccess: 4678319, failed: 0, totalFailed: 0
running for 54 seconds, online: 500000, NumGoroutine: 1001, success: 99298, totalSuccess: 4777617, failed: 0, totalFailed: 0
running for 55 seconds, online: 500000, NumGoroutine: 1001, success: 98663, totalSuccess: 4876280, failed: 0, totalFailed: 0
running for 56 seconds, online: 500000, NumGoroutine: 1001, success: 83489, totalSuccess: 4959769, failed: 0, totalFailed: 0
running for 57 seconds, online: 500000, NumGoroutine: 1001, success: 94015, totalSuccess: 5053784, failed: 0, totalFailed: 0
running for 58 seconds, online: 500000, NumGoroutine: 1001, success: 93981, totalSuccess: 5147765, failed: 0, totalFailed: 0
running for 59 seconds, online: 500000, NumGoroutine: 1001, success: 84348, totalSuccess: 5232113, failed: 0, totalFailed: 0
running for 60 seconds, online: 500000, NumGoroutine: 1001, success: 87356, totalSuccess: 5319469, failed: 0, totalFailed: 0
running for 61 seconds, online: 500000, NumGoroutine: 1001, success: 98284, totalSuccess: 5417753, failed: 0, totalFailed: 0
running for 62 seconds, online: 500000, NumGoroutine: 1001, success: 97754, totalSuccess: 5515507, failed: 0, totalFailed: 0
running for 63 seconds, online: 500000, NumGoroutine: 1001, success: 82670, totalSuccess: 5598177, failed: 0, totalFailed: 0
running for 64 seconds, online: 500000, NumGoroutine: 1001, success: 90031, totalSuccess: 5688208, failed: 0, totalFailed: 0
running for 65 seconds, online: 500000, NumGoroutine: 1001, success: 98516, totalSuccess: 5786724, failed: 0, totalFailed: 0
running for 66 seconds, online: 500000, NumGoroutine: 1001, success: 101117, totalSuccess: 5887841, failed: 0, totalFailed: 0
running for 67 seconds, online: 500000, NumGoroutine: 1001, success: 79006, totalSuccess: 5966847, failed: 0, totalFailed: 0
running for 68 seconds, online: 500000, NumGoroutine: 1001, success: 100489, totalSuccess: 6067336, failed: 0, totalFailed: 0
running for 69 seconds, online: 500000, NumGoroutine: 1001, success: 97181, totalSuccess: 6164517, failed: 0, totalFailed: 0
running for 70 seconds, online: 500000, NumGoroutine: 1001, success: 97435, totalSuccess: 6261952, failed: 0, totalFailed: 0
running for 71 seconds, online: 500000, NumGoroutine: 1001, success: 76442, totalSuccess: 6338394, failed: 0, totalFailed: 0
running for 72 seconds, online: 500000, NumGoroutine: 1001, success: 98346, totalSuccess: 6436740, failed: 0, totalFailed: 0
running for 73 seconds, online: 500000, NumGoroutine: 1001, success: 101912, totalSuccess: 6538652, failed: 0, totalFailed: 0
running for 74 seconds, online: 500000, NumGoroutine: 1001, success: 94463, totalSuccess: 6633115, failed: 0, totalFailed: 0
running for 75 seconds, online: 500000, NumGoroutine: 1001, success: 74964, totalSuccess: 6708079, failed: 0, totalFailed: 0
running for 76 seconds, online: 500000, NumGoroutine: 1001, success: 97997, totalSuccess: 6806076, failed: 0, totalFailed: 0
running for 77 seconds, online: 500000, NumGoroutine: 1001, success: 101100, totalSuccess: 6907176, failed: 0, totalFailed: 0
running for 78 seconds, online: 500000, NumGoroutine: 1001, success: 100266, totalSuccess: 7007442, failed: 0, totalFailed: 0
running for 79 seconds, online: 500000, NumGoroutine: 1001, success: 76948, totalSuccess: 7084390, failed: 0, totalFailed: 0
running for 80 seconds, online: 500000, NumGoroutine: 1001, success: 95402, totalSuccess: 7179792, failed: 0, totalFailed: 0
running for 81 seconds, online: 500000, NumGoroutine: 1001, success: 99296, totalSuccess: 7279088, failed: 0, totalFailed: 0
running for 82 seconds, online: 500000, NumGoroutine: 1001, success: 94681, totalSuccess: 7373769, failed: 0, totalFailed: 0
running for 83 seconds, online: 500000, NumGoroutine: 1001, success: 85122, totalSuccess: 7458891, failed: 0, totalFailed: 0
running for 84 seconds, online: 500000, NumGoroutine: 1001, success: 95900, totalSuccess: 7554791, failed: 0, totalFailed: 0
running for 85 seconds, online: 500000, NumGoroutine: 1001, success: 96551, totalSuccess: 7651342, failed: 0, totalFailed: 0
running for 86 seconds, online: 500000, NumGoroutine: 1001, success: 87017, totalSuccess: 7738359, failed: 0, totalFailed: 0
running for 87 seconds, online: 500000, NumGoroutine: 1001, success: 83920, totalSuccess: 7822279, failed: 0, totalFailed: 0
running for 88 seconds, online: 500000, NumGoroutine: 1001, success: 89833, totalSuccess: 7912112, failed: 0, totalFailed: 0
running for 89 seconds, online: 500000, NumGoroutine: 1001, success: 96766, totalSuccess: 8008878, failed: 0, totalFailed: 0
running for 90 seconds, online: 500000, NumGoroutine: 1001, success: 89377, totalSuccess: 8098255, failed: 0, totalFailed: 0
running for 91 seconds, online: 500000, NumGoroutine: 1001, success: 81048, totalSuccess: 8179303, failed: 0, totalFailed: 0
running for 92 seconds, online: 500000, NumGoroutine: 1001, success: 94305, totalSuccess: 8273608, failed: 0, totalFailed: 0
running for 93 seconds, online: 500000, NumGoroutine: 1001, success: 100855, totalSuccess: 8374463, failed: 0, totalFailed: 0
running for 94 seconds, online: 500000, NumGoroutine: 1001, success: 91703, totalSuccess: 8466166, failed: 0, totalFailed: 0
running for 95 seconds, online: 500000, NumGoroutine: 1001, success: 92517, totalSuccess: 8558683, failed: 0, totalFailed: 0
running for 96 seconds, online: 500000, NumGoroutine: 1001, success: 94160, totalSuccess: 8652843, failed: 0, totalFailed: 0
running for 97 seconds, online: 500000, NumGoroutine: 1001, success: 94903, totalSuccess: 8747746, failed: 0, totalFailed: 0

消耗:

top -d 1

image

lesismal commented 2 years ago

加上1024 size的buffer和cpu消耗后,nbio的内存占用仍然稳定,cpu利用率持续稳定,qps仍然能保持再9-10w级别,不像标准库下降到几k、不稳定、runtime半假死、cpu利用不起来了

lesismal commented 2 years ago

你可以在自己环境再用不同buffer size和cpu消耗模拟下,再看看异步库实际是否有优势

irying commented 2 years ago

@lesismal 是真的快,太猛了,我试试

irying commented 2 years ago

@lesismal 但是你贴的是client的output,server的是漏了吗,不过没关系,我自己试试吧

lesismal commented 2 years ago

@lesismal 但是你贴的是client的output,server的是漏了吗,不过没关系,我自己试试吧

server的output只是打印协程数、qps,qps client日志已经有了,nbio的协程数就是几百到几千的范围,标准库是一连接一协程50+w个,所以server的日志没啥用但是挺长的占篇幅我就没放,主要是看server的top占用我截了图的

lesismal commented 2 years ago

标准库与nbio,连接数、mem与cpu的空间换时间,runtime的调度、gc和stw,存在很多平衡点、阈值,这个仓库主要是测吞吐,nbio_examples主要是提供各种示例没有做专业测试,所以不要跑简单示例后轻易下结论,要根据实际情况分析这里列出来的这些消耗、平衡点来考虑