-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
166 lines (147 loc) · 4.79 KB
/
benchmark.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
165
166
import sys
import os
import git
import re
import subprocess
from optparse import OptionParser
from time import sleep
LAST_COMMITS_MASTER=30
ITERATIONS = 3
TIME = 15
class WebSeverCommand:
def __init__(self, server_path=None):
self.server_path = server_path if server_path else os.path.join(os.getcwd(), "web_uvloop.py")
def __str__(self):
return str(self.command())
def command(self):
return (
"python3",
self.server_path
)
class WrkCommand:
def __init__(self, connections=20, time=TIME, threads=20, uri='http://localhost:5000/', wrk_path=None):
self.connections = connections
self.time = time
self.threads = threads
self.uri = uri
self.wrk_path = wrk_path if wrk_path else "wrk"
def __str__(self):
return str(self.command())
def command(self):
return (
self.wrk_path,
"-c",
str(self.connections),
"-d",
str(self.time),
"-t" ,
str(self.threads),
self.uri
)
def find_req_sec(data):
return float(re.search(".*(Requests/sec:)\s+([0-9]*\.[0-9]*).*", str(data)).groups()[1])
def run_web_server(aiohttp_git_path):
env = os.environ.copy()
env['PYTHONPATH'] = aiohttp_git_path
cmd = WebSeverCommand()
process = subprocess.Popen(
cmd.command(),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
env=env)
sleep(2)
return process
def run_benchmark(wrk_path=None):
cmd = WrkCommand(wrk_path=wrk_path)
return subprocess.check_output(
cmd.command()
)
def run_reference_benchmark(aiohttp_git_path, reference, wrk_path=None):
repo = git.Git(aiohttp_git_path)
repo.checkout(reference)
process = run_web_server(aiohttp_git_path)
try:
ret = []
for i in range(0, ITERATIONS):
output = str(run_benchmark(wrk_path=wrk_path))
assert "Non-2xx" not in output
ret.append(find_req_sec(output))
print("[{}] Benchmark req/sec {}".format(reference, round(max(ret), 2))
finally:
process.kill()
def run_tag_benchmark(aiohttp_git_path, tag_re, wrk_path=None):
repo = git.Git(aiohttp_git_path)
tags = filter(lambda tag: re.search(tag_re, tag), repo.tag().split("\n"))
for tag in tags:
run_reference_benchmark(aiohttp_git_path, tag, wrk_path=wrk_path)
def run_v3_benchmark(aiohttp_git_path, wrk_path=None):
repo = git.Git(aiohttp_git_path)
repo.checkout("master")
output = subprocess.check_output(
('git', 'log', '--oneline', '-n', str(LAST_COMMITS_MASTER)),
cwd=aiohttp_git_path
)
for l in output.decode().split("\n")[:-1]:
commit = l.split(" ")[0]
run_reference_benchmark(aiohttp_git_path, commit, wrk_path=wrk_path)
USAGE = """usage: %prog [options] <aiohttp_git_path>
Without options will run the test against the master reference
"""
if __name__ == "__main__":
parser = OptionParser(usage=USAGE)
parser.add_option(
"--server-path",
action="store",
dest="server_path",
type=str,
help="Alternative Server path, default to",
default=None)
parser.add_option(
"--wrk-path",
action="store",
dest="wrk_path",
type=str,
help="Alternative WRK path, default delegates to be find by the system",
default=None)
parser.add_option(
"--reference",
action="store",
dest="reference",
type=str,
help="Checkout a specific reference and run the tests within",
default=None)
parser.add_option(
"--v1",
action="store_true",
dest="v1",
help="Test all aiohttp v1 releases",
default=False)
parser.add_option(
"--v2",
action="store_true",
dest="v2",
help="Test all aiohttp v2 releases",
default=False)
parser.add_option(
"--v3",
action="store_true",
dest="v3",
help="Test last {} commits in master".format(LAST_COMMITS_MASTER),
default=False)
try:
(options, args) = parser.parse_args()
aiohttp_git_path = args[0]
except IndexError:
parser.print_help()
sys.exit(1)
if options.reference:
run_reference_benchmark(aiohttp_git_path, options.reference, wrk_path=options.wrk_path)
elif options.v1:
run_tag_benchmark(aiohttp_git_path, "^(v?)1.*", wrk_path=options.wrk_path)
elif options.v2:
run_tag_benchmark(aiohttp_git_path, "^(v?)2.*", wrk_path=options.wrk_path)
elif options.v3:
run_v3_benchmark(aiohttp_git_path, wrk_path=options.wrk_path)
else:
print("Default using master reference")
run_reference_benchmark(aiohttp_git_path, "master", wrk_path=options.wrk_path)