Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 77 lines (55 sloc) 2.412 kb
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
"""
Benchmark python web servers

run each server in ./servers and call ab a number of times, writing results to results.txt
"""


import sys
import csv
import subprocess
import time
import re

GETS = 10000
concurrencies = [(4 ** x) for x in (1,2,3,4)]
REPS = [1,2,3]

is_pypy = hasattr(sys, 'pypy_version_info')
gevent = [] if is_pypy else ['gevent']

def servers():
    yield 'cyclone', 'cyclone', subprocess.Popen('python servers/cyc.py'.split())
    yield 'tornado', 'tornado', subprocess.Popen('python servers/torn.py'.split())


    for host in gevent + ['twisted', 'tornado', 'paste', 'rocket']: #excluded wsgiref twisted
        yield 'bottle', host, subprocess.Popen(('python servers/bot.py %s' % host).split())

    for host in gevent + ['tornado']:
        yield 'flask', host, subprocess.Popen(('python servers/fla.py %s' % host).split())
yield 'pyramid', host, subprocess.Popen(('python servers/pyr.py %s' % host).split())



def metrics(result):
    mets = {}

    mets['reqs_per_sec'] = float(re.search('Requests per second:\s+(\S+)', result).group(1))

    reqs = re.search('Total:\s+(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\d+)', result)
    mets['rs.min'] = reqs.group(1)
    mets['rs.mean'] = reqs.group(2)
    mets['rs.std'] = reqs.group(3)
    mets['rs.median'] = reqs.group(4)
    mets['rs.max'] = reqs.group(5)
    print mets
    return mets


headers = 'pypy name host conc rep reqs_per_sec rs.min rs.mean rs.std rs.median rs.max'.split()

out = csv.writer(file('results.txt', 'w'), delimiter='\t')
out.writerow(headers)

def write_result(setup, result):
    out.writerow(setup + [result[x] for x in headers[len(setup):]])

for conc in concurrencies:

    for name, host, server in servers():
        print 'testing %(name)s, %(host)s at concurrency %(conc)s' % locals()
        time.sleep(2)
        try:
            for rep in REPS:


                command = 'ab -n %(GETS)s -c %(conc)s http://127.0.0.1:8000/' % locals()
                ab = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
                ab.wait()
                result = ab.stdout.read()
                if ab.returncode == 0:
                    write_result([is_pypy, name, host, conc, rep], metrics(result))
                else:
                    write_result([is_pypy, name, host, conc, rep], dict([(x,'') for x in headers]))
        finally:
            server.terminate()
            server.wait()
Something went wrong with that request. Please try again.