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