def add_request_stats(self, data): # print "received %r from %s:%d" % (data, host, port) endpoint = data['endpoint'] method = data['method'] code = data['code'] simple_code = str(code / 100) method_endpoint = "%s:%s" % (method, endpoint) method_code_endpoint = "%s:%s:%s" % (method, simple_code, endpoint) stats.incr(simple_code) stats.incr(method_endpoint) stats.incr(method_code_endpoint) stats.incr("%s:%s" % (method, simple_code)) stats.incr(endpoint) stats.incr("%s:%s" % (simple_code, endpoint)) stats.add_timing(simple_code, data['elapsed']) stats.add_timing(endpoint, data['elapsed']) stats.add_timing(method_endpoint, data['elapsed']) for k, v in data.get('stats', {}).items(): stats.add_timing("stats:%s" % k, v) stats.incr("stats:%s" % k) for group, v in data.get('groups', {}).items(): stats.incr('%s:%s' % (group, v)) stats.incr('%s:%s:%s' % (group, simple_code, v)) stats.add_timing("%s:%s" % (group, v), data['elapsed']) self.add_response(method_code_endpoint, data) self.add_response(simple_code, data)
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 add_stats(self, data): # assume they are using the standard ostrich histogram buckets if # not specified bucket_offsets = data.get('bucket_offsets', ORIG_BUCKET_OFFSETS) for k, v in data['timings'].iteritems(): if v['count'] > 0: stats.add_timing(k, TimingStat.from_raw_dict(v, bucket_offsets)) for k, v in data['counters'].iteritems(): if v > 0: stats.incr(k, v)
def add_stat(self, data): if data.has_key('stats'): for k, v in data['stats'].items(): stats.add_timing(k, v) if data.has_key('elapsed'): stats.add_timing(data['name'], data['elapsed']) elif data.has_key('name'): stats.incr(data['name']) else: pass
def test_timing_add(self): x = 0 with stats.time("hundred"): for i in xrange(100): x += 1 self.assertEquals(1, len(stats.get_timing_stats(reset=False))) stats.add_timing("foobar", timing.TimingStat(1, 0, 0)) self.assertEquals(2, len(stats.get_timing_stats(reset=False))) self.assertEquals(1, stats.get_timing_stats(reset=True)["foobar"].count) stats.add_timing("foobar", timing.TimingStat(3, 0, 0)) self.assertEquals(3, stats.get_timing_stats(reset=False)["foobar"].count)
def test_timing_bundle(self): timing_stat = timing.TimingStat(3, 20, 10, 15.0, 50.0, histogram.Histogram(10, 15, 20)) stats.add_timing("test", timing_stat) stats.add_timing("test", 25) test = stats.get_timing_stats(reset=False)["test"] self.assertEqual(4, test.count) self.assertEqual(17, test.average) self.assertEqual(6, int(test.std_dev)) stats.clear_all() timing_stat1 = timing.TimingStat(2, 25, 15, 20.0, 50.0, histogram.Histogram(15, 25)) timing_stat2 = timing.TimingStat(2, 20, 10, 15.0, 50.0, histogram.Histogram(10, 20)) stats.add_timing("test", timing_stat1) stats.add_timing("test", timing_stat2) test = stats.get_timing_stats(reset=False)["test"] self.assertEqual(4, test.count) self.assertEqual(17, test.average) self.assertEqual(6, int(test.std_dev))
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())
def test_timing_report_sorted(self): stats.add_timing("alpha", timing.TimingStat(1, 0, 0)) string = str(stats.get_timing_stats(reset=False)["alpha"]) self.assertEquals("(average=0, count=1, maximum=0, minimum=0, p25=0, p50=0, p75=0, p90=0, p99=0, p999=0, p9999=0, standard_deviation=0)", string)