#!/usr/bin/python import os import sys import math import time import random sys.path.insert(0, '../..') import rolling_checksum_py_mod import rolling_checksum_pyx_mod def my_range(up_to): value = 0 while value < up_to: yield value value += 1 class Perf: def __init__(self, module, description, too_long): self.module = module self.description = description self.too_long = too_long def took_too_long(self): if self.too_long == 0: return False if self.duration() > self.too_long: return True else: return False def duration(self): return self.t1 - self.t0 def __str__(self): return self.description def start(self): self.t0 = time.time() def finish(self): self.t1 = time.time() self.difference = self.t1 - self.t0 def avg(list_): total = sum(list_) return total / len(list_) def stddev(list_, average): total = 0.0 for element in list_: total += (element - average) ** 2 return math.sqrt(total) def main(): top = 2**21 while sys.argv[1:]: if sys.argv[1] == '--top': top = int(sys.argv[2]) del sys.argv[1] else: sys.stderr.write('Illegal option: %s\n' % sys.argv[1]) sys.exit(1) del sys.argv[1] dummy_pyx_mod = Perf(rolling_checksum_pyx_mod, 'cache primer', 0) pyx_mod = Perf(rolling_checksum_pyx_mod, 'pyx_mod', 60 * 10) py_mod = Perf(rolling_checksum_py_mod, 'py_mod', 60 * 50) # we do pyx_mod twice to avoid cache effects #for pm in [ pyx_mod ]: all_good = True for pm in [ dummy_pyx_mod, pyx_mod, py_mod ]: random.seed(3) pm.start() rolling_checksum = pm.module.Rolling_checksum() file_handle = os.open('input-data', os.O_RDONLY) for dummy in pm.module.Chunker(rolling_checksum, file_handle): pass os.close(file_handle) pm.finish() if pm.took_too_long(): sys.stderr.write('%s: duration of %s too long: %f\n' % (sys.argv[0], str(pm), pm.duration())) all_good = False if all_good: sys.stderr.write('%s: All tests passed\n' % sys.argv[0]) sys.exit(0) else: sys.stderr.write('%s: One or more tests failed\n' % sys.argv[0]) sys.exit(1) main()