aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/test/java/com/yahoo/vespa/hosted/provision/NodeListMicroBenchmarkTest.java
blob: b275cb8a4c64989689129a2892d3ebbc4d4f9f71 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision;

import com.yahoo.config.provision.Flavor;
import com.yahoo.config.provision.NodeFlavors;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.NodeType;
import com.yahoo.config.provisioning.FlavorsConfig;
import com.yahoo.vespa.hosted.provision.node.IP;
import com.yahoo.vespa.hosted.provision.provisioning.FlavorConfigBuilder;
import com.yahoo.vespa.hosted.provision.provisioning.ProvisioningTester;
import org.junit.Ignore;
import org.junit.Test;

import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Random;

/**
 * @author hmusum
 */
public class NodeListMicroBenchmarkTest {

    private static final NodeFlavors nodeFlavors = createNodeFlavors();
    private final NodeResources resources0 = new NodeResources(1, 30, 20, 1.5);
    private int nodeCounter = 0;
    private static final int hostCount = 1000;

    @Ignore
    @Test
    public void testChildrenOf() {
        List<Node> nodes = createHosts();

        List<Node> childNodes = nodes.stream().map(host -> createNodes(host.hostname())).flatMap(Collection::stream).toList();
        nodes.addAll(childNodes);
        NodeList nodeList = new NodeList(nodes, false);

        int iterations = 100000;
        Random random = new Random(0);
        ArrayList<Integer> indexes = new ArrayList<>();
        for (int i = 0; i < iterations; i++) {
            indexes.add(random.nextInt(hostCount));
        }
        // Warmup for stable results.
        for (int i = 0; i < 10000; i++) {
            nodeList.childrenOf(nodes.get(indexes.get(i)));
        }

        Instant start = Instant.now();
        for (int i = 0; i < iterations; i++) {
            nodeList.childrenOf(nodes.get(indexes.get(i)));
        }
        Duration duration = Duration.between(start, Instant.now());
        System.out.println("Calling NodeList.childrenOf took " + duration + " (" + duration.toNanos() / iterations / 1000 + " microseconds per invocation)");
    }

    private List<Node> createHosts() {
        List<Node> hosts = new ArrayList<>();
        for (int i = 0; i < hostCount; i++) {
            hosts.add(Node.create("host" + i, IP.Config.of(List.of("::1"), createIps(), List.of()),
                               "host" + i, getFlavor("host"), NodeType.host).build());
        }
        return hosts;
    }

    private List<Node> createNodes(String parentHostname) {
        List<Node> nodes = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            nodeCounter++;
            Node node = Node.reserve(List.of("::2"), "node" + nodeCounter, parentHostname, resources0, NodeType.tenant).build();
            nodes.add(node);
        }
        return nodes;
    }

    private static NodeFlavors createNodeFlavors() {
        FlavorConfigBuilder builder = new FlavorConfigBuilder();
        builder.addFlavor("host", 30, 30, 40, 3, Flavor.Type.BARE_METAL);
        builder.addFlavor("node", 3, 3, 4, 3, Flavor.Type.DOCKER_CONTAINER);
        FlavorsConfig flavorsConfig = builder.build();
        return new NodeFlavors(Optional.ofNullable(flavorsConfig).orElseGet(ProvisioningTester::createConfig));
    }

    private Flavor getFlavor(String name) {
        return nodeFlavors.getFlavor(name).orElseThrow(() -> new RuntimeException("Unknown flavor"));
    }

    private List<String> createIps() {
        // Allow 4 containers
        int start = 2;
        int count = 4;
        var ipAddressPool = new ArrayList<String>();
        for (int i = start; i < (start + count); i++) {
            ipAddressPool.add("::" + i);
        }
        return ipAddressPool;
    }

}