Skip to content

Commit

Permalink
Fix off-by-one issue when generating statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 31, 2011
1 parent 3756cb9 commit dc39148
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.2.0 (unreleased)

- Add a TCP "aliveness_check" option for easier monitoring.
- Fix off-by-one issue in calculating percentile statistics.

## 0.1.0 (August 16, 2011)

Expand Down
12 changes: 10 additions & 2 deletions statsite/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,19 @@ def fold(cls, lst, now, percentile=90):
lower_idx = (len(vals) - inner_indexes) / 2
upper_idx = lower_idx + inner_indexes

val_sum_pct = sum(vals[lower_idx:upper_idx+1])
# If we only have one item, then the percentile is just the
# values itself, otherwise the lower_idx:upper_idx slice returns
# an empty list.
if len(vals) == 1:
vals_pct = vals
else:
vals_pct = vals[lower_idx:upper_idx]

val_sum_pct = sum(vals_pct)
val_avg_pct = val_sum_pct / inner_indexes if inner_indexes > 0 else val_sum_pct
val_min_pct = vals[lower_idx]
val_max_pct = vals[upper_idx]
val_stdev_pct = cls._stdev(vals[lower_idx:upper_idx+1], val_avg_pct)
val_stdev_pct = cls._stdev(vals_pct, val_avg_pct)

outputs.append(("timers.%s.sum" % key, val_sum, now))
outputs.append(("timers.%s.mean" % key, val_avg, now))
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_metric_timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,22 @@ def test_fold_stdev(self):
assert ("timers.k.stdev", Timer._stdev([10, 15], 12.5), now) == self._get_metric("timers.k.stdev", result)
assert ("timers.j.stdev", Timer._stdev([7.9, 8], 7.95), now) == self._get_metric("timers.j.stdev", result)

def test_sum_percentile(self):
"""
Tests the percentile sum is properly counted.
"""
now = 10
result = Timer.fold(self._100_timers, now)
assert ("timers.k.sum_90", 4545, now) == self._get_metric("timers.k.sum_90", result)

def test_mean_percentile(self):
"""
Tests the percentile sum is properly counted.
"""
now = 10
result = Timer.fold(self._100_timers, now)
assert ("timers.k.mean_90", 50, now) == self._get_metric("timers.k.mean_90", result)

def test_stdev(self):
"""
Tests that the standard deviation is properly computed.
Expand All @@ -109,3 +125,11 @@ def _get_metric(self, key, metrics):
return metric

return None

@property
def _100_timers(self):
result = []
for i in xrange(1, 101):
result.append(Timer("k", i))

return result

0 comments on commit dc39148

Please sign in to comment.