|
5 | 5 | Benchmark against pyvsc library for equivalent testcases. |
6 | 6 | ''' |
7 | 7 |
|
8 | | -import unittest |
9 | | -import timeit |
10 | | -import sys |
11 | 8 |
|
12 | | -from argparse import ArgumentParser |
13 | | -from random import Random |
| 9 | +from tests.main import main |
14 | 10 |
|
15 | | -from benchmarks.pyvsc.basic import vsc_basic, cr_basic |
16 | | -from benchmarks.pyvsc.in_keyword import vsc_in, cr_in, cr_in_order |
17 | | -from benchmarks.pyvsc.ldinstr import vsc_ldinstr |
18 | | -from benchmarks.pyvsc.randlist import vscRandListSumZero, \ |
19 | | - crRandListSumZero, vscRandListUnique, crRandListUnique, \ |
20 | | - crRandListSumZeroFaster, crRandListUniqueFaster |
21 | | -from examples.ldinstr import ldInstr |
22 | | - |
23 | | - |
24 | | -TEST_LENGTH_MULTIPLIER = 1 |
25 | | - |
26 | | - |
27 | | -class BenchmarkTests(unittest.TestCase): |
28 | | - |
29 | | - def run_one(self, name, randobj, iterations): |
30 | | - ''' |
31 | | - Benchmark one object that implements the .randomize() function |
32 | | - for N iterations. |
33 | | - ''' |
34 | | - start_time = timeit.default_timer() |
35 | | - for _ in range(iterations): |
36 | | - randobj.randomize() |
37 | | - end_time = timeit.default_timer() |
38 | | - total_time = end_time - start_time |
39 | | - hz = iterations / total_time |
40 | | - print(f'{self._testMethodName}: {name} took {total_time:.4g}s for {iterations} iterations ({hz:.1f}Hz)') |
41 | | - return total_time, hz |
42 | | - |
43 | | - def run_benchmark(self, randobjs, iterations, check): |
44 | | - ''' |
45 | | - Reusable function to run a fair benchmark between |
46 | | - two or more randomizable objects. |
47 | | -
|
48 | | - randobjs: dictionary where key is name, value is an object that implements .randomize() |
49 | | - iterations: how many times to call .randomize() |
50 | | - check: function taking a dictionary of results to check. |
51 | | - ''' |
52 | | - iterations *= TEST_LENGTH_MULTIPLIER |
53 | | - results = {} |
54 | | - winner = None |
55 | | - best_hz = 0 |
56 | | - for name, randobj in randobjs.items(): |
57 | | - total_time, hz = self.run_one(name, randobj, iterations) |
58 | | - if hz > best_hz: |
59 | | - winner = name |
60 | | - best_hz = hz |
61 | | - # Store both total time and Hz in case the test wants to specify |
62 | | - # checks on how long should be taken in wall clock time. |
63 | | - results[name] = total_time, hz |
64 | | - print(f'{self._testMethodName}: The winner is {winner} with {best_hz:.1f}Hz!') |
65 | | - # Print summary of hz delta |
66 | | - for name, (_total_time, hz) in results.items(): |
67 | | - if name == winner: |
68 | | - continue |
69 | | - speedup = best_hz / hz |
70 | | - print(f'{self._testMethodName}: {winner} was {speedup:.2f}x faster than {name}') |
71 | | - check(results) |
72 | | - |
73 | | - def test_basic(self): |
74 | | - ''' |
75 | | - Test basic randomizable object. |
76 | | - ''' |
77 | | - randobjs = {'vsc': vsc_basic(), 'cr': cr_basic(Random(0))} |
78 | | - def check(results): |
79 | | - self.assertGreater(results['cr'][1], results['vsc'][1]) |
80 | | - # This testcase is typically 40-50x faster, which may vary depending |
81 | | - # on machine. Ensure it doesn't fall below 30x. |
82 | | - speedup = results['cr'][1] / results['vsc'][1] |
83 | | - self.assertGreater(speedup, 30, "Performance has degraded!") |
84 | | - self.run_benchmark(randobjs, 100, check) |
85 | | - |
86 | | - def test_in(self): |
87 | | - ''' |
88 | | - Test object using 'in' keyword. |
89 | | - ''' |
90 | | - randobjs = { |
91 | | - 'vsc': vsc_in(), |
92 | | - 'cr': cr_in(Random(0)), |
93 | | - 'cr_order': cr_in_order(Random(0)), |
94 | | - } |
95 | | - def check(results): |
96 | | - self.assertGreater(results['cr'][1], results['vsc'][1]) |
97 | | - # This testcase is typically 13-15x faster, which may vary depending |
98 | | - # on machine. Ensure it doesn't fall below 10x. |
99 | | - speedup = results['cr'][1] / results['vsc'][1] |
100 | | - self.assertGreater(speedup, 10, "Performance has degraded!") |
101 | | - self.run_benchmark(randobjs, 100, check) |
102 | | - |
103 | | - def test_ldinstr(self): |
104 | | - ''' |
105 | | - Test LD instruction example. |
106 | | - ''' |
107 | | - randobjs = { |
108 | | - 'vsc': vsc_ldinstr(), |
109 | | - 'cr': ldInstr(Random(0)), |
110 | | - } |
111 | | - def check(results): |
112 | | - self.assertGreater(results['cr'][1], results['vsc'][1]) |
113 | | - # This testcase is typically 13-15x faster, which may vary depending |
114 | | - # on machine. Ensure it doesn't fall below 10x. |
115 | | - speedup = results['cr'][1] / results['vsc'][1] |
116 | | - self.assertGreater(speedup, 10, "Performance has degraded!") |
117 | | - self.run_benchmark(randobjs, 100, check) |
118 | | - |
119 | | - def test_randlist_sumzero(self): |
120 | | - ''' |
121 | | - Test random list example where the list must sum to zero. |
122 | | - ''' |
123 | | - randobjs = { |
124 | | - 'vsc': vscRandListSumZero(), |
125 | | - 'cr': crRandListSumZero(Random(0)), |
126 | | - 'cr_faster': crRandListSumZeroFaster(Random(0)), |
127 | | - } |
128 | | - def check(results): |
129 | | - self.assertGreater(results['cr'][1], results['vsc'][1]) |
130 | | - self.assertGreater(results['cr_faster'][1], results['vsc'][1]) |
131 | | - # This testcase is typically 20x faster, which may vary depending |
132 | | - # on machine. Ensure it doesn't fall below 15x. |
133 | | - speedup = results['cr'][1] / results['vsc'][1] |
134 | | - self.assertGreater(speedup, 15, "Performance has degraded!") |
135 | | - speedup = results['cr_faster'][1] / results['vsc'][1] |
136 | | - self.assertGreater(speedup, 15, "Performance has degraded!") |
137 | | - self.run_benchmark(randobjs, 100, check) |
138 | | - |
139 | | - def test_randlist_unique(self): |
140 | | - ''' |
141 | | - Test random list example where the list must be unique. |
142 | | - ''' |
143 | | - randobjs = { |
144 | | - 'vsc': vscRandListUnique(), |
145 | | - 'cr': crRandListUnique(Random(0)), |
146 | | - 'cr_faster': crRandListUniqueFaster(Random(0)), |
147 | | - } |
148 | | - def check(results): |
149 | | - self.assertGreater(results['cr_faster'][1], results['vsc'][1]) |
150 | | - self.assertGreater(results['cr'][1], results['vsc'][1]) |
151 | | - # With the naive solver, this testcase is typically 3-4x faster, |
152 | | - # which may vary depending on machine. Ensure it doesn't fall |
153 | | - # below 2x. |
154 | | - speedup = results['cr'][1] / results['vsc'][1] |
155 | | - self.assertGreater(speedup, 2, "Performance has degraded!") |
156 | | - # This testcase is typically 10-13x faster, which may vary depending |
157 | | - # on machine. Ensure it doesn't fall below 10x. |
158 | | - speedup = results['cr_faster'][1] / results['vsc'][1] |
159 | | - self.assertGreater(speedup, 10, "Performance has degraded!") |
160 | | - self.run_benchmark(randobjs, 100, check) |
161 | | - |
162 | | - |
163 | | -def parse_args(): |
164 | | - parser = ArgumentParser(description='Run unit tests for constrainedrandom library') |
165 | | - parser.add_argument('--length-mul', type=int, default=1, help='Multiplier for test length, when desiring greater certainty on performance.') |
166 | | - args, extra = parser.parse_known_args() |
167 | | - return args, extra |
| 11 | +from benchmarks.pyvsc.basic import VSCBasic |
| 12 | +from benchmarks.pyvsc.in_keyword import VSCIn |
| 13 | +from benchmarks.pyvsc.ldinstr import VSCInstr |
| 14 | +from benchmarks.pyvsc.randlist import VSCRandListSumZero |
| 15 | +from benchmarks.pyvsc.randlist import VSCRandListUnique |
168 | 16 |
|
169 | 17 |
|
170 | 18 | if __name__ == "__main__": |
171 | | - args, extra = parse_args() |
172 | | - TEST_LENGTH_MULTIPLIER = args.length_mul |
173 | | - # Reconstruct argv |
174 | | - argv = [sys.argv[0]] + extra |
175 | | - unittest.main(argv=argv) |
| 19 | + main() |
0 commit comments