服务端socket开发之多线程和gevent框架并发测试[python语言]

简介:

测试下多线程和gevent在socket服务端的小包表现能力,测试的方法不太严谨,

有点属于自娱自乐,要是有问题之处,请大家喷之 !


每个连接都特意堵塞了0.5秒钟 !

012011164.jpg


在大批量tcp测试下,threading的开销越来越大,所以造成了在并发数加大的情况下,出现threading崩溃的情况 !  gevent是 libevent和协程的融合,一个线程里面都可以跑超多的协程! 利用libevent做io堵塞的调度 ,gevent体系下,同一时间只有一个任务在运行 !    



先来测试下多线程:   我们就不加线程池了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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的时候~

012606797.jpg


gevent:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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服务:

并发数值是500的时候!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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的时候:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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的时候:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 可以加个队列,来限制协程的数目,但是数目限制了,虽然稳定了,但是并发数上不去。

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




这里测试有点简单,虽然已经安排了连接的堵塞,但是毕竟不符合业务。 有时间把后端的任务改成才mongodb取数据 !






 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1342536,如需转载请自行联系原作者



相关文章
|
4月前
|
数据采集 存储 JSON
Python爬取知乎评论:多线程与异步爬虫的性能优化
Python爬取知乎评论:多线程与异步爬虫的性能优化
|
4月前
|
人工智能 安全 调度
Python并发编程之线程同步详解
并发编程在Python中至关重要,线程同步确保多线程程序正确运行。本文详解线程同步机制,包括互斥锁、信号量、事件、条件变量和队列,探讨全局解释器锁(GIL)的影响及解决线程同步问题的最佳实践,如避免全局变量、使用线程安全数据结构、精细化锁的使用等。通过示例代码帮助开发者理解并提升多线程程序的性能与可靠性。
174 0
|
25天前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
195 0
|
3月前
|
数据采集 消息中间件 并行计算
Python多线程与多进程性能对比:从原理到实战的深度解析
在Python编程中,多线程与多进程是提升并发性能的关键手段。本文通过实验数据、代码示例和通俗比喻,深入解析两者在不同任务类型下的性能表现,帮助开发者科学选择并发策略,优化程序效率。
228 1
|
4月前
|
数据采集 监控 调度
干货分享“用 多线程 爬取数据”:单线程 + 协程的效率反超 3 倍,这才是 Python 异步的正确打开方式
在 Python 爬虫中,多线程因 GIL 和切换开销效率低下,而协程通过用户态调度实现高并发,大幅提升爬取效率。本文详解协程原理、实战对比多线程性能,并提供最佳实践,助你掌握异步爬虫核心技术。
|
5月前
|
JSON 算法 Java
打造终端里的下载利器:Python实现可恢复式多线程下载器
在数字时代,大文件下载已成为日常需求。本文教你用Python打造专业级下载器,支持断点续传、多线程加速、速度限制等功能,显著提升终端下载体验。内容涵盖智能续传、多线程分块下载、限速控制及Rich库构建现代终端界面,助你从零构建高效下载工具。
348 1
|
4月前
|
数据采集 存储 Java
多线程Python爬虫:加速大规模学术文献采集
多线程Python爬虫:加速大规模学术文献采集
|
5月前
|
数据采集 网络协议 前端开发
Python多线程爬虫模板:从原理到实战的完整指南
多线程爬虫通过并发请求大幅提升数据采集效率,适用于大规模网页抓取。本文详解其原理与实现,涵盖任务队列、线程池、会话保持、异常处理、反爬对抗等核心技术,并提供可扩展的Python模板代码,助力高效稳定的数据采集实践。
241 0
|
10月前
|
并行计算 安全 Java
Python GIL(全局解释器锁)机制对多线程性能影响的深度分析
在Python开发中,GIL(全局解释器锁)一直备受关注。本文基于CPython解释器,探讨GIL的技术本质及其对程序性能的影响。GIL确保同一时刻只有一个线程执行代码,以保护内存管理的安全性,但也限制了多线程并行计算的效率。文章分析了GIL的必要性、局限性,并介绍了多进程、异步编程等替代方案。尽管Python 3.13计划移除GIL,但该特性至少要到2028年才会默认禁用,因此理解GIL仍至关重要。
711 16
Python GIL(全局解释器锁)机制对多线程性能影响的深度分析

推荐镜像

更多