|
43 | 43 | import sys
|
44 | 44 | from time import time
|
45 | 45 |
|
46 |
| -_HRULE = '-'.join(['' for i in range(120)]) |
47 |
| -ATTR_WARMUP = '__warmup__' |
| 46 | +_HRULE = '-'.join(['' for i in range(80)]) |
48 | 47 | ATTR_BENCHMARK = '__benchmark__'
|
49 | 48 |
|
50 | 49 |
|
| 50 | +def _as_int(value): |
| 51 | + if isinstance(value, (list, tuple)): |
| 52 | + value = value[0] |
| 53 | + |
| 54 | + if not isinstance(value, int): |
| 55 | + return int(value) |
| 56 | + return value |
| 57 | + |
| 58 | + |
51 | 59 | class BenchRunner(object):
|
52 |
| - def __init__(self, bench_file, bench_args=None, iterations=1, verbose=False): |
| 60 | + def __init__(self, bench_file, bench_args=None, iterations=1, warmup=0): |
53 | 61 | if bench_args is None:
|
54 | 62 | bench_args = []
|
55 | 63 | self.bench_module = BenchRunner.get_bench_module(bench_file)
|
56 | 64 | self.bench_args = bench_args
|
57 |
| - self.verbose = verbose |
58 |
| - if isinstance(iterations, (list, tuple)): |
59 |
| - iterations = iterations[0] |
60 |
| - if isinstance(iterations, str): |
61 |
| - iterations = int(iterations) |
62 |
| - self.iterations = iterations |
| 65 | + self.iterations = _as_int(iterations) |
| 66 | + assert isinstance(self.iterations, int) |
| 67 | + self.warmup = _as_int(warmup) |
| 68 | + assert isinstance(self.warmup, int) |
63 | 69 |
|
64 | 70 | @staticmethod
|
65 | 71 | def get_bench_module(bench_file):
|
@@ -95,33 +101,33 @@ def _call_attr(self, attr_name):
|
95 | 101 | attr()
|
96 | 102 |
|
97 | 103 | def run(self):
|
98 |
| - if self.verbose: |
99 |
| - print(_HRULE) |
100 |
| - print(self.bench_module.__name__) |
101 |
| - print(_HRULE) |
102 |
| - |
103 |
| - print("### warming up ... ") |
104 |
| - self._call_attr(ATTR_WARMUP) |
105 |
| - print("### running benchmark ... ") |
| 104 | + print(_HRULE) |
| 105 | + print("### %s, %s warmup iterations, %s bench iterations " % (self.bench_module.__name__, self.warmup, self.iterations)) |
| 106 | + print(_HRULE) |
106 | 107 |
|
107 | 108 | bench_func = self._get_attr(ATTR_BENCHMARK)
|
108 | 109 | if bench_func and hasattr(bench_func, '__call__'):
|
109 |
| - for i in range(self.iterations): |
| 110 | + if self.warmup: |
| 111 | + print("### warming up for %s iterations ... " % self.warmup) |
| 112 | + for _ in range(self.warmup): |
| 113 | + bench_func(*self.bench_args) |
| 114 | + |
| 115 | + for iteration in range(self.iterations): |
110 | 116 | start = time()
|
111 | 117 | bench_func(*self.bench_args)
|
112 |
| - duration = "%.3f\n" % (time() - start) |
113 |
| - print("### iteration={}, name={}, duration={}".format(i, self.bench_module.__name__, duration)) |
| 118 | + duration = "%.3f" % (time() - start) |
| 119 | + print("### iteration=%s, name=%s, duration=%s" % (iteration, self.bench_module.__name__, duration)) |
114 | 120 |
|
115 | 121 |
|
116 | 122 | def run_benchmark(prog, args):
|
117 | 123 | parser = argparse.ArgumentParser(prog=prog, description="Run specified benchmark.")
|
118 |
| - parser.add_argument("-v", "--verbose", help="Verbose output.", action="store_true") |
| 124 | + parser.add_argument("-w", "--warmup", help="The number of iterations to skip as warmup.", default=0) |
119 | 125 | parser.add_argument("-i", "--iterations", help="The number of iterations top run each benchmark.", default=1)
|
120 | 126 | parser.add_argument("bench_file", metavar='BENCH', help="Path to the benchmark to execute.", nargs=1)
|
121 | 127 | parser.add_argument("bench_args", metavar='ARGS', help="Path to the benchmarks to execute.", nargs='*', default=None)
|
122 | 128 |
|
123 | 129 | args = parser.parse_args(args)
|
124 |
| - BenchRunner(args.bench_file[0], bench_args=args.bench_args, iterations=args.iterations, verbose=args.verbose).run() |
| 130 | + BenchRunner(args.bench_file[0], bench_args=args.bench_args, iterations=args.iterations, warmup=args.warmup).run() |
125 | 131 |
|
126 | 132 |
|
127 | 133 | if __name__ == '__main__':
|
|
0 commit comments