diff options
author | gjoranv <gjoranv@gmail.com> | 2023-08-17 17:14:52 +0200 |
---|---|---|
committer | gjoranv <gjoranv@gmail.com> | 2023-08-18 13:48:32 +0200 |
commit | d6631e2d139815f4fcbe94785c30d9b9457b000f (patch) | |
tree | 95c40462cba866ef843cf2d7970bc7726f86f9b8 /metrics | |
parent | d1ec754bceb31c541619fde39ecb5c6e1bcccbfe (diff) |
Move unit tests for Metric and MetricSet to the 'metrics' module.
Diffstat (limited to 'metrics')
-rw-r--r-- | metrics/pom.xml | 17 | ||||
-rw-r--r-- | metrics/src/test/java/ai/vespa/metrics/MetricSetTest.java | 109 | ||||
-rw-r--r-- | metrics/src/test/java/ai/vespa/metrics/MetricTest.java | 39 |
3 files changed, 165 insertions, 0 deletions
diff --git a/metrics/pom.xml b/metrics/pom.xml index e8303e5a01f..a2fd17b7ea5 100644 --- a/metrics/pom.xml +++ b/metrics/pom.xml @@ -12,8 +12,14 @@ <packaging>jar</packaging> <version>8-SNAPSHOT</version> <name>metrics</name> + <dependencies> <dependency> + <groupId>com.google.guava</groupId> + <artifactId>guava</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.yahoo.vespa</groupId> <artifactId>annotations</artifactId> <version>${project.version}</version> @@ -25,7 +31,18 @@ <version>${project.version}</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <scope>test</scope> + </dependency> </dependencies> + <build> <plugins> <plugin> diff --git a/metrics/src/test/java/ai/vespa/metrics/MetricSetTest.java b/metrics/src/test/java/ai/vespa/metrics/MetricSetTest.java new file mode 100644 index 00000000000..d537f51e06b --- /dev/null +++ b/metrics/src/test/java/ai/vespa/metrics/MetricSetTest.java @@ -0,0 +1,109 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.metrics; + +import ai.vespa.metrics.set.Metric; +import ai.vespa.metrics.set.MetricSet; +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Map; + +import static java.util.Collections.emptyList; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * @author gjoranv + */ +public class MetricSetTest { + + @Test + void metrics_from_children_are_added() { + MetricSet child1 = new MetricSet("child1", List.of(new Metric("child1_metric"))); + MetricSet child2 = new MetricSet("child2", List.of(new Metric("child2_metric"))); + MetricSet parent = new MetricSet("parent", emptyList(), List.of(child1, child2)); + + Map<String, Metric> parentMetrics = parent.getMetrics(); + assertEquals(2, parentMetrics.size()); + assertNotNull(parentMetrics.get("child1_metric")); + assertNotNull(parentMetrics.get("child2_metric")); + } + + @Test + void adding_the_same_child_set_twice_has_no_effect() { + MetricSet child = new MetricSet("child", List.of(new Metric("child_metric"))); + MetricSet parent = new MetricSet("parent", emptyList(), List.of(child, child)); + + Map<String, Metric> parentMetrics = parent.getMetrics(); + assertEquals(1, parentMetrics.size()); + assertNotNull(parentMetrics.get("child_metric")); + } + + @Test + void internal_metrics_take_precedence_over_metrics_from_children() { + String METRIC_NAME = "metric1"; + String COMMON_DIMENSION_KEY = "commonKey"; + + Map<String, String> childDimensions = Map.of(COMMON_DIMENSION_KEY, "childCommonVal", "childKey", "childVal"); + Metric childMetric = new Metric(METRIC_NAME, "child-output-name", "child-description", childDimensions); + + Map<String, String> parentDimensions = Map.of(COMMON_DIMENSION_KEY, "parentCommonVal","parentKey", "parentVal"); + Metric parentMetric = new Metric(METRIC_NAME, "parent-output-name", "parent-description", parentDimensions); + + MetricSet child = new MetricSet("set1", Sets.newHashSet(childMetric)); + MetricSet parent = new MetricSet("set1", Sets.newHashSet(parentMetric), Sets.newHashSet(child)); + + Metric combinedMetric = parent.getMetrics().get(METRIC_NAME); + assertEquals("parent-output-name", combinedMetric.outputName); + assertEquals("parent-description", combinedMetric.description); + assertEquals(3, combinedMetric.dimensions.size()); + assertEquals("parentCommonVal", combinedMetric.dimensions.get(COMMON_DIMENSION_KEY)); + } + + @Test + void it_can_be_generated_from_builder() { + MetricSet metricSet = new MetricSet.Builder("test") + .metric("metric1") + .metric(TestMetrics.ENUM_METRIC.last()) + .metric(new Metric("metric2")) + .metrics(List.of(new Metric("metric3"))) + .metricSet(new MetricSet.Builder("child") + .metric("child_metric1") + .metric("child_metric2") + .build()) + .build(); + + Map<String, Metric> metrics = metricSet.getMetrics(); + assertEquals(6, metrics.size()); + assertNotNull(metrics.get("metric1")); + assertNotNull(metrics.get("emum-metric.last")); + assertNotNull(metrics.get("metric2")); + assertNotNull(metrics.get("metric3")); + assertNotNull(metrics.get("child_metric1")); + assertNotNull(metrics.get("child_metric1")); + } + + enum TestMetrics implements VespaMetrics { + ENUM_METRIC("emum-metric"); + + private final String name; + + TestMetrics(String name) { + this.name = name; + } + + public String baseName() { + return name; + } + + public Unit unit() { + return null; + } + + public String description() { + return null; + } + + } +} diff --git a/metrics/src/test/java/ai/vespa/metrics/MetricTest.java b/metrics/src/test/java/ai/vespa/metrics/MetricTest.java new file mode 100644 index 00000000000..7a0b85f82cc --- /dev/null +++ b/metrics/src/test/java/ai/vespa/metrics/MetricTest.java @@ -0,0 +1,39 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.metrics; + +import ai.vespa.metrics.set.Metric; +import com.google.common.collect.ImmutableMap; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * @author gjoranv + */ +public class MetricTest { + + @Test + void this_metric_takes_precedence_when_combined_with_another_metric() { + String COMMON_DIMENSION_KEY = "commonKey"; + + Map<String, String> thisDimensions = ImmutableMap.<String, String>builder() + .put(COMMON_DIMENSION_KEY, "thisCommonVal") + .put("thisKey", "thisVal") + .build(); + Metric thisMetric = new Metric("thisMetric", "this-output-name", "this-description", thisDimensions); + + Map<String, String> thatDimensions = ImmutableMap.<String, String>builder() + .put(COMMON_DIMENSION_KEY, "thatCommonVal") + .put("thatKey", "thatVal") + .build(); + Metric thatMetric = new Metric("thatMetric", "that-output-name", "that-description", thatDimensions); + + Metric combinedMetric = thisMetric.addDimensionsFrom(thatMetric); + assertEquals("this-output-name", combinedMetric.outputName); + assertEquals("this-description", combinedMetric.description); + assertEquals(3, combinedMetric.dimensions.size()); + assertEquals("thisCommonVal", combinedMetric.dimensions.get(COMMON_DIMENSION_KEY)); + } +} |