1import pytest
2from pytest import mark
3from pytest import raises
4
5
6def test_single(benchmark):
7    runs = []
8    benchmark.pedantic(runs.append, args=[123])
9    assert runs == [123]
10
11
12def test_setup(benchmark):
13    runs = []
14
15    def stuff(foo, bar=123):
16        runs.append((foo, bar))
17
18    def setup():
19        return [1], {"bar": 2}
20
21    benchmark.pedantic(stuff, setup=setup)
22    assert runs == [(1, 2)]
23
24
25@pytest.mark.benchmark(cprofile=True)
26def test_setup_cprofile(benchmark):
27    runs = []
28
29    def stuff(foo, bar=123):
30        runs.append((foo, bar))
31
32    def setup():
33        return [1], {"bar": 2}
34
35    benchmark.pedantic(stuff, setup=setup)
36    assert runs == [(1, 2), (1, 2)]
37
38
39def test_args_kwargs(benchmark):
40    runs = []
41
42    def stuff(foo, bar=123):
43        runs.append((foo, bar))
44
45    benchmark.pedantic(stuff, args=[1], kwargs={"bar": 2})
46    assert runs == [(1, 2)]
47
48
49def test_iterations(benchmark):
50    runs = []
51
52    benchmark.pedantic(runs.append, args=[1], iterations=10)
53    assert runs == [1] * 11
54
55
56def test_rounds_iterations(benchmark):
57    runs = []
58
59    benchmark.pedantic(runs.append, args=[1], iterations=10, rounds=15)
60    assert runs == [1] * 151
61
62
63def test_rounds(benchmark):
64    runs = []
65
66    benchmark.pedantic(runs.append, args=[1], rounds=15)
67    assert runs == [1] * 15
68
69
70def test_warmup_rounds(benchmark):
71    runs = []
72
73    benchmark.pedantic(runs.append, args=[1], warmup_rounds=15, rounds=5)
74    assert runs == [1] * 20
75
76
77@mark.parametrize("value", [0, "x"])
78def test_rounds_must_be_int(benchmark, value):
79    runs = []
80    raises(ValueError, benchmark.pedantic, runs.append, args=[1], rounds=value)
81    assert runs == []
82
83
84@mark.parametrize("value", [-15, "x"])
85def test_warmup_rounds_must_be_int(benchmark, value):
86    runs = []
87    raises(ValueError, benchmark.pedantic, runs.append, args=[1], warmup_rounds=value)
88    assert runs == []
89
90
91def test_setup_many_rounds(benchmark):
92    runs = []
93
94    def stuff(foo, bar=123):
95        runs.append((foo, bar))
96
97    def setup():
98        return [1], {"bar": 2}
99
100    benchmark.pedantic(stuff, setup=setup, rounds=10)
101    assert runs == [(1, 2)] * 10
102
103
104def test_cant_use_both_args_and_setup_with_return(benchmark):
105    runs = []
106
107    def stuff(foo, bar=123):
108        runs.append((foo, bar))
109
110    def setup():
111        return [1], {"bar": 2}
112
113    raises(TypeError, benchmark.pedantic, stuff, setup=setup, args=[123])
114    assert runs == []
115
116
117def test_can_use_both_args_and_setup_without_return(benchmark):
118    runs = []
119
120    def stuff(foo, bar=123):
121        runs.append((foo, bar))
122
123    benchmark.pedantic(stuff, setup=lambda: None, args=[123])
124    assert runs == [(123, 123)]
125
126
127def test_cant_use_setup_with_many_iterations(benchmark):
128    raises(ValueError, benchmark.pedantic, None, setup=lambda: None, iterations=2)
129
130
131@mark.parametrize("value", [0, -1, "asdf"])
132def test_iterations_must_be_positive_int(benchmark, value):
133    raises(ValueError, benchmark.pedantic, None, setup=lambda: None, iterations=value)
134