这两天要做性能测试,自己没事用python写了个脚本,用于压力测试

上一篇 / 下一篇  2017-04-06 22:58:55 / 个人分类:python

这两天要做性能测试,自己没事用python写了个脚本,用于压力测试

 

Python代码  收藏代码
  1. # -*- coding: utf8 -*-  
  2. # code by Shurrik  
  3. import threading, time, httplib  
  4. HOST = "www.baidu.com"#主机地址 例如192.168.1.101  
  5. PORT = 80 #端口  
  6. URI = "/?123" #相对地址,加参数防止缓存,否则可能会返回304  
  7. TOTAL = 0 #总数  
  8. SUCC = 0 #响应成功数  
  9. FAIL = 0 #响应失败数  
  10. EXCEPT = 0 #响应异常数  
  11. MAXTIME=0 #最大响应时间  
  12. MINTIME=100 #最小响应时间,初始值为100秒  
  13. GT3=0 #统计3秒内响应的  
  14. LT3=0 #统计大于3秒响应的  
  15. # 创建一个 threading.Thread 的派生类  
  16. class RequestThread(threading.Thread):  
  17.     # 构造函数  
  18.     def __init__(self, thread_name):  
  19.         threading.Thread.__init__(self)  
  20.         self.test_count = 0  
  21.   
  22.     # 线程运行的入口函数  
  23.     def run(self):  
  24.   
  25.         self.test_performace()  
  26.   
  27.   
  28.     def test_performace(self):  
  29.             global TOTAL  
  30.             global SUCC  
  31.             global FAIL  
  32.             global EXCEPT  
  33.             global GT3  
  34.             global LT3  
  35.             try:  
  36.                 st = time.time()  
  37.                 conn = httplib.HTTPConnection(HOST, PORT, False)    
  38.                 conn.request('GET', URI)  
  39.                 res = conn.getresponse()    
  40.                 #print 'version:', res.version    
  41.                 #print 'reason:', res.reason    
  42.                 #print 'status:', res.status    
  43.                 #print 'msg:', res.msg    
  44.                 #print 'headers:', res.getheaders()  
  45.                 start_time  
  46.                 if res.status == 200:  
  47.                     TOTAL+=1  
  48.                     SUCC+=1  
  49.                 else:  
  50.                     TOTAL+=1  
  51.                     FAIL+=1  
  52.                 time_span = time.time()-st  
  53.                 print '%s:%f\n'%(self.name,time_span)  
  54.                 self.maxtime(time_span)  
  55.                 self.mintime(time_span)  
  56.                 if time_span>3:  
  57.                     GT3+=1  
  58.                 else:  
  59.                     LT3+=1                      
  60.             except Exception,e:  
  61.                 print e  
  62.                 TOTAL+=1  
  63.                 EXCEPT+=1  
  64.             conn.close()  
  65.     def maxtime(self,ts):  
  66.             global MAXTIME  
  67.             print ts  
  68.             if ts>MAXTIME:  
  69.                 MAXTIME=ts  
  70.     def mintime(self,ts):  
  71.             global MINTIME  
  72.             if ts<MINTIME:  
  73.                 MINTIME=ts  
  74.           
  75. # main 代码开始  
  76. print '===========task start==========='  
  77. # 开始的时间  
  78. start_time = time.time()  
  79. # 并发的线程数  
  80. thread_count = 300  
  81.   
  82. i = 0  
  83. while i <= thread_count:  
  84.     t = RequestThread("thread" + str(i))  
  85.     t.start()  
  86.     i += 1  
  87. t=0  
  88. #并发数所有都完成或大于50秒就结束  
  89. while TOTAL<thread_count|t>50:  
  90.         print "total:%d,succ:%d,fail:%d,except:%d\n"%(TOTAL,SUCC,FAIL,EXCEPT)  
  91.         print HOST,URI  
  92.         t+=1  
  93.         time.sleep(1)  
  94. print '===========task end==========='  
  95. print "total:%d,succ:%d,fail:%d,except:%d"%(TOTAL,SUCC,FAIL,EXCEPT)  
  96. print 'response maxtime:',MAXTIME  
  97. print 'response mintime',MINTIME  
  98. print 'great than 3 seconds:%d,percent:%0.2f'%(GT3,float(GT3)/TOTAL)  
  99. print 'less than 3 seconds:%d,percent:%0.2f'%(LT3,float(LT3)/TOTAL)  

TAG: 压力测试 Python python

 

评分:0

我来说两句

Open Toolbar