-
-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathtest_commands.py
More file actions
522 lines (445 loc) · 14.9 KB
/
test_commands.py
File metadata and controls
522 lines (445 loc) · 14.9 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import os
import os.path
import shutil
import textwrap
import unittest
import pyperformance
from pyperformance import tests
class FullStackTests(tests.Functional, unittest.TestCase):
maxDiff = 80 * 100
@classmethod
def setUpClass(cls):
# pyperformance must be installed in order to run,
# so we make sure it is.
cls.ensure_venv()
cls.ensure_pyperformance()
super().setUpClass()
@classmethod
def ensure_pyperformance(cls):
ec, stdout, _ = cls.run_python(
os.path.join(tests.DATA_DIR, "find-pyperformance.py"),
capture="stdout",
onfail="raise",
verbose=False,
)
assert ec == 0, ec
stdout = stdout.strip()
if stdout.strip():
# It is already installed.
return
print("#" * 40)
print("# installing pyperformance into the venv")
print("#" * 40)
print()
# Install it.
reporoot = os.path.dirname(pyperformance.PKG_ROOT)
# XXX Ignore the output (and optionally log it).
ec, _, _ = cls.run_pip("install", "--editable", reporoot)
assert ec == 0, ec
# Clean up extraneous files.
egg_info = "pyperformance.egg-info"
print(f"(tests) Remove directory {egg_info}", flush=True)
try:
shutil.rmtree(egg_info)
except FileNotFoundError:
pass
print()
print("#" * 40)
print("# DONE: installing pyperformance into the venv")
print("#" * 40)
print()
def run_pyperformance(
self,
cmd,
*args,
exitcode=0,
capture="both",
verbose=True,
):
ec, stdout, stderr = self.run_module(
"pyperformance",
cmd,
*args,
capture=capture,
onfail=None,
verbose=verbose,
)
if exitcode is True:
self.assertGreater(ec, 0, repr(stdout))
else:
self.assertEqual(ec, exitcode, repr(stdout))
if stdout:
stdout = stdout.rstrip()
return stdout
###################################
# info
def test_list(self):
# XXX Capture and check the output.
self.run_pyperformance("list", capture=None)
def test_list_groups(self):
# XXX Capture and check the output.
self.run_pyperformance("list_groups", capture=None)
###################################
# venv
def test_venv(self):
# XXX Capture and check the output.
root = self.resolve_tmp("venv", unique=True)
def div():
print()
print("---")
print()
def expect_success(*args):
self.run_pyperformance(
*args,
capture=None,
)
def expect_failure(*args):
self.run_pyperformance(
*args,
capture=None,
exitcode=1,
)
# It doesn't exist yet.
expect_success("venv", "show", "--venv", root)
div()
# It gets created.
expect_success("venv", "create", "--venv", root)
div()
expect_success("venv", "show", "--venv", root)
div()
# It alraedy exists.
expect_failure("venv", "create", "--venv", root)
div()
expect_success("venv", "show", "--venv", root)
div()
# It gets re-created.
expect_success("venv", "recreate", "--venv", root)
div()
expect_success("venv", "show", "--venv", root)
div()
# It get deleted.
expect_success("venv", "remove", "--venv", root)
div()
expect_success("venv", "show", "--venv", root)
###################################
# run
def test_run_and_show(self):
filename = self.resolve_tmp("bench.json")
# -b all: check that *all* benchmark work
#
# --debug-single-value: benchmark results don't matter, we only
# check that running benchmarks don't fail.
# XXX Capture and check the output.
self.run_pyperformance(
"run",
"-b",
"all",
"--debug-single-value",
"-o",
filename,
capture=None,
)
# Display slowest benchmarks
# XXX Capture and check the output.
self.run_module("pyperf", "slowest", filename)
def test_run_test_benchmarks(self):
# Run the benchmarks that exist only for testing
# in pyperformance/tests/data
filename = self.resolve_tmp("bench-test.json")
self.run_pyperformance(
"run",
"--manifest",
os.path.join(tests.DATA_DIR, "MANIFEST"),
"-b",
"all",
"-o",
filename,
capture=None,
)
def test_run_with_hook(self):
# We expect this to fail, since pystats requires a special build of Python
filename = self.resolve_tmp("bench-test-hook.json")
stdout = self.run_pyperformance(
"run",
"--manifest",
os.path.join(tests.DATA_DIR, "MANIFEST"),
"-b",
"all",
"-o",
filename,
"--hook",
"pystats",
exitcode=1,
capture="combined",
)
self.assertIn(
"Can not collect pystats because python was not built with --enable-pystats",
stdout,
)
###################################
# compile
def ensure_cpython_repo(self, reporoot=None):
if not reporoot:
reporoot = os.environ.get("PYPERFORMANCE_TESTS_CPYTHON")
if not reporoot:
reporoot = os.path.join(tests.DATA_DIR, "cpython")
for markerfile in [
os.path.join(reporoot, ".git"),
os.path.join(reporoot, "Python/ceval.c"),
]:
if not os.path.exists(markerfile):
break
else:
return reporoot
# Clone the repo.
print("#" * 40)
print("# cloning the cpython repo")
print("#" * 40)
print()
tests.run_cmd(
shutil.which("git"),
"clone",
"https://github.com/python/cpython",
reporoot,
)
print("#" * 40)
print("# DONE: cloning the cpython repo")
print("#" * 40)
print()
return reporoot
def create_compile_config(
self,
*revisions,
outdir=None,
fast=True,
upload=None,
):
if not outdir:
outdir = self.resolve_tmp("compile-cmd-outdir", unique=True)
cpython = self.ensure_cpython_repo()
text = textwrap.dedent(f"""
[config]
json_dir = {outdir}
debug = {fast}
[scm]
repo_dir = {cpython}
update = False
git_remote = remotes/origin
[compile]
bench_dir = {outdir}
lto = {not fast}
pgo = {not fast}
jit = no
install = True
[run_benchmark]
system_tune = False
upload = False
[upload]
url = {upload}
""")
if revisions:
text += "".join(
line + os.linesep
for line in [
"",
"[compile_all_revisions]",
*(f"{r} =" for r in revisions),
]
)
cfgfile = os.path.join(outdir, "compile.ini")
print(f"(writing config file to {cfgfile})")
os.makedirs(outdir, exist_ok=True)
with open(cfgfile, "w", encoding="utf-8") as outfile:
outfile.write(text)
return cfgfile
@tests.CPYTHON_ONLY
@tests.NON_WINDOWS_ONLY
@tests.SLOW
def test_compile(self):
cfgfile = self.create_compile_config()
revision = "a58ebcc701dd" # tag: v3.10.2
# XXX Capture and check the output.
self.run_pyperformance(
"compile",
cfgfile,
revision,
capture=None,
)
@tests.CPYTHON_ONLY
@tests.NON_WINDOWS_ONLY
@tests.SLOW
def test_compile_all(self):
rev1 = "2cd268a3a934" # tag: v3.10.1
rev2 = "a58ebcc701dd" # tag: v3.10.2
cfgfile = self.create_compile_config(rev1, rev2)
# XXX Capture and check the output.
self.run_pyperformance(
"compile_all",
cfgfile,
capture=None,
)
@tests.CPYTHON_ONLY
@tests.NON_WINDOWS_ONLY
@unittest.expectedFailure
def test_upload(self):
url = "<bogus>"
cfgfile = self.create_compile_config(upload=url)
resfile = os.path.join(tests.DATA_DIR, "py36.json")
# XXX Capture and check the output.
self.run_pyperformance(
"upload",
cfgfile,
resfile,
capture=None,
)
###################################
# show
def test_show(self):
for filename in (
os.path.join(tests.DATA_DIR, "py36.json"),
os.path.join(tests.DATA_DIR, "mem1.json"),
):
with self.subTest(filename):
# XXX Capture and check the output.
self.run_pyperformance("show", filename, capture=None)
###################################
# compare
def compare(self, *args, exitcode=0, dataset="py", file2="py38.json", **kw):
if dataset == "mem":
file1 = "mem1.json"
file2 = "mem2.json"
else:
file1 = "py36.json"
marker = file1
stdout = self.run_pyperformance(
"compare",
os.path.join(tests.DATA_DIR, file1),
os.path.join(tests.DATA_DIR, file2),
*args,
exitcode=exitcode,
verbose=False,
)
if marker in stdout:
stdout = stdout[stdout.index(marker) :]
return stdout + "\n"
def test_compare(self):
stdout = self.compare()
self.assertEqual(
stdout,
textwrap.dedent("""
py36.json
=========
Performance version: 1.0.1
Python version: 3.6.10 (64-bit)
Report on Linux-5.5.9-200.fc31.x86_64-x86_64-with-fedora-31-Thirty_One
Number of logical CPUs: 8
Start date: 2020-03-26 15:50:39.816020
End date: 2020-03-26 15:50:56.406559
py38.json
=========
Performance version: 1.0.1
Python version: 3.8.2 (64-bit)
Report on Linux-5.5.9-200.fc31.x86_64-x86_64-with-glibc2.2.5
Number of logical CPUs: 8
Start date: 2020-03-26 15:54:12.331569
End date: 2020-03-26 15:54:23.900355
### telco ###
Mean +- std dev: 10.7 ms +- 0.5 ms -> 7.2 ms +- 0.3 ms: 1.49x faster
Significant (t=44.97)
""").lstrip(),
)
def test_compare_wrong_version(self):
stdout = self.compare(file2="py3_performance03.json", exitcode=1)
self.assertEqual(
stdout,
textwrap.dedent("""
py36.json
=========
Performance version: 1.0.1
Python version: 3.6.10 (64-bit)
Report on Linux-5.5.9-200.fc31.x86_64-x86_64-with-fedora-31-Thirty_One
Number of logical CPUs: 8
Start date: 2020-03-26 15:50:39.816020
End date: 2020-03-26 15:50:56.406559
py3_performance03.json
======================
Performance version: 0.3
Skipped 1 benchmarks only in py36.json: telco
Skipped 1 benchmarks only in py3_performance03.json: call_simple
ERROR: Performance versions are different (1.0.1 != 0.3)
""").lstrip(),
)
def test_compare_single_value(self):
stdout = self.compare(dataset="mem")
self.assertEqual(
stdout,
textwrap.dedent("""
mem1.json
=========
Performance version: 0.2
mem2.json
=========
Performance version: 0.2
### call_simple ###
7896.0 KiB -> 7900.0 KiB: 1.00x larger
""").lstrip(),
)
def test_compare_csv(self):
expected = textwrap.dedent("""
Benchmark,Base,Changed
telco,0.01073,0.00722
""").lstrip()
filename = self.resolve_tmp("outfile.csv", unique=True)
with tests.CleanupFile(filename):
self.compare("--csv", filename)
with open(filename, "r", encoding="utf-8") as infile:
csv = infile.read()
self.assertEqual(csv, expected)
def test_compare_table(self):
stdout = self.compare("-O", "table")
self.assertEqual(
stdout,
textwrap.dedent("""
py36.json
=========
Performance version: 1.0.1
Python version: 3.6.10 (64-bit)
Report on Linux-5.5.9-200.fc31.x86_64-x86_64-with-fedora-31-Thirty_One
Number of logical CPUs: 8
Start date: 2020-03-26 15:50:39.816020
End date: 2020-03-26 15:50:56.406559
py38.json
=========
Performance version: 1.0.1
Python version: 3.8.2 (64-bit)
Report on Linux-5.5.9-200.fc31.x86_64-x86_64-with-glibc2.2.5
Number of logical CPUs: 8
Start date: 2020-03-26 15:54:12.331569
End date: 2020-03-26 15:54:23.900355
+-----------+-----------+-----------+--------------+-----------------------+
| Benchmark | py36.json | py38.json | Change | Significance |
+===========+===========+===========+==============+=======================+
| telco | 10.7 ms | 7.22 ms | 1.49x faster | Significant (t=44.97) |
+-----------+-----------+-----------+--------------+-----------------------+
""").lstrip(),
)
def test_compare_table_single_value(self):
stdout = self.compare("-O", "table", dataset="mem")
self.assertEqual(
stdout,
textwrap.dedent("""
mem1.json
=========
Performance version: 0.2
mem2.json
=========
Performance version: 0.2
+-------------+------------+------------+--------------+------------------------------------------+
| Benchmark | mem1.json | mem2.json | Change | Significance |
+=============+============+============+==============+==========================================+
| call_simple | 7896.0 KiB | 7900.0 KiB | 1.00x larger | (benchmark only contains a single value) |
+-------------+------------+------------+--------------+------------------------------------------+
""").lstrip(),
)
if __name__ == "__main__":
unittest.main()