服務端socket開發之多線程和gevent框架併發測試[python語言]

請多關注下個人我的博客,blog.xiaorui.cchtml


測試下多線程和gevent在socket服務端的小包表現能力,測試的方法不太嚴謹,python

有點屬於自娛自樂,要是有問題之處,請你們噴之 !
mongodb


每一個鏈接都特地堵塞了0.5秒鐘 !apache

012011164.jpg


在大批量tcp測試下,threading的開銷愈來愈大,因此形成了在併發數加大的狀況下,出現threading崩潰的狀況 !  gevent是 libevent和協程的融合,一個線程裏面均可以跑超多的協程! 利用libevent作io堵塞的調度 ,gevent體系下,同一時間只有一個任務在運行 !    後端



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

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#xiaorui.cc
import sys
import socket
import time
import threading
#xiaorui.cc
def threads(port):
    s = socket.socket()
    s.bind(('0.0.0.0', port))
    s.listen(500)
    while True:
        cli, addr = s.accept()
        t = threading.Thread(target=handle_request, args=(cli, time.sleep))
        t.daemon = True
        t.start()
def 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.flush()
        s.close()
if __name__ == '__main__':
    threads(4444)


用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://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:
Server Hostname:        127.0.0.1
Server Port:            4444
Document Path:          /
Document Length:        0 bytes
Concurrency Level:      500
Time taken for tests:   11.123 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      470000 bytes
HTML transferred:       0 bytes
Requests 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] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   33 177.0      0    1000
Processing:   500  508  33.9    501    1132
Waiting:      500  508  33.9    501    1132
Total:        500  541 201.8    501    2132
Percentage 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.145s
user    0m0.210s
sys     0m0.961s



012312118.jpg

加到800的時候~socket

012606797.jpg


gevent:tcp

#xiaorui.cc
import sys
import socket
import time
import gevent
from gevent import socket
def server(port):
    s = socket.socket()
    s.bind(('0.0.0.0', port))
    s.listen(500)
    while True:
        cli, addr = s.accept()
        gevent.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.cc !!!''')
        print data
        request_string = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\nServer: xiaorui.cc\n" %('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.close()
if __name__ == '__main__':
    server(7777)



gevent跑socket服務:ide

併發數值是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://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:
Server Hostname:        127.0.0.1
Server Port:            7777
Document Path:          /
Document Length:        0 bytes
Concurrency Level:      500
Time taken for tests:   11.312 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      20000 bytes
HTML transferred:       0 bytes
Requests 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] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   44 202.7      0    1001
Processing:   500  513  10.1    511     707
Waiting:      500  513  10.1    511     707
Total:        500  557 204.1    512    1525
Percentage 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.334s
user    0m0.159s
sys     0m0.730s

010236465.jpg

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


010104472.jpg


併發是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://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:
Server Hostname:        127.0.0.1
Server Port:            7777
Document Path:          /
Document Length:        0 bytes
Concurrency Level:      1000
Time taken for tests:   7.406 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      20000 bytes
HTML transferred:       0 bytes
Requests 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] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  175 491.7      0    3000
Processing:   500  520  17.7    515     707
Waiting:      500  520  17.7    515     707
Total:        500  695 492.5    517    3521
Percentage 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.428s
user    0m0.208s
sys     0m0.741s


當併發到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://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 127.0.0.1 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software:
Server Hostname:        127.0.0.1
Server Port:            7777
Document Path:          /
Document Length:        0 bytes
Concurrency Level:      1500
Time taken for tests:   5.290 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      20000 bytes
HTML transferred:       0 bytes
Requests 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] received
Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  214 404.9      1    1003
Processing:   500  522  23.0    514     716
Waiting:      500  522  23.0    514     716
Total:        500  736 406.7    520    1712
Percentage 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.313s
user    0m0.275s
sys     0m0.763s



出現了少許的報錯:

010710202.jpg


gevent 能夠加個隊列,來限制協程的數目,可是數目限制了,雖然穩定了,可是併發數上不去。

from gevent.pool import Pool
pool = Pool(N)




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

相關文章
相關標籤/搜索