blob: dd398f5cbadfa63867fb6c20729c1c37bfa85c18 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001#!/usr/local/bin/python -O
2
3""" A Python Benchmark Suite
4
5"""
6#
7# Note: Please keep this module compatible to Python 1.5.2.
8#
9# Tests may include features in later Python versions, but these
10# should then be embedded in try-except clauses in the configuration
11# module Setup.py.
12#
13
14# pybench Copyright
15__copyright__ = """\
16Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)
17Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com)
18
19 All Rights Reserved.
20
21Permission to use, copy, modify, and distribute this software and its
22documentation for any purpose and without fee or royalty is hereby
23granted, provided that the above copyright notice appear in all copies
24and that both that copyright notice and this permission notice appear
25in supporting documentation or portions thereof, including
26modifications, that you make.
27
28THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO
29THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
30FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,
31INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
32FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
33NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
34WITH THE USE OR PERFORMANCE OF THIS SOFTWARE !
35"""
36
Guido van Rossum486364b2007-06-30 05:01:58 +000037import sys, time, operator, platform
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000038from CommandLine import *
39
40try:
41 import cPickle
42 pickle = cPickle
43except ImportError:
44 import pickle
45
Thomas Wouters0e3f5912006-08-11 14:57:12 +000046# Version number; version history: see README file !
47__version__ = '2.0'
48
49### Constants
50
51# Second fractions
52MILLI_SECONDS = 1e3
53MICRO_SECONDS = 1e6
54
55# Percent unit
56PERCENT = 100
57
58# Horizontal line length
59LINE = 79
60
61# Minimum test run-time
62MIN_TEST_RUNTIME = 1e-3
63
64# Number of calibration runs to use for calibrating the tests
65CALIBRATION_RUNS = 20
66
67# Number of calibration loops to run for each calibration run
68CALIBRATION_LOOPS = 20
69
70# Allow skipping calibration ?
71ALLOW_SKIPPING_CALIBRATION = 1
72
73# Timer types
74TIMER_TIME_TIME = 'time.time'
75TIMER_TIME_CLOCK = 'time.clock'
76TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime'
77
78# Choose platform default timer
79if sys.platform[:3] == 'win':
80 # On WinXP this has 2.5ms resolution
81 TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK
82else:
83 # On Linux this has 1ms resolution
84 TIMER_PLATFORM_DEFAULT = TIMER_TIME_TIME
85
86# Print debug information ?
87_debug = 0
88
89### Helpers
90
91def get_timer(timertype):
92
93 if timertype == TIMER_TIME_TIME:
94 return time.time
95 elif timertype == TIMER_TIME_CLOCK:
96 return time.clock
97 elif timertype == TIMER_SYSTIMES_PROCESSTIME:
98 import systimes
99 return systimes.processtime
100 else:
101 raise TypeError('unknown timer type: %s' % timertype)
102
103def get_machine_details():
104
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000105 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000106 print('Getting machine details...')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000107 buildno, builddate = platform.python_build()
108 python = platform.python_version()
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000109 try:
110 unichr(100000)
111 except ValueError:
112 # UCS2 build (standard)
113 unicode = 'UCS2'
114 except NameError:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000115 unicode = None
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000116 else:
117 # UCS4 build (most recent Linux distros)
118 unicode = 'UCS4'
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000119 bits, linkage = platform.architecture()
120 return {
121 'platform': platform.platform(),
122 'processor': platform.processor(),
123 'executable': sys.executable,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000124 'implementation': platform.python_implementation(),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000125 'python': platform.python_version(),
126 'compiler': platform.python_compiler(),
127 'buildno': buildno,
128 'builddate': builddate,
129 'unicode': unicode,
130 'bits': bits,
131 }
132
133def print_machine_details(d, indent=''):
134
135 l = ['Machine Details:',
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000136 ' Platform ID: %s' % d.get('platform', 'n/a'),
137 ' Processor: %s' % d.get('processor', 'n/a'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000138 '',
139 'Python:',
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000140 ' Implementation: %s' % d.get('implementation', 'n/a'),
141 ' Executable: %s' % d.get('executable', 'n/a'),
142 ' Version: %s' % d.get('python', 'n/a'),
143 ' Compiler: %s' % d.get('compiler', 'n/a'),
144 ' Bits: %s' % d.get('bits', 'n/a'),
145 ' Build: %s (#%s)' % (d.get('builddate', 'n/a'),
146 d.get('buildno', 'n/a')),
147 ' Unicode: %s' % d.get('unicode', 'n/a'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000148 ]
Guido van Rossum486364b2007-06-30 05:01:58 +0000149 joiner = '\n' + indent
150 print(indent + joiner.join(l) + '\n')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152### Test baseclass
153
154class Test:
155
156 """ All test must have this class as baseclass. It provides
157 the necessary interface to the benchmark machinery.
158
159 The tests must set .rounds to a value high enough to let the
160 test run between 20-50 seconds. This is needed because
161 clock()-timing only gives rather inaccurate values (on Linux,
162 for example, it is accurate to a few hundreths of a
163 second). If you don't want to wait that long, use a warp
164 factor larger than 1.
165
166 It is also important to set the .operations variable to a
167 value representing the number of "virtual operations" done per
168 call of .run().
169
170 If you change a test in some way, don't forget to increase
Guido van Rossumd8faa362007-04-27 19:54:29 +0000171 its version number.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000172
173 """
174
175 ### Instance variables that each test should override
176
177 # Version number of the test as float (x.yy); this is important
178 # for comparisons of benchmark runs - tests with unequal version
179 # number will not get compared.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000180 version = 2.0
Thomas Wouters477c8d52006-05-27 19:21:47 +0000181
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000182 # The number of abstract operations done in each round of the
183 # test. An operation is the basic unit of what you want to
184 # measure. The benchmark will output the amount of run-time per
185 # operation. Note that in order to raise the measured timings
186 # significantly above noise level, it is often required to repeat
187 # sets of operations more than once per test round. The measured
188 # overhead per test round should be less than 1 second.
189 operations = 1
190
191 # Number of rounds to execute per test run. This should be
192 # adjusted to a figure that results in a test run-time of between
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000193 # 1-2 seconds.
194 rounds = 100000
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000195
196 ### Internal variables
197
198 # Mark this class as implementing a test
199 is_a_test = 1
200
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000201 # Last timing: (real, run, overhead)
202 last_timing = (0.0, 0.0, 0.0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000203
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204 # Warp factor to use for this test
205 warp = 1
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000206
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000207 # Number of calibration runs to use
208 calibration_runs = CALIBRATION_RUNS
209
210 # List of calibration timings
211 overhead_times = None
212
213 # List of test run timings
214 times = []
215
216 # Timer used for the benchmark
217 timer = TIMER_PLATFORM_DEFAULT
218
219 def __init__(self, warp=None, calibration_runs=None, timer=None):
220
221 # Set parameters
222 if warp is not None:
223 self.rounds = int(self.rounds / warp)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000224 if self.rounds == 0:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000225 raise ValueError('warp factor set too high')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226 self.warp = warp
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000227 if calibration_runs is not None:
228 if (not ALLOW_SKIPPING_CALIBRATION and
229 calibration_runs < 1):
230 raise ValueError('at least one calibration run is required')
231 self.calibration_runs = calibration_runs
232 if timer is not None:
233 timer = timer
234
235 # Init variables
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000236 self.times = []
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000237 self.overhead_times = []
238
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000239 # We want these to be in the instance dict, so that pickle
240 # saves them
241 self.version = self.version
242 self.operations = self.operations
243 self.rounds = self.rounds
244
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245 def get_timer(self):
246
247 """ Return the timer function to use for the test.
248
249 """
250 return get_timer(self.timer)
251
252 def compatible(self, other):
253
254 """ Return 1/0 depending on whether the test is compatible
255 with the other Test instance or not.
256
257 """
258 if self.version != other.version:
259 return 0
260 if self.rounds != other.rounds:
261 return 0
262 return 1
263
264 def calibrate_test(self):
265
266 if self.calibration_runs == 0:
267 self.overhead_times = [0.0]
268 return
269
270 calibrate = self.calibrate
271 timer = self.get_timer()
272 calibration_loops = range(CALIBRATION_LOOPS)
273
274 # Time the calibration loop overhead
275 prep_times = []
276 for i in range(self.calibration_runs):
277 t = timer()
278 for i in calibration_loops:
279 pass
280 t = timer() - t
281 prep_times.append(t)
282 min_prep_time = min(prep_times)
283 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000284 print()
285 print('Calib. prep time = %.6fms' % (
286 min_prep_time * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287
288 # Time the calibration runs (doing CALIBRATION_LOOPS loops of
289 # .calibrate() method calls each)
290 for i in range(self.calibration_runs):
291 t = timer()
292 for i in calibration_loops:
293 calibrate()
294 t = timer() - t
295 self.overhead_times.append(t / CALIBRATION_LOOPS
296 - min_prep_time)
297
298 # Check the measured times
299 min_overhead = min(self.overhead_times)
300 max_overhead = max(self.overhead_times)
301 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000302 print('Calib. overhead time = %.6fms' % (
303 min_overhead * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000304 if min_overhead < 0.0:
305 raise ValueError('calibration setup did not work')
306 if max_overhead - min_overhead > 0.1:
307 raise ValueError(
308 'overhead calibration timing range too inaccurate: '
309 '%r - %r' % (min_overhead, max_overhead))
310
311 def run(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312
313 """ Run the test in two phases: first calibrate, then
314 do the actual test. Be careful to keep the calibration
315 timing low w/r to the test timing.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000316
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000317 """
318 test = self.test
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000319 timer = self.get_timer()
320
321 # Get calibration
322 min_overhead = min(self.overhead_times)
323
324 # Test run
325 t = timer()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000326 test()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000327 t = timer() - t
328 if t < MIN_TEST_RUNTIME:
329 raise ValueError('warp factor too high: '
330 'test times are < 10ms')
331 eff_time = t - min_overhead
332 if eff_time < 0:
333 raise ValueError('wrong calibration')
334 self.last_timing = (eff_time, t, min_overhead)
335 self.times.append(eff_time)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336
337 def calibrate(self):
338
Thomas Wouters477c8d52006-05-27 19:21:47 +0000339 """ Calibrate the test.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340
Thomas Wouters477c8d52006-05-27 19:21:47 +0000341 This method should execute everything that is needed to
342 setup and run the test - except for the actual operations
343 that you intend to measure. pybench uses this method to
344 measure the test implementation overhead.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000345
346 """
347 return
348
349 def test(self):
350
Thomas Wouters477c8d52006-05-27 19:21:47 +0000351 """ Run the test.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000352
Thomas Wouters477c8d52006-05-27 19:21:47 +0000353 The test needs to run self.rounds executing
354 self.operations number of operations each.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355
356 """
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000357 return
Thomas Wouters477c8d52006-05-27 19:21:47 +0000358
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 def stat(self):
360
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000361 """ Return test run statistics as tuple:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000362
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000363 (minimum run time,
364 average run time,
365 total run time,
366 average time per operation,
367 minimum overhead time)
368
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 """
370 runs = len(self.times)
371 if runs == 0:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000372 return 0.0, 0.0, 0.0, 0.0
373 min_time = min(self.times)
Guido van Rossum89da5d72006-08-22 00:21:25 +0000374 total_time = sum(self.times)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000375 avg_time = total_time / float(runs)
376 operation_avg = total_time / float(runs
377 * self.rounds
378 * self.operations)
379 if self.overhead_times:
380 min_overhead = min(self.overhead_times)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000381 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000382 min_overhead = self.last_timing[2]
383 return min_time, avg_time, total_time, operation_avg, min_overhead
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384
385### Load Setup
386
387# This has to be done after the definition of the Test class, since
388# the Setup module will import subclasses using this class.
389
390import Setup
391
392### Benchmark base class
393
394class Benchmark:
395
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000396 # Name of the benchmark
397 name = ''
398
399 # Number of benchmark rounds to run
400 rounds = 1
401
402 # Warp factor use to run the tests
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000403 warp = 1 # Warp factor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000405 # Average benchmark round time
406 roundtime = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000408 # Benchmark version number as float x.yy
409 version = 2.0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000411 # Produce verbose output ?
412 verbose = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000414 # Dictionary with the machine details
415 machine_details = None
416
417 # Timer used for the benchmark
418 timer = TIMER_PLATFORM_DEFAULT
419
420 def __init__(self, name, verbose=None, timer=None, warp=None,
421 calibration_runs=None):
422
423 if name:
424 self.name = name
Thomas Wouters477c8d52006-05-27 19:21:47 +0000425 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000426 self.name = '%04i-%02i-%02i %02i:%02i:%02i' % \
427 (time.localtime(time.time())[:6])
428 if verbose is not None:
429 self.verbose = verbose
430 if timer is not None:
431 self.timer = timer
432 if warp is not None:
433 self.warp = warp
434 if calibration_runs is not None:
435 self.calibration_runs = calibration_runs
436
437 # Init vars
438 self.tests = {}
439 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000440 print('Getting machine details...')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000441 self.machine_details = get_machine_details()
442
443 # Make .version an instance attribute to have it saved in the
444 # Benchmark pickle
445 self.version = self.version
446
447 def get_timer(self):
448
449 """ Return the timer function to use for the test.
450
451 """
452 return get_timer(self.timer)
453
454 def compatible(self, other):
455
456 """ Return 1/0 depending on whether the benchmark is
457 compatible with the other Benchmark instance or not.
458
459 """
460 if self.version != other.version:
461 return 0
462 if (self.machine_details == other.machine_details and
463 self.timer != other.timer):
464 return 0
465 if (self.calibration_runs == 0 and
466 other.calibration_runs != 0):
467 return 0
468 if (self.calibration_runs != 0 and
469 other.calibration_runs == 0):
470 return 0
471 return 1
472
473 def load_tests(self, setupmod, limitnames=None):
474
475 # Add tests
476 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000477 print('Searching for tests ...')
478 print('--------------------------------------')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000479 for testclass in setupmod.__dict__.values():
480 if not hasattr(testclass, 'is_a_test'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000481 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000482 name = testclass.__name__
Thomas Wouters477c8d52006-05-27 19:21:47 +0000483 if name == 'Test':
484 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000485 if (limitnames is not None and
486 limitnames.search(name) is None):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000487 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000488 self.tests[name] = testclass(
489 warp=self.warp,
490 calibration_runs=self.calibration_runs,
491 timer=self.timer)
Guido van Rossum486364b2007-06-30 05:01:58 +0000492 l = sorted(self.tests)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000493 if self.verbose:
494 for name in l:
Guido van Rossum486364b2007-06-30 05:01:58 +0000495 print(' %s' % name)
496 print('--------------------------------------')
497 print(' %i tests found' % len(l))
498 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000499
500 def calibrate(self):
501
Guido van Rossum486364b2007-06-30 05:01:58 +0000502 print('Calibrating tests. Please wait...', end=' ')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000503 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000504 print()
505 print()
506 print('Test min max')
507 print('-' * LINE)
508 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000509 for i in range(len(tests)):
510 name, test = tests[i]
511 test.calibrate_test()
512 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000513 print('%30s: %6.3fms %6.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000514 (name,
515 min(test.overhead_times) * MILLI_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000516 max(test.overhead_times) * MILLI_SECONDS))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000517 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000518 print()
519 print('Done with the calibration.')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000520 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000521 print('done.')
522 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000523
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000524 def run(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000525
Guido van Rossum486364b2007-06-30 05:01:58 +0000526 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000527 timer = self.get_timer()
Guido van Rossum486364b2007-06-30 05:01:58 +0000528 print('Running %i round(s) of the suite at warp factor %i:' % \
529 (self.rounds, self.warp))
530 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000531 self.roundtimes = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000532 for i in range(self.rounds):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000533 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000534 print(' Round %-25i effective absolute overhead' % (i+1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000535 total_eff_time = 0.0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 for j in range(len(tests)):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000537 name, test = tests[j]
538 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000539 print('%30s:' % name, end=' ')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000540 test.run()
541 (eff_time, abs_time, min_overhead) = test.last_timing
542 total_eff_time = total_eff_time + eff_time
543 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000544 print(' %5.0fms %5.0fms %7.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000545 (eff_time * MILLI_SECONDS,
546 abs_time * MILLI_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000547 min_overhead * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000548 self.roundtimes.append(total_eff_time)
549 if self.verbose:
Collin Winter6afaeb72007-08-03 17:06:41 +0000550 print(' '
551 ' ------------------------------')
552 print(' '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000553 ' Totals: %6.0fms' %
Collin Winter6afaeb72007-08-03 17:06:41 +0000554 (total_eff_time * MILLI_SECONDS))
Guido van Rossum486364b2007-06-30 05:01:58 +0000555 print()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000556 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000557 print('* Round %i done in %.3f seconds.' % (i+1,
558 total_eff_time))
559 print()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000560
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000561 def stat(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000562
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000563 """ Return benchmark run statistics as tuple:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000564
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000565 (minimum round time,
566 average round time,
567 maximum round time)
568
569 XXX Currently not used, since the benchmark does test
570 statistics across all rounds.
571
572 """
573 runs = len(self.roundtimes)
574 if runs == 0:
575 return 0.0, 0.0
576 min_time = min(self.roundtimes)
Guido van Rossum89da5d72006-08-22 00:21:25 +0000577 total_time = sum(self.roundtimes)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000578 avg_time = total_time / float(runs)
579 max_time = max(self.roundtimes)
580 return (min_time, avg_time, max_time)
581
582 def print_header(self, title='Benchmark'):
583
Guido van Rossum486364b2007-06-30 05:01:58 +0000584 print('-' * LINE)
585 print('%s: %s' % (title, self.name))
586 print('-' * LINE)
587 print()
588 print(' Rounds: %s' % self.rounds)
589 print(' Warp: %s' % self.warp)
590 print(' Timer: %s' % self.timer)
591 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000592 if self.machine_details:
593 print_machine_details(self.machine_details, indent=' ')
Guido van Rossum486364b2007-06-30 05:01:58 +0000594 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000595
596 def print_benchmark(self, hidenoise=0, limitnames=None):
597
Collin Winter6afaeb72007-08-03 17:06:41 +0000598 print('Test '
599 ' minimum average operation overhead')
Guido van Rossum486364b2007-06-30 05:01:58 +0000600 print('-' * LINE)
601 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000602 total_min_time = 0.0
603 total_avg_time = 0.0
604 for name, test in tests:
605 if (limitnames is not None and
606 limitnames.search(name) is None):
607 continue
608 (min_time,
609 avg_time,
610 total_time,
611 op_avg,
612 min_overhead) = test.stat()
613 total_min_time = total_min_time + min_time
614 total_avg_time = total_avg_time + avg_time
Guido van Rossum486364b2007-06-30 05:01:58 +0000615 print('%30s: %5.0fms %5.0fms %6.2fus %7.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000616 (name,
617 min_time * MILLI_SECONDS,
618 avg_time * MILLI_SECONDS,
619 op_avg * MICRO_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000620 min_overhead *MILLI_SECONDS))
621 print('-' * LINE)
Collin Winter6afaeb72007-08-03 17:06:41 +0000622 print('Totals: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000623 ' %6.0fms %6.0fms' %
624 (total_min_time * MILLI_SECONDS,
625 total_avg_time * MILLI_SECONDS,
Collin Winter6afaeb72007-08-03 17:06:41 +0000626 ))
Guido van Rossum486364b2007-06-30 05:01:58 +0000627 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000628
629 def print_comparison(self, compare_to, hidenoise=0, limitnames=None):
630
631 # Check benchmark versions
632 if compare_to.version != self.version:
Collin Winter6afaeb72007-08-03 17:06:41 +0000633 print('* Benchmark versions differ: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 'cannot compare this benchmark to "%s" !' %
Collin Winter6afaeb72007-08-03 17:06:41 +0000635 compare_to.name)
Guido van Rossum486364b2007-06-30 05:01:58 +0000636 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637 self.print_benchmark(hidenoise=hidenoise,
638 limitnames=limitnames)
639 return
640
641 # Print header
642 compare_to.print_header('Comparing with')
Collin Winter6afaeb72007-08-03 17:06:41 +0000643 print('Test '
644 ' minimum run-time average run-time')
645 print(' '
646 ' this other diff this other diff')
Guido van Rossum486364b2007-06-30 05:01:58 +0000647 print('-' * LINE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000648
649 # Print test comparisons
Guido van Rossum486364b2007-06-30 05:01:58 +0000650 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000651 total_min_time = other_total_min_time = 0.0
652 total_avg_time = other_total_avg_time = 0.0
653 benchmarks_compatible = self.compatible(compare_to)
654 tests_compatible = 1
655 for name, test in tests:
656 if (limitnames is not None and
657 limitnames.search(name) is None):
658 continue
659 (min_time,
660 avg_time,
661 total_time,
662 op_avg,
663 min_overhead) = test.stat()
664 total_min_time = total_min_time + min_time
665 total_avg_time = total_avg_time + avg_time
666 try:
667 other = compare_to.tests[name]
668 except KeyError:
669 other = None
670 if other is None:
671 # Other benchmark doesn't include the given test
672 min_diff, avg_diff = 'n/a', 'n/a'
673 other_min_time = 0.0
674 other_avg_time = 0.0
675 tests_compatible = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000676 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000677 (other_min_time,
678 other_avg_time,
679 other_total_time,
680 other_op_avg,
681 other_min_overhead) = other.stat()
682 other_total_min_time = other_total_min_time + other_min_time
683 other_total_avg_time = other_total_avg_time + other_avg_time
684 if (benchmarks_compatible and
685 test.compatible(other)):
686 # Both benchmark and tests are comparible
687 min_diff = ((min_time * self.warp) /
688 (other_min_time * other.warp) - 1.0)
689 avg_diff = ((avg_time * self.warp) /
690 (other_avg_time * other.warp) - 1.0)
691 if hidenoise and abs(min_diff) < 10.0:
692 min_diff = ''
693 else:
694 min_diff = '%+5.1f%%' % (min_diff * PERCENT)
695 if hidenoise and abs(avg_diff) < 10.0:
696 avg_diff = ''
697 else:
698 avg_diff = '%+5.1f%%' % (avg_diff * PERCENT)
699 else:
700 # Benchmark or tests are not comparible
701 min_diff, avg_diff = 'n/a', 'n/a'
702 tests_compatible = 0
Guido van Rossum486364b2007-06-30 05:01:58 +0000703 print('%30s: %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000704 (name,
705 min_time * MILLI_SECONDS,
706 other_min_time * MILLI_SECONDS * compare_to.warp / self.warp,
707 min_diff,
708 avg_time * MILLI_SECONDS,
709 other_avg_time * MILLI_SECONDS * compare_to.warp / self.warp,
Guido van Rossum486364b2007-06-30 05:01:58 +0000710 avg_diff))
711 print('-' * LINE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000712
713 # Summarise test results
714 if not benchmarks_compatible or not tests_compatible:
715 min_diff, avg_diff = 'n/a', 'n/a'
716 else:
717 if other_total_min_time != 0.0:
718 min_diff = '%+5.1f%%' % (
719 ((total_min_time * self.warp) /
720 (other_total_min_time * compare_to.warp) - 1.0) * PERCENT)
721 else:
722 min_diff = 'n/a'
723 if other_total_avg_time != 0.0:
724 avg_diff = '%+5.1f%%' % (
725 ((total_avg_time * self.warp) /
726 (other_total_avg_time * compare_to.warp) - 1.0) * PERCENT)
727 else:
728 avg_diff = 'n/a'
Collin Winter6afaeb72007-08-03 17:06:41 +0000729 print('Totals: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000730 ' %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' %
731 (total_min_time * MILLI_SECONDS,
732 (other_total_min_time * compare_to.warp/self.warp
733 * MILLI_SECONDS),
734 min_diff,
735 total_avg_time * MILLI_SECONDS,
736 (other_total_avg_time * compare_to.warp/self.warp
737 * MILLI_SECONDS),
738 avg_diff
Collin Winter6afaeb72007-08-03 17:06:41 +0000739 ))
Guido van Rossum486364b2007-06-30 05:01:58 +0000740 print()
741 print('(this=%s, other=%s)' % (self.name,
742 compare_to.name))
743 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744
745class PyBenchCmdline(Application):
746
747 header = ("PYBENCH - a benchmark test suite for Python "
748 "interpreters/compilers.")
749
750 version = __version__
751
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000752 debug = _debug
753
754 options = [ArgumentOption('-n',
755 'number of rounds',
756 Setup.Number_of_rounds),
757 ArgumentOption('-f',
758 'save benchmark to file arg',
759 ''),
760 ArgumentOption('-c',
761 'compare benchmark with the one in file arg',
762 ''),
763 ArgumentOption('-s',
764 'show benchmark in file arg, then exit',
765 ''),
766 ArgumentOption('-w',
767 'set warp factor to arg',
768 Setup.Warp_factor),
769 ArgumentOption('-t',
770 'run only tests with names matching arg',
771 ''),
772 ArgumentOption('-C',
773 'set the number of calibration runs to arg',
774 CALIBRATION_RUNS),
775 SwitchOption('-d',
776 'hide noise in comparisons',
777 0),
778 SwitchOption('-v',
779 'verbose output (not recommended)',
780 0),
781 SwitchOption('--with-gc',
782 'enable garbage collection',
783 0),
784 SwitchOption('--with-syscheck',
785 'use default sys check interval',
786 0),
787 ArgumentOption('--timer',
788 'use given timer',
789 TIMER_PLATFORM_DEFAULT),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790 ]
791
792 about = """\
793The normal operation is to run the suite and display the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000794results. Use -f to save them for later reuse or comparisons.
795
796Available timers:
797
798 time.time
799 time.clock
800 systimes.processtime
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000801
802Examples:
803
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000804python2.1 pybench.py -f p21.pybench
805python2.5 pybench.py -f p25.pybench
806python pybench.py -s p25.pybench -c p21.pybench
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000807"""
808 copyright = __copyright__
809
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000810 def main(self):
811
812 rounds = self.values['-n']
813 reportfile = self.values['-f']
814 show_bench = self.values['-s']
815 compare_to = self.values['-c']
816 hidenoise = self.values['-d']
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000817 warp = int(self.values['-w'])
818 withgc = self.values['--with-gc']
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819 limitnames = self.values['-t']
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000820 if limitnames:
821 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000822 print('* limiting test names to one with substring "%s"' % \
823 limitnames)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000824 limitnames = re.compile(limitnames, re.I)
825 else:
826 limitnames = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000827 verbose = self.verbose
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000828 withsyscheck = self.values['--with-syscheck']
829 calibration_runs = self.values['-C']
830 timer = self.values['--timer']
Thomas Wouters477c8d52006-05-27 19:21:47 +0000831
Guido van Rossum486364b2007-06-30 05:01:58 +0000832 print('-' * LINE)
833 print('PYBENCH %s' % __version__)
834 print('-' * LINE)
835 print('* using %s %s' % (
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000836 platform.python_implementation(),
Guido van Rossum486364b2007-06-30 05:01:58 +0000837 ' '.join(sys.version.split())))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000838
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000839 # Switch off garbage collection
840 if not withgc:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000841 try:
842 import gc
843 except ImportError:
Guido van Rossum486364b2007-06-30 05:01:58 +0000844 print('* Python version doesn\'t support garbage collection')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000845 else:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000846 try:
847 gc.disable()
848 except NotImplementedError:
Guido van Rossum486364b2007-06-30 05:01:58 +0000849 print('* Python version doesn\'t support gc.disable')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000850 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000851 print('* disabled garbage collection')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000852
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000853 # "Disable" sys check interval
854 if not withsyscheck:
855 # Too bad the check interval uses an int instead of a long...
856 value = 2147483647
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000857 try:
858 sys.setcheckinterval(value)
859 except (AttributeError, NotImplementedError):
Guido van Rossum486364b2007-06-30 05:01:58 +0000860 print('* Python version doesn\'t support sys.setcheckinterval')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000861 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000862 print('* system check interval set to maximum: %s' % value)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000863
864 if timer == TIMER_SYSTIMES_PROCESSTIME:
865 import systimes
Guido van Rossum486364b2007-06-30 05:01:58 +0000866 print('* using timer: systimes.processtime (%s)' % \
867 systimes.SYSTIMES_IMPLEMENTATION)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000868 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000869 print('* using timer: %s' % timer)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000870
Guido van Rossum486364b2007-06-30 05:01:58 +0000871 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873 if compare_to:
874 try:
875 f = open(compare_to,'rb')
876 bench = pickle.load(f)
877 bench.name = compare_to
878 f.close()
879 compare_to = bench
Guido van Rossumb940e112007-01-10 16:19:56 +0000880 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000881 print('* Error opening/reading file %s: %s' % (
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000882 repr(compare_to),
Guido van Rossum486364b2007-06-30 05:01:58 +0000883 reason))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884 compare_to = None
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000885
886 if show_bench:
887 try:
888 f = open(show_bench,'rb')
889 bench = pickle.load(f)
890 bench.name = show_bench
891 f.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000892 bench.print_header()
893 if compare_to:
894 bench.print_comparison(compare_to,
895 hidenoise=hidenoise,
896 limitnames=limitnames)
897 else:
898 bench.print_benchmark(hidenoise=hidenoise,
899 limitnames=limitnames)
Guido van Rossumb940e112007-01-10 16:19:56 +0000900 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000901 print('* Error opening/reading file %s: %s' % (
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000902 repr(show_bench),
Guido van Rossum486364b2007-06-30 05:01:58 +0000903 reason))
904 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000905 return
906
907 if reportfile:
Guido van Rossum486364b2007-06-30 05:01:58 +0000908 print('Creating benchmark: %s (rounds=%i, warp=%i)' % \
909 (reportfile, rounds, warp))
910 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000911
912 # Create benchmark object
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000913 bench = Benchmark(reportfile,
914 verbose=verbose,
915 timer=timer,
916 warp=warp,
917 calibration_runs=calibration_runs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000918 bench.rounds = rounds
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000919 bench.load_tests(Setup, limitnames=limitnames)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000920 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000921 bench.calibrate()
922 bench.run()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 except KeyboardInterrupt:
Guido van Rossum486364b2007-06-30 05:01:58 +0000924 print()
925 print('*** KeyboardInterrupt -- Aborting')
926 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928 bench.print_header()
929 if compare_to:
930 bench.print_comparison(compare_to,
931 hidenoise=hidenoise,
932 limitnames=limitnames)
933 else:
934 bench.print_benchmark(hidenoise=hidenoise,
935 limitnames=limitnames)
936
937 # Ring bell
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000938 sys.stderr.write('\007')
939
940 if reportfile:
941 try:
942 f = open(reportfile,'wb')
943 bench.name = reportfile
944 pickle.dump(bench,f)
945 f.close()
Guido van Rossumb940e112007-01-10 16:19:56 +0000946 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000947 print('* Error opening/writing reportfile')
Guido van Rossumb940e112007-01-10 16:19:56 +0000948 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000949 print('* Error opening/writing reportfile %s: %s' % (
Thomas Wouters89f507f2006-12-13 04:49:30 +0000950 reportfile,
Guido van Rossum486364b2007-06-30 05:01:58 +0000951 reason))
952 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953
954if __name__ == '__main__':
955 PyBenchCmdline()