aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/test/java/com/yahoo/processing/request/test/CompoundNameBenchmark.java
blob: a2d62267da62f8de990f5a7ebad5fa12664edcc2 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.request.test;

import com.yahoo.processing.request.CompoundName;

/**
 * @author baldersheim
 */
public class CompoundNameBenchmark {
    public void run() {
        long result=0;
        String [] strings = createStrings(1000);
        // Warm-up
        out("Warming up...");
        for (int i=0; i<2*1000; i++)
            result+=createCompundName(strings);

        long startTime=System.currentTimeMillis();
        out("Running...");
        for (int i=0; i<10*1000; i++)
            result+=createCompundName(strings);
        out("Ignore this: " + result); // Make sure we are not fooled by optimization by creating an observable result
        long endTime=System.currentTimeMillis();
        out("Compoundification 1000 strings 100.000 times took " + (endTime-startTime) + " ms");
    }

    private String [] createStrings(int num) {
        String [] strings = new String [num];
        for(int i=0; i < strings.length; i++) {
            strings[i] = "this.is.a.short.compound.name." + i;
        }
        return strings;
    }

    private int createCompundName(String [] strings) {
        int retval = 0;
        CompoundName toAppend = new CompoundName("appended.twice");
        for (String s : strings) {
            CompoundName n = new CompoundName(s);
            retval += n.size();
            CompoundName a = n.append(toAppend);
            retval += a.size();
        }
        return retval;
    }

    private void out(String string) {
        System.out.println(string);
    }

    public static void main(String[] args) {
        new CompoundNameBenchmark().run();
    }

}