-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark-router.py
executable file
·165 lines (144 loc) · 4.04 KB
/
benchmark-router.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/python3
import time
import logging
import json
import argparse
import statistics
import platform
import sys
import degu
from degu.misc import TempServer
from degu.tests.helpers import TempDir
from degu.client import Client
REQUESTS = 10000
RUNS = 20
parser = argparse.ArgumentParser()
parser.add_argument('--json', action='store_true', default=False,
help='Output result in machine-readable JSON'
)
parser.add_argument('--send-host', action='store_true', default=False,
help='Send an HTTP Host header for AF_INET6'
)
parser.add_argument('--unix', action='store_true', default=False,
help='Use AF_UNIX instead of AF_INET6'
)
parser.add_argument('--requests', type=int, metavar='N', default=REQUESTS,
help='Number of requests per run; default={}'.format(REQUESTS)
)
parser.add_argument('--runs', type=int, metavar='N', default=RUNS,
help='Number of runs; default={}'.format(RUNS)
)
parser.add_argument('--flat', action='store_true', default=False,
help='Use flat appmap instead of nested'
)
parser.add_argument('--py', action='store_true', default=False,
help='Use Python instead of C implementation of Router'
)
args = parser.parse_args()
logging.basicConfig(
level=logging.DEBUG,
format='\t'.join([
'%(levelname)s',
'%(threadName)s',
'%(message)s',
]),
)
if args.py:
from degu._basepy import Router
else:
from degu._base import Router
#@AllowedMethods('GET')
def app(session, request, bodies):
return (200, 'OK', {}, None)
if args.flat:
router = Router({'a':
Router({'b':
Router({'c':
Router({'d':
Router({'e': app})
})
})
})
})
else:
appmap = {
'a': {
'b': {
'c': {
'd': {
'e': app,
},
},
},
},
}
router = Router(appmap)
if args.unix:
tmp = TempDir()
address = tmp.join('my.socket')
else:
tmp = None
address = degu.IPv6_LOOPBACK
server = TempServer(address, router, max_requests=args.requests)
if args.send_host:
client = Client(server.address)
else:
client = Client(server.address, host=None)
deltas = []
for i in range(args.runs):
conn = client.connect()
start = time.monotonic()
for i in range(args.requests):
conn.get('/a/b/c/d/e', {})
deltas.append(time.monotonic() - start)
conn.close()
del tmp
server.terminate()
rates = tuple(args.requests / d for d in deltas)
_max = max(rates)
_mean = statistics.mean(rates)
_min = min(rates)
_stdev = statistics.stdev(rates)
pyinfo = '{}; {}; {}'.format(
platform.python_version(),
platform.machine(),
platform.system(),
)
family = ('AF_UNIX' if args.unix else 'AF_INET6')
if args.json:
doc = {
'Degu': degu.__version__,
'Python': pyinfo,
'family': family,
'requests': args.requests,
'runs': args.runs,
'fastest': _max,
'average': _mean,
'slowest': _min,
'stdev': _stdev,
}
print(json.dumps(doc, sort_keys=True, indent=4))
sys.exit(0)
fastest_run = str(rates.index(_max) + 1)
slowest_run = str(rates.index(_min) + 1)
width1 = max(len(s) for s in [fastest_run, slowest_run])
fastest = '{:,.0f}'.format(_max)
mean = '{:,.0f}'.format(_mean)
slowest = '{:,.0f}'.format(_min)
stdev = '{:,.0f}'.format(_stdev)
width2 = max(len(s) for s in [fastest, mean, slowest, stdev])
print('')
print('Degu: {}'.format(degu.__version__))
print('Python: {}'.format(pyinfo))
print('Test: {}; {:,} requests per run; {} runs'.format(
family, args.requests, args.runs)
)
print('-' * 72)
print('Run {} of {} was fastest'.format(fastest_run.rjust(width1), args.runs))
print('Run {} of {} was slowest'.format(slowest_run.rjust(width1), args.runs))
print('Requests per second:')
print(' fastest: {}'.format(fastest.rjust(width2)))
print(' average: {}'.format(mean.rjust(width2)))
print(' slowest: {}'.format(slowest.rjust(width2)))
print(' stdev: {}'.format(stdev.rjust(width2)))
print('-' * 72)