forked from AltSchool/dynamic-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_bench.py
More file actions
247 lines (216 loc) · 7.51 KB
/
test_bench.py
File metadata and controls
247 lines (216 loc) · 7.51 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""Benchmark tests."""
from __future__ import absolute_import
import importlib
import json
import random
import statistics
import string
from collections import defaultdict
from datetime import datetime
from rest_framework.test import APITestCase
from benchmarks_app.models import Group, Permission, User
DREST_VERSION = importlib.import_module("dynamic_rest")
DREST_VERSION = DREST_VERSION.__version__
DRF_VERSION = importlib.import_module("rest_framework").__version__
AVERAGE_TYPE = "median"
# BENCHMARKS: configuration for benchmarks
BENCHMARKS = [
{
# name: benchmark name
"name": "linear",
# drest: DREST endpoint
"drest": "/drest/users/",
# drf: DRF endpoint
"drf": "/drf/users/",
# min_size: minimum sample size
"min_size": 1,
# max_size: maximum sample size
"max_size": 16,
# multiplier: number of records in one sample
"multiplier": 256,
# samples: number of samples to take
"samples": 12,
},
{
"name": "quadratic",
"drest": "/drest/users?include[]=groups.",
"drf": "/drf/users_with_groups/",
"min_size": 1,
"max_size": 16,
"multiplier": 4,
"samples": 12,
},
{
"name": "cubic",
"drest": ("/drest/users/" "?include[]=groups.permissions."),
"drf": "/drf/users_with_all/",
"min_size": 1,
"max_size": 16,
"multiplier": 1,
"samples": 12,
},
]
CHART_HEAD = """
<head>
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
</head>
"""
CHART_TEMPLATE = """
<script>
$(function () {{
var {benchmark_name}_chart = new Highcharts.Chart({{
chart: {{
renderTo: '{benchmark_name}',
type: 'line',
}},
title: {{
text: '{benchmark_name}',
x: -20 //center
}},
xAxis: {{
title: {{
text: '# of records'
}}
}},
yAxis: {{
title: {{
text: 'Response time (seconds)'
}},
plotLines: [{{
value: 0,
width: 1,
color: '#808080'
}}]
}},
legend: {{
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
}},
series: {data}
}});
}});
</script>
<div id="{benchmark_name}" style="width:100%"></div>
<br/>
"""
def get_average(values):
"""Get mean or median of values."""
if len(values) == 0:
return 0
elif AVERAGE_TYPE == "mean":
return statistics.mean(values)
elif AVERAGE_TYPE == "median":
return statistics.median(values)
class BenchmarkTest(APITestCase):
"""Benchmark tests."""
@classmethod
def setUpClass(cls):
"""Initialize results."""
# initialize results: a 4x nested dictionary
cls._results = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
@classmethod
def tearDownClass(cls):
"""Tear down class."""
# save results to an HTML file
with open("benchmarks.html", "w", encoding="utf-8") as file:
file.write(CHART_HEAD)
for benchmark_name, implementations in sorted(cls._results.items()):
data = []
for implementation_name, implementation_data in sorted(
implementations.items()
):
for key in implementation_data.keys():
values = sorted(implementation_data[key].values())
implementation_data[key] = get_average(values)
implementation_data = sorted(implementation_data.items())
data.append(
{"name": implementation_name, "data": implementation_data}
)
file.write(
CHART_TEMPLATE.format(
benchmark_name=benchmark_name, data=json.dumps(data)
)
)
def bench(self, implementation_name, benchmark_name, url, amount, sample_num):
"""Benchmark a single URL."""
start = datetime.now()
response = self.client.get(url)
end = datetime.now()
self.assertEqual(response.status_code, 200)
diff = end - start
d = diff.total_seconds()
self._results[benchmark_name][implementation_name][amount][sample_num] = d
def generate_linear(self, amount):
"""Generate linear data."""
users = [User(name=str(i)) for i in range(amount)]
User.objects.bulk_create(users)
return len(users)
def generate_quadratic(self, amount):
"""Generate quadratic data."""
users = [User(name=f"{i}") for i in range(amount)]
total = len(users)
User.objects.bulk_create(users)
users = User.objects.all()
for user in users:
groups = [
Group(name=f"{user.name}-{j}", max_size=amount) for j in range(amount)
]
Group.objects.bulk_create(groups)
total += len(groups)
user.groups.set(groups)
return total
def generate_cubic(self, amount):
"""Generate cubic data."""
users = [User(name=f"{i}") for i in range(amount)]
total = len(users)
User.objects.bulk_create(users)
users = User.objects.all()
for user in users:
groups = [
Group(name=f"{user.name}-{j}", max_size=amount) for j in range(amount)
]
Group.objects.bulk_create(groups)
total += len(groups)
user.groups.set(groups)
for group in groups:
permissions = [
Permission(name=f"{user.name}-{group.name}-{k}")
for k in range(amount)
]
Permission.objects.bulk_create(permissions)
total += len(permissions)
group.permissions.set(permissions)
return total
def generate_benchmark(name, title, drest, drf, size, sample):
"""Generate benchmark."""
def bench(self):
"""Benchmark a single test."""
total_size = getattr(self, f"generate_{name}")(size)
self.bench(f"DREST {DREST_VERSION}", title, drest, total_size, sample)
self.bench(f"DRF {DRF_VERSION}", title, drf, total_size, sample)
return bench
def get_random_string(length):
"""Get random string of length."""
return "".join(random.choice(string.ascii_uppercase) for _ in range(length))
def generate_test_methods():
"""Generate test methods."""
for benchmark in BENCHMARKS:
name = benchmark["name"]
title = name.title()
min_size = benchmark["min_size"]
max_size = benchmark["max_size"]
drf = benchmark["drf"]
drest = benchmark["drest"]
multiplier = benchmark["multiplier"]
samples = benchmark["samples"]
for size in range(min_size, max_size + 1):
size *= multiplier
for sample in range(samples):
test_name = f"test_{get_random_string(4)}_{name}_{size}_{sample}"
test = generate_benchmark(name, title, drest, drf, size, sample)
setattr(BenchmarkTest, test_name, test)
del test_name, test
generate_test_methods()