Skip to content

Instantly share code, notes, and snippets.

@brickgao
Last active December 24, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brickgao/6812931 to your computer and use it in GitHub Desktop.
Save brickgao/6812931 to your computer and use it in GitHub Desktop.
一个简单的并发压力测试脚本
# -*- coding: utf8 -*-
import httplib, threading, time
total = 0
succ = 0
fail = 0
exce = 0
maxtime = 0
mintime = 9999
gt3 = 0
lt3 = 0
class RequestThread(threading.Thread):
def __init__(self, thread_name, now):
self.now = now
threading.Thread.__init__(self)
def run(self):
self.doit()
def doit(self):
global total
global succ
global fail
global exce
global gt3
global lt3
try:
startt = time.time()
conn = httplib.HTTPConnection('localhost', 80, False)
conn.request('GET', '/req?id=' + str(self.now))
res = conn.getresponse()
if res.status == 200:
total += 1
succ += 1
else:
total += 1
fail += 1
time_use = time.time() - startt
if time_use > 3:
gt3 += 1
else:
lt3 += 1
print '%s(req to %d): %f\n' % (self.name, self.now, time_use)
self.maxtime(time_use)
self.mintime(time_use)
except Exception, e:
print '%s(req to %d): %s\n' % (self.name, self.now, e)
total += 1
exce += 1
def maxtime(self, t):
global maxtime
if t > maxtime:
maxtime = t
def mintime(self, t):
global mintime
if t < mintime:
mintime = t
if __name__ == '__main__':
thread_count = 300
print '==========task start=========='
i = 0
while i <= thread_count:
t = RequestThread('thread ' + str(i), i)
t.start()
i += 1
t = 50;
while total < thread_count | t > 0:
print "total: %d, succ: %d, fail: %d, except: %d\n" % (total, succ, fail, exce)
t -= 1
time.sleep(1)
print '==========task end=========='
print "total: %d, succ: %d, fail: %d, except: %d\n" % (total, succ, fail, exce)
print 'req maxtime: ', maxtime
print 'req mintime: ', mintime
print 'great than 3 seconds: %d, precent: %0.2f' % (gt3, float(gt3) / total)
print 'less than 3 seconds: %d, precent: %0.2f' % (lt3, float(lt3) / total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment