Skip to content

Instantly share code, notes, and snippets.

@prof7bit
Last active March 29, 2021 18:05
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 prof7bit/3033b8d19b2a3155a40cfb5dacffa705 to your computer and use it in GitHub Desktop.
Save prof7bit/3033b8d19b2a3155a40cfb5dacffa705 to your computer and use it in GitHub Desktop.
test app and benchmarking for wsgi/asgi servers
from subprocess import check_output
import matplotlib
import matplotlib.pyplot as plt
from time import sleep
TITLE = 'gunicorn/gevent 50ms'
SERVER = 'localhost'
PORT = 8888
TIME = .2
LOWEST = 100
HIGHEST = 6000
STEP = 50
YSCALE_T = 3500
YSCALE_C = 1024
def call_httperf(rate):
output = check_output(['httperf', '--server', SERVER, '--port', str(PORT) ,'--num-conns', str(int(TIME * rate)), '--rate', str(rate)])
lines = output.decode().splitlines()
result = {}
result['rate'] = rate
for line in lines:
if line.startswith('Connection time [ms]: min'):
words = line.split(' ')
result['min'] = float(words[4])
result['avg'] = float(words[6])
result['max'] = float(words[8])
result['med'] = float(words[10])
if 'concurrent connections' in line:
conc = line.split('<=')[1].split(' ')[0]
result['conc'] = int(conc)
return result
results = []
for rate in range(LOWEST, HIGHEST, STEP):
res = call_httperf(rate)
print(res)
results.append(res)
rates = [r['rate'] for r in results]
avgs = [r['avg'] for r in results]
maxs = [r['max'] for r in results]
meds = [r['med'] for r in results]
conc = [r['conc'] for r in results]
matplotlib.use('Qt5Agg')
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
#ax1.plot(rates, meds, label='median time', color='green')
ax1.yaxis.label.set_color('blue')
ax1.set_ylim([0, YSCALE_T])
ax1.set_ylabel('response-time / ms')
ax1.set_xlabel('request-rate / Hz')
ax1.plot(rates, avgs, label='average time', color='orange')
ax1.plot(rates, maxs, label='max time', color='blue')
ax2.yaxis.label.set_color('red')
ax2.set_ylim([0, YSCALE_C])
ax2.set_ylabel('concurrent connections')
ax2.plot(rates, conc, label='concurrent conn', color='red')
ax1.legend(loc=2)
ax2.legend(loc=1)
plt.title(TITLE)
plt.show()
from asyncio import sleep
async def app(scope, receive, send):
await sleep(0.05)
await send({
'type': 'http.response.start',
'status': 200,
'headers': [
[b'content-type', b'text/plain'],
],
})
await send({
'type': 'http.response.body',
'body': b'Hello, world!',
})
from time import sleep # will be monkey-patched
def app(env, resp):
sleep(0.05)
resp('200 OK', [('Content-Type', 'text/plain')])
return [b'hello world']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment