#!/usr/local/bin/python -O """ A Python Benchmark Suite """## Note: Please keep this module compatible to Python 1.5.2.## Tests may include features in later Python versions, but these# should then be embedded in try-except clauses in the configuration# module Setup.py.# # pybench Copyright__copyright__ = """\Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com)Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com) All Rights Reserved. Permission to use, copy, modify, and distribute this software and itsdocumentation for any purpose and without fee or royalty is herebygranted, provided that the above copyright notice appear in all copiesand that both that copyright notice and this permission notice appearin supporting documentation or portions thereof, includingmodifications, that you make. THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TOTHIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY ANDFITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL,INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTINGFROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTIONWITH THE USE OR PERFORMANCE OF THIS SOFTWARE !""" import sys, time, operator, stringfrom CommandLine import * try: import cPickle pickle = cPickleexcept ImportError: import pickle # Version number; version history: see README file !__version__ = '2.0' ### Constants # Second fractionsMILLI_SECONDS = 1e3MICRO_SECONDS = 1e6 # Percent unitPERCENT = 100 # Horizontal line lengthLINE = 79 # Minimum test run-timeMIN_TEST_RUNTIME = 1e-3 # Number of calibration runs to use for calibrating the testsCALIBRATION_RUNS = 20 # Number of calibration loops to run for each calibration runCALIBRATION_LOOPS = 20 # Allow skipping calibration ?ALLOW_SKIPPING_CALIBRATION = 1 # Timer typesTIMER_TIME_TIME = 'time.time'TIMER_TIME_CLOCK = 'time.clock'TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' # Choose platform default timerif sys.platform[:3] == 'win': # On WinXP this has 2.5ms resolution TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCKelse: # On Linux this has 1ms resolution TIMER_PLATFORM_DEFAULT = TIMER_TIME_TIME # Print debug information ?_debug = 0 ### Helpers def get_timer(timertype): if timertype == TIMER_TIME_TIME: return time.time elif timertype == TIMER_TIME_CLOCK: return time.clock elif timertype == TIMER_SYSTIMES_PROCESSTIME: import systimes return systimes.processtime else: raise TypeError('unknown timer type: %s' % timertype) def get_machine_details(): import platform if _debug: print 'Getting machine details...' buildno, builddate = platform.python_build() python = platform.python_version() if python > '2.0': try: unichr(100000) except ValueError: # UCS2 build (standard) unicode = 'UCS2' else: # UCS4 build (most recent Linux distros) unicode = 'UCS4' else: unicode = None bits, linkage = platform.architecture() return { 'platform': platform.platform(), 'processor': platform.processor(), 'executable': sys.executable, 'python': platform.python_version(), 'compiler': platform.python_compiler(), 'buildno': buildno, 'builddate': builddate, 'unicode': unicode, 'bits': bits, } def print_machine_details(d, indent=''): l = ['Machine Details:', ' Platform ID: %s' % d.get('platform', 'n/a'), ' Processor: %s' % d.get('processor', 'n/a'), '', 'Python:', ' Executable: %s' % d.get('executable', 'n/a'), ' Version: %s' % d.get('python', 'n/a'), ' Compiler: %s' % d.get('compiler', 'n/a'), ' Bits: %s' % d.get('bits', 'n/a'), ' Build: %s (#%s)' % (d.get('builddate', 'n/a'), d.get('buildno', 'n/a')), ' Unicode: %s' % d.get('unicode', 'n/a'), ] print indent + string.join(l, '\n' + indent) + '\n' ### Test baseclass class Test: """ All test must have this class as baseclass. It provides the necessary interface to the benchmark machinery. The tests must set .rounds to a value high enough to let the test run between 20-50 seconds. This is needed because clock()-timing only gives rather inaccurate values (on Linux, for example, it is accurate to a few hundreths of a second). If you don't want to wait that long, use a warp factor larger than 1. It is also important to set the .operations variable to a value representing the number of "virtual operations" done per call of .run(). If you change a test in some way, don't forget to increase it's version number. """ ### Instance variables that each test should override # Version number of the test as float (x.yy); this is important # for comparisons of benchmark runs - tests with unequal version # number will not get compared. version = 2.0 # The number of abstract operations done in each round of the # test. An operation is the basic unit of what you want to # measure. The benchmark will output the amount of run-time per # operation. Note that in order to raise the measured timings # significantly above noise level, it is often required to repeat # sets of operations more than once per test round. The measured # overhead per test round should be less than 1 second. operations = 1 # Number of rounds to execute per test run. This should be # adjusted to a figure that results in a test run-time of between # 1-2 seconds. rounds = 100000 ### Internal variables # Mark this class as implementing a test is_a_test = 1 # Last timing: (real, run, overhead) last_timing = (0.0, 0.0, 0.0) # Warp factor to use for this test warp = 1 # Number of calibration runs to use calibration_runs = CALIBRATION_RUNS # List of calibration timings overhead_times = None # List of test run timings times = [] # Timer used for the benchmark timer = TIMER_PLATFORM_DEFAULT def __init__(self, warp=None, calibration_runs=None, timer=None): # Set parameters if warp is not None: self.rounds = int(self.rounds / warp) if self.rounds == 0: raise ValueError('warp factor set too high') self.warp = warp if calibration_runs is not None: if (not ALLOW_SKIPPING_CALIBRATION and calibration_runs < 1): raise ValueError('at least one calibration run is required') self.calibration_runs = calibration_runs if timer is not None: timer = timer # Init variables self.times = [] self.overhead_times = [] # We want these to be in the instance dict, so that pickle # saves them self.version = self.version self.operations = self.operations self.rounds = self.rounds def get_timer(self): """ Return the timer function to use for the test. """ return get_timer(self.timer) def compatible(self, other): """ Return 1/0 depending on whether the test is compatible with the other Test instance or not. """ if self.version != other.version: return 0 if self.rounds != other.rounds: return 0 return 1 def calibrate_test(self): if self.calibration_runs == 0: self.overhead_times = [0.0] return calibrate = self.calibrate timer = self.get_timer() calibration_loops = range(CALIBRATION_LOOPS) # Time the calibration loop overhead prep_times = [] for i in range(self.calibration_runs): t = timer() for i in calibration_loops: pass t = timer() - t prep_times.append(t) min_prep_time = min(prep_times) if _debug: print print 'Calib. prep time = %.6fms' % ( min_prep_time * MILLI_SECONDS) # Time the calibration runs (doing CALIBRATION_LOOPS loops of # .calibrate() method calls each) for i in range(self.calibration_runs): t = timer() for i in calibration_loops: calibrate() t = timer() - t self.overhead_times.append(t / CALIBRATION_LOOPS - min_prep_time) # Check the measured times min_overhead = min(self.overhead_times) max_overhead = max(self.overhead_times) if _debug: print 'Calib. overhead time = %.6fms' % ( min_overhead * MILLI_SECONDS) if min_overhead < 0.0: raise ValueError('calibration setup did not work') if max_overhead - min_overhead > 0.1: raise ValueError( 'overhead calibration timing range too inaccurate: ' '%r - %r' % (min_overhead, max_overhead)) def run(self): """ Run the test in two phases: first calibrate, then do the actual test. Be careful to keep the calibration timing low w/r to the test timing. """ test = self.test timer = self.get_timer() # Get calibration min_overhead = min(self.overhead_times) # Test run t = timer() test() t = timer() - t if t < MIN_TEST_RUNTIME: raise ValueError('warp factor too high: ' 'test times are < 10ms') eff_time = t - min_overhead if eff_time < 0: raise ValueError('wrong calibration') self.last_timing = (eff_time, t, min_overhead) self.times.append(eff_time) def calibrate(self): """ Calibrate the test. This method should execute everything that is needed to setup and run the test - except for the actual operations that you intend to measure. pybench uses this method to measure the test implementation overhead. """ return def test(self): """ Run the test. The test needs to run self.rounds executing self.operations number of operations each. """ return def stat(self): """ Return test run statistics as tuple: (minimum run time, average run time, total run time, average time per operation, minimum overhead time) """ runs = len(self.times) if runs == 0: return 0.0, 0.0, 0.0, 0.0 min_time = min(self.times) total_time = reduce(operator.add, self.times, 0.0) avg_time = total_time / float(runs) operation_avg = total_time / float(runs * self.rounds * self.operations) if self.overhead_times: min_overhead = min(self.overhead_times) else: min_overhead = self.last_timing[2] return min_time, avg_time, total_time, operation_avg, min_overhead ### Load Setup # This has to be done after the definition of the Test class, since# the Setup module will import subclasses using this class. import Setup ### Benchmark base class class Benchmark: # Name of the benchmark name = '' # Number of benchmark rounds to run rounds = 1 # Warp factor use to run the tests warp = 1 # Warp factor # Average benchmark round time roundtime = 0 # Benchmark version number as float x.yy version = 2.0 # Produce verbose output ? verbose = 0 # Dictionary with the machine details machine_details = None # Timer used for the benchmark timer = TIMER_PLATFORM_DEFAULT def __init__(self, name, verbose=None, timer=None, warp=None, calibration_runs=None): if name: self.name = name else: self.name = '%04i-%02i-%02i %02i:%02i:%02i' % \ (time.localtime(time.time())[:6]) if verbose is not None: self.verbose = verbose if timer is not None: self.timer = timer if warp is not None: self.warp = warp if calibration_runs is not None: self.calibration_runs = calibration_runs # Init vars self.tests = {} if _debug: print 'Getting machine details...' self.machine_details = get_machine_details() # Make .version an instance attribute to have it saved in the # Benchmark pickle self.version = self.version def get_timer(self): """ Return the timer function to use for the test. """ return get_timer(self.timer) def compatible(self, other): """ Return 1/0 depending on whether the benchmark is compatible with the other Benchmark instance or not. """ if self.version != other.version: return 0 if (self.machine_details == other.machine_details and self.timer != other.timer): return 0 if (self.calibration_runs == 0 and other.calibration_runs != 0): return 0 if (self.calibration_runs != 0 and other.calibration_runs == 0): return 0 return 1 def load_tests(self, setupmod, limitnames=None): # Add tests if self.verbose: print 'Searching for tests ...' print '--------------------------------------' for testclass in setupmod.__dict__.values(): if not hasattr(testclass, 'is_a_test'): continue name = testclass.__name__ if name == 'Test': continue if (limitnames is not None and limitnames.search(name) is None): continue self.tests[name] = testclass( warp=self.warp, calibration_runs=self.calibration_runs, timer=self.timer) l = self.tests.keys() l.sort() if self.verbose: for name in l: print ' %s' % name print '--------------------------------------' print ' %i tests found' % len(l) print def calibrate(self):Lines 1–500 of 943. Load more
#!/usr/local/bin/python -O """ A Python Benchmark Suite """ # # Note: Please keep this module compatible to Python 1.5.2. # # Tests may include features in later Python versions, but these # should then be embedded in try-except clauses in the configuration # module Setup.py. # # pybench Copyright __copyright__ = """\ Copyright (c), 1997-2006, Marc-Andre Lemburg (mal@lemburg.com) Copyright (c), 2000-2006, eGenix.com Software GmbH (info@egenix.com) All Rights Reserved. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee or royalty is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation or portions thereof, including modifications, that you make. THE AUTHOR MARC-ANDRE LEMBURG DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE ! """ import sys, time, operator, string from CommandLine import * try: import cPickle pickle = cPickle except ImportError: import pickle # Version number; version history: see README file ! __version__ = '2.0' ### Constants # Second fractions MILLI_SECONDS = 1e3 MICRO_SECONDS = 1e6 # Percent unit PERCENT = 100 # Horizontal line length LINE = 79 # Minimum test run-time MIN_TEST_RUNTIME = 1e-3 # Number of calibration runs to use for calibrating the tests CALIBRATION_RUNS = 20 # Number of calibration loops to run for each calibration run CALIBRATION_LOOPS = 20 # Allow skipping calibration ? ALLOW_SKIPPING_CALIBRATION = 1 # Timer types TIMER_TIME_TIME = 'time.time' TIMER_TIME_CLOCK = 'time.clock' TIMER_SYSTIMES_PROCESSTIME = 'systimes.processtime' # Choose platform default timer if sys.platform[:3] == 'win': # On WinXP this has 2.5ms resolution TIMER_PLATFORM_DEFAULT = TIMER_TIME_CLOCK else: # On Linux this has 1ms resolution TIMER_PLATFORM_DEFAULT = TIMER_TIME_TIME # Print debug information ? _debug = 0 ### Helpers def get_timer(timertype): if timertype == TIMER_TIME_TIME: return time.time elif timertype == TIMER_TIME_CLOCK: return time.clock elif timertype == TIMER_SYSTIMES_PROCESSTIME: import systimes return systimes.processtime else: raise TypeError('unknown timer type: %s' % timertype) def get_machine_details(): import platform if _debug: print 'Getting machine details...' buildno, builddate = platform.python_build() python = platform.python_version() if python > '2.0': try: unichr(100000) except ValueError: # UCS2 build (standard) unicode = 'UCS2' else: # UCS4 build (most recent Linux distros) unicode = 'UCS4' else: unicode = None bits, linkage = platform.architecture() return { 'platform': platform.platform(), 'processor': platform.processor(), 'executable': sys.executable, 'python': platform.python_version(), 'compiler': platform.python_compiler(), 'buildno': buildno, 'builddate': builddate, 'unicode': unicode, 'bits': bits, } def print_machine_details(d, indent=''): l = ['Machine Details:', ' Platform ID: %s' % d.get('platform', 'n/a'), ' Processor: %s' % d.get('processor', 'n/a'), '', 'Python:', ' Executable: %s' % d.get('executable', 'n/a'), ' Version: %s' % d.get('python', 'n/a'), ' Compiler: %s' % d.get('compiler', 'n/a'), ' Bits: %s' % d.get('bits', 'n/a'), ' Build: %s (#%s)' % (d.get('builddate', 'n/a'), d.get('buildno', 'n/a')), ' Unicode: %s' % d.get('unicode', 'n/a'), ] print indent + string.join(l, '\n' + indent) + '\n' ### Test baseclass class Test: """ All test must have this class as baseclass. It provides the necessary interface to the benchmark machinery. The tests must set .rounds to a value high enough to let the test run between 20-50 seconds. This is needed because clock()-timing only gives rather inaccurate values (on Linux, for example, it is accurate to a few hundreths of a second). If you don't want to wait that long, use a warp factor larger than 1. It is also important to set the .operations variable to a value representing the number of "virtual operations" done per call of .run(). If you change a test in some way, don't forget to increase it's version number. """ ### Instance variables that each test should override # Version number of the test as float (x.yy); this is important # for comparisons of benchmark runs - tests with unequal version # number will not get compared. version = 2.0 # The number of abstract operations done in each round of the # test. An operation is the basic unit of what you want to # measure. The benchmark will output the amount of run-time per # operation. Note that in order to raise the measured timings # significantly above noise level, it is often required to repeat # sets of operations more than once per test round. The measured # overhead per test round should be less than 1 second. operations = 1 # Number of rounds to execute per test run. This should be # adjusted to a figure that results in a test run-time of between # 1-2 seconds. rounds = 100000 ### Internal variables # Mark this class as implementing a test is_a_test = 1 # Last timing: (real, run, overhead) last_timing = (0.0, 0.0, 0.0) # Warp factor to use for this test warp = 1 # Number of calibration runs to use calibration_runs = CALIBRATION_RUNS # List of calibration timings overhead_times = None # List of test run timings times = [] # Timer used for the benchmark timer = TIMER_PLATFORM_DEFAULT def __init__(self, warp=None, calibration_runs=None, timer=None): # Set parameters if warp is not None: self.rounds = int(self.rounds / warp) if self.rounds == 0: raise ValueError('warp factor set too high') self.warp = warp if calibration_runs is not None: if (not ALLOW_SKIPPING_CALIBRATION and calibration_runs < 1): raise ValueError('at least one calibration run is required') self.calibration_runs = calibration_runs if timer is not None: timer = timer # Init variables self.times = [] self.overhead_times = [] # We want these to be in the instance dict, so that pickle # saves them self.version = self.version self.operations = self.operations self.rounds = self.rounds def get_timer(self): """ Return the timer function to use for the test. """ return get_timer(self.timer) def compatible(self, other): """ Return 1/0 depending on whether the test is compatible with the other Test instance or not. """ if self.version != other.version: return 0 if self.rounds != other.rounds: return 0 return 1 def calibrate_test(self): if self.calibration_runs == 0: self.overhead_times = [0.0] return calibrate = self.calibrate timer = self.get_timer() calibration_loops = range(CALIBRATION_LOOPS) # Time the calibration loop overhead prep_times = [] for i in range(self.calibration_runs): t = timer() for i in calibration_loops: pass t = timer() - t prep_times.append(t) min_prep_time = min(prep_times) if _debug: print print 'Calib. prep time = %.6fms' % ( min_prep_time * MILLI_SECONDS) # Time the calibration runs (doing CALIBRATION_LOOPS loops of # .calibrate() method calls each) for i in range(self.calibration_runs): t = timer() for i in calibration_loops: calibrate() t = timer() - t self.overhead_times.append(t / CALIBRATION_LOOPS - min_prep_time) # Check the measured times min_overhead = min(self.overhead_times) max_overhead = max(self.overhead_times) if _debug: print 'Calib. overhead time = %.6fms' % ( min_overhead * MILLI_SECONDS) if min_overhead < 0.0: raise ValueError('calibration setup did not work') if max_overhead - min_overhead > 0.1: raise ValueError( 'overhead calibration timing range too inaccurate: ' '%r - %r' % (min_overhead, max_overhead)) def run(self): """ Run the test in two phases: first calibrate, then do the actual test. Be careful to keep the calibration timing low w/r to the test timing. """ test = self.test timer = self.get_timer() # Get calibration min_overhead = min(self.overhead_times) # Test run t = timer() test() t = timer() - t if t < MIN_TEST_RUNTIME: raise ValueError('warp factor too high: ' 'test times are < 10ms') eff_time = t - min_overhead if eff_time < 0: raise ValueError('wrong calibration') self.last_timing = (eff_time, t, min_overhead) self.times.append(eff_time) def calibrate(self): """ Calibrate the test. This method should execute everything that is needed to setup and run the test - except for the actual operations that you intend to measure. pybench uses this method to measure the test implementation overhead. """ return def test(self): """ Run the test. The test needs to run self.rounds executing self.operations number of operations each. """ return def stat(self): """ Return test run statistics as tuple: (minimum run time, average run time, total run time, average time per operation, minimum overhead time) """ runs = len(self.times) if runs == 0: return 0.0, 0.0, 0.0, 0.0 min_time = min(self.times) total_time = reduce(operator.add, self.times, 0.0) avg_time = total_time / float(runs) operation_avg = total_time / float(runs * self.rounds * self.operations) if self.overhead_times: min_overhead = min(self.overhead_times) else: min_overhead = self.last_timing[2] return min_time, avg_time, total_time, operation_avg, min_overhead ### Load Setup # This has to be done after the definition of the Test class, since # the Setup module will import subclasses using this class. import Setup ### Benchmark base class class Benchmark: # Name of the benchmark name = '' # Number of benchmark rounds to run rounds = 1 # Warp factor use to run the tests warp = 1 # Warp factor # Average benchmark round time roundtime = 0 # Benchmark version number as float x.yy version = 2.0 # Produce verbose output ? verbose = 0 # Dictionary with the machine details machine_details = None # Timer used for the benchmark timer = TIMER_PLATFORM_DEFAULT def __init__(self, name, verbose=None, timer=None, warp=None, calibration_runs=None): if name: self.name = name else: self.name = '%04i-%02i-%02i %02i:%02i:%02i' % \ (time.localtime(time.time())[:6]) if verbose is not None: self.verbose = verbose if timer is not None: self.timer = timer if warp is not None: self.warp = warp if calibration_runs is not None: self.calibration_runs = calibration_runs # Init vars self.tests = {} if _debug: print 'Getting machine details...' self.machine_details = get_machine_details() # Make .version an instance attribute to have it saved in the # Benchmark pickle self.version = self.version def get_timer(self): """ Return the timer function to use for the test. """ return get_timer(self.timer) def compatible(self, other): """ Return 1/0 depending on whether the benchmark is compatible with the other Benchmark instance or not. """ if self.version != other.version: return 0 if (self.machine_details == other.machine_details and self.timer != other.timer): return 0 if (self.calibration_runs == 0 and other.calibration_runs != 0): return 0 if (self.calibration_runs != 0 and other.calibration_runs == 0): return 0 return 1 def load_tests(self, setupmod, limitnames=None): # Add tests if self.verbose: print 'Searching for tests ...' print '--------------------------------------' for testclass in setupmod.__dict__.values(): if not hasattr(testclass, 'is_a_test'): continue name = testclass.__name__ if name == 'Test': continue if (limitnames is not None and limitnames.search(name) is None): continue self.tests[name] = testclass( warp=self.warp, calibration_runs=self.calibration_runs, timer=self.timer) l = self.tests.keys() l.sort() if self.verbose: for name in l: print ' %s' % name print '--------------------------------------' print ' %i tests found' % len(l) print def calibrate(self):