Skip to content

Instantly share code, notes, and snippets.

@jackyyf
Created August 5, 2014 07:02
Show Gist options
  • Save jackyyf/8a0a057814f5798d98ce to your computer and use it in GitHub Desktop.
Save jackyyf/8a0a057814f5798d98ce to your computer and use it in GitHub Desktop.
Gist by paste.py @ 2014-08-05 15:02:45.794858
CNT = 0
LIM = 4000
Alive = 0
MEMBER = range(1, 23)
TimeCost = dict()
StatusCode = dict()
ConCurrent = 100
Debug = False
from tornado.httpclient import AsyncHTTPClient, HTTPClient, HTTPRequest
from tornado.ioloop import IOLoop
import time
from Crypto.Random.random import choice
# First clear all previous result
def finalize(resp):
global TimeCost
global StatusCode
global Alive
req = resp.request
delta = time.time() - req.timestamp
StatusCode[resp.code] = StatusCode.get(resp.code, 0) + 1
TimeCost[req.vid] = delta
if Debug:
print resp.body
Alive -= 1
next_request()
if Alive == 0:
IOLoop.instance().stop()
def next_request():
global CNT
global Alive
if CNT >= LIM:
return
CNT += 1
if (CNT & 127) == 0:
print '%d requests made.' % CNT
print 'Current Alive request: %d' % Alive
vote = choice(MEMBER)
vid = 'TV%04d' % CNT
req = HTTPRequest("http://stu.fudan.edu.cn/teleport/gateway/request?returnurl=http://stu.fudan.edu.cn/ztalents/php/userprofile.php&appid=050921da0e7e0556e87cdc86605ddf05ab6e982ca6fbcbe2744a63835e31deb7&state=mI1dflnt&sign=50a33e6266d81403235d5bb879031c869eaf78ac26c102fd9b8fba43212e59b4", method="GET")
req.vid = CNT # Identify
req.timestamp = time.time()
Alive += 1
AsyncHTTPClient().fetch(req, finalize)
for _ in range(ConCurrent):
next_request()
IOLoop.instance().start()
TimeCost = map(lambda x : x * 1000, sorted(TimeCost.values())) # Convert to ms
# Do some simple analyze
AvgResp = sum(TimeCost) / LIM
MidResp = TimeCost[LIM / 2]
P80Resp = TimeCost[LIM * 4 / 5]
P90Resp = TimeCost[LIM * 9 / 10]
P95Resp = TimeCost[LIM * 19 / 20]
P99Resp = TimeCost[LIM * 99 / 100]
MaxResp = TimeCost[-1]
print '-' * 70
print 'Avg response time: %.3f ms' % AvgResp
print '50%% request finished in: %.3f ms' % MidResp
print '80%% request finished in: %.3f ms' % P80Resp
print '90%% request finished in: %.3f ms' % P90Resp
print '95%% request finished in: %.3f ms' % P95Resp
print '99%% request finished in: %.3f ms' % P99Resp
print 'Longest request finished in: %.3f ms' % MaxResp
print ''
print '-' * 70
print ''
for k, v in StatusCode.iteritems():
print '%d requests responsed with code %d' % (v, k)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment