Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1 | #!/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__ = """\ |
| 16 | Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com) |
| 17 | Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com) |
| 18 | |
| 19 | All Rights Reserved. |
| 20 | |
| 21 | Permission to use, copy, modify, and distribute this software and its |
| 22 | documentation for any purpose and without fee or royalty is hereby |
| 23 | granted, provided that the above copyright notice appear in all copies |
| 24 | and that both that copyright notice and this permission notice appear |
| 25 | in supporting documentation or portions thereof, including |
| 26 | modifications, that you make. |
| 27 | |
| 28 | THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 29 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 30 | FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, |
| 31 | INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 32 | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 33 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 34 | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! |
| 35 | """ |
| 36 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 37 | import sys, time, operator, platform |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 38 | from CommandLine import * |
| 39 | |
| 40 | try: |
| 41 | import cPickle |
| 42 | pickle = cPickle |
| 43 | except ImportError: |
| 44 | import pickle |
| 45 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 46 | # Version number; version history: see README file ! |
| 47 | __version__ = '2.0' |
| 48 | |
| 49 | ### Constants |
| 50 | |
| 51 | # Second fractions |
| 52 | MILLI_SECONDS = 1e3 |
| 53 | MICRO_SECONDS = 1e6 |
| 54 | |
| 55 | # Percent unit |
| 56 | PERCENT = 100 |
| 57 | |
| 58 | # Horizontal line length |
| 59 | LINE = 79 |
| 60 | |
| 61 | # Minimum test run-time |
| 62 | MIN_TEST_RUNTIME = 1e-3 |
| 63 | |
| 64 | # Number of calibration runs to use for calibrating the tests |
| 65 | CALIBRATION_RUNS = 20 |
| 66 | |
| 67 | # Number of calibration loops to run for each calibration run |
| 68 | CALIBRATION_LOOPS = 20 |
| 69 | |
| 70 | # Allow skipping calibration ? |
| 71 | ALLOW_SKIPPING_CALIBRATION = 1 |
| 72 | |
| 73 | # Timer types |
| 74 | TIMER_TIME_TIME = 'time.time' |
| 75 | TIMER_TIME_CLOCK = 'time.clock' |
| 76 | TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' |
| 77 | |
| 78 | # Choose platform default timer |
| 79 | if sys.platform[:3] == 'win': |
| 80 | # On WinXP this has 2.5ms resolution |
| 81 | TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK |
| 82 | else: |
| 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 | |
| 91 | def 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 | |
| 103 | def get_machine_details(): |
| 104 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 105 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 106 | print('Getting machine details...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 107 | buildno, builddate = platform.python_build() |
| 108 | python = platform.python_version() |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 109 | try: |
| 110 | unichr(100000) |
| 111 | except ValueError: |
| 112 | # UCS2 build (standard) |
| 113 | unicode = 'UCS2' |
| 114 | except NameError: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 115 | unicode = None |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 116 | else: |
| 117 | # UCS4 build (most recent Linux distros) |
| 118 | unicode = 'UCS4' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 119 | bits, linkage = platform.architecture() |
| 120 | return { |
| 121 | 'platform': platform.platform(), |
| 122 | 'processor': platform.processor(), |
| 123 | 'executable': sys.executable, |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 124 | 'implementation': getattr(platform, 'python_implementation', |
| 125 | lambda:'n/a')(), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 126 | 'python': platform.python_version(), |
| 127 | 'compiler': platform.python_compiler(), |
| 128 | 'buildno': buildno, |
| 129 | 'builddate': builddate, |
| 130 | 'unicode': unicode, |
| 131 | 'bits': bits, |
| 132 | } |
| 133 | |
| 134 | def print_machine_details(d, indent=''): |
| 135 | |
| 136 | l = ['Machine Details:', |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 137 | ' Platform ID: %s' % d.get('platform', 'n/a'), |
| 138 | ' Processor: %s' % d.get('processor', 'n/a'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 139 | '', |
| 140 | 'Python:', |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 141 | ' 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 149 | ] |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 150 | joiner = '\n' + indent |
| 151 | print(indent + joiner.join(l) + '\n') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 152 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 153 | ### Test baseclass |
| 154 | |
| 155 | class 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 Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 172 | its version number. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 173 | |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 181 | version = 2.0 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 182 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 183 | # 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 194 | # 1-2 seconds. |
| 195 | rounds = 100000 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 196 | |
| 197 | ### Internal variables |
| 198 | |
| 199 | # Mark this class as implementing a test |
| 200 | is_a_test = 1 |
| 201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | # Last timing: (real, run, overhead) |
| 203 | last_timing = (0.0, 0.0, 0.0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 204 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 205 | # Warp factor to use for this test |
| 206 | warp = 1 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 207 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 208 | # 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 225 | if self.rounds == 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 226 | raise ValueError('warp factor set too high') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 227 | self.warp = warp |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 228 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 237 | self.times = [] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 238 | self.overhead_times = [] |
| 239 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 240 | # 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 246 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 285 | print() |
| 286 | print('Calib. prep time = %.6fms' % ( |
| 287 | min_prep_time * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 288 | |
| 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 303 | print('Calib. overhead time = %.6fms' % ( |
| 304 | min_overhead * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 305 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 313 | |
| 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 317 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 318 | """ |
| 319 | test = self.test |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 320 | timer = self.get_timer() |
| 321 | |
| 322 | # Get calibration |
| 323 | min_overhead = min(self.overhead_times) |
| 324 | |
| 325 | # Test run |
| 326 | t = timer() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 327 | test() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 328 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 337 | |
| 338 | def calibrate(self): |
| 339 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 340 | """ Calibrate the test. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 341 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 342 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 346 | |
| 347 | """ |
| 348 | return |
| 349 | |
| 350 | def test(self): |
| 351 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 352 | """ Run the test. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 354 | The test needs to run self.rounds executing |
| 355 | self.operations number of operations each. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 356 | |
| 357 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 358 | return |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 359 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 360 | def stat(self): |
| 361 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 362 | """ Return test run statistics as tuple: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 363 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 364 | (minimum run time, |
| 365 | average run time, |
| 366 | total run time, |
| 367 | average time per operation, |
| 368 | minimum overhead time) |
| 369 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 370 | """ |
| 371 | runs = len(self.times) |
| 372 | if runs == 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 373 | return 0.0, 0.0, 0.0, 0.0 |
| 374 | min_time = min(self.times) |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 375 | total_time = sum(self.times) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 376 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 382 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 383 | min_overhead = self.last_timing[2] |
| 384 | return min_time, avg_time, total_time, operation_avg, min_overhead |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 385 | |
| 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 | |
| 391 | import Setup |
| 392 | |
| 393 | ### Benchmark base class |
| 394 | |
| 395 | class Benchmark: |
| 396 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 397 | # 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 404 | warp = 1 # Warp factor |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 405 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 406 | # Average benchmark round time |
| 407 | roundtime = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 408 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 409 | # Benchmark version number as float x.yy |
| 410 | version = 2.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 411 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 412 | # Produce verbose output ? |
| 413 | verbose = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 414 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 415 | # 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 Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 426 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 427 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 441 | print('Getting machine details...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 442 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 478 | print('Searching for tests ...') |
| 479 | print('--------------------------------------') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 480 | for testclass in setupmod.__dict__.values(): |
| 481 | if not hasattr(testclass, 'is_a_test'): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 482 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 483 | name = testclass.__name__ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 484 | if name == 'Test': |
| 485 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 486 | if (limitnames is not None and |
| 487 | limitnames.search(name) is None): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 488 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 489 | self.tests[name] = testclass( |
| 490 | warp=self.warp, |
| 491 | calibration_runs=self.calibration_runs, |
| 492 | timer=self.timer) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 493 | l = sorted(self.tests) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 494 | if self.verbose: |
| 495 | for name in l: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 496 | print(' %s' % name) |
| 497 | print('--------------------------------------') |
| 498 | print(' %i tests found' % len(l)) |
| 499 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 500 | |
| 501 | def calibrate(self): |
| 502 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 503 | print('Calibrating tests. Please wait...', end=' ') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 504 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 505 | print() |
| 506 | print() |
| 507 | print('Test min max') |
| 508 | print('-' * LINE) |
| 509 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 510 | for i in range(len(tests)): |
| 511 | name, test = tests[i] |
| 512 | test.calibrate_test() |
| 513 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 514 | print('%30s: %6.3fms %6.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 515 | (name, |
| 516 | min(test.overhead_times) * MILLI_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 517 | max(test.overhead_times) * MILLI_SECONDS)) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 518 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 519 | print() |
| 520 | print('Done with the calibration.') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 521 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 522 | print('done.') |
| 523 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 524 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 525 | def run(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 526 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 527 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 528 | timer = self.get_timer() |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 529 | print('Running %i round(s) of the suite at warp factor %i:' % \ |
| 530 | (self.rounds, self.warp)) |
| 531 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 532 | self.roundtimes = [] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 533 | for i in range(self.rounds): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 534 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 535 | print(' Round %-25i effective absolute overhead' % (i+1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 536 | total_eff_time = 0.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 537 | for j in range(len(tests)): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 538 | name, test = tests[j] |
| 539 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 540 | print('%30s:' % name, end=' ') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 541 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 545 | print(' %5.0fms %5.0fms %7.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 546 | (eff_time * MILLI_SECONDS, |
| 547 | abs_time * MILLI_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 548 | min_overhead * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 549 | self.roundtimes.append(total_eff_time) |
| 550 | if self.verbose: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 551 | print(' ' |
| 552 | ' ------------------------------') |
| 553 | print(' ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 554 | ' Totals: %6.0fms' % |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 555 | (total_eff_time * MILLI_SECONDS)) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 556 | print() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 557 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 558 | print('* Round %i done in %.3f seconds.' % (i+1, |
| 559 | total_eff_time)) |
| 560 | print() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 561 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 562 | def stat(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 563 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 564 | """ Return benchmark run statistics as tuple: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 565 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 566 | (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 Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 578 | total_time = sum(self.roundtimes) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 579 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 585 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 593 | if self.machine_details: |
| 594 | print_machine_details(self.machine_details, indent=' ') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 595 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 596 | |
| 597 | def print_benchmark(self, hidenoise=0, limitnames=None): |
| 598 | |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 599 | print('Test ' |
| 600 | ' minimum average operation overhead') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 601 | print('-' * LINE) |
| 602 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 603 | 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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 616 | print('%30s: %5.0fms %5.0fms %6.2fus %7.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 617 | (name, |
| 618 | min_time * MILLI_SECONDS, |
| 619 | avg_time * MILLI_SECONDS, |
| 620 | op_avg * MICRO_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 621 | min_overhead *MILLI_SECONDS)) |
| 622 | print('-' * LINE) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 623 | print('Totals: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 624 | ' %6.0fms %6.0fms' % |
| 625 | (total_min_time * MILLI_SECONDS, |
| 626 | total_avg_time * MILLI_SECONDS, |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 627 | )) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 628 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 629 | |
| 630 | def print_comparison(self, compare_to, hidenoise=0, limitnames=None): |
| 631 | |
| 632 | # Check benchmark versions |
| 633 | if compare_to.version != self.version: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 634 | print('* Benchmark versions differ: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 635 | 'cannot compare this benchmark to "%s" !' % |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 636 | compare_to.name) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 637 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 638 | self.print_benchmark(hidenoise=hidenoise, |
| 639 | limitnames=limitnames) |
| 640 | return |
| 641 | |
| 642 | # Print header |
| 643 | compare_to.print_header('Comparing with') |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 644 | print('Test ' |
| 645 | ' minimum run-time average run-time') |
| 646 | print(' ' |
| 647 | ' this other diff this other diff') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 648 | print('-' * LINE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 649 | |
| 650 | # Print test comparisons |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 651 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 652 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 677 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 678 | (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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 704 | print('%30s: %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 705 | (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 Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 711 | avg_diff)) |
| 712 | print('-' * LINE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 713 | |
| 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 Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 730 | print('Totals: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 731 | ' %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 Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 740 | )) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 741 | print() |
| 742 | print('(this=%s, other=%s)' % (self.name, |
| 743 | compare_to.name)) |
| 744 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 745 | |
| 746 | class PyBenchCmdline(Application): |
| 747 | |
| 748 | header = ("PYBENCH - a benchmark test suite for Python " |
| 749 | "interpreters/compilers.") |
| 750 | |
| 751 | version = __version__ |
| 752 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 753 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 791 | ] |
| 792 | |
| 793 | about = """\ |
| 794 | The normal operation is to run the suite and display the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 795 | results. Use -f to save them for later reuse or comparisons. |
| 796 | |
| 797 | Available timers: |
| 798 | |
| 799 | time.time |
| 800 | time.clock |
| 801 | systimes.processtime |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 802 | |
| 803 | Examples: |
| 804 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 805 | python2.1 pybench.py -f p21.pybench |
| 806 | python2.5 pybench.py -f p25.pybench |
| 807 | python pybench.py -s p25.pybench -c p21.pybench |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 808 | """ |
| 809 | copyright = __copyright__ |
| 810 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 811 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 818 | warp = int(self.values['-w']) |
| 819 | withgc = self.values['--with-gc'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 820 | limitnames = self.values['-t'] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 821 | if limitnames: |
| 822 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 823 | print('* limiting test names to one with substring "%s"' % \ |
| 824 | limitnames) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 825 | limitnames = re.compile(limitnames, re.I) |
| 826 | else: |
| 827 | limitnames = None |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 828 | verbose = self.verbose |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 829 | withsyscheck = self.values['--with-syscheck'] |
| 830 | calibration_runs = self.values['-C'] |
| 831 | timer = self.values['--timer'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 832 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 833 | print('-' * LINE) |
| 834 | print('PYBENCH %s' % __version__) |
| 835 | print('-' * LINE) |
| 836 | print('* using %s %s' % ( |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 837 | getattr(platform, 'python_implementation', lambda:'Python')(), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 838 | ' '.join(sys.version.split()))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 839 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 840 | # Switch off garbage collection |
| 841 | if not withgc: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 842 | try: |
| 843 | import gc |
| 844 | except ImportError: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 845 | print('* Python version doesn\'t support garbage collection') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 846 | else: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 847 | try: |
| 848 | gc.disable() |
| 849 | except NotImplementedError: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 850 | print('* Python version doesn\'t support gc.disable') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 851 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 852 | print('* disabled garbage collection') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 853 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 854 | # "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 Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 858 | try: |
| 859 | sys.setcheckinterval(value) |
| 860 | except (AttributeError, NotImplementedError): |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 861 | print('* Python version doesn\'t support sys.setcheckinterval') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 862 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 863 | print('* system check interval set to maximum: %s' % value) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 864 | |
| 865 | if timer == TIMER_SYSTIMES_PROCESSTIME: |
| 866 | import systimes |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 867 | print('* using timer: systimes.processtime (%s)' % \ |
| 868 | systimes.SYSTIMES_IMPLEMENTATION) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 869 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 870 | print('* using timer: %s' % timer) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 871 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 872 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 873 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 874 | 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 Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 881 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 882 | print('* Error opening/reading file %s: %s' % ( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 883 | repr(compare_to), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 884 | reason)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 885 | compare_to = None |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 886 | |
| 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 893 | 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 Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 901 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 902 | print('* Error opening/reading file %s: %s' % ( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 903 | repr(show_bench), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 904 | reason)) |
| 905 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 906 | return |
| 907 | |
| 908 | if reportfile: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 909 | print('Creating benchmark: %s (rounds=%i, warp=%i)' % \ |
| 910 | (reportfile, rounds, warp)) |
| 911 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 912 | |
| 913 | # Create benchmark object |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 914 | bench = Benchmark(reportfile, |
| 915 | verbose=verbose, |
| 916 | timer=timer, |
| 917 | warp=warp, |
| 918 | calibration_runs=calibration_runs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 919 | bench.rounds = rounds |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 920 | bench.load_tests(Setup, limitnames=limitnames) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 921 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 922 | bench.calibrate() |
| 923 | bench.run() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 924 | except KeyboardInterrupt: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 925 | print() |
| 926 | print('*** KeyboardInterrupt -- Aborting') |
| 927 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 928 | return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 929 | 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 Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 939 | 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 Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 947 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 948 | print('* Error opening/writing reportfile') |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 949 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 950 | print('* Error opening/writing reportfile %s: %s' % ( |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 951 | reportfile, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 952 | reason)) |
| 953 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 954 | |
| 955 | if __name__ == '__main__': |
| 956 | PyBenchCmdline() |