aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/profile/test/QueryProfileListPropertiesMicroBenchmark.java
blob: 87980f7398717b75cfa84898e349245b65f7bfcf (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
104
105
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.test;

import com.yahoo.jdisc.http.HttpRequest.Method;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.search.Query;
import com.yahoo.search.query.profile.QueryProfile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bratseth
 */
public class QueryProfileListPropertiesMicroBenchmark {

    private final String description;
    private final String propertyPrefix;
    private final boolean useDimensions;

    public QueryProfileListPropertiesMicroBenchmark(String description, String propertyPrefix, boolean useDimensions) {
        this.description=description;
        this.propertyPrefix=propertyPrefix;
        this.useDimensions=useDimensions;
    }

    public void benchmark(int count) {
        Query query=createQuery();
        listValues(10000, query); // warm-up
        System.out.println(description);
        long startTime=System.currentTimeMillis();
        listValues(count, query);
        long endTime=System.currentTimeMillis();
        long totalTime=(endTime-startTime);
        System.out.println("Done in " + totalTime + " ms (" + ((float)totalTime*1000/(count) + " microsecond per listProperties)"));
    }

    private Query createQuery() {
        Map<String,String> dimensions=null;
        if (useDimensions) {
            dimensions=new HashMap<>();
            dimensions.put("x","1");
            dimensions.put("y","2");
        }

        QueryProfile main=new QueryProfile("main");
        setValues(10,main,dimensions);
        QueryProfile parent=new QueryProfile("parent");
        setValues(5,main,dimensions);
        main.addInherited(parent);
        main.freeze();
        Query query = new Query(HttpRequest.createTestRequest("?query=test&x=1&y=2", Method.GET), main.compile(null));
        return query;
    }

    private void setValues(int count,QueryProfile profile,Map<String,String> dimensions) {
        for (int i=0; i<count; i++) {
            String thisPrefix=propertyPrefix;
            if ( ! thisPrefix.isEmpty())
                thisPrefix+=".";
            profile.set(thisPrefix + "property" + i, "value" + i, dimensions, null);
        }
    }

    private void listValues(int count,Query query) {
        final int dotInterval=1000000;
        for (int i=0; i<count; i++) {
            if (count>dotInterval && i%(count/dotInterval)==0)
                System.out.print(".");
            Map<String,Object> properties = query.properties().listProperties(propertyPrefix);
            int expectedSize = 10 + (propertyPrefix.isEmpty() ? 3 : 0); // 3 extra properties on the root
            if ( properties.size() != expectedSize )
                throw new RuntimeException("Expected a map of 10 elements, but got " + expectedSize + ": \n" + toString(properties));
        }
    }

    private String toString(Map<String,Object> map) {
        StringBuilder b=new StringBuilder();
        for (Map.Entry<String,Object> entry : map.entrySet())
            b.append("   ")
                .append(entry.getKey())
                .append(" = ")
                .append(entry.getValue().toString())
                .append("\n");
        return b.toString();
    }

    public static void main(String[] args) {
        int count=1000000;
        new QueryProfileListPropertiesMicroBenchmark("Listing values in root, no dimensions                     ","",false).benchmark(count);
        System.out.println("");
        new QueryProfileListPropertiesMicroBenchmark("Listing values in 1-level nested profiles, no dimensions  ","a",false).benchmark(count);
        System.out.println("");
        new QueryProfileListPropertiesMicroBenchmark("Listing values in 2-level nested profiles, no dimensions  ","a.b",false).benchmark(count);
        System.out.println("");
        new QueryProfileListPropertiesMicroBenchmark("Listing values in root, with dimensions                   ","",true).benchmark(count);
        System.out.println("");
        new QueryProfileListPropertiesMicroBenchmark("Listing values in 1-level nested profiles, with dimensions","a",true).benchmark(count);
        System.out.println("");
        new QueryProfileListPropertiesMicroBenchmark("Listing values in 2-level nested profiles, with dimensions","a.b",true).benchmark(count);
        System.out.println("");
    }

}