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
|
import pytest
def test_single(benchmark):
runs = []
benchmark.pedantic(runs.append, args=[123])
assert runs == [123]
def test_setup(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
def setup():
return [1], {'bar': 2}
benchmark.pedantic(stuff, setup=setup)
assert runs == [(1, 2)]
def test_teardown(benchmark):
runs = []
def stuff(foo, bar=1234):
runs.append((foo, bar))
def teardown(foo, bar=1234):
assert foo == 1
assert bar == 2
runs.append('teardown')
benchmark.pedantic(stuff, args=[1], kwargs={'bar': 2}, teardown=teardown)
assert runs == [(1, 2), 'teardown']
@pytest.mark.benchmark(cprofile=True)
def test_setup_cprofile(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
def setup():
return [1], {'bar': 2}
benchmark.pedantic(stuff, setup=setup)
assert runs == [(1, 2), (1, 2)]
@pytest.mark.benchmark(cprofile=True)
def test_teardown_cprofile(benchmark):
runs = []
def stuff():
runs.append('stuff')
def teardown():
runs.append('teardown')
benchmark.pedantic(stuff, teardown=teardown)
assert runs == ['stuff', 'teardown', 'stuff', 'teardown']
runs = []
def test_args_kwargs(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
benchmark.pedantic(stuff, args=[1], kwargs={'bar': 2})
assert runs == [(1, 2)]
def test_iterations(benchmark):
runs = []
benchmark.pedantic(runs.append, args=[1], iterations=10)
assert runs == [1] * 11
def test_rounds_iterations(benchmark):
runs = []
benchmark.pedantic(runs.append, args=[1], iterations=10, rounds=15)
assert runs == [1] * 151
def test_rounds(benchmark):
runs = []
benchmark.pedantic(runs.append, args=[1], rounds=15)
assert runs == [1] * 15
def test_warmup_rounds(benchmark):
runs = []
benchmark.pedantic(runs.append, args=[1], warmup_rounds=15, rounds=5)
assert runs == [1] * 20
@pytest.mark.parametrize('value', [0, 'x'])
def test_rounds_must_be_int(benchmark, value):
runs = []
pytest.raises(ValueError, benchmark.pedantic, runs.append, args=[1], rounds=value)
assert runs == []
@pytest.mark.parametrize('value', [-15, 'x'])
def test_warmup_rounds_must_be_int(benchmark, value):
runs = []
pytest.raises(ValueError, benchmark.pedantic, runs.append, args=[1], warmup_rounds=value)
assert runs == []
def test_setup_many_rounds(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
def setup():
return [1], {'bar': 2}
benchmark.pedantic(stuff, setup=setup, rounds=10)
assert runs == [(1, 2)] * 10
def test_teardown_many_rounds(benchmark):
runs = []
def stuff():
runs.append('stuff')
def teardown():
runs.append('teardown')
benchmark.pedantic(stuff, teardown=teardown, rounds=10)
assert runs == ['stuff', 'teardown'] * 10
def test_teardown_many_iterations(benchmark):
runs = []
def stuff():
runs.append('stuff')
def teardown():
runs.append('teardown')
benchmark.pedantic(stuff, teardown=teardown, iterations=3)
assert runs == [
'stuff',
'stuff',
'stuff',
'teardown', # first round
'stuff',
'teardown', # computing the final result
]
def test_cant_use_both_args_and_setup_with_return(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
def setup():
return [1], {'bar': 2}
pytest.raises(TypeError, benchmark.pedantic, stuff, setup=setup, args=[123])
assert runs == []
def test_can_use_both_args_and_setup_without_return(benchmark):
runs = []
def stuff(foo, bar=123):
runs.append((foo, bar))
benchmark.pedantic(stuff, setup=lambda: None, args=[123])
assert runs == [(123, 123)]
def test_cant_use_setup_with_many_iterations(benchmark):
pytest.raises(ValueError, benchmark.pedantic, None, setup=lambda: None, iterations=2)
@pytest.mark.parametrize('value', [0, -1, 'asdf'])
def test_iterations_must_be_positive_int(benchmark, value):
pytest.raises(ValueError, benchmark.pedantic, None, setup=lambda: None, iterations=value)
|