def bench(self, BSIZES=( 128, 250, 2048, 5000, 2**13, 2**20, )): from pyutil import benchutil funcs = ( "_benchmark_insert", "_benchmark_init_and_has_key_and_del", "_benchmark_init_and_remove", "_benchmark_init_and_popitem", "_benchmark_update", "_benchmark_init", ) max = 0 for func in funcs: if len(func) > max: max = len(func) for func in funcs: print func + " " * (max + 1 - len(func)) for BSIZE in BSIZES: f = getattr(self, func) benchutil.rep_bench(f, BSIZE, self._generic_benchmarking_init, MAXREPS=self.MAXREPS, MAXTIME=self.MAXTIME)
def run_benchmarks(self, profile=False): for (initfunc, func) in [(self.init_for_unpack, self.unpack), (self.init_for_pack, self.pack), (self.init_for_unpack, self.unpack_and_repack)]: print "benchmarking %s" % (func,) for N in 16, 512, 2048, 16384: print "%5d" % N, benchutil.rep_bench(func, N, initfunc=initfunc, MAXREPS=20, UNITS_PER_SECOND=1000) benchutil.print_bench_footer(UNITS_PER_SECOND=1000) print "(milliseconds)"
def bench(self, BSIZES=(128, 250, 2048, 5000, 2**13, 2**20,)): from pyutil import benchutil funcs = ("_benchmark_insert", "_benchmark_init_and_has_key_and_del", "_benchmark_init_and_remove", "_benchmark_init_and_popitem", "_benchmark_update", "_benchmark_init",) max = 0 for func in funcs: if len(func) > max: max = len(func) for func in funcs: print func + " " * (max + 1 - len(func)) for BSIZE in BSIZES: f = getattr(self, func) benchutil.rep_bench(f, BSIZE, self._generic_benchmarking_init, MAXREPS=self.MAXREPS, MAXTIME=self.MAXTIME)
def bench(k, m): SIZE = 10**6 MAXREPS = 64 # for f in [_encode_file_stringy_easyfec, _encode_file_stringy, _encode_file, _encode_file_not_really,]: # for f in [_encode_file,]: # for f in [_encode_file_not_really, _encode_file_not_really_and_hash, _encode_file, _encode_file_and_hash,]: # for f in [_encode_data_not_really, _encode_data_easyfec, _encode_data_fec,]: print "measuring encoding of data with K=%d, M=%d, reporting results in nanoseconds per byte after encoding %d bytes %d times in a row..." % ( k, m, SIZE, MAXREPS) # for f in [_encode_data_fec, _encode_data_not_really]: for f in [_encode_data_fec]: def _init_func(size): return _make_new_rand_data(size, k, m) for BSIZE in [SIZE]: results = benchutil.rep_bench(f, n=BSIZE, initfunc=_init_func, MAXREPS=MAXREPS, MAXTIME=None, UNITS_PER_SECOND=1000000000) print "and now represented in MB/s..." print best = results['best'] mean = results['mean'] worst = results['worst'] print "best: % 4.3f MB/sec" % (10**3 / best) print "mean: % 4.3f MB/sec" % (10**3 / mean) print "worst: % 4.3f MB/sec" % (10**3 / worst)
def byte_at_a_time_cracker(test_guess, secretlen, alphabetsize): # If we were cleverer, we'd add some backtracking behaviour where, if we can't find any x such that ABCx stands out from the crowd as taking longer than all the other ABCy's, then we start to think that we've taken a wrong step and we go back to trying ABy's. Make sense? But we're not that clever. Once we take a step, we don't backtrack. print() guess = [] while len(guess) < secretlen: best_next_byte = None best_next_byte_time = None # For each possible byte... for next_byte in range(alphabetsize): c = chr(next_byte) # Construct a guess with our best candidate so far... candidate_guess = guess[:] # Plus that byte... candidate_guess.append(c) s = ''.join(candidate_guess) # Plus random bytes... s += os.urandom(32 - len(s)) # And see how long it takes the test_guess to consider it... def f(n): for i in range(n): test_guess(s) times = benchutil.rep_bench(f, 10**7, MAXREPS=10**3, quiet=True) fastesttime = times['mean'] print("%s..." % (c.encode('hex'), ), end=' ') if best_next_byte is None or fastesttime > best_next_byte_time: print("new candidate for slowest next-char: %s, took: %s" % ( c.encode('hex'), fastesttime, ), end=' ') best_next_byte_time = fastesttime best_next_byte = c # Okay we've tried all possible next bytes. Our guess is this one (the one that took longest to be tested by test_guess): guess.append(best_next_byte) print("SLOWEST next-char %s! Current guess at secret: %s" % ( best_next_byte.encode('hex'), ''.join(guess).encode('hex'), )) guess = ''.join(guess) print("Our guess for the secret: %r" % (guess, )) return guess
def print_measurements(): N=10**4 REPS=10**2 print "all times are in nanoseconds per comparison (in scientific notation)" print for comparator in [eqeqcomp, hashcomp, xorcomp, sillycomp]: print "using comparator ", comparator # for (a, b, desc) in [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')]: trials = [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')] random.shuffle(trials) for (a, b, desc) in trials: print "comparing two strings that are %s to each other" % (desc,) def f(n): compare(n, comparator, a, b) benchutil.rep_bench(f, N, UNITS_PER_SECOND=10**9, MAXREPS=REPS) print
def print_measurements(): N = 10**4 REPS = 10**2 print "all times are in nanoseconds per comparison (in scientific notation)" print for comparator in [eqeqcomp, hashcomp, xorcomp, sillycomp]: print "using comparator ", comparator # for (a, b, desc) in [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')]: trials = [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')] random.shuffle(trials) for (a, b, desc) in trials: print "comparing two strings that are %s to each other" % (desc, ) def f(n): compare(n, comparator, a, b) benchutil.rep_bench(f, N, UNITS_PER_SECOND=10**9, MAXREPS=REPS) print
def byte_at_a_time_cracker(test_guess, secretlen, alphabetsize): # If we were cleverer, we'd add some backtracking behaviour where, if we can't find any x such that ABCx stands out from the crowd as taking longer than all the other ABCy's, then we start to think that we've taken a wrong step and we go back to trying ABy's. Make sense? But we're not that clever. Once we take a step, we don't backtrack. print guess=[] while len(guess) < secretlen: best_next_byte = None best_next_byte_time = None # For each possible byte... for next_byte in range(alphabetsize): c = chr(next_byte) # Construct a guess with our best candidate so far... candidate_guess = guess[:] # Plus that byte... candidate_guess.append(c) s = ''.join(candidate_guess) # Plus random bytes... s += os.urandom(32 - len(s)) # And see how long it takes the test_guess to consider it... def f(n): for i in xrange(n): test_guess(s) times = benchutil.rep_bench(f, 10**7, MAXREPS=10**3, quiet=True) fastesttime = times['mean'] print "%s..."%(c.encode('hex'),), if best_next_byte is None or fastesttime > best_next_byte_time: print "new candidate for slowest next-char: %s, took: %s" % (c.encode('hex'), fastesttime,), best_next_byte_time = fastesttime best_next_byte = c # Okay we've tried all possible next bytes. Our guess is this one (the one that took longest to be tested by test_guess): guess.append(best_next_byte) print "SLOWEST next-char %s! Current guess at secret: %s" % (best_next_byte.encode('hex'), ''.join(guess).encode('hex'),) guess = ''.join(guess) print "Our guess for the secret: %r" % (guess,) return guess
def eqeqcomp(a, b): return a == b def hashcomp(a, b): salt = os.urandom(32) return hashlib.md5(salt + a).digest() == hashlib.md5(salt + b).digest() N = 10**4 REPS = 10**2 print "all times are in nanoseconds per comparison (scientific notation)" print for comparator in [eqeqcomp, hashcomp]: print "using comparator ", comparator # for (a, b, desc) in [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')]: trials = [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')] random.shuffle(trials) for (a, b, desc) in trials: print "comparing two strings that are %s to each other" % (desc, ) def f(n): compare(n, comparator, a, b) benchutil.rep_bench(f, N, UNITS_PER_SECOND=10**9, MAXREPS=REPS) print
# self.stats['get'] = self.stats.get('get', 0) + 1 elif REMOVE_R.search(inline): mo = REMOVE_R.search(inline) start = int(mo.group(1)) length = int(mo.group(2)) self.s.remove(start, length) # self.stats['remove'] = self.stats.get('remove', 0) + 1 elif POP_R.search(inline): mo = POP_R.search(inline) start = int(mo.group(1)) length = int(mo.group(2)) self.s.pop(start, length) # self.stats['pop'] = self.stats.get('pop', 0) + 1 elif INIT_S in inline: pass else: print("Warning, didn't recognize this line: %r" % (inline, )) count += 1 inline = self.inf.readline() # print self.stats benchutil.print_bench_footer(UNITS_PER_SECOND=1000000) print("(microseconds)") for N in [600, 6000, 60000]: b = B(open(sys.argv[1], 'rU')) print("%7d" % N, end=' ') benchutil.rep_bench(b.run, N, b.init, UNITS_PER_SECOND=1000000)
f(a, b) def eqeqcomp(a, b): return a == b def hashcomp(a, b): salt = os.urandom(32) return hashlib.md5(salt+ a).digest() == hashlib.md5(salt+b).digest() N=10**4 REPS=10**2 print "all times are in nanoseconds per comparison (scientific notation)" print for comparator in [eqeqcomp, hashcomp]: print "using comparator ", comparator # for (a, b, desc) in [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')]: trials = [(p1, p1a, 'same'), (p1, p2, 'close'), (p1, p3, 'far')] random.shuffle(trials) for (a, b, desc) in trials: print "comparing two strings that are %s to each other" % (desc,) def f(n): compare(n, comparator, a, b) benchutil.rep_bench(f, N, UNITS_PER_SECOND=10**9, MAXREPS=REPS) print
d.addCallback(lambda res: self.banana.transport.getvalue()) def f(o): self._encoded_huge_string = o d.addCallback(f) reactor.runUntilCurrent() def bench_huge_string_decode(self, N): """ This is actually a test for acceptable performance, and it needs to be made more explicit, perhaps by being moved into a separate benchmarking suite instead of living in this test suite. """ o = self._encoded_huge_string # results = [] self.banana.prepare() # d.addCallback(results.append) CHOMP = 4096 for i in range(0, len(o), CHOMP): self.banana.dataReceived(o[i:i + CHOMP]) # print results import sys from twisted.internet import reactor from pyutil import benchutil b = B() for N in 10**3, 10**4, 10**5, 10**6, 10**7: print "%8d" % N, sys.stdout.flush() benchutil.rep_bench(b.bench_huge_string_decode, N, b.setup_huge_string)
# self.stats['get'] = self.stats.get('get', 0) + 1 elif REMOVE_R.search(inline): mo = REMOVE_R.search(inline) start = int(mo.group(1)) length = int(mo.group(2)) self.s.remove(start, length) # self.stats['remove'] = self.stats.get('remove', 0) + 1 elif POP_R.search(inline): mo = POP_R.search(inline) start = int(mo.group(1)) length = int(mo.group(2)) self.s.pop(start, length) # self.stats['pop'] = self.stats.get('pop', 0) + 1 elif INIT_S in inline: pass else: print "Warning, didn't recognize this line: %r" % (inline,) count += 1 inline = self.inf.readline() # print self.stats benchutil.print_bench_footer(UNITS_PER_SECOND=1000000) print "(microseconds)" for N in [600, 6000, 60000]: b = B(open(sys.argv[1], 'rU')) print "%7d" % N, benchutil.rep_bench(b.run, N, b.init, UNITS_PER_SECOND=1000000)
self.banana.transport = TestTransport() self.banana.connectionMade() d = self.banana.send("a"*N) d.addCallback(lambda res: self.banana.transport.getvalue()) def f(o): self._encoded_huge_string = o d.addCallback(f) reactor.runUntilCurrent() def bench_huge_string_decode(self, N): """ This is actually a test for acceptable performance, and it needs to be made more explicit, perhaps by being moved into a separate benchmarking suite instead of living in this test suite. """ o = self._encoded_huge_string # results = [] self.banana.prepare() # d.addCallback(results.append) CHOMP = 4096 for i in range(0, len(o), CHOMP): self.banana.dataReceived(o[i:i+CHOMP]) # print results import sys from twisted.internet import reactor from pyutil import benchutil b = B() for N in 10**3, 10**4, 10**5, 10**6, 10**7: print "%8d" % N, sys.stdout.flush() benchutil.rep_bench(b.bench_huge_string_decode, N, b.setup_huge_string)