diff options
author | gjoranv <gv@verizonmedia.com> | 2021-04-13 17:03:46 +0200 |
---|---|---|
committer | gjoranv <gv@verizonmedia.com> | 2021-04-13 17:03:46 +0200 |
commit | 3f46e126abf3da75ae561ff9f5070b977b3004e3 (patch) | |
tree | 581403a84038d1299edf14d5ed54aeae0541d5cc /container-core/src/test/java/com/yahoo/metrics | |
parent | 94611f2c72d033a15b251b59781ff7dddfef63fe (diff) |
Add java source from simplemetrics.
Diffstat (limited to 'container-core/src/test/java/com/yahoo/metrics')
7 files changed, 725 insertions, 0 deletions
diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java new file mode 100644 index 00000000000..b33da4bd531 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/BucketTest.java @@ -0,0 +1,242 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import static org.junit.Assert.*; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.logging.Handler; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import java.util.Set; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.google.common.collect.ImmutableMap; +import com.yahoo.metrics.simple.UntypedMetric.AssumedType; + +/** + * Functional tests for the value buckets, as implemented in the class Bucket, + * and by extension the value store itself, UntypedValue. + * + * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + */ +public class BucketTest { + private Bucket bucket; + + @Before + public void setUp() throws Exception { + bucket = new Bucket(); + } + + @After + public void tearDown() throws Exception { + bucket = null; + } + + @Test + public final void testEntrySet() { + assertEquals(0, bucket.entrySet().size()); + for (int i = 0; i < 4; ++i) { + bucket.put(new Sample(new Measurement(i), new Identifier("nalle_" + i, null), AssumedType.GAUGE)); + } + assertEquals(4, bucket.entrySet().size()); + for (int i = 0; i < 4; ++i) { + bucket.put(new Sample(new Measurement(i), new Identifier("nalle", + new Point(new ImmutableMap.Builder<String, Integer>().put("dim", Integer.valueOf(i)).build())), + AssumedType.GAUGE)); + } + assertEquals(8, bucket.entrySet().size()); + int nalle = 0, nalle0 = 0, nalle1 = 0, nalle2 = 0, nalle3 = 0; + for (Entry<Identifier, UntypedMetric> x : bucket.entrySet()) { + String metricName = x.getKey().getName(); + switch (metricName) { + case "nalle": + ++nalle; + break; + case "nalle_0": + ++nalle0; + break; + case "nalle_1": + ++nalle1; + break; + case "nalle_2": + ++nalle2; + break; + case "nalle_3": + ++nalle3; + break; + default: + throw new IllegalStateException(); + } + } + assertEquals(4, nalle); + assertEquals(1, nalle0); + assertEquals(1, nalle1); + assertEquals(1, nalle2); + assertEquals(1, nalle3); + } + + @Test + public final void testPutSampleWithUnsupportedType() { + boolean caughtIt = false; + try { + bucket.put(new Sample(new Measurement(1), new Identifier("nalle", null), AssumedType.NONE)); + } catch (Exception e) { + caughtIt = true; + } + assertTrue(caughtIt); + } + + @Test + public final void testPutIdentifierUntypedValue() { + UntypedMetric v = new UntypedMetric(null); + v.add(2); + bucket.put(new Sample(new Measurement(3), new Identifier("nalle", null), AssumedType.GAUGE)); + bucket.put(new Identifier("nalle", null), v); + assertEquals(1, bucket.entrySet().size()); + // check raw overwriting + Entry<Identifier, UntypedMetric> stored = bucket.entrySet().iterator().next(); + assertEquals(new Identifier("nalle", null), stored.getKey()); + assertTrue(stored.getValue().isCounter()); + } + + @Test + public final void testHasIdentifier() { + for (int i = 0; i < 4; ++i) { + bucket.put(new Sample(new Measurement(i), new Identifier("nalle_" + i, new Point( + new ImmutableMap.Builder<String, Integer>().put(String.valueOf(i), Integer.valueOf(i)).build())), + AssumedType.GAUGE)); + } + for (int i = 0; i < 4; ++i) { + assertTrue(bucket.hasIdentifier(new Identifier("nalle_" + i, new Point(new ImmutableMap.Builder<String, Integer>().put( + String.valueOf(i), Integer.valueOf(i)).build())))); + } + } + + @Test + public final void testOkMerge() { + bucket.put(new Sample(new Measurement(2), new Identifier("nalle", null), AssumedType.GAUGE)); + Bucket otherNew = new Bucket(); + otherNew.put(new Sample(new Measurement(3), new Identifier("nalle", null), AssumedType.GAUGE)); + Bucket otherOld = new Bucket(); + otherOld.put(new Sample(new Measurement(5), new Identifier("nalle", null), AssumedType.GAUGE)); + bucket.merge(otherNew, true); + bucket.merge(otherOld, false); + Set<Entry<Identifier, UntypedMetric>> entries = bucket.entrySet(); + assertEquals(1, entries.size()); + Entry<Identifier, UntypedMetric> entry = entries.iterator().next(); + assertEquals(10, entry.getValue().getSum(), 0.0); + assertEquals(3, entry.getValue().getLast(), 0.0); + assertEquals(2, entry.getValue().getMin(), 0.0); + assertEquals(5, entry.getValue().getMax(), 0.0); + assertEquals(3, entry.getValue().getCount()); + } + + @Test + public final void testMergeDifferentMetrics() { + bucket.put(new Sample(new Measurement(2), new Identifier("nalle", null), AssumedType.GAUGE)); + Bucket otherNew = new Bucket(); + otherNew.put(new Sample(new Measurement(3), new Identifier("other", null), AssumedType.GAUGE)); + bucket.merge(otherNew, true); + Set<Entry<Identifier, UntypedMetric>> entries = bucket.entrySet(); + assertEquals(2, entries.size()); + + Collection<Map.Entry<Point, UntypedMetric>> nalle_values = bucket.getValuesForMetric("nalle"); + assertEquals(1, nalle_values.size()); + Collection<Map.Entry<Point, UntypedMetric>> other_values = bucket.getValuesForMetric("other"); + assertEquals(1, other_values.size()); + + UntypedMetric nalle_v = nalle_values.iterator().next().getValue(); + assertEquals(1, nalle_v.getCount()); + assertEquals(2, nalle_v.getSum(), 0.0); + assertEquals(2, nalle_v.getLast(), 0.0); + assertEquals(2, nalle_v.getMin(), 0.0); + assertEquals(2, nalle_v.getMax(), 0.0); + + UntypedMetric other_v = other_values.iterator().next().getValue(); + assertEquals(1, other_v.getCount()); + assertEquals(3, other_v.getSum(), 0.0); + assertEquals(3, other_v.getLast(), 0.0); + assertEquals(3, other_v.getMax(), 0.0); + assertEquals(3, other_v.getMin(), 0.0); + } + + private static class CheckThatItWasLogged extends Handler { + final boolean[] loggingMarker; + + public CheckThatItWasLogged(boolean[] loggingMarker) { + this.loggingMarker = loggingMarker; + } + + @Override + public void publish(LogRecord record) { + loggingMarker[0] = true; + } + + @Override + public void flush() { + } + + @Override + public void close() throws SecurityException { + } + } + + @Test + public final void testMismatchedMerge() { + Logger log = Logger.getLogger(Bucket.class.getName()); + boolean[] loggingMarker = new boolean[1]; + loggingMarker[0] = false; + log.setUseParentHandlers(false); + Handler logHandler = new CheckThatItWasLogged(loggingMarker); + log.addHandler(logHandler); + Bucket other = new Bucket(); + bucket.put(new Sample(new Measurement(2), new Identifier("nalle", null), AssumedType.GAUGE)); + other.put(new Sample(new Measurement(3), new Identifier("nalle", null), AssumedType.COUNTER)); + bucket.merge(other, true); + assertTrue(loggingMarker[0]); + log.removeHandler(logHandler); + log.setUseParentHandlers(true); + } + + @Test + public final void testGetAllMetricNames() { + twoMetricsUniqueDimensions(); + Collection<String> names = bucket.getAllMetricNames(); + assertEquals(2, names.size()); + assertTrue(names.contains("nalle")); + assertTrue(names.contains("nalle2")); + } + + @Test + public final void testGetValuesForMetric() { + twoMetricsUniqueDimensions(); + Collection<Entry<Point, UntypedMetric>> values = bucket.getValuesForMetric("nalle"); + assertEquals(4, values.size()); + } + + private void twoMetricsUniqueDimensions() { + for (int i = 0; i < 4; ++i) { + bucket.put(new Sample(new Measurement(i), new Identifier("nalle", new Point(new ImmutableMap.Builder<String, Integer>() + .put(String.valueOf(i), Integer.valueOf(i)).build())), AssumedType.GAUGE)); + bucket.put(new Sample(new Measurement(i), new Identifier("nalle2", new Point( + new ImmutableMap.Builder<String, Integer>().put(String.valueOf(i), Integer.valueOf(i)).build())), + AssumedType.GAUGE)); + } + } + + @Test + public final void testGetValuesByMetricName() { + twoMetricsUniqueDimensions(); + Map<String, List<Entry<Point, UntypedMetric>>> values = bucket.getValuesByMetricName(); + assertEquals(2, values.size()); + assertEquals(4, values.get("nalle").size()); + assertEquals(4, values.get("nalle2").size()); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java new file mode 100644 index 00000000000..dc097f71a6b --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/CounterTest.java @@ -0,0 +1,111 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Functional tests for counters. + * + * @author steinar + */ +public class CounterTest { + + MetricReceiver receiver; + + @Before + public void setUp() throws Exception { + receiver = new MetricReceiver.MockReceiver(); + } + + @After + public void tearDown() throws Exception { + receiver = null; + } + + @Test + public final void testAdd() throws InterruptedException { + final String metricName = "unitTestCounter"; + Counter c = receiver.declareCounter(metricName); + c.add(); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + assertEquals(Point.emptyPoint(), x.get(0).getKey()); + assertEquals(1L, x.get(0).getValue().getCount()); + } + + @Test + public final void testAddLong() throws InterruptedException { + final String metricName = "unitTestCounter"; + Counter c = receiver.declareCounter(metricName); + final long twoToThePowerOfFourtyeight = 65536L * 65536L * 65536L; + c.add(twoToThePowerOfFourtyeight); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + assertEquals(Point.emptyPoint(), x.get(0).getKey()); + assertEquals(twoToThePowerOfFourtyeight, x.get(0).getValue().getCount()); + } + + @Test + public final void testAddPoint() throws InterruptedException { + final String metricName = "unitTestCounter"; + Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build(); + Counter c = receiver.declareCounter(metricName, p); + c.add(); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + assertEquals(p, x.get(0).getKey()); + assertEquals(1, x.get(0).getValue().getCount()); + } + + @Test + public final void testAddLongPoint() throws InterruptedException { + final String metricName = "unitTestCounter"; + Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build(); + Counter c = receiver.declareCounter(metricName, p); + final long twoToThePowerOfFourtyeight = 65536L * 65536L * 65536L; + c.add(twoToThePowerOfFourtyeight, c.builder().set("x", 7).set("_y", 11.0d).set("Z", "13").build()); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + Point actual = x.get(0).getKey(); + assertEquals(5, actual.dimensionality()); + List<String> dimensions = actual.dimensions(); + List<Value> location = actual.location(); + assertEquals(dimensions.size(), location.size()); + Iterator<String> i0 = dimensions.iterator(); + Iterator<Value> i1 = location.iterator(); + Map<String, Value> asMap = new HashMap<>(); + while (i0.hasNext() && i1.hasNext()) { + asMap.put(i0.next(), i1.next()); + } + assertEquals(Value.of(7), asMap.get("x")); + assertEquals(Value.of(3.0d), asMap.get("y")); + assertEquals(Value.of("5"), asMap.get("z")); + assertEquals(Value.of(11.0d), asMap.get("_y")); + assertEquals(Value.of("13"), asMap.get("Z")); + assertEquals(twoToThePowerOfFourtyeight, x.get(0).getValue().getCount()); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/DimensionsCacheTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/DimensionsCacheTest.java new file mode 100644 index 00000000000..0fde3bcf588 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/DimensionsCacheTest.java @@ -0,0 +1,127 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import static org.junit.Assert.*; + +import java.util.Collection; +import java.util.Map; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.yahoo.metrics.simple.UntypedMetric.AssumedType; + +/** + * Functional test for point persistence layer. + * + * @author Steinar Knutsen + */ +public class DimensionsCacheTest { + + private static final int POINTS_TO_KEEP = 3; + DimensionCache cache; + + @Before + public void setUp() throws Exception { + cache = new DimensionCache(POINTS_TO_KEEP); + } + + @After + public void tearDown() throws Exception { + cache = null; + } + + @Test + public final void smokeTest() { + String metricName = "testMetric"; + Bucket first = new Bucket(); + for (int i = 0; i < 4; ++i) { + populateSingleValue(metricName, first, i); + } + cache.updateDimensionPersistence(null, first); + Bucket second = new Bucket(); + final int newest = 42; + populateSingleValue(metricName, second, newest); + cache.updateDimensionPersistence(first, second); + assertEquals(POINTS_TO_KEEP, second.getValuesForMetric(metricName).size()); + boolean newestFound = false; + for (Entry<Point, UntypedMetric> x : second.getValuesForMetric(metricName)) { + if (x.getValue().getLast() == newest) { + newestFound = true; + } + } + assertTrue("Kept newest measurement when padding points.", newestFound); + } + + @Test + public final void testNoBoomWithEmptyBuckets() { + Bucket check = new Bucket(); + cache.updateDimensionPersistence(null, new Bucket()); + cache.updateDimensionPersistence(null, new Bucket()); + cache.updateDimensionPersistence(new Bucket(), check); + assertEquals(0, check.entrySet().size()); + } + + @Test + public final void testUpdateWithNullThenDataThenData() { + Bucket first = new Bucket(); + populateDimensionLessValue("one", first, 2); + cache.updateDimensionPersistence(null, first); + Bucket second = new Bucket(); + populateDimensionLessValue("other", second, 3); + cache.updateDimensionPersistence(first, second); + Collection<String> names = second.getAllMetricNames(); + assertEquals(2, names.size()); + assertTrue(names.contains("one")); + assertTrue(names.contains("other")); + } + + @Test + public final void requireThatOldDataIsForgotten() { + Bucket first = new Bucket(); // "now" as timestamp + populateDimensionLessValue("one", first, 2); + cache.updateDimensionPersistence(first, new Bucket()); + Bucket second = new Bucket(17, 42); // really old timestamp + populateDimensionLessValue("other", second, 3); + Bucket third = new Bucket(); + populateDimensionLessValue("two", third, 4); + cache.updateDimensionPersistence(second, third); + Collection<String> names = third.getAllMetricNames(); + assertEquals(2, names.size()); + assertTrue(names.contains("one")); + assertTrue(names.contains("two")); + } + + @Test + public final void testUpdateWithNullThenDataThenNoDataThenData() { + Bucket first = new Bucket(); + Bucket second = new Bucket(); + populateDimensionLessValue("first", first, 1.0d); + populateDimensionLessValue("second", second, 2.0d); + cache.updateDimensionPersistence(null, first); + cache.updateDimensionPersistence(first, new Bucket()); + cache.updateDimensionPersistence(new Bucket(), second); + assertEquals(2, second.entrySet().size()); + assertTrue(second.getAllMetricNames().contains("first")); + assertTrue(second.getAllMetricNames().contains("second")); + } + + private void populateDimensionLessValue(String metricName, Bucket bucket, double x) { + Identifier id = new Identifier(metricName, null); + Sample wrappedX = new Sample(new Measurement(Double.valueOf(x)), id, AssumedType.GAUGE); + bucket.put(wrappedX); + } + + private void populateSingleValue(String metricName, Bucket bucket, int i) { + Map<String, Integer> m = new TreeMap<>(); + m.put(String.valueOf(i), Integer.valueOf(i)); + Point p = new Point(m); + Identifier id = new Identifier(metricName, p); + Sample x = new Sample(new Measurement(Double.valueOf(i)), id, AssumedType.GAUGE); + bucket.put(x); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/GaugeTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/GaugeTest.java new file mode 100644 index 00000000000..fef56c27114 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/GaugeTest.java @@ -0,0 +1,83 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import static org.junit.Assert.*; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.yahoo.metrics.ManagerConfig; + +/** + * Functional tests for gauges. + * + * @author steinar + */ +public class GaugeTest { + + MetricReceiver receiver; + + @Before + public void setUp() throws Exception { + receiver = new MetricReceiver.MockReceiver(); + } + + @After + public void tearDown() throws Exception { + receiver = null; + } + + @Test + public final void testSampleDouble() throws InterruptedException { + final String metricName = "unitTestGauge"; + Gauge g = receiver.declareGauge(metricName); + g.sample(1.0d); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + assertEquals(Point.emptyPoint(), x.get(0).getKey()); + assertEquals(1L, x.get(0).getValue().getCount()); + assertEquals(1.0d, x.get(0).getValue().getLast(), 0.0d); + } + + @Test + public final void testSampleDoublePoint() throws InterruptedException { + final String metricName = "unitTestGauge"; + Point p = receiver.pointBuilder().set("x", 2L).set("y", 3.0d).set("z", "5").build(); + Gauge g = receiver.declareGauge(metricName, p); + g.sample(Math.E, g.builder().set("x", 7).set("_y", 11.0d).set("Z", "13").build()); + Bucket b = receiver.getSnapshot(); + final Map<String, List<Entry<Point, UntypedMetric>>> valuesByMetricName = b.getValuesByMetricName(); + assertEquals(1, valuesByMetricName.size()); + List<Entry<Point, UntypedMetric>> x = valuesByMetricName.get(metricName); + assertEquals(1, x.size()); + Point actual = x.get(0).getKey(); + assertEquals(5, actual.dimensionality()); + List<String> dimensions = actual.dimensions(); + List<Value> location = actual.location(); + assertEquals(dimensions.size(), location.size()); + Iterator<String> i0 = dimensions.iterator(); + Iterator<Value> i1 = location.iterator(); + Map<String, Value> asMap = new HashMap<>(); + while (i0.hasNext() && i1.hasNext()) { + asMap.put(i0.next(), i1.next()); + } + assertEquals(Value.of(7), asMap.get("x")); + assertEquals(Value.of(3.0d), asMap.get("y")); + assertEquals(Value.of("5"), asMap.get("z")); + assertEquals(Value.of(11.0d), asMap.get("_y")); + assertEquals(Value.of("13"), asMap.get("Z")); + assertEquals(Math.E, x.get(0).getValue().getLast(), 1e-15); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java new file mode 100644 index 00000000000..0450e5db5f5 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/MetricsTest.java @@ -0,0 +1,61 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import static org.junit.Assert.*; + +import java.util.Collection; +import java.util.Map.Entry; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.yahoo.metrics.ManagerConfig; +import com.yahoo.metrics.simple.jdisc.JdiscMetricsFactory; +import com.yahoo.metrics.simple.jdisc.SimpleMetricConsumer; + +/** + * Functional test for simple metric implementation. + * + * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a> + */ +public class MetricsTest extends UnitTestSetup { + SimpleMetricConsumer metricApi; + + @Before + public void setUp() throws Exception { + super.init(); + metricApi = (SimpleMetricConsumer) new JdiscMetricsFactory(metricManager.get()).newInstance(); + } + + @After + public void tearDown() throws Exception { + super.fini(); + } + + @Test + public final void smokeTest() throws InterruptedException { + final String metricName = "testMetric"; + metricApi.set(metricName, Double.valueOf(1.0d), null); + updater.gotData.await(10, TimeUnit.SECONDS); + Bucket s = getUpdatedSnapshot(); + Collection<Entry<Point, UntypedMetric>> values = s.getValuesForMetric(metricName); + assertEquals(1, values.size()); + Entry<Point, UntypedMetric> value = values.iterator().next(); + assertEquals(Point.emptyPoint(), value.getKey()); + assertEquals(1.0d, value.getValue().getLast(), 0.0d); // using number exactly expressible as doubles + assertEquals(1L, value.getValue().getCount()); + } + + @Test + public final void testRedefinition() { + MetricReceiver r = metricManager.get(); + final String metricName = "gah"; + r.addMetricDefinition(metricName, new MetricSettings.Builder().build()); + r.addMetricDefinition(metricName, new MetricSettings.Builder().histogram(true).build()); + assertTrue(r.getMetricDefinition(metricName).isHistogram()); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/PointTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/PointTest.java new file mode 100644 index 00000000000..802bea3c463 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/PointTest.java @@ -0,0 +1,24 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple; + +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; + +import static org.junit.Assert.assertEquals; + +/** + * @author bratseth + */ +public class PointTest { + + @Test + public void testPointEquality() { + Point a = new Point(Collections.emptyMap()); + Point b = new Point(new HashMap<>(0)); + assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a, b); + } + +} diff --git a/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java b/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java new file mode 100644 index 00000000000..13f7ba55e61 --- /dev/null +++ b/container-core/src/test/java/com/yahoo/metrics/simple/jdisc/SnapshotConverterTest.java @@ -0,0 +1,77 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.metrics.simple.jdisc; + +import com.yahoo.container.jdisc.state.CountMetric; +import com.yahoo.container.jdisc.state.GaugeMetric; +import com.yahoo.container.jdisc.state.MetricDimensions; +import com.yahoo.container.jdisc.state.MetricSet; +import com.yahoo.container.jdisc.state.MetricSnapshot; +import com.yahoo.container.jdisc.state.MetricValue; +import com.yahoo.metrics.simple.Bucket; +import com.yahoo.metrics.simple.Identifier; +import com.yahoo.metrics.simple.MetricReceiver; +import com.yahoo.metrics.simple.Point; +import com.yahoo.metrics.simple.UntypedMetric; +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author bratseth + */ +public class SnapshotConverterTest { + + @Test + public void testPointConversion() { + MetricDimensions a = SnapshotConverter.convert(new Point(Collections.emptyMap())); + MetricDimensions b = SnapshotConverter.convert(new Point(new HashMap<>(0))); + MetricDimensions c = SnapshotConverter.convert((Point)null); + assertEquals(a.hashCode(), b.hashCode()); + assertEquals(a, b); + assertEquals(a.hashCode(), c.hashCode()); + assertEquals(a, c); + assertEquals(b.hashCode(), c.hashCode()); + assertEquals(b, c); + } + + @Test + public void testConversion() { + MetricReceiver mock = new MetricReceiver.MockReceiver(); + mock.declareCounter("foo").add(1); + mock.declareGauge("quuux").sample(42.25); + mock.declareCounter("bar", new Point(new HashMap<String,String>())).add(4); + + MetricSnapshot snapshot = new SnapshotConverter(mock.getSnapshot()).convert(); + + for (Map.Entry<MetricDimensions, MetricSet> entry : snapshot) { + for (Map.Entry<String, String> dv : entry.getKey()) { + assertTrue(false); + } + + int cnt = 0; + for (Map.Entry<String, MetricValue> mv : entry.getValue()) { + ++cnt; + if ("foo".equals(mv.getKey())) { + assertTrue(mv.getValue() instanceof CountMetric); + assertEquals(1, ((CountMetric)mv.getValue()).getCount()); + } else if ("bar".equals(mv.getKey())) { + assertTrue(mv.getValue() instanceof CountMetric); + assertEquals(4, ((CountMetric)mv.getValue()).getCount()); + } else if ("quuux".equals(mv.getKey())) { + assertTrue(mv.getValue() instanceof GaugeMetric); + assertEquals(42.25, ((GaugeMetric)mv.getValue()).getLast(), 0.001); + assertEquals(1, ((GaugeMetric)mv.getValue()).getCount()); + } else { + assertTrue(false); + } + } + assertEquals(3, cnt); + } + } + +} |