aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/metrics/ClusterSearchNodeMetricsRetrieverTest.java
blob: 7e6f8b5d4c2758f7f4b85841fd82f921e880c978 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.metrics;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.Assert.assertEquals;

public class ClusterSearchNodeMetricsRetrieverTest {

    @Rule
    public final WireMockRule wireMock = new WireMockRule(options().dynamicPort(), true);


    @Test
    public void testMetricAggregation() throws IOException {
        List<URI> hosts = Stream.of(1, 2)
                .map(item -> URI.create("http://localhost:" + wireMock.port() + "/metrics" + item + "/v2/values"))
                .toList();

        stubFor(get(urlEqualTo("/metrics1/v2/values"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(nodeMetrics("_1"))));

        stubFor(get(urlEqualTo("/metrics2/v2/values"))
                .willReturn(aResponse()
                        .withStatus(200)
                        .withBody(nodeMetrics("_2"))));

        String expectedClusterNameContent = "content/content/0/0";
        String expectedClusterNameMusic = "content/music/0/0";
        Map<String, SearchNodeMetricsAggregator> aggregatorMap = new ClusterSearchNodeMetricsRetriever().requestMetricsGroupedByCluster(hosts);

        compareAggregators(
                new SearchNodeMetricsAggregator()
                .addDocumentReadyCount(1275)
                .addDocumentActiveCount(1275)
                .addDocumentTotalCount(1275)
                .addDocumentDiskUsage(14781856)
                .addResourceDiskUsageAverage(0.0009083386306)
                .addResourceMemoryUsageAverage(0.0183488434436),
                aggregatorMap.get(expectedClusterNameContent)
        );

        compareAggregators(
                new SearchNodeMetricsAggregator()
                .addDocumentReadyCount(3008)
                .addDocumentActiveCount(3008)
                .addDocumentTotalCount(3008)
                .addDocumentDiskUsage(331157)
                .addResourceDiskUsageAverage(0.0000152263558)
                .addResourceMemoryUsageAverage(0.0156505524171),
                aggregatorMap.get(expectedClusterNameMusic)
        );

        wireMock.stop();
    }

    private String nodeMetrics(String extension) throws IOException {
        return Files.readString(Path.of("src/test/resources/metrics/node_metrics" + extension + ".json"));
    }

    // Same tolerance value as used internally in MetricsAggregator.isZero
    private static final double metricsTolerance = 0.001;

    private void compareAggregators(SearchNodeMetricsAggregator expected, SearchNodeMetricsAggregator actual) {
        assertEquals(expected.aggregateDocumentDiskUsage(), actual.aggregateDocumentDiskUsage(), metricsTolerance);
    }

}