|
| 1 | +package com.baeldung.metrics.core; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 4 | +import static org.junit.Assert.assertEquals; |
| 5 | +import static org.junit.Assert.assertThat; |
| 6 | + |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | + |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +import com.codahale.metrics.*; |
| 14 | + |
| 15 | +public class MetricsTest { |
| 16 | + @Test |
| 17 | + public void whenMarkMeter_thenCorrectRates() throws InterruptedException { |
| 18 | + Meter meter = new Meter(); |
| 19 | + |
| 20 | + long initCount = meter.getCount(); |
| 21 | + assertThat(initCount, equalTo(0L)); |
| 22 | + |
| 23 | + meter.mark(); |
| 24 | + assertThat(meter.getCount(), equalTo(1L)); |
| 25 | + |
| 26 | + meter.mark(20); |
| 27 | + assertThat(meter.getCount(), equalTo(21L)); |
| 28 | + |
| 29 | + // not use assert for these rate values because they change every time when this test is run |
| 30 | + double meanRate = meter.getMeanRate(); |
| 31 | + double oneMinRate = meter.getOneMinuteRate(); |
| 32 | + double fiveMinRate = meter.getFiveMinuteRate(); |
| 33 | + double fifteenMinRate = meter.getFifteenMinuteRate(); |
| 34 | + System.out.println(meanRate); |
| 35 | + System.out.println(oneMinRate); |
| 36 | + System.out.println(fiveMinRate); |
| 37 | + System.out.println(fifteenMinRate); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenInitRatioGauge_thenCorrectRatio() { |
| 42 | + Gauge<Double> ratioGauge = new AttendanceRatioGauge(15, 20); |
| 43 | + assertThat(ratioGauge.getValue(), equalTo(0.75)); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void whenUseCacheGauge_thenCorrectGauge() { |
| 48 | + Gauge<List<Long>> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); |
| 49 | + List<Long> expected = new ArrayList<Long>(); |
| 50 | + expected.add(12L); |
| 51 | + assertThat(activeUsersGauge.getValue(), equalTo(expected)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void whenUseDerivativeGauge_thenCorrectGaugeFromBase() { |
| 56 | + Gauge<List<Long>> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); |
| 57 | + Gauge<Integer> activeUserCountGauge = new ActiveUserCountGauge(activeUsersGauge); |
| 58 | + assertThat(activeUserCountGauge.getValue(), equalTo(1)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenIncDecCounter_thenCorrectCount() { |
| 63 | + Counter counter = new Counter(); |
| 64 | + |
| 65 | + long initCount = counter.getCount(); |
| 66 | + assertThat(initCount, equalTo(0L)); |
| 67 | + |
| 68 | + counter.inc(); |
| 69 | + assertThat(counter.getCount(), equalTo(1L)); |
| 70 | + |
| 71 | + counter.inc(11); |
| 72 | + assertThat(counter.getCount(), equalTo(12L)); |
| 73 | + |
| 74 | + counter.dec(); |
| 75 | + assertThat(counter.getCount(), equalTo(11L)); |
| 76 | + |
| 77 | + counter.dec(6); |
| 78 | + assertThat(counter.getCount(), equalTo(5L)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenUpdateHistogram_thenCorrectDistributionData() { |
| 83 | + Histogram histogram = new Histogram(new UniformReservoir()); |
| 84 | + |
| 85 | + histogram.update(5); |
| 86 | + long count1 = histogram.getCount(); |
| 87 | + assertThat(count1, equalTo(1L)); |
| 88 | + Snapshot snapshot1 = histogram.getSnapshot(); |
| 89 | + assertThat(snapshot1.getValues().length, equalTo(1)); |
| 90 | + assertThat(snapshot1.getValues()[0], equalTo(5L)); |
| 91 | + assertThat(snapshot1.getMax(), equalTo(5L)); |
| 92 | + assertThat(snapshot1.getMin(), equalTo(5L)); |
| 93 | + assertThat(snapshot1.getMean(), equalTo(5.0)); |
| 94 | + assertThat(snapshot1.getMedian(), equalTo(5.0)); |
| 95 | + assertThat(snapshot1.getStdDev(), equalTo(0.0)); |
| 96 | + assertThat(snapshot1.get75thPercentile(), equalTo(5.0)); |
| 97 | + assertThat(snapshot1.get95thPercentile(), equalTo(5.0)); |
| 98 | + assertThat(snapshot1.get98thPercentile(), equalTo(5.0)); |
| 99 | + assertThat(snapshot1.get99thPercentile(), equalTo(5.0)); |
| 100 | + assertThat(snapshot1.get999thPercentile(), equalTo(5.0)); |
| 101 | + |
| 102 | + histogram.update(20); |
| 103 | + long count2 = histogram.getCount(); |
| 104 | + assertThat(count2, equalTo(2L)); |
| 105 | + Snapshot snapshot2 = histogram.getSnapshot(); |
| 106 | + assertThat(snapshot2.getValues().length, equalTo(2)); |
| 107 | + assertThat(snapshot2.getValues()[0], equalTo(5L)); |
| 108 | + assertThat(snapshot2.getValues()[1], equalTo(20L)); |
| 109 | + assertThat(snapshot2.getMax(), equalTo(20L)); |
| 110 | + assertThat(snapshot2.getMin(), equalTo(5L)); |
| 111 | + assertThat(snapshot2.getMean(), equalTo(12.5)); |
| 112 | + assertThat(snapshot2.getMedian(), equalTo(12.5)); |
| 113 | + assertEquals(10.6, snapshot2.getStdDev(), 0.1); |
| 114 | + assertThat(snapshot2.get75thPercentile(), equalTo(20.0)); |
| 115 | + assertThat(snapshot2.get95thPercentile(), equalTo(20.0)); |
| 116 | + assertThat(snapshot2.get98thPercentile(), equalTo(20.0)); |
| 117 | + assertThat(snapshot2.get99thPercentile(), equalTo(20.0)); |
| 118 | + assertThat(snapshot2.get999thPercentile(), equalTo(20.0)); |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + public void whenUseTimer_thenCorrectTimerContexts() throws InterruptedException { |
| 123 | + Timer timer = new Timer(); |
| 124 | + |
| 125 | + Timer.Context context1 = timer.time(); |
| 126 | + TimeUnit.SECONDS.sleep(5); |
| 127 | + |
| 128 | + long elapsed1 = context1.stop(); |
| 129 | + assertEquals(5000000000L, elapsed1, 1000000); |
| 130 | + assertThat(timer.getCount(), equalTo(1L)); |
| 131 | + assertEquals(0.2, timer.getMeanRate(), 0.1); |
| 132 | + |
| 133 | + Timer.Context context2 = timer.time(); |
| 134 | + TimeUnit.SECONDS.sleep(2); |
| 135 | + context2.close(); |
| 136 | + assertThat(timer.getCount(), equalTo(2L)); |
| 137 | + assertEquals(0.3, timer.getMeanRate(), 0.1); |
| 138 | + } |
| 139 | +} |
0 commit comments