|
| 1 | +package com.twitter.finagle.stats |
| 2 | + |
| 3 | +import com.twitter.conversions.DurationOps._ |
| 4 | +import com.twitter.util.MockTimer |
| 5 | +import com.twitter.util.Time |
| 6 | +import org.scalatest.funsuite.AnyFunSuite |
| 7 | + |
| 8 | +class HistogramCounterTest extends AnyFunSuite { |
| 9 | + |
| 10 | + test("Records stat at given frequency") { |
| 11 | + Time.withCurrentTimeFrozen { tc => |
| 12 | + val timer = new MockTimer() |
| 13 | + val statsReceiver = new InMemoryStatsReceiver |
| 14 | + val histogramCounterFactory = new HistogramCounterFactory(timer, () => Time.now.inMillis) |
| 15 | + val histogramCounter = |
| 16 | + histogramCounterFactory( |
| 17 | + Seq("foo", "bar"), |
| 18 | + StatsFrequency.HundredMilliSecondly, |
| 19 | + statsReceiver |
| 20 | + ) |
| 21 | + histogramCounter.incr(5) |
| 22 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")).isEmpty) |
| 23 | + histogramCounter.incr() |
| 24 | + |
| 25 | + tc.advance(100.millis) |
| 26 | + timer.tick() |
| 27 | + |
| 28 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(6)) |
| 29 | + |
| 30 | + tc.advance(100.millis) |
| 31 | + timer.tick() |
| 32 | + |
| 33 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(6, 0)) |
| 34 | + |
| 35 | + histogramCounter.incr(2) |
| 36 | + |
| 37 | + tc.advance(50.millis) |
| 38 | + timer.tick() |
| 39 | + |
| 40 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(6, 0)) |
| 41 | + histogramCounter.incr(3) |
| 42 | + |
| 43 | + tc.advance(50.millis) |
| 44 | + timer.tick() |
| 45 | + |
| 46 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(6, 0, 5)) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + test("Normalizes recorded stat to the elapsed time since last recording") { |
| 51 | + Time.withCurrentTimeFrozen { tc => |
| 52 | + val timer = new MockTimer() |
| 53 | + val statsReceiver = new InMemoryStatsReceiver |
| 54 | + val histogramCounterFactory = new HistogramCounterFactory(timer, () => Time.now.inMillis) |
| 55 | + val histogramCounter = |
| 56 | + histogramCounterFactory( |
| 57 | + Seq("foo", "bar"), |
| 58 | + StatsFrequency.HundredMilliSecondly, |
| 59 | + statsReceiver |
| 60 | + ) |
| 61 | + histogramCounter.incr(5) |
| 62 | + histogramCounter.incr(10) |
| 63 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")).isEmpty) |
| 64 | + |
| 65 | + // Our task was slow to execute on the timer :( |
| 66 | + tc.advance(150.millis) |
| 67 | + timer.tick() |
| 68 | + |
| 69 | + // We have 15 requests in 1.5 windows, so 10 requests in 1 window. |
| 70 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(10)) |
| 71 | + |
| 72 | + histogramCounter.incr(12) |
| 73 | + |
| 74 | + tc.advance(300.millis) |
| 75 | + timer.tick() |
| 76 | + |
| 77 | + // We have 12 requests in 3 windows, so 4 requests in 1 window. |
| 78 | + assert(statsReceiver.stats(Seq("foo", "bar", "hundredMilliSecondly")) == Seq(10, 4)) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + test("Returns the same histogramCounter object for equivalent names") { |
| 83 | + val timer = new MockTimer() |
| 84 | + val metrics = Metrics.createDetached() |
| 85 | + val statsReceiver = new MetricsStatsReceiver(metrics) |
| 86 | + |
| 87 | + val histogramCounterFactory = new HistogramCounterFactory(timer, () => Time.now.inMillis) |
| 88 | + |
| 89 | + val histogramCounter1 = |
| 90 | + histogramCounterFactory(Seq("foo", "bar"), StatsFrequency.HundredMilliSecondly, statsReceiver) |
| 91 | + val histogramCounter2 = |
| 92 | + histogramCounterFactory(Seq("foo", "bar"), StatsFrequency.HundredMilliSecondly, statsReceiver) |
| 93 | + val histogramCounter3 = |
| 94 | + histogramCounterFactory(Seq("foo/bar"), StatsFrequency.HundredMilliSecondly, statsReceiver) |
| 95 | + |
| 96 | + val scopedStatsReceiver = statsReceiver.scope("foo") |
| 97 | + |
| 98 | + val histogramCounter4 = |
| 99 | + histogramCounterFactory(Seq("bar"), StatsFrequency.HundredMilliSecondly, scopedStatsReceiver) |
| 100 | + |
| 101 | + assert(histogramCounter1 eq histogramCounter2) |
| 102 | + assert(histogramCounter1 eq histogramCounter3) |
| 103 | + assert(histogramCounter1 eq histogramCounter4) |
| 104 | + } |
| 105 | + |
| 106 | + test("Doesn't schedule recording of stats after close is called") { |
| 107 | + Time.withCurrentTimeFrozen { tc => |
| 108 | + val timer = new MockTimer() |
| 109 | + assert(timer.tasks.size == 0) |
| 110 | + val histogramCounterFactory = new HistogramCounterFactory(timer, () => Time.now.inMillis) |
| 111 | + assert(timer.tasks.size == 1) |
| 112 | + histogramCounterFactory.close() |
| 113 | + tc.advance(100.millis) |
| 114 | + timer.tick() |
| 115 | + assert(timer.tasks.size == 0) |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments