44
44
45
45
46
46
_HRULE = '-' .join (['' for i in range (80 )])
47
- ATTR_BENCHMARK = '__benchmark__'
47
+
48
+ #: this function is used to pre-process the arguments as expected by the __benchmark__ and __setup__ entry points
48
49
ATTR_PROCESS_ARGS = '__process_args__'
50
+ #: gets called with the preprocessed arguments before __benchmark__
51
+ ATTR_SETUP = '__setup__'
52
+ #: gets called with the preprocessed arguments N times
53
+ ATTR_BENCHMARK = '__benchmark__'
54
+ #: performs any teardown needed in the benchmark
49
55
ATTR_TEARDOWN = '__teardown__'
50
56
51
57
@@ -132,7 +138,6 @@ def _call_attr(self, attr_name, *args):
132
138
return attr (* args )
133
139
134
140
def run (self ):
135
- print (_HRULE )
136
141
if self ._run_once :
137
142
print ("### %s, exactly one iteration (no warmup curves)" % (self .bench_module .__name__ ))
138
143
else :
@@ -144,9 +149,13 @@ def run(self):
144
149
# default args processor considers all args as ints
145
150
args = list (map (int , self .bench_args ))
146
151
147
- print ("### args = %s" % args )
152
+ print ("### args = " , args )
148
153
print (_HRULE )
149
154
155
+ print ("### setup ... " )
156
+ self ._call_attr (ATTR_SETUP , * args )
157
+ print ("### start benchmark ... " )
158
+
150
159
bench_func = self ._get_attr (ATTR_BENCHMARK )
151
160
if bench_func and hasattr (bench_func , '__call__' ):
152
161
if self .warmup :
@@ -163,16 +172,19 @@ def run(self):
163
172
else :
164
173
print ("### iteration=%s, name=%s, duration=%s" % (iteration , self .bench_module .__name__ , duration ))
165
174
166
- print ("teardown ... " )
175
+ print (_HRULE )
176
+ print ("### teardown ... " )
167
177
self ._call_attr (ATTR_TEARDOWN )
168
- print ("benchmark complete" )
178
+ print ("### benchmark complete" )
179
+ print (_HRULE )
169
180
170
181
171
- def run_benchmark (prog , args ):
182
+ def run_benchmark (args ):
172
183
warmup = 0
173
184
iterations = 1
174
185
bench_file = None
175
186
bench_args = []
187
+ paths = []
176
188
177
189
i = 0
178
190
while i < len (args ):
@@ -182,19 +194,36 @@ def run_benchmark(prog, args):
182
194
iterations = _as_int (args [i ])
183
195
elif arg .startswith ("--iterations" ):
184
196
iterations = _as_int (arg .split ("=" )[1 ])
197
+
185
198
elif arg == '-w' :
186
199
i += 1
187
200
warmup = _as_int (args [i ])
188
201
elif arg .startswith ("--warmup" ):
189
202
warmup = _as_int (arg .split ("=" )[1 ])
203
+
204
+ elif arg == '-p' :
205
+ i += 1
206
+ paths = args [i ].split ("," )
207
+ elif arg .startswith ("--path" ):
208
+ paths = arg .split ("=" )[1 ].split ("," )
209
+
190
210
elif bench_file is None :
191
211
bench_file = arg
192
212
else :
193
213
bench_args .append (arg )
194
214
i += 1
195
215
216
+ # set the paths if specified
217
+ print (_HRULE )
218
+ if paths :
219
+ for pth in paths :
220
+ print ("### adding module path: %s" % pth )
221
+ sys .path .append (pth )
222
+ else :
223
+ print ("### no extra module search paths specified" )
224
+
196
225
BenchRunner (bench_file , bench_args = bench_args , iterations = iterations , warmup = warmup ).run ()
197
226
198
227
199
228
if __name__ == '__main__' :
200
- run_benchmark (sys .argv [0 ], sys . argv [ 1 :])
229
+ run_benchmark (sys .argv [1 :])
0 commit comments