測試下多線程和 gevent 在 socket 服務端的小包表現能力,測試的方法不太嚴謹,有點屬於自娛自樂,要是有問題之處,請大家噴之!


    每個連接都特意堵塞了 0.5 秒鍾!


    圖片 21.1 pic


    在大批量 tcp 測試下,threading 的開銷越來越大,所以造成了在並發數加大的情況下,出現 threading 崩潰的情況!gevent 是 libevent 和協程的融合,一個線程裏麵都可以跑超多的協程! 利用 libevent 做 io 堵塞的調度,gevent 體係下,同一時間隻有一個任務在運行!


    先來測試下多線程:   我們就不加線程池了


    #!/usr/bin/env python # -*- coding: utf-8 -*- #xiaoruiimport sysimport socketimport timeimport threading #xiaoruidef threads(port):    s = socket.socket    s.bind((\''0.0.0.0\'', port))    s.listen(500)    while true:cli, addr = s.eptt = threading.thread(target=handle_request, args=(cli, time.sleep))t.daemon = truet.startdef handle_request(s, sleep):    try:s.recv(1024)sleep(0.5)   s.send(\''\''\''http/1.0 200 ok  hello world! \''\''\'')s.shutdown(socket.shut_wr)print \''.\'',    except exception, ex:print ex    finally:sys.stdout.flushs.closeif __name__ == \''__main__\'':    threads(4444)  </pre>


    用 threading 跑 socket,每個連接堵塞的時間是 0.5


    time ab -n 10000 -c 500 http://127.0.0.1:4444/this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://.zeustech/licensed to the apache software foundation, http://.apache.org/benchmarking 127.0.0.1 (be patientpleted 1000 requestpleted 2000 requestpleted 3000 requestpleted 4000 requestpleted 5000 requestpleted 6000 requestpleted 7000 requestpleted 8000 requestpleted 9000 requestpleted 10000 requestsfinished 10000 requestsserver software:server hostname:127.0.0.1server port:    4444document path:  /document length:0 bytesconcurrency level:      500time taken for tests:   11.123 secondplete requests:      10000failed requests:0write errors:   0total transferred:      470000 byteshtml transferred:       0 bytesrequests per second:    899.01 [#/sec] (mean)time per request:       556.166 [ms] (mean)time per request:       1.112 [ms] (mean, across all concurrent requests)transfer rate:  41.26 [kbytes/sec] receivedconnection times (ms)      min  mean[+/-sd] median   maxconnect:0   33 177.0      0    1000processing:   500  508  33.9    501    1132waiting:      500  508  33.9    501    1132total:500  541 201.8    501    2132percentage of the requests served within a certain time (ms)  50%    501  66%    501  75%    502  80%    505  90%    522  95%    532  98%   1534  99%   1722 100%   2132 (longest request)real    0m11.145suser    0m0.210ssys     0m0.961s  </pre>


    圖片 21.2 pic


    加到 800 的時候~


    圖片 21.3 pic


    gevent:


    #xiaoruiimport sysimport socketimport timeimport geventfrom gevent import socketdef server(port):    s = socket.socket    s.bind((\''0.0.0.0\'', port))    s.listen(500)    while true:cli, addr = s.eptgevent.spawn(handle_request, cli, gevent.sleep)def handle_request(s, sleep):    try:data=s.recv(1024)sleep(0.5)s.send(\''\''\''http/1.0 200 ok  hello world! this is xiaorui !!!\''\''\'')print datarequest_string = "get %s http/1.1rnhost: %srnrnserver: xiaoruin" %(\''index.html\'', \''127.0.0.1\'')     s.send(request_string)s.shutdown(socket.shut_wr)print \''.\'',‘be killed’    except exception, ex:print ex    finally:s.closeif __name__ == \''__main__\'':    server(7777)  </pre>


    gevent 跑 socket 服務:


    並發數值是 500 的時候!


    time ab -n 10000 -c 500 http://127.0.0.1:7777/this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://.zeustech/licensed to the apache software foundation, http://.apache.org/benchmarking 127.0.0.1 (be patientpleted 1000 requestpleted 2000 requestpleted 3000 requestpleted 4000 requestpleted 5000 requestpleted 6000 requestpleted 7000 requestpleted 8000 requestpleted 9000 requestpleted 10000 requestsfinished 10000 requestsserver software:server hostname:127.0.0.1server port:    7777document path:  /document length:0 bytesconcurrency level:      500time taken for tests:   11.312 secondplete requests:      10000failed requests:0write errors:   0total transferred:      20000 byteshtml transferred:       0 bytesrequests per second:    884.04 [#/sec] (mean)time per request:       565.584 [ms] (mean)time per request:       1.131 [ms] (mean, across all concurrent requests)transfer rate:  1.73 [kbytes/sec] receivedconnection times (ms)      min  mean[+/-sd] median   maxconnect:0   44 202.7      0    1001processing:   500  513  10.1    511     707waiting:      500  513  10.1    511     707total:500  557 204.1    512    1525percentage of the requests served within a certain time (ms)  50%    512  66%    515  75%    517  80%    519  90%    531  95%    552  98%   1521  99%   1523 100%   1525 (longest request)real    0m11.334suser    0m0.159ssys     0m0.730s </pre>


    圖片 21.4 pic


    服務端看到的信息都是正常的!


    圖片 21.5 pic


    並發是 1000 的時候:


    time ab -n 10000 -c 1000 http://127.0.0.1:7777/this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://.zeustech/licensed to the apache software foundation, http://.apache.org/benchmarking 127.0.0.1 (be patientpleted 1000 requestpleted 2000 requestpleted 3000 requestpleted 4000 requestpleted 5000 requestpleted 6000 requestpleted 7000 requestpleted 8000 requestpleted 9000 requestpleted 10000 requestsfinished 10000 requestsserver software:server hostname:127.0.0.1server port:    7777document path:  /document length:0 bytesconcurrency level:      1000time taken for tests:   7.406 secondplete requests:      10000failed requests:0write errors:   0total transferred:      20000 byteshtml transferred:       0 bytesrequests per second:    1350.22 [#/sec] (mean)time per request:       740.623 [ms] (mean)time per request:       0.741 [ms] (mean, across all concurrent requests)transfer rate:  2.64 [kbytes/sec] receivedconnection times (ms)      min  mean[+/-sd] median   maxconnect:0  175 491.7      0    3000processing:   500  520  17.7    515     707waiting:      500  520  17.7    515     707total:500  695 492.5    517    3521percentage of the requests served within a certain time (ms)  50%    517  66%    523  75%    538  80%    569  90%   1515  95%   1530  98%   1539  99%   3514 100%   3521 (longest request)real    0m7.428suser    0m0.208ssys     0m0.741s  </pre>


    當並發到 1500 的時候:


    time ab -n 10000 -c 1500 http://127.0.0.1:7777/this is apachebench, version 2.3 <$revision: 655654 $>copyright 1996 adam twiss, zeus technology ltd, http://.zeustech/licensed to the apache software foundation, http://.apache.org/benchmarking 127.0.0.1 (be patientpleted 1000 requestpleted 2000 requestpleted 3000 requestpleted 4000 requestpleted 5000 requestpleted 6000 requestpleted 7000 requestpleted 8000 requestpleted 9000 requestpleted 10000 requestsfinished 10000 requestsserver software:server hostname:127.0.0.1server port:    7777document path:  /document length:0 bytesconcurrency level:      1500time taken for tests:   5.290 secondplete requests:      10000failed requests:0write errors:   0total transferred:      20000 byteshtml transferred:       0 bytesrequests per second:    1890.27 [#/sec] (mean)time per request:       793.536 [ms] (mean)time per request:       0.529 [ms] (mean, across all concurrent requests)transfer rate:  3.69 [kbytes/sec] receivedconnection times (ms)      min  mean[+/-sd] median   maxconnect:0  214 404.9      1    1003processing:   500  522  23.0    514     716waiting:      500  522  23.0    514     716total:500  736 406.7    520    1712percentage of the requests served within a certain time (ms)  50%    520  66%    558  75%    602  80%   1506  90%   1526  95%   1531  98%   1535  99%   1548 100%   1712 (longest request)real    0m5.313suser    0m0.275ssys     0m0.763s  </pre>


    出現了少量的報錯:


    圖片 21.6 pic


    gevent 可以加個隊列,來限製協程的數目,但是數目限製了,雖然穩定了,但是並發數上不去。


    from gevent.pool import poolpool = pool(n)  </pre>


    這裏測試有點簡單,雖然已經安排了連接的堵塞,但是畢竟不符合業務。 有時間把後端的任務改成才 mongodb 取數據 !


    本文出自 “峰雲,就她了。” 博客,謝絕轉載!

章節目錄

閱讀記錄

Python實戰-從菜鳥到大牛的進階之路所有內容均來自互聯網,uu小說網隻為原作者極客學院的小說進行宣傳。歡迎各位書友支持極客學院並收藏Python實戰-從菜鳥到大牛的進階之路最新章節