def test_timer_no_context_manager(): elapsed = bench.timer() with pytest.raises(bench.TimerNotStarted): elapsed.start with pytest.raises(bench.TimerNotStopped): elapsed.stop with pytest.raises(bench.TimerNotStarted): elapsed.stop_timer()
def test_timer(): wait = 0.2 tolerance = 0.006 start = datetime.datetime.utcnow() with bench.timer() as elapsed: time.sleep(wait) mid = elapsed.delta end = datetime.datetime.utcnow() assert (elapsed.seconds - wait) < tolerance assert (start - elapsed.start).total_seconds() < tolerance assert (end - elapsed.stop).total_seconds() < tolerance assert elapsed.seconds - mid.total_seconds() < tolerance
def test_timer_exceptions(): with pytest.raises(bench.TimerNotStopped): with bench.timer() as elapsed: elapsed.stop
class WordCount(MemMapReduce): def mapper(self, item): return zip(item.lower().split(), it.repeat(1)) def reducer(self, key, values): yield key, sum(values) def output(self, items): return tools.single_key_output(items) if __name__ == '__main__': if len(sys.argv) == 1: print(__doc__.strip()) exit(1) infile = sys.argv[1] print("Running wordcount tests on: {}".format(infile)) print("Running tinymr ...") with open(infile) as f, timer() as elapsed, WordCount() as wc: tuple(wc(f)) print(elapsed.seconds) print("Running builtin ...") with open(infile) as f, timer() as elapsed: builtin_mr(f) print(elapsed.seconds)