blob: 624347f50e489c10d0e5af79003bfb7283e31aff [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,
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000124 'implementation': getattr(platform, 'python_implementation',
125 lambda:'n/a')(),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000126 'python': platform.python_version(),
127 'compiler': platform.python_compiler(),
128 'buildno': buildno,
129 'builddate': builddate,
130 'unicode': unicode,
131 'bits': bits,
132 }
133
134def print_machine_details(d, indent=''):
135
136 l = ['Machine Details:',
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000137 ' Platform ID: %s' % d.get('platform', 'n/a'),
138 ' Processor: %s' % d.get('processor', 'n/a'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000139 '',
140 'Python:',
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000141 ' Implementation: %s' % d.get('implementation', 'n/a'),
142 ' Executable: %s' % d.get('executable', 'n/a'),
143 ' Version: %s' % d.get('python', 'n/a'),
144 ' Compiler: %s' % d.get('compiler', 'n/a'),
145 ' Bits: %s' % d.get('bits', 'n/a'),
146 ' Build: %s (#%s)' % (d.get('builddate', 'n/a'),
147 d.get('buildno', 'n/a')),
148 ' Unicode: %s' % d.get('unicode', 'n/a'),
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000149 ]
Guido van Rossum486364b2007-06-30 05:01:58 +0000150 joiner = '\n' + indent
151 print(indent + joiner.join(l) + '\n')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000152
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000153### Test baseclass
154
155class Test:
156
157 """ All test must have this class as baseclass. It provides
158 the necessary interface to the benchmark machinery.
159
160 The tests must set .rounds to a value high enough to let the
161 test run between 20-50 seconds. This is needed because
162 clock()-timing only gives rather inaccurate values (on Linux,
163 for example, it is accurate to a few hundreths of a
164 second). If you don't want to wait that long, use a warp
165 factor larger than 1.
166
167 It is also important to set the .operations variable to a
168 value representing the number of "virtual operations" done per
169 call of .run().
170
171 If you change a test in some way, don't forget to increase
Guido van Rossumd8faa362007-04-27 19:54:29 +0000172 its version number.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000173
174 """
175
176 ### Instance variables that each test should override
177
178 # Version number of the test as float (x.yy); this is important
179 # for comparisons of benchmark runs - tests with unequal version
180 # number will not get compared.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000181 version = 2.0
Thomas Wouters477c8d52006-05-27 19:21:47 +0000182
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183 # The number of abstract operations done in each round of the
184 # test. An operation is the basic unit of what you want to
185 # measure. The benchmark will output the amount of run-time per
186 # operation. Note that in order to raise the measured timings
187 # significantly above noise level, it is often required to repeat
188 # sets of operations more than once per test round. The measured
189 # overhead per test round should be less than 1 second.
190 operations = 1
191
192 # Number of rounds to execute per test run. This should be
193 # adjusted to a figure that results in a test run-time of between
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000194 # 1-2 seconds.
195 rounds = 100000
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000196
197 ### Internal variables
198
199 # Mark this class as implementing a test
200 is_a_test = 1
201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202 # Last timing: (real, run, overhead)
203 last_timing = (0.0, 0.0, 0.0)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000204
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000205 # Warp factor to use for this test
206 warp = 1
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000207
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000208 # Number of calibration runs to use
209 calibration_runs = CALIBRATION_RUNS
210
211 # List of calibration timings
212 overhead_times = None
213
214 # List of test run timings
215 times = []
216
217 # Timer used for the benchmark
218 timer = TIMER_PLATFORM_DEFAULT
219
220 def __init__(self, warp=None, calibration_runs=None, timer=None):
221
222 # Set parameters
223 if warp is not None:
224 self.rounds = int(self.rounds / warp)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000225 if self.rounds == 0:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000226 raise ValueError('warp factor set too high')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227 self.warp = warp
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000228 if calibration_runs is not None:
229 if (not ALLOW_SKIPPING_CALIBRATION and
230 calibration_runs < 1):
231 raise ValueError('at least one calibration run is required')
232 self.calibration_runs = calibration_runs
233 if timer is not None:
234 timer = timer
235
236 # Init variables
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 self.times = []
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000238 self.overhead_times = []
239
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 # We want these to be in the instance dict, so that pickle
241 # saves them
242 self.version = self.version
243 self.operations = self.operations
244 self.rounds = self.rounds
245
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000246 def get_timer(self):
247
248 """ Return the timer function to use for the test.
249
250 """
251 return get_timer(self.timer)
252
253 def compatible(self, other):
254
255 """ Return 1/0 depending on whether the test is compatible
256 with the other Test instance or not.
257
258 """
259 if self.version != other.version:
260 return 0
261 if self.rounds != other.rounds:
262 return 0
263 return 1
264
265 def calibrate_test(self):
266
267 if self.calibration_runs == 0:
268 self.overhead_times = [0.0]
269 return
270
271 calibrate = self.calibrate
272 timer = self.get_timer()
273 calibration_loops = range(CALIBRATION_LOOPS)
274
275 # Time the calibration loop overhead
276 prep_times = []
277 for i in range(self.calibration_runs):
278 t = timer()
279 for i in calibration_loops:
280 pass
281 t = timer() - t
282 prep_times.append(t)
283 min_prep_time = min(prep_times)
284 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000285 print()
286 print('Calib. prep time = %.6fms' % (
287 min_prep_time * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000288
289 # Time the calibration runs (doing CALIBRATION_LOOPS loops of
290 # .calibrate() method calls each)
291 for i in range(self.calibration_runs):
292 t = timer()
293 for i in calibration_loops:
294 calibrate()
295 t = timer() - t
296 self.overhead_times.append(t / CALIBRATION_LOOPS
297 - min_prep_time)
298
299 # Check the measured times
300 min_overhead = min(self.overhead_times)
301 max_overhead = max(self.overhead_times)
302 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000303 print('Calib. overhead time = %.6fms' % (
304 min_overhead * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000305 if min_overhead < 0.0:
306 raise ValueError('calibration setup did not work')
307 if max_overhead - min_overhead > 0.1:
308 raise ValueError(
309 'overhead calibration timing range too inaccurate: '
310 '%r - %r' % (min_overhead, max_overhead))
311
312 def run(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313
314 """ Run the test in two phases: first calibrate, then
315 do the actual test. Be careful to keep the calibration
316 timing low w/r to the test timing.
Thomas Wouters477c8d52006-05-27 19:21:47 +0000317
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 """
319 test = self.test
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000320 timer = self.get_timer()
321
322 # Get calibration
323 min_overhead = min(self.overhead_times)
324
325 # Test run
326 t = timer()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000327 test()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000328 t = timer() - t
329 if t < MIN_TEST_RUNTIME:
330 raise ValueError('warp factor too high: '
331 'test times are < 10ms')
332 eff_time = t - min_overhead
333 if eff_time < 0:
334 raise ValueError('wrong calibration')
335 self.last_timing = (eff_time, t, min_overhead)
336 self.times.append(eff_time)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000337
338 def calibrate(self):
339
Thomas Wouters477c8d52006-05-27 19:21:47 +0000340 """ Calibrate the test.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341
Thomas Wouters477c8d52006-05-27 19:21:47 +0000342 This method should execute everything that is needed to
343 setup and run the test - except for the actual operations
344 that you intend to measure. pybench uses this method to
345 measure the test implementation overhead.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346
347 """
348 return
349
350 def test(self):
351
Thomas Wouters477c8d52006-05-27 19:21:47 +0000352 """ Run the test.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353
Thomas Wouters477c8d52006-05-27 19:21:47 +0000354 The test needs to run self.rounds executing
355 self.operations number of operations each.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000356
357 """
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000358 return
Thomas Wouters477c8d52006-05-27 19:21:47 +0000359
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 def stat(self):
361
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000362 """ Return test run statistics as tuple:
Thomas Wouters477c8d52006-05-27 19:21:47 +0000363
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000364 (minimum run time,
365 average run time,
366 total run time,
367 average time per operation,
368 minimum overhead time)
369
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000370 """
371 runs = len(self.times)
372 if runs == 0:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000373 return 0.0, 0.0, 0.0, 0.0
374 min_time = min(self.times)
Guido van Rossum89da5d72006-08-22 00:21:25 +0000375 total_time = sum(self.times)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000376 avg_time = total_time / float(runs)
377 operation_avg = total_time / float(runs
378 * self.rounds
379 * self.operations)
380 if self.overhead_times:
381 min_overhead = min(self.overhead_times)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000383 min_overhead = self.last_timing[2]
384 return min_time, avg_time, total_time, operation_avg, min_overhead
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000385
386### Load Setup
387
388# This has to be done after the definition of the Test class, since
389# the Setup module will import subclasses using this class.
390
391import Setup
392
393### Benchmark base class
394
395class Benchmark:
396
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000397 # Name of the benchmark
398 name = ''
399
400 # Number of benchmark rounds to run
401 rounds = 1
402
403 # Warp factor use to run the tests
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000404 warp = 1 # Warp factor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000406 # Average benchmark round time
407 roundtime = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000409 # Benchmark version number as float x.yy
410 version = 2.0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000411
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000412 # Produce verbose output ?
413 verbose = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000414
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000415 # Dictionary with the machine details
416 machine_details = None
417
418 # Timer used for the benchmark
419 timer = TIMER_PLATFORM_DEFAULT
420
421 def __init__(self, name, verbose=None, timer=None, warp=None,
422 calibration_runs=None):
423
424 if name:
425 self.name = name
Thomas Wouters477c8d52006-05-27 19:21:47 +0000426 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000427 self.name = '%04i-%02i-%02i %02i:%02i:%02i' % \
428 (time.localtime(time.time())[:6])
429 if verbose is not None:
430 self.verbose = verbose
431 if timer is not None:
432 self.timer = timer
433 if warp is not None:
434 self.warp = warp
435 if calibration_runs is not None:
436 self.calibration_runs = calibration_runs
437
438 # Init vars
439 self.tests = {}
440 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000441 print('Getting machine details...')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000442 self.machine_details = get_machine_details()
443
444 # Make .version an instance attribute to have it saved in the
445 # Benchmark pickle
446 self.version = self.version
447
448 def get_timer(self):
449
450 """ Return the timer function to use for the test.
451
452 """
453 return get_timer(self.timer)
454
455 def compatible(self, other):
456
457 """ Return 1/0 depending on whether the benchmark is
458 compatible with the other Benchmark instance or not.
459
460 """
461 if self.version != other.version:
462 return 0
463 if (self.machine_details == other.machine_details and
464 self.timer != other.timer):
465 return 0
466 if (self.calibration_runs == 0 and
467 other.calibration_runs != 0):
468 return 0
469 if (self.calibration_runs != 0 and
470 other.calibration_runs == 0):
471 return 0
472 return 1
473
474 def load_tests(self, setupmod, limitnames=None):
475
476 # Add tests
477 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000478 print('Searching for tests ...')
479 print('--------------------------------------')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000480 for testclass in setupmod.__dict__.values():
481 if not hasattr(testclass, 'is_a_test'):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000482 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000483 name = testclass.__name__
Thomas Wouters477c8d52006-05-27 19:21:47 +0000484 if name == 'Test':
485 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000486 if (limitnames is not None and
487 limitnames.search(name) is None):
Thomas Wouters477c8d52006-05-27 19:21:47 +0000488 continue
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000489 self.tests[name] = testclass(
490 warp=self.warp,
491 calibration_runs=self.calibration_runs,
492 timer=self.timer)
Guido van Rossum486364b2007-06-30 05:01:58 +0000493 l = sorted(self.tests)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000494 if self.verbose:
495 for name in l:
Guido van Rossum486364b2007-06-30 05:01:58 +0000496 print(' %s' % name)
497 print('--------------------------------------')
498 print(' %i tests found' % len(l))
499 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000500
501 def calibrate(self):
502
Guido van Rossum486364b2007-06-30 05:01:58 +0000503 print('Calibrating tests. Please wait...', end=' ')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000504 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000505 print()
506 print()
507 print('Test min max')
508 print('-' * LINE)
509 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000510 for i in range(len(tests)):
511 name, test = tests[i]
512 test.calibrate_test()
513 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000514 print('%30s: %6.3fms %6.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000515 (name,
516 min(test.overhead_times) * MILLI_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000517 max(test.overhead_times) * MILLI_SECONDS))
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000518 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000519 print()
520 print('Done with the calibration.')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000521 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000522 print('done.')
523 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000524
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000525 def run(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000526
Guido van Rossum486364b2007-06-30 05:01:58 +0000527 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528 timer = self.get_timer()
Guido van Rossum486364b2007-06-30 05:01:58 +0000529 print('Running %i round(s) of the suite at warp factor %i:' % \
530 (self.rounds, self.warp))
531 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000532 self.roundtimes = []
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 for i in range(self.rounds):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000534 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000535 print(' Round %-25i effective absolute overhead' % (i+1))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000536 total_eff_time = 0.0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 for j in range(len(tests)):
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000538 name, test = tests[j]
539 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000540 print('%30s:' % name, end=' ')
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000541 test.run()
542 (eff_time, abs_time, min_overhead) = test.last_timing
543 total_eff_time = total_eff_time + eff_time
544 if self.verbose:
Guido van Rossum486364b2007-06-30 05:01:58 +0000545 print(' %5.0fms %5.0fms %7.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000546 (eff_time * MILLI_SECONDS,
547 abs_time * MILLI_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000548 min_overhead * MILLI_SECONDS))
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000549 self.roundtimes.append(total_eff_time)
550 if self.verbose:
Collin Winter6afaeb72007-08-03 17:06:41 +0000551 print(' '
552 ' ------------------------------')
553 print(' '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000554 ' Totals: %6.0fms' %
Collin Winter6afaeb72007-08-03 17:06:41 +0000555 (total_eff_time * MILLI_SECONDS))
Guido van Rossum486364b2007-06-30 05:01:58 +0000556 print()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000557 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000558 print('* Round %i done in %.3f seconds.' % (i+1,
559 total_eff_time))
560 print()
Thomas Wouters477c8d52006-05-27 19:21:47 +0000561
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000562 def stat(self):
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000563
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000564 """ Return benchmark run statistics as tuple:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000566 (minimum round time,
567 average round time,
568 maximum round time)
569
570 XXX Currently not used, since the benchmark does test
571 statistics across all rounds.
572
573 """
574 runs = len(self.roundtimes)
575 if runs == 0:
576 return 0.0, 0.0
577 min_time = min(self.roundtimes)
Guido van Rossum89da5d72006-08-22 00:21:25 +0000578 total_time = sum(self.roundtimes)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000579 avg_time = total_time / float(runs)
580 max_time = max(self.roundtimes)
581 return (min_time, avg_time, max_time)
582
583 def print_header(self, title='Benchmark'):
584
Guido van Rossum486364b2007-06-30 05:01:58 +0000585 print('-' * LINE)
586 print('%s: %s' % (title, self.name))
587 print('-' * LINE)
588 print()
589 print(' Rounds: %s' % self.rounds)
590 print(' Warp: %s' % self.warp)
591 print(' Timer: %s' % self.timer)
592 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000593 if self.machine_details:
594 print_machine_details(self.machine_details, indent=' ')
Guido van Rossum486364b2007-06-30 05:01:58 +0000595 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000596
597 def print_benchmark(self, hidenoise=0, limitnames=None):
598
Collin Winter6afaeb72007-08-03 17:06:41 +0000599 print('Test '
600 ' minimum average operation overhead')
Guido van Rossum486364b2007-06-30 05:01:58 +0000601 print('-' * LINE)
602 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000603 total_min_time = 0.0
604 total_avg_time = 0.0
605 for name, test in tests:
606 if (limitnames is not None and
607 limitnames.search(name) is None):
608 continue
609 (min_time,
610 avg_time,
611 total_time,
612 op_avg,
613 min_overhead) = test.stat()
614 total_min_time = total_min_time + min_time
615 total_avg_time = total_avg_time + avg_time
Guido van Rossum486364b2007-06-30 05:01:58 +0000616 print('%30s: %5.0fms %5.0fms %6.2fus %7.3fms' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000617 (name,
618 min_time * MILLI_SECONDS,
619 avg_time * MILLI_SECONDS,
620 op_avg * MICRO_SECONDS,
Guido van Rossum486364b2007-06-30 05:01:58 +0000621 min_overhead *MILLI_SECONDS))
622 print('-' * LINE)
Collin Winter6afaeb72007-08-03 17:06:41 +0000623 print('Totals: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000624 ' %6.0fms %6.0fms' %
625 (total_min_time * MILLI_SECONDS,
626 total_avg_time * MILLI_SECONDS,
Collin Winter6afaeb72007-08-03 17:06:41 +0000627 ))
Guido van Rossum486364b2007-06-30 05:01:58 +0000628 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000629
630 def print_comparison(self, compare_to, hidenoise=0, limitnames=None):
631
632 # Check benchmark versions
633 if compare_to.version != self.version:
Collin Winter6afaeb72007-08-03 17:06:41 +0000634 print('* Benchmark versions differ: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000635 'cannot compare this benchmark to "%s" !' %
Collin Winter6afaeb72007-08-03 17:06:41 +0000636 compare_to.name)
Guido van Rossum486364b2007-06-30 05:01:58 +0000637 print()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000638 self.print_benchmark(hidenoise=hidenoise,
639 limitnames=limitnames)
640 return
641
642 # Print header
643 compare_to.print_header('Comparing with')
Collin Winter6afaeb72007-08-03 17:06:41 +0000644 print('Test '
645 ' minimum run-time average run-time')
646 print(' '
647 ' this other diff this other diff')
Guido van Rossum486364b2007-06-30 05:01:58 +0000648 print('-' * LINE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000649
650 # Print test comparisons
Guido van Rossum486364b2007-06-30 05:01:58 +0000651 tests = sorted(self.tests.items())
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000652 total_min_time = other_total_min_time = 0.0
653 total_avg_time = other_total_avg_time = 0.0
654 benchmarks_compatible = self.compatible(compare_to)
655 tests_compatible = 1
656 for name, test in tests:
657 if (limitnames is not None and
658 limitnames.search(name) is None):
659 continue
660 (min_time,
661 avg_time,
662 total_time,
663 op_avg,
664 min_overhead) = test.stat()
665 total_min_time = total_min_time + min_time
666 total_avg_time = total_avg_time + avg_time
667 try:
668 other = compare_to.tests[name]
669 except KeyError:
670 other = None
671 if other is None:
672 # Other benchmark doesn't include the given test
673 min_diff, avg_diff = 'n/a', 'n/a'
674 other_min_time = 0.0
675 other_avg_time = 0.0
676 tests_compatible = 0
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 else:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000678 (other_min_time,
679 other_avg_time,
680 other_total_time,
681 other_op_avg,
682 other_min_overhead) = other.stat()
683 other_total_min_time = other_total_min_time + other_min_time
684 other_total_avg_time = other_total_avg_time + other_avg_time
685 if (benchmarks_compatible and
686 test.compatible(other)):
687 # Both benchmark and tests are comparible
688 min_diff = ((min_time * self.warp) /
689 (other_min_time * other.warp) - 1.0)
690 avg_diff = ((avg_time * self.warp) /
691 (other_avg_time * other.warp) - 1.0)
692 if hidenoise and abs(min_diff) < 10.0:
693 min_diff = ''
694 else:
695 min_diff = '%+5.1f%%' % (min_diff * PERCENT)
696 if hidenoise and abs(avg_diff) < 10.0:
697 avg_diff = ''
698 else:
699 avg_diff = '%+5.1f%%' % (avg_diff * PERCENT)
700 else:
701 # Benchmark or tests are not comparible
702 min_diff, avg_diff = 'n/a', 'n/a'
703 tests_compatible = 0
Guido van Rossum486364b2007-06-30 05:01:58 +0000704 print('%30s: %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' % \
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000705 (name,
706 min_time * MILLI_SECONDS,
707 other_min_time * MILLI_SECONDS * compare_to.warp / self.warp,
708 min_diff,
709 avg_time * MILLI_SECONDS,
710 other_avg_time * MILLI_SECONDS * compare_to.warp / self.warp,
Guido van Rossum486364b2007-06-30 05:01:58 +0000711 avg_diff))
712 print('-' * LINE)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000713
714 # Summarise test results
715 if not benchmarks_compatible or not tests_compatible:
716 min_diff, avg_diff = 'n/a', 'n/a'
717 else:
718 if other_total_min_time != 0.0:
719 min_diff = '%+5.1f%%' % (
720 ((total_min_time * self.warp) /
721 (other_total_min_time * compare_to.warp) - 1.0) * PERCENT)
722 else:
723 min_diff = 'n/a'
724 if other_total_avg_time != 0.0:
725 avg_diff = '%+5.1f%%' % (
726 ((total_avg_time * self.warp) /
727 (other_total_avg_time * compare_to.warp) - 1.0) * PERCENT)
728 else:
729 avg_diff = 'n/a'
Collin Winter6afaeb72007-08-03 17:06:41 +0000730 print('Totals: '
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000731 ' %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' %
732 (total_min_time * MILLI_SECONDS,
733 (other_total_min_time * compare_to.warp/self.warp
734 * MILLI_SECONDS),
735 min_diff,
736 total_avg_time * MILLI_SECONDS,
737 (other_total_avg_time * compare_to.warp/self.warp
738 * MILLI_SECONDS),
739 avg_diff
Collin Winter6afaeb72007-08-03 17:06:41 +0000740 ))
Guido van Rossum486364b2007-06-30 05:01:58 +0000741 print()
742 print('(this=%s, other=%s)' % (self.name,
743 compare_to.name))
744 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000745
746class PyBenchCmdline(Application):
747
748 header = ("PYBENCH - a benchmark test suite for Python "
749 "interpreters/compilers.")
750
751 version = __version__
752
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000753 debug = _debug
754
755 options = [ArgumentOption('-n',
756 'number of rounds',
757 Setup.Number_of_rounds),
758 ArgumentOption('-f',
759 'save benchmark to file arg',
760 ''),
761 ArgumentOption('-c',
762 'compare benchmark with the one in file arg',
763 ''),
764 ArgumentOption('-s',
765 'show benchmark in file arg, then exit',
766 ''),
767 ArgumentOption('-w',
768 'set warp factor to arg',
769 Setup.Warp_factor),
770 ArgumentOption('-t',
771 'run only tests with names matching arg',
772 ''),
773 ArgumentOption('-C',
774 'set the number of calibration runs to arg',
775 CALIBRATION_RUNS),
776 SwitchOption('-d',
777 'hide noise in comparisons',
778 0),
779 SwitchOption('-v',
780 'verbose output (not recommended)',
781 0),
782 SwitchOption('--with-gc',
783 'enable garbage collection',
784 0),
785 SwitchOption('--with-syscheck',
786 'use default sys check interval',
787 0),
788 ArgumentOption('--timer',
789 'use given timer',
790 TIMER_PLATFORM_DEFAULT),
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791 ]
792
793 about = """\
794The normal operation is to run the suite and display the
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000795results. Use -f to save them for later reuse or comparisons.
796
797Available timers:
798
799 time.time
800 time.clock
801 systimes.processtime
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000802
803Examples:
804
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000805python2.1 pybench.py -f p21.pybench
806python2.5 pybench.py -f p25.pybench
807python pybench.py -s p25.pybench -c p21.pybench
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000808"""
809 copyright = __copyright__
810
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811 def main(self):
812
813 rounds = self.values['-n']
814 reportfile = self.values['-f']
815 show_bench = self.values['-s']
816 compare_to = self.values['-c']
817 hidenoise = self.values['-d']
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000818 warp = int(self.values['-w'])
819 withgc = self.values['--with-gc']
Thomas Wouters477c8d52006-05-27 19:21:47 +0000820 limitnames = self.values['-t']
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000821 if limitnames:
822 if _debug:
Guido van Rossum486364b2007-06-30 05:01:58 +0000823 print('* limiting test names to one with substring "%s"' % \
824 limitnames)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000825 limitnames = re.compile(limitnames, re.I)
826 else:
827 limitnames = None
Thomas Wouters477c8d52006-05-27 19:21:47 +0000828 verbose = self.verbose
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000829 withsyscheck = self.values['--with-syscheck']
830 calibration_runs = self.values['-C']
831 timer = self.values['--timer']
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832
Guido van Rossum486364b2007-06-30 05:01:58 +0000833 print('-' * LINE)
834 print('PYBENCH %s' % __version__)
835 print('-' * LINE)
836 print('* using %s %s' % (
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000837 getattr(platform, 'python_implementation', lambda:'Python')(),
Guido van Rossum486364b2007-06-30 05:01:58 +0000838 ' '.join(sys.version.split())))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000839
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000840 # Switch off garbage collection
841 if not withgc:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000842 try:
843 import gc
844 except ImportError:
Guido van Rossum486364b2007-06-30 05:01:58 +0000845 print('* Python version doesn\'t support garbage collection')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000846 else:
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000847 try:
848 gc.disable()
849 except NotImplementedError:
Guido van Rossum486364b2007-06-30 05:01:58 +0000850 print('* Python version doesn\'t support gc.disable')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000851 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000852 print('* disabled garbage collection')
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000853
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000854 # "Disable" sys check interval
855 if not withsyscheck:
856 # Too bad the check interval uses an int instead of a long...
857 value = 2147483647
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000858 try:
859 sys.setcheckinterval(value)
860 except (AttributeError, NotImplementedError):
Guido van Rossum486364b2007-06-30 05:01:58 +0000861 print('* Python version doesn\'t support sys.setcheckinterval')
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000862 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000863 print('* system check interval set to maximum: %s' % value)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000864
865 if timer == TIMER_SYSTIMES_PROCESSTIME:
866 import systimes
Guido van Rossum486364b2007-06-30 05:01:58 +0000867 print('* using timer: systimes.processtime (%s)' % \
868 systimes.SYSTIMES_IMPLEMENTATION)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000869 else:
Guido van Rossum486364b2007-06-30 05:01:58 +0000870 print('* using timer: %s' % timer)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000871
Guido van Rossum486364b2007-06-30 05:01:58 +0000872 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000873
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874 if compare_to:
875 try:
876 f = open(compare_to,'rb')
877 bench = pickle.load(f)
878 bench.name = compare_to
879 f.close()
880 compare_to = bench
Guido van Rossumb940e112007-01-10 16:19:56 +0000881 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000882 print('* Error opening/reading file %s: %s' % (
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000883 repr(compare_to),
Guido van Rossum486364b2007-06-30 05:01:58 +0000884 reason))
Thomas Wouters477c8d52006-05-27 19:21:47 +0000885 compare_to = None
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886
887 if show_bench:
888 try:
889 f = open(show_bench,'rb')
890 bench = pickle.load(f)
891 bench.name = show_bench
892 f.close()
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000893 bench.print_header()
894 if compare_to:
895 bench.print_comparison(compare_to,
896 hidenoise=hidenoise,
897 limitnames=limitnames)
898 else:
899 bench.print_benchmark(hidenoise=hidenoise,
900 limitnames=limitnames)
Guido van Rossumb940e112007-01-10 16:19:56 +0000901 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000902 print('* Error opening/reading file %s: %s' % (
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000903 repr(show_bench),
Guido van Rossum486364b2007-06-30 05:01:58 +0000904 reason))
905 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000906 return
907
908 if reportfile:
Guido van Rossum486364b2007-06-30 05:01:58 +0000909 print('Creating benchmark: %s (rounds=%i, warp=%i)' % \
910 (reportfile, rounds, warp))
911 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000912
913 # Create benchmark object
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000914 bench = Benchmark(reportfile,
915 verbose=verbose,
916 timer=timer,
917 warp=warp,
918 calibration_runs=calibration_runs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 bench.rounds = rounds
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000920 bench.load_tests(Setup, limitnames=limitnames)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921 try:
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000922 bench.calibrate()
923 bench.run()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000924 except KeyboardInterrupt:
Guido van Rossum486364b2007-06-30 05:01:58 +0000925 print()
926 print('*** KeyboardInterrupt -- Aborting')
927 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000928 return
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000929 bench.print_header()
930 if compare_to:
931 bench.print_comparison(compare_to,
932 hidenoise=hidenoise,
933 limitnames=limitnames)
934 else:
935 bench.print_benchmark(hidenoise=hidenoise,
936 limitnames=limitnames)
937
938 # Ring bell
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000939 sys.stderr.write('\007')
940
941 if reportfile:
942 try:
943 f = open(reportfile,'wb')
944 bench.name = reportfile
945 pickle.dump(bench,f)
946 f.close()
Guido van Rossumb940e112007-01-10 16:19:56 +0000947 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000948 print('* Error opening/writing reportfile')
Guido van Rossumb940e112007-01-10 16:19:56 +0000949 except IOError as reason:
Guido van Rossum486364b2007-06-30 05:01:58 +0000950 print('* Error opening/writing reportfile %s: %s' % (
Thomas Wouters89f507f2006-12-13 04:49:30 +0000951 reportfile,
Guido van Rossum486364b2007-06-30 05:01:58 +0000952 reason))
953 print()
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954
955if __name__ == '__main__':
956 PyBenchCmdline()