import resource, time def workload(rounds): x = 0 for i in range(rounds): x = x + 1 def microbench(): print 'Microbench' sleeptime = 10e-6 sleep = time.sleep timer = time.time rtype = resource.RUSAGE_SELF rounds = 100 while 1: times = [] rstart = resource.getrusage(rtype) for i in range(100): # Make sure the test is run at the start of a scheduling time # slice sleep(sleeptime) # Test start = timer() workload(rounds) stop = timer() times.append(stop - start) rstop = resource.getrusage(rtype) volswitches = rstop[-2] - rstart[-2] forcedswitches = rstop[-1] - rstart[-1] min_time = min(times) max_time = max(times) diff = max_time - min_time if forcedswitches == 0: print 'Rounds: %i' % rounds print ' min time: %f seconds' % min_time print ' max time: %f seconds' % max_time print ' diff: %f %% = %f seconds' % (diff / min_time * 100.0, diff) print ' context switches: %r %r' % (volswitches, forcedswitches) print elif forcedswitches > 10: break rounds += 100 microbench()