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 | """ |
Antoine Pitrou | 8a68122 | 2009-02-07 17:13:31 +0000 | [diff] [blame] | 6 | # Note: Please keep this module compatible to Python 2.6. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 7 | # |
| 8 | # Tests may include features in later Python versions, but these |
| 9 | # should then be embedded in try-except clauses in the configuration |
| 10 | # module Setup.py. |
| 11 | # |
| 12 | |
Antoine Pitrou | 8a68122 | 2009-02-07 17:13:31 +0000 | [diff] [blame] | 13 | from __future__ import print_function |
| 14 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 15 | # pybench Copyright |
| 16 | __copyright__ = """\ |
| 17 | Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com) |
| 18 | Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com) |
| 19 | |
| 20 | All Rights Reserved. |
| 21 | |
| 22 | Permission to use, copy, modify, and distribute this software and its |
| 23 | documentation for any purpose and without fee or royalty is hereby |
| 24 | granted, provided that the above copyright notice appear in all copies |
| 25 | and that both that copyright notice and this permission notice appear |
| 26 | in supporting documentation or portions thereof, including |
| 27 | modifications, that you make. |
| 28 | |
| 29 | THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 30 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 31 | FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, |
| 32 | INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING |
| 33 | FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
| 34 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION |
| 35 | WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! |
| 36 | """ |
| 37 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 38 | import sys, time, operator, platform |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 39 | from CommandLine import * |
| 40 | |
| 41 | try: |
| 42 | import cPickle |
| 43 | pickle = cPickle |
| 44 | except ImportError: |
| 45 | import pickle |
| 46 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 47 | # Version number; version history: see README file ! |
Antoine Pitrou | 8a68122 | 2009-02-07 17:13:31 +0000 | [diff] [blame] | 48 | __version__ = '2.1' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 49 | |
| 50 | ### Constants |
| 51 | |
| 52 | # Second fractions |
| 53 | MILLI_SECONDS = 1e3 |
| 54 | MICRO_SECONDS = 1e6 |
| 55 | |
| 56 | # Percent unit |
| 57 | PERCENT = 100 |
| 58 | |
| 59 | # Horizontal line length |
| 60 | LINE = 79 |
| 61 | |
| 62 | # Minimum test run-time |
| 63 | MIN_TEST_RUNTIME = 1e-3 |
| 64 | |
| 65 | # Number of calibration runs to use for calibrating the tests |
| 66 | CALIBRATION_RUNS = 20 |
| 67 | |
| 68 | # Number of calibration loops to run for each calibration run |
| 69 | CALIBRATION_LOOPS = 20 |
| 70 | |
| 71 | # Allow skipping calibration ? |
| 72 | ALLOW_SKIPPING_CALIBRATION = 1 |
| 73 | |
| 74 | # Timer types |
| 75 | TIMER_TIME_TIME = 'time.time' |
| 76 | TIMER_TIME_CLOCK = 'time.clock' |
| 77 | TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' |
| 78 | |
| 79 | # Choose platform default timer |
| 80 | if sys.platform[:3] == 'win': |
| 81 | # On WinXP this has 2.5ms resolution |
| 82 | TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK |
| 83 | else: |
| 84 | # On Linux this has 1ms resolution |
| 85 | TIMER_PLATFORM_DEFAULT = TIMER_TIME_TIME |
| 86 | |
| 87 | # Print debug information ? |
| 88 | _debug = 0 |
| 89 | |
| 90 | ### Helpers |
| 91 | |
| 92 | def get_timer(timertype): |
| 93 | |
| 94 | if timertype == TIMER_TIME_TIME: |
| 95 | return time.time |
| 96 | elif timertype == TIMER_TIME_CLOCK: |
| 97 | return time.clock |
| 98 | elif timertype == TIMER_SYSTIMES_PROCESSTIME: |
| 99 | import systimes |
| 100 | return systimes.processtime |
| 101 | else: |
| 102 | raise TypeError('unknown timer type: %s' % timertype) |
| 103 | |
| 104 | def get_machine_details(): |
| 105 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 106 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 107 | print('Getting machine details...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 108 | buildno, builddate = platform.python_build() |
| 109 | python = platform.python_version() |
Antoine Pitrou | 251803b | 2008-07-22 18:03:03 +0000 | [diff] [blame] | 110 | if sys.maxunicode == 65535: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 111 | # UCS2 build (standard) |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 112 | unitype = 'UCS2' |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 113 | else: |
| 114 | # UCS4 build (most recent Linux distros) |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 115 | unitype = 'UCS4' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 116 | bits, linkage = platform.architecture() |
| 117 | return { |
| 118 | 'platform': platform.platform(), |
| 119 | 'processor': platform.processor(), |
| 120 | 'executable': sys.executable, |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 121 | 'implementation': getattr(platform, 'python_implementation', |
| 122 | lambda:'n/a')(), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 123 | 'python': platform.python_version(), |
| 124 | 'compiler': platform.python_compiler(), |
| 125 | 'buildno': buildno, |
| 126 | 'builddate': builddate, |
Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 127 | 'unicode': unitype, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 128 | 'bits': bits, |
| 129 | } |
| 130 | |
| 131 | def print_machine_details(d, indent=''): |
| 132 | |
| 133 | l = ['Machine Details:', |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 134 | ' Platform ID: %s' % d.get('platform', 'n/a'), |
| 135 | ' Processor: %s' % d.get('processor', 'n/a'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 136 | '', |
| 137 | 'Python:', |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 138 | ' Implementation: %s' % d.get('implementation', 'n/a'), |
| 139 | ' Executable: %s' % d.get('executable', 'n/a'), |
| 140 | ' Version: %s' % d.get('python', 'n/a'), |
| 141 | ' Compiler: %s' % d.get('compiler', 'n/a'), |
| 142 | ' Bits: %s' % d.get('bits', 'n/a'), |
| 143 | ' Build: %s (#%s)' % (d.get('builddate', 'n/a'), |
| 144 | d.get('buildno', 'n/a')), |
| 145 | ' Unicode: %s' % d.get('unicode', 'n/a'), |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 146 | ] |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 147 | joiner = '\n' + indent |
| 148 | print(indent + joiner.join(l) + '\n') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 149 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 150 | ### Test baseclass |
| 151 | |
| 152 | class Test: |
| 153 | |
| 154 | """ All test must have this class as baseclass. It provides |
| 155 | the necessary interface to the benchmark machinery. |
| 156 | |
| 157 | The tests must set .rounds to a value high enough to let the |
| 158 | test run between 20-50 seconds. This is needed because |
| 159 | clock()-timing only gives rather inaccurate values (on Linux, |
| 160 | for example, it is accurate to a few hundreths of a |
| 161 | second). If you don't want to wait that long, use a warp |
| 162 | factor larger than 1. |
| 163 | |
| 164 | It is also important to set the .operations variable to a |
| 165 | value representing the number of "virtual operations" done per |
| 166 | call of .run(). |
| 167 | |
| 168 | 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] | 169 | its version number. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 170 | |
| 171 | """ |
| 172 | |
| 173 | ### Instance variables that each test should override |
| 174 | |
| 175 | # Version number of the test as float (x.yy); this is important |
| 176 | # for comparisons of benchmark runs - tests with unequal version |
| 177 | # number will not get compared. |
Antoine Pitrou | 8a68122 | 2009-02-07 17:13:31 +0000 | [diff] [blame] | 178 | version = 2.1 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 179 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 180 | # The number of abstract operations done in each round of the |
| 181 | # test. An operation is the basic unit of what you want to |
| 182 | # measure. The benchmark will output the amount of run-time per |
| 183 | # operation. Note that in order to raise the measured timings |
| 184 | # significantly above noise level, it is often required to repeat |
| 185 | # sets of operations more than once per test round. The measured |
| 186 | # overhead per test round should be less than 1 second. |
| 187 | operations = 1 |
| 188 | |
| 189 | # Number of rounds to execute per test run. This should be |
| 190 | # 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] | 191 | # 1-2 seconds. |
| 192 | rounds = 100000 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 193 | |
| 194 | ### Internal variables |
| 195 | |
| 196 | # Mark this class as implementing a test |
| 197 | is_a_test = 1 |
| 198 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 199 | # Last timing: (real, run, overhead) |
| 200 | last_timing = (0.0, 0.0, 0.0) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 201 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 202 | # Warp factor to use for this test |
| 203 | warp = 1 |
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 | # Number of calibration runs to use |
| 206 | calibration_runs = CALIBRATION_RUNS |
| 207 | |
| 208 | # List of calibration timings |
| 209 | overhead_times = None |
| 210 | |
| 211 | # List of test run timings |
| 212 | times = [] |
| 213 | |
| 214 | # Timer used for the benchmark |
| 215 | timer = TIMER_PLATFORM_DEFAULT |
| 216 | |
| 217 | def __init__(self, warp=None, calibration_runs=None, timer=None): |
| 218 | |
| 219 | # Set parameters |
| 220 | if warp is not None: |
| 221 | self.rounds = int(self.rounds / warp) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 222 | if self.rounds == 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 223 | raise ValueError('warp factor set too high') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 224 | self.warp = warp |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 225 | if calibration_runs is not None: |
| 226 | if (not ALLOW_SKIPPING_CALIBRATION and |
| 227 | calibration_runs < 1): |
| 228 | raise ValueError('at least one calibration run is required') |
| 229 | self.calibration_runs = calibration_runs |
| 230 | if timer is not None: |
| 231 | timer = timer |
| 232 | |
| 233 | # Init variables |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 234 | self.times = [] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 235 | self.overhead_times = [] |
| 236 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 237 | # We want these to be in the instance dict, so that pickle |
| 238 | # saves them |
| 239 | self.version = self.version |
| 240 | self.operations = self.operations |
| 241 | self.rounds = self.rounds |
| 242 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 243 | def get_timer(self): |
| 244 | |
| 245 | """ Return the timer function to use for the test. |
| 246 | |
| 247 | """ |
| 248 | return get_timer(self.timer) |
| 249 | |
| 250 | def compatible(self, other): |
| 251 | |
| 252 | """ Return 1/0 depending on whether the test is compatible |
| 253 | with the other Test instance or not. |
| 254 | |
| 255 | """ |
| 256 | if self.version != other.version: |
| 257 | return 0 |
| 258 | if self.rounds != other.rounds: |
| 259 | return 0 |
| 260 | return 1 |
| 261 | |
| 262 | def calibrate_test(self): |
| 263 | |
| 264 | if self.calibration_runs == 0: |
| 265 | self.overhead_times = [0.0] |
| 266 | return |
| 267 | |
| 268 | calibrate = self.calibrate |
| 269 | timer = self.get_timer() |
| 270 | calibration_loops = range(CALIBRATION_LOOPS) |
| 271 | |
| 272 | # Time the calibration loop overhead |
| 273 | prep_times = [] |
| 274 | for i in range(self.calibration_runs): |
| 275 | t = timer() |
| 276 | for i in calibration_loops: |
| 277 | pass |
| 278 | t = timer() - t |
| 279 | prep_times.append(t) |
| 280 | min_prep_time = min(prep_times) |
| 281 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 282 | print() |
| 283 | print('Calib. prep time = %.6fms' % ( |
| 284 | min_prep_time * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 285 | |
| 286 | # Time the calibration runs (doing CALIBRATION_LOOPS loops of |
| 287 | # .calibrate() method calls each) |
| 288 | for i in range(self.calibration_runs): |
| 289 | t = timer() |
| 290 | for i in calibration_loops: |
| 291 | calibrate() |
| 292 | t = timer() - t |
| 293 | self.overhead_times.append(t / CALIBRATION_LOOPS |
| 294 | - min_prep_time) |
| 295 | |
| 296 | # Check the measured times |
| 297 | min_overhead = min(self.overhead_times) |
| 298 | max_overhead = max(self.overhead_times) |
| 299 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 300 | print('Calib. overhead time = %.6fms' % ( |
| 301 | min_overhead * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 302 | if min_overhead < 0.0: |
| 303 | raise ValueError('calibration setup did not work') |
| 304 | if max_overhead - min_overhead > 0.1: |
| 305 | raise ValueError( |
| 306 | 'overhead calibration timing range too inaccurate: ' |
| 307 | '%r - %r' % (min_overhead, max_overhead)) |
| 308 | |
| 309 | def run(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 310 | |
| 311 | """ Run the test in two phases: first calibrate, then |
| 312 | do the actual test. Be careful to keep the calibration |
| 313 | timing low w/r to the test timing. |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 314 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 315 | """ |
| 316 | test = self.test |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 317 | timer = self.get_timer() |
| 318 | |
| 319 | # Get calibration |
| 320 | min_overhead = min(self.overhead_times) |
| 321 | |
| 322 | # Test run |
| 323 | t = timer() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 324 | test() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 325 | t = timer() - t |
| 326 | if t < MIN_TEST_RUNTIME: |
| 327 | raise ValueError('warp factor too high: ' |
| 328 | 'test times are < 10ms') |
| 329 | eff_time = t - min_overhead |
| 330 | if eff_time < 0: |
| 331 | raise ValueError('wrong calibration') |
| 332 | self.last_timing = (eff_time, t, min_overhead) |
| 333 | self.times.append(eff_time) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 334 | |
| 335 | def calibrate(self): |
| 336 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 337 | """ Calibrate the test. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 338 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 339 | This method should execute everything that is needed to |
| 340 | setup and run the test - except for the actual operations |
| 341 | that you intend to measure. pybench uses this method to |
| 342 | measure the test implementation overhead. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 343 | |
| 344 | """ |
| 345 | return |
| 346 | |
| 347 | def test(self): |
| 348 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 349 | """ Run the test. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 350 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 351 | The test needs to run self.rounds executing |
| 352 | self.operations number of operations each. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 353 | |
| 354 | """ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 355 | return |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 356 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 357 | def stat(self): |
| 358 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 359 | """ Return test run statistics as tuple: |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 360 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 361 | (minimum run time, |
| 362 | average run time, |
| 363 | total run time, |
| 364 | average time per operation, |
| 365 | minimum overhead time) |
| 366 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 367 | """ |
| 368 | runs = len(self.times) |
| 369 | if runs == 0: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 370 | return 0.0, 0.0, 0.0, 0.0 |
| 371 | min_time = min(self.times) |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 372 | total_time = sum(self.times) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 373 | avg_time = total_time / float(runs) |
| 374 | operation_avg = total_time / float(runs |
| 375 | * self.rounds |
| 376 | * self.operations) |
| 377 | if self.overhead_times: |
| 378 | min_overhead = min(self.overhead_times) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 379 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 380 | min_overhead = self.last_timing[2] |
| 381 | return min_time, avg_time, total_time, operation_avg, min_overhead |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 382 | |
| 383 | ### Load Setup |
| 384 | |
| 385 | # This has to be done after the definition of the Test class, since |
| 386 | # the Setup module will import subclasses using this class. |
| 387 | |
| 388 | import Setup |
| 389 | |
| 390 | ### Benchmark base class |
| 391 | |
| 392 | class Benchmark: |
| 393 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 394 | # Name of the benchmark |
| 395 | name = '' |
| 396 | |
| 397 | # Number of benchmark rounds to run |
| 398 | rounds = 1 |
| 399 | |
| 400 | # Warp factor use to run the tests |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 401 | warp = 1 # Warp factor |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 402 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 403 | # Average benchmark round time |
| 404 | roundtime = 0 |
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 | # Benchmark version number as float x.yy |
Antoine Pitrou | 8a68122 | 2009-02-07 17:13:31 +0000 | [diff] [blame] | 407 | version = 2.1 |
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 | # Produce verbose output ? |
| 410 | verbose = 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 | # Dictionary with the machine details |
| 413 | machine_details = None |
| 414 | |
| 415 | # Timer used for the benchmark |
| 416 | timer = TIMER_PLATFORM_DEFAULT |
| 417 | |
| 418 | def __init__(self, name, verbose=None, timer=None, warp=None, |
| 419 | calibration_runs=None): |
| 420 | |
| 421 | if name: |
| 422 | self.name = name |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 423 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 424 | self.name = '%04i-%02i-%02i %02i:%02i:%02i' % \ |
| 425 | (time.localtime(time.time())[:6]) |
| 426 | if verbose is not None: |
| 427 | self.verbose = verbose |
| 428 | if timer is not None: |
| 429 | self.timer = timer |
| 430 | if warp is not None: |
| 431 | self.warp = warp |
| 432 | if calibration_runs is not None: |
| 433 | self.calibration_runs = calibration_runs |
| 434 | |
| 435 | # Init vars |
| 436 | self.tests = {} |
| 437 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 438 | print('Getting machine details...') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 439 | self.machine_details = get_machine_details() |
| 440 | |
| 441 | # Make .version an instance attribute to have it saved in the |
| 442 | # Benchmark pickle |
| 443 | self.version = self.version |
| 444 | |
| 445 | def get_timer(self): |
| 446 | |
| 447 | """ Return the timer function to use for the test. |
| 448 | |
| 449 | """ |
| 450 | return get_timer(self.timer) |
| 451 | |
| 452 | def compatible(self, other): |
| 453 | |
| 454 | """ Return 1/0 depending on whether the benchmark is |
| 455 | compatible with the other Benchmark instance or not. |
| 456 | |
| 457 | """ |
| 458 | if self.version != other.version: |
| 459 | return 0 |
| 460 | if (self.machine_details == other.machine_details and |
| 461 | self.timer != other.timer): |
| 462 | return 0 |
| 463 | if (self.calibration_runs == 0 and |
| 464 | other.calibration_runs != 0): |
| 465 | return 0 |
| 466 | if (self.calibration_runs != 0 and |
| 467 | other.calibration_runs == 0): |
| 468 | return 0 |
| 469 | return 1 |
| 470 | |
| 471 | def load_tests(self, setupmod, limitnames=None): |
| 472 | |
| 473 | # Add tests |
| 474 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 475 | print('Searching for tests ...') |
| 476 | print('--------------------------------------') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 477 | for testclass in setupmod.__dict__.values(): |
| 478 | if not hasattr(testclass, 'is_a_test'): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 479 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 480 | name = testclass.__name__ |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 481 | if name == 'Test': |
| 482 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 483 | if (limitnames is not None and |
| 484 | limitnames.search(name) is None): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 485 | continue |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 486 | self.tests[name] = testclass( |
| 487 | warp=self.warp, |
| 488 | calibration_runs=self.calibration_runs, |
| 489 | timer=self.timer) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 490 | l = sorted(self.tests) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 491 | if self.verbose: |
| 492 | for name in l: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 493 | print(' %s' % name) |
| 494 | print('--------------------------------------') |
| 495 | print(' %i tests found' % len(l)) |
| 496 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 497 | |
| 498 | def calibrate(self): |
| 499 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 500 | print('Calibrating tests. Please wait...', end=' ') |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 501 | sys.stdout.flush() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 502 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 503 | print() |
| 504 | print() |
| 505 | print('Test min max') |
| 506 | print('-' * LINE) |
| 507 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 508 | for i in range(len(tests)): |
| 509 | name, test = tests[i] |
| 510 | test.calibrate_test() |
| 511 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 512 | print('%30s: %6.3fms %6.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 513 | (name, |
| 514 | min(test.overhead_times) * MILLI_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 515 | max(test.overhead_times) * MILLI_SECONDS)) |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 516 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 517 | print() |
| 518 | print('Done with the calibration.') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 519 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 520 | print('done.') |
| 521 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 522 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 523 | def run(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 524 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 525 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 526 | timer = self.get_timer() |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 527 | print('Running %i round(s) of the suite at warp factor %i:' % \ |
| 528 | (self.rounds, self.warp)) |
| 529 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 530 | self.roundtimes = [] |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 531 | for i in range(self.rounds): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 532 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 533 | print(' Round %-25i effective absolute overhead' % (i+1)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 534 | total_eff_time = 0.0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 535 | for j in range(len(tests)): |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 536 | name, test = tests[j] |
| 537 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 538 | print('%30s:' % name, end=' ') |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 539 | test.run() |
| 540 | (eff_time, abs_time, min_overhead) = test.last_timing |
| 541 | total_eff_time = total_eff_time + eff_time |
| 542 | if self.verbose: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 543 | print(' %5.0fms %5.0fms %7.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 544 | (eff_time * MILLI_SECONDS, |
| 545 | abs_time * MILLI_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 546 | min_overhead * MILLI_SECONDS)) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 547 | self.roundtimes.append(total_eff_time) |
| 548 | if self.verbose: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 549 | print(' ' |
| 550 | ' ------------------------------') |
| 551 | print(' ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 552 | ' Totals: %6.0fms' % |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 553 | (total_eff_time * MILLI_SECONDS)) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 554 | print() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 555 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 556 | print('* Round %i done in %.3f seconds.' % (i+1, |
| 557 | total_eff_time)) |
| 558 | print() |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 559 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 560 | def stat(self): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 561 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 562 | """ Return benchmark run statistics as tuple: |
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 | (minimum round time, |
| 565 | average round time, |
| 566 | maximum round time) |
| 567 | |
| 568 | XXX Currently not used, since the benchmark does test |
| 569 | statistics across all rounds. |
| 570 | |
| 571 | """ |
| 572 | runs = len(self.roundtimes) |
| 573 | if runs == 0: |
| 574 | return 0.0, 0.0 |
| 575 | min_time = min(self.roundtimes) |
Guido van Rossum | 89da5d7 | 2006-08-22 00:21:25 +0000 | [diff] [blame] | 576 | total_time = sum(self.roundtimes) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 577 | avg_time = total_time / float(runs) |
| 578 | max_time = max(self.roundtimes) |
| 579 | return (min_time, avg_time, max_time) |
| 580 | |
| 581 | def print_header(self, title='Benchmark'): |
| 582 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 583 | print('-' * LINE) |
| 584 | print('%s: %s' % (title, self.name)) |
| 585 | print('-' * LINE) |
| 586 | print() |
| 587 | print(' Rounds: %s' % self.rounds) |
| 588 | print(' Warp: %s' % self.warp) |
| 589 | print(' Timer: %s' % self.timer) |
| 590 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 591 | if self.machine_details: |
| 592 | print_machine_details(self.machine_details, indent=' ') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 593 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 594 | |
| 595 | def print_benchmark(self, hidenoise=0, limitnames=None): |
| 596 | |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 597 | print('Test ' |
| 598 | ' minimum average operation overhead') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 599 | print('-' * LINE) |
| 600 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 601 | total_min_time = 0.0 |
| 602 | total_avg_time = 0.0 |
| 603 | for name, test in tests: |
| 604 | if (limitnames is not None and |
| 605 | limitnames.search(name) is None): |
| 606 | continue |
| 607 | (min_time, |
| 608 | avg_time, |
| 609 | total_time, |
| 610 | op_avg, |
| 611 | min_overhead) = test.stat() |
| 612 | total_min_time = total_min_time + min_time |
| 613 | total_avg_time = total_avg_time + avg_time |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 614 | print('%30s: %5.0fms %5.0fms %6.2fus %7.3fms' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 615 | (name, |
| 616 | min_time * MILLI_SECONDS, |
| 617 | avg_time * MILLI_SECONDS, |
| 618 | op_avg * MICRO_SECONDS, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 619 | min_overhead *MILLI_SECONDS)) |
| 620 | print('-' * LINE) |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 621 | print('Totals: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 622 | ' %6.0fms %6.0fms' % |
| 623 | (total_min_time * MILLI_SECONDS, |
| 624 | total_avg_time * MILLI_SECONDS, |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 625 | )) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 626 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 627 | |
| 628 | def print_comparison(self, compare_to, hidenoise=0, limitnames=None): |
| 629 | |
| 630 | # Check benchmark versions |
| 631 | if compare_to.version != self.version: |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 632 | print('* Benchmark versions differ: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 633 | 'cannot compare this benchmark to "%s" !' % |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 634 | compare_to.name) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 635 | print() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 636 | self.print_benchmark(hidenoise=hidenoise, |
| 637 | limitnames=limitnames) |
| 638 | return |
| 639 | |
| 640 | # Print header |
| 641 | compare_to.print_header('Comparing with') |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 642 | print('Test ' |
| 643 | ' minimum run-time average run-time') |
| 644 | print(' ' |
| 645 | ' this other diff this other diff') |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 646 | print('-' * LINE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 647 | |
| 648 | # Print test comparisons |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 649 | tests = sorted(self.tests.items()) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 650 | total_min_time = other_total_min_time = 0.0 |
| 651 | total_avg_time = other_total_avg_time = 0.0 |
| 652 | benchmarks_compatible = self.compatible(compare_to) |
| 653 | tests_compatible = 1 |
| 654 | for name, test in tests: |
| 655 | if (limitnames is not None and |
| 656 | limitnames.search(name) is None): |
| 657 | continue |
| 658 | (min_time, |
| 659 | avg_time, |
| 660 | total_time, |
| 661 | op_avg, |
| 662 | min_overhead) = test.stat() |
| 663 | total_min_time = total_min_time + min_time |
| 664 | total_avg_time = total_avg_time + avg_time |
| 665 | try: |
| 666 | other = compare_to.tests[name] |
| 667 | except KeyError: |
| 668 | other = None |
| 669 | if other is None: |
| 670 | # Other benchmark doesn't include the given test |
| 671 | min_diff, avg_diff = 'n/a', 'n/a' |
| 672 | other_min_time = 0.0 |
| 673 | other_avg_time = 0.0 |
| 674 | tests_compatible = 0 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 675 | else: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 676 | (other_min_time, |
| 677 | other_avg_time, |
| 678 | other_total_time, |
| 679 | other_op_avg, |
| 680 | other_min_overhead) = other.stat() |
| 681 | other_total_min_time = other_total_min_time + other_min_time |
| 682 | other_total_avg_time = other_total_avg_time + other_avg_time |
| 683 | if (benchmarks_compatible and |
| 684 | test.compatible(other)): |
| 685 | # Both benchmark and tests are comparible |
| 686 | min_diff = ((min_time * self.warp) / |
| 687 | (other_min_time * other.warp) - 1.0) |
| 688 | avg_diff = ((avg_time * self.warp) / |
| 689 | (other_avg_time * other.warp) - 1.0) |
| 690 | if hidenoise and abs(min_diff) < 10.0: |
| 691 | min_diff = '' |
| 692 | else: |
| 693 | min_diff = '%+5.1f%%' % (min_diff * PERCENT) |
| 694 | if hidenoise and abs(avg_diff) < 10.0: |
| 695 | avg_diff = '' |
| 696 | else: |
| 697 | avg_diff = '%+5.1f%%' % (avg_diff * PERCENT) |
| 698 | else: |
| 699 | # Benchmark or tests are not comparible |
| 700 | min_diff, avg_diff = 'n/a', 'n/a' |
| 701 | tests_compatible = 0 |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 702 | print('%30s: %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' % \ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 703 | (name, |
| 704 | min_time * MILLI_SECONDS, |
| 705 | other_min_time * MILLI_SECONDS * compare_to.warp / self.warp, |
| 706 | min_diff, |
| 707 | avg_time * MILLI_SECONDS, |
| 708 | other_avg_time * MILLI_SECONDS * compare_to.warp / self.warp, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 709 | avg_diff)) |
| 710 | print('-' * LINE) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 711 | |
| 712 | # Summarise test results |
| 713 | if not benchmarks_compatible or not tests_compatible: |
| 714 | min_diff, avg_diff = 'n/a', 'n/a' |
| 715 | else: |
| 716 | if other_total_min_time != 0.0: |
| 717 | min_diff = '%+5.1f%%' % ( |
| 718 | ((total_min_time * self.warp) / |
| 719 | (other_total_min_time * compare_to.warp) - 1.0) * PERCENT) |
| 720 | else: |
| 721 | min_diff = 'n/a' |
| 722 | if other_total_avg_time != 0.0: |
| 723 | avg_diff = '%+5.1f%%' % ( |
| 724 | ((total_avg_time * self.warp) / |
| 725 | (other_total_avg_time * compare_to.warp) - 1.0) * PERCENT) |
| 726 | else: |
| 727 | avg_diff = 'n/a' |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 728 | print('Totals: ' |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 729 | ' %5.0fms %5.0fms %7s %5.0fms %5.0fms %7s' % |
| 730 | (total_min_time * MILLI_SECONDS, |
| 731 | (other_total_min_time * compare_to.warp/self.warp |
| 732 | * MILLI_SECONDS), |
| 733 | min_diff, |
| 734 | total_avg_time * MILLI_SECONDS, |
| 735 | (other_total_avg_time * compare_to.warp/self.warp |
| 736 | * MILLI_SECONDS), |
| 737 | avg_diff |
Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 738 | )) |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 739 | print() |
| 740 | print('(this=%s, other=%s)' % (self.name, |
| 741 | compare_to.name)) |
| 742 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 743 | |
| 744 | class PyBenchCmdline(Application): |
| 745 | |
| 746 | header = ("PYBENCH - a benchmark test suite for Python " |
| 747 | "interpreters/compilers.") |
| 748 | |
| 749 | version = __version__ |
| 750 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 751 | debug = _debug |
| 752 | |
| 753 | options = [ArgumentOption('-n', |
| 754 | 'number of rounds', |
| 755 | Setup.Number_of_rounds), |
| 756 | ArgumentOption('-f', |
| 757 | 'save benchmark to file arg', |
| 758 | ''), |
| 759 | ArgumentOption('-c', |
| 760 | 'compare benchmark with the one in file arg', |
| 761 | ''), |
| 762 | ArgumentOption('-s', |
| 763 | 'show benchmark in file arg, then exit', |
| 764 | ''), |
| 765 | ArgumentOption('-w', |
| 766 | 'set warp factor to arg', |
| 767 | Setup.Warp_factor), |
| 768 | ArgumentOption('-t', |
| 769 | 'run only tests with names matching arg', |
| 770 | ''), |
| 771 | ArgumentOption('-C', |
| 772 | 'set the number of calibration runs to arg', |
| 773 | CALIBRATION_RUNS), |
| 774 | SwitchOption('-d', |
| 775 | 'hide noise in comparisons', |
| 776 | 0), |
| 777 | SwitchOption('-v', |
| 778 | 'verbose output (not recommended)', |
| 779 | 0), |
| 780 | SwitchOption('--with-gc', |
| 781 | 'enable garbage collection', |
| 782 | 0), |
| 783 | SwitchOption('--with-syscheck', |
| 784 | 'use default sys check interval', |
| 785 | 0), |
| 786 | ArgumentOption('--timer', |
| 787 | 'use given timer', |
| 788 | TIMER_PLATFORM_DEFAULT), |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 789 | ] |
| 790 | |
| 791 | about = """\ |
| 792 | The normal operation is to run the suite and display the |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 793 | results. Use -f to save them for later reuse or comparisons. |
| 794 | |
| 795 | Available timers: |
| 796 | |
| 797 | time.time |
| 798 | time.clock |
| 799 | systimes.processtime |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 800 | |
| 801 | Examples: |
| 802 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 803 | python2.1 pybench.py -f p21.pybench |
| 804 | python2.5 pybench.py -f p25.pybench |
| 805 | python pybench.py -s p25.pybench -c p21.pybench |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 806 | """ |
| 807 | copyright = __copyright__ |
| 808 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 809 | def main(self): |
| 810 | |
| 811 | rounds = self.values['-n'] |
| 812 | reportfile = self.values['-f'] |
| 813 | show_bench = self.values['-s'] |
| 814 | compare_to = self.values['-c'] |
| 815 | hidenoise = self.values['-d'] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 816 | warp = int(self.values['-w']) |
| 817 | withgc = self.values['--with-gc'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 818 | limitnames = self.values['-t'] |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 819 | if limitnames: |
| 820 | if _debug: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 821 | print('* limiting test names to one with substring "%s"' % \ |
| 822 | limitnames) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 823 | limitnames = re.compile(limitnames, re.I) |
| 824 | else: |
| 825 | limitnames = None |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 826 | verbose = self.verbose |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 827 | withsyscheck = self.values['--with-syscheck'] |
| 828 | calibration_runs = self.values['-C'] |
| 829 | timer = self.values['--timer'] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 830 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 831 | print('-' * LINE) |
| 832 | print('PYBENCH %s' % __version__) |
| 833 | print('-' * LINE) |
| 834 | print('* using %s %s' % ( |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 835 | getattr(platform, 'python_implementation', lambda:'Python')(), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 836 | ' '.join(sys.version.split()))) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 837 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 838 | # Switch off garbage collection |
| 839 | if not withgc: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 840 | try: |
| 841 | import gc |
| 842 | except ImportError: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 843 | print('* Python version doesn\'t support garbage collection') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 844 | else: |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 845 | try: |
| 846 | gc.disable() |
| 847 | except NotImplementedError: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 848 | print('* Python version doesn\'t support gc.disable') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 849 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 850 | print('* disabled garbage collection') |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 851 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 852 | # "Disable" sys check interval |
| 853 | if not withsyscheck: |
| 854 | # Too bad the check interval uses an int instead of a long... |
| 855 | value = 2147483647 |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 856 | try: |
| 857 | sys.setcheckinterval(value) |
| 858 | except (AttributeError, NotImplementedError): |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 859 | print('* Python version doesn\'t support sys.setcheckinterval') |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 860 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 861 | print('* system check interval set to maximum: %s' % value) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 862 | |
| 863 | if timer == TIMER_SYSTIMES_PROCESSTIME: |
| 864 | import systimes |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 865 | print('* using timer: systimes.processtime (%s)' % \ |
| 866 | systimes.SYSTIMES_IMPLEMENTATION) |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 867 | else: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 868 | print('* using timer: %s' % timer) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 869 | |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 870 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 871 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 872 | if compare_to: |
| 873 | try: |
| 874 | f = open(compare_to,'rb') |
| 875 | bench = pickle.load(f) |
| 876 | bench.name = compare_to |
| 877 | f.close() |
| 878 | compare_to = bench |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 879 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 880 | print('* Error opening/reading file %s: %s' % ( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 881 | repr(compare_to), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 882 | reason)) |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 883 | compare_to = None |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 884 | |
| 885 | if show_bench: |
| 886 | try: |
| 887 | f = open(show_bench,'rb') |
| 888 | bench = pickle.load(f) |
| 889 | bench.name = show_bench |
| 890 | f.close() |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 891 | bench.print_header() |
| 892 | if compare_to: |
| 893 | bench.print_comparison(compare_to, |
| 894 | hidenoise=hidenoise, |
| 895 | limitnames=limitnames) |
| 896 | else: |
| 897 | bench.print_benchmark(hidenoise=hidenoise, |
| 898 | limitnames=limitnames) |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 899 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 900 | print('* Error opening/reading file %s: %s' % ( |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 901 | repr(show_bench), |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 902 | reason)) |
| 903 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 904 | return |
| 905 | |
| 906 | if reportfile: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 907 | print('Creating benchmark: %s (rounds=%i, warp=%i)' % \ |
| 908 | (reportfile, rounds, warp)) |
| 909 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 910 | |
| 911 | # Create benchmark object |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 912 | bench = Benchmark(reportfile, |
| 913 | verbose=verbose, |
| 914 | timer=timer, |
| 915 | warp=warp, |
| 916 | calibration_runs=calibration_runs) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 917 | bench.rounds = rounds |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 918 | bench.load_tests(Setup, limitnames=limitnames) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 919 | try: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 920 | bench.calibrate() |
| 921 | bench.run() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 922 | except KeyboardInterrupt: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 923 | print() |
| 924 | print('*** KeyboardInterrupt -- Aborting') |
| 925 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 926 | return |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 927 | bench.print_header() |
| 928 | if compare_to: |
| 929 | bench.print_comparison(compare_to, |
| 930 | hidenoise=hidenoise, |
| 931 | limitnames=limitnames) |
| 932 | else: |
| 933 | bench.print_benchmark(hidenoise=hidenoise, |
| 934 | limitnames=limitnames) |
| 935 | |
| 936 | # Ring bell |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 937 | sys.stderr.write('\007') |
| 938 | |
| 939 | if reportfile: |
| 940 | try: |
| 941 | f = open(reportfile,'wb') |
| 942 | bench.name = reportfile |
| 943 | pickle.dump(bench,f) |
| 944 | f.close() |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 945 | except IOError as reason: |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 946 | print('* Error opening/writing reportfile') |
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 %s: %s' % ( |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 949 | reportfile, |
Guido van Rossum | 486364b | 2007-06-30 05:01:58 +0000 | [diff] [blame] | 950 | reason)) |
| 951 | print() |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 952 | |
| 953 | if __name__ == '__main__': |
| 954 | PyBenchCmdline() |