Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8dd6484

Browse files
committed
Add some infrastructure for benchmark tests that don't run by default
And add in one simple one
1 parent e9291d5 commit 8dd6484

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
pytest_plugins = [
22
'mypy.test.data',
33
]
4+
# This function name is special to pytest. See
5+
# http://doc.pytest.org/en/latest/writing_plugins.html#initialization-command-line-and-configuration-hooks
6+
def pytest_addoption(parser)-> None:
7+
parser.addoption('--bench', action='store_true', default=False,
8+
help='Enable the benchmark test runs')

mypyc/test/test_bench.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""Benchmark run test cases that don't run by default
2+
3+
All of the real code for this lives in test_run.py.
4+
"""
5+
6+
# We can't "import TestRun from ..." because that will cause pytest
7+
# to collect the non-caching tests when running this file.
8+
import mypyc.test.test_run
9+
10+
11+
class TestBench(mypyc.test.test_run.TestRun):
12+
benchmark = True
13+
files = ['run-bench.test']

mypyc/test/test_run.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@
1717
ICODE_GEN_BUILTINS, use_custom_builtins, MypycDataSuite, assert_test_output,
1818
)
1919

20+
import pytest # type: ignore # no pytest in typeshed
2021

2122
files = ['run.test',
2223
'run-classes.test']
2324

24-
2525
class TestRun(MypycDataSuite):
2626
"""Test cases that build a C extension and run code."""
2727
files = files
2828
base_path = test_temp_dir
2929
optional_out = True
30+
benchmark = False
3031

3132
def run_case(self, testcase: DataDrivenTestCase) -> None:
33+
if self.benchmark and not testcase.config.getoption('--bench', False):
34+
pytest.skip()
35+
3236
with use_custom_builtins(os.path.join(self.data_prefix, ICODE_GEN_BUILTINS), testcase):
3337
text = '\n'.join(testcase.input)
3438

@@ -42,6 +46,8 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
4246
source_path = 'tmp/py/native.py'
4347
with open(source_path, 'w') as f:
4448
f.write(text)
49+
with open('tmp/interpreted.py', 'w') as f:
50+
f.write(text)
4551

4652
source = build.BuildSource(source_path, 'native', text)
4753

@@ -90,7 +96,11 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
9096
print('*** Exit status: %d' % proc.returncode)
9197

9298
# Verify output.
93-
assert_test_output(testcase, outlines, 'Invalid output')
99+
if self.benchmark:
100+
print('Test output:')
101+
print(output)
102+
else:
103+
assert_test_output(testcase, outlines, 'Invalid output')
94104

95105
assert proc.returncode == 0
96106

test-data/run-bench.test

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
[case testTree]
2+
from typing import Optional
3+
# This is bullshit
4+
class Node:
5+
def __init__(self) -> None:
6+
self.value = 0
7+
self.left = None # type: Optional[Node]
8+
self.right = None # type: Optional[Node]
9+
def sum(self) -> int:
10+
left = 0
11+
if self.left is not None:
12+
left = self.left.sum()
13+
right = 0
14+
if self.right is not None:
15+
right = self.right.sum()
16+
return self.value + left + right
17+
def node(v: int) -> Node:
18+
x = Node()
19+
x.value = v
20+
x.left = None
21+
x.right = None
22+
return x
23+
def sum_tree(x: Optional[Node]) -> int:
24+
if x is None:
25+
return 0
26+
return x.value + sum_tree(x.left) + sum_tree(x.right)
27+
def lol(n: int) -> Optional[Node]:
28+
if n == 0:
29+
return None
30+
x = node(n)
31+
x.left = lol(n - 1)
32+
x.right = x.left
33+
return x
34+
[file driver.py]
35+
from typing import Optional
36+
import native
37+
import interpreted
38+
from timeit import timeit
39+
40+
def test(m):
41+
tree = m.lol(5)
42+
assert(m.sum_tree(tree) == 57)
43+
assert(tree.sum() == 57)
44+
45+
g = {**globals(), **locals()}
46+
sum = timeit('m.sum_tree(tree)', globals=g)
47+
sum2 = timeit('tree.sum()', globals=g)
48+
build = timeit('m.lol(5)', globals=g)
49+
return (sum, sum2, build)
50+
51+
nsum, nsum2, nbuild = test(native)
52+
isum, isum2, ibuild = test(interpreted)
53+
print("Sum speedup:", isum/nsum)
54+
print("Sum method speedup:", isum2/nsum2)
55+
print("Build speedup:", ibuild/nbuild)

0 commit comments

Comments
 (0)