def test_timing_decorator(self): @stats.time("test") def _test_timing_decorator(): time.sleep(0.01) _test_timing_decorator() test = stats.get_timing("test") self.assertTrue(test.get().average >= 10)
def seqtimer(seq, name=None, interval=None, items=None, length=None, summary=True, output=None): """Return an iterator over a sequence, with timing stats. This is used to instrument the consumption of a large list or generator. To dump stats every X items: seqtimer(input_seq, items=X) To dump stats every X seconds: seqtimer(input_seq, interval=X) You may provide both arguments. If seq has no length, you may provide it with the length keyword argument. Statistics will be written to output, which should be a file-like object. """ output = output or sys.stderr prefix = name + '_' if name else "" stats = ostrich.stats.Stats() timer_name = "%sitem_time" % prefix timing = stats.get_timing(timer_name) timer = Timer(stats, "%sitem_time" % prefix) last_dump_time = time.time() last_dump_item = timing.count warned = False seq_len = len(seq) if hasattr(seq, '__len__') else length or INF start = time.time() dump = partial(dump_stats, output, name, timing, seq_len, start) for item in seq: with timer: yield item # Periodically print stats. if ((interval and last_dump_time + interval <= time.time()) or (items and timing.count % items == 0)): dump(last_dump_item, last_dump_time) last_dump_time = time.time() last_dump_item = timing.count if not warned and timing.count > seq_len: warnings.warn( "Sequence %sis longer than its declared length" % (name + " " if name else ""), RuntimeWarning) seq_len = INF if last_dump_item < timing.count: dump(last_dump_item, last_dump_time) if summary: generate_summary(output, name, start, timing)
def test_timing_boundarys(self): stats.add_timing("test", sys.maxint) stats.add_timing("test", 5) sum = 5.0 + sys.maxint avg = sum / 2.0 sumsq = 5.0 * 5.0 + float(sys.maxint) * sys.maxint partial = sumsq - sum * avg test = stats.get_timing("test") self.assertEquals(timing.TimingStat(2, sys.maxint, 5, avg, partial, histogram.Histogram(5, sys.maxint)), test.get())
def seqtimer(seq, name=None, interval=None, items=None, length=None, summary=True, output=None): """Return an iterator over a sequence, with timing stats. This is used to instrument the consumption of a large list or generator. To dump stats every X items: seqtimer(input_seq, items=X) To dump stats every X seconds: seqtimer(input_seq, interval=X) You may provide both arguments. If seq has no length, you may provide it with the length keyword argument. Statistics will be written to output, which should be a file-like object. """ output = output or sys.stderr prefix = name + '_' if name else "" stats = ostrich.stats.Stats() timer_name = "%sitem_time" % prefix timing = stats.get_timing(timer_name) timer = Timer(stats, "%sitem_time" % prefix) last_dump_time = time.time() last_dump_item = timing.count warned = False seq_len = len(seq) if hasattr(seq, '__len__') else length or INF start = time.time() dump = partial(dump_stats, output, name, timing, seq_len, start) for item in seq: with timer: yield item # Periodically print stats. if ((interval and last_dump_time + interval <= time.time()) or (items and timing.count % items == 0)): dump(last_dump_item, last_dump_time) last_dump_time = time.time() last_dump_item = timing.count if not warned and timing.count > seq_len: warnings.warn( "Sequence %sis longer than its declared length" % ( name + " " if name else ""), RuntimeWarning) seq_len = INF if last_dump_item < timing.count: dump(last_dump_item, last_dump_time) if summary: generate_summary(output, name, start, timing)
def test_timing_with(self): with stats.time("test"): time.sleep(0.01) test = stats.get_timing("test") self.assertTrue(test.get().average >= 10)
def test_timings_negative(self): stats.add_timing("test", 1) stats.add_timing("test", -1) test = stats.get_timing("test") self.assertEquals(timing.TimingStat(1, 1, 1, 1.0, 0.0, histogram.Histogram(1)), test.get())
def test_timings_average(self): stats.add_timing("test", 0) test = stats.get_timing("test") self.assertEquals(timing.TimingStat(1, 0, 0), test.get())
def test_timings_basic(self): stats.add_timing("test", 1) stats.add_timing("test", 2) stats.add_timing("test", 3) test = stats.get_timing("test") self.assertEqual(timing.TimingStat(3, 3, 1, 2.0, 2.0, histogram.Histogram(1, 2, 3)), test.get())
def test_timings_empty(self): stats.add_timing("test", 0) test = stats.get_timing("test") self.assertEqual(timing.TimingStat(1, 0, 0), test.get(reset=True)) # the timings list will be empty here: self.assertEqual(timing.TimingStat(0, 0, 0), test.get())