4444
4545
4646_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
4849ATTR_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
4955ATTR_TEARDOWN = '__teardown__'
5056
5157
@@ -132,7 +138,6 @@ def _call_attr(self, attr_name, *args):
132138 return attr (* args )
133139
134140 def run (self ):
135- print (_HRULE )
136141 if self ._run_once :
137142 print ("### %s, exactly one iteration (no warmup curves)" % (self .bench_module .__name__ ))
138143 else :
@@ -144,9 +149,13 @@ def run(self):
144149 # default args processor considers all args as ints
145150 args = list (map (int , self .bench_args ))
146151
147- print ("### args = %s" % args )
152+ print ("### args = " , args )
148153 print (_HRULE )
149154
155+ print ("### setup ... " )
156+ self ._call_attr (ATTR_SETUP , * args )
157+ print ("### start benchmark ... " )
158+
150159 bench_func = self ._get_attr (ATTR_BENCHMARK )
151160 if bench_func and hasattr (bench_func , '__call__' ):
152161 if self .warmup :
@@ -163,16 +172,19 @@ def run(self):
163172 else :
164173 print ("### iteration=%s, name=%s, duration=%s" % (iteration , self .bench_module .__name__ , duration ))
165174
166- print ("teardown ... " )
175+ print (_HRULE )
176+ print ("### teardown ... " )
167177 self ._call_attr (ATTR_TEARDOWN )
168- print ("benchmark complete" )
178+ print ("### benchmark complete" )
179+ print (_HRULE )
169180
170181
171- def run_benchmark (prog , args ):
182+ def run_benchmark (args ):
172183 warmup = 0
173184 iterations = 1
174185 bench_file = None
175186 bench_args = []
187+ paths = []
176188
177189 i = 0
178190 while i < len (args ):
@@ -182,19 +194,36 @@ def run_benchmark(prog, args):
182194 iterations = _as_int (args [i ])
183195 elif arg .startswith ("--iterations" ):
184196 iterations = _as_int (arg .split ("=" )[1 ])
197+
185198 elif arg == '-w' :
186199 i += 1
187200 warmup = _as_int (args [i ])
188201 elif arg .startswith ("--warmup" ):
189202 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+
190210 elif bench_file is None :
191211 bench_file = arg
192212 else :
193213 bench_args .append (arg )
194214 i += 1
195215
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+
196225 BenchRunner (bench_file , bench_args = bench_args , iterations = iterations , warmup = warmup ).run ()
197226
198227
199228if __name__ == '__main__' :
200- run_benchmark (sys .argv [0 ], sys . argv [ 1 :])
229+ run_benchmark (sys .argv [1 :])
0 commit comments