aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/collections/CollectionsBenchMark.java
blob: f577dcad302b37d8306375981af3456824fee184 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.collections;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author baldersheim
 */
public class CollectionsBenchMark {
    abstract static class BenchMark {
        protected BenchMark(int numWarmup, int repetitions) {
            this.numWarmup = numWarmup;
            this.repetitions = repetitions;
        }
        abstract void runOnce();
        abstract String getEndComment();
        protected void run() {
            System.out.println("Starting benchmark warmup '" + getClass().getName() + "'.");
            for (int i=0; i < numWarmup; i++) {
                runOnce();
            }
            System.out.println("Starting benchmark '" + getClass().getName() + "'.");
            long startTime=System.currentTimeMillis();
            for (int i=0; i < repetitions; i++) {
                runOnce();
            }
            long endTime=System.currentTimeMillis();
            long totalTime=(endTime-startTime);
            System.out.println("Done in " + totalTime + " ms (" + ((float) totalTime * 1000 / repetitions + " microsecond per repetition.)")); // *2 because we do 2 gets
            System.out.println("Final remark: " + getEndComment());
        }


        final private int repetitions;
        final private int numWarmup;
    }

    static class MapFilterBenchMark extends BenchMark {
        MapFilterBenchMark(Map<Integer, Integer> s, int numWarmup, int numRepetitions, int numObjects) {
            super(numWarmup, numRepetitions);
            this.s = s;
            objects = new Integer[numObjects];
            for (int i=0; i < numObjects; i++) {
                objects[i] = i;
            }
        }
        void runOnce() {
            for (Integer o : objects) {
                if (s.put(o, o) == null) {
                    uniqueCount += o;
                }
            }
        }
        String getEndComment() { return " Unique sum is '" + uniqueCount + "'"; }
        private final Map<Integer,Integer> s;
        final Integer [] objects;
        long uniqueCount = 0;
    }

    static class SetFilterBenchMark extends BenchMark {
        SetFilterBenchMark(Set<Integer> s, int numWarmup, int numRepetitions, int numObjects) {
            super(numWarmup, numRepetitions);
            this.s = s;
            objects = new Integer[numObjects];
            for (int i=0; i < numObjects; i++) {
                objects[i] = i;
            }
        }
        void runOnce() {
            for (Integer o : objects) {
                if ( s.add(o) ) {
                    uniqueCount += o;
                }
            }
        }
        String getEndComment() { return " Unique sum is '" + uniqueCount + "'"; }
        private final Set<Integer> s;
        final Integer [] objects;
        long uniqueCount = 0;
    }

    static abstract class SmallMapsBenchMark extends BenchMark {
        SmallMapsBenchMark(int numWarmup, int numRepetitions, int numObjects, int numUnique) {
            super(numWarmup, numRepetitions);
            objects = new Integer[numObjects];
            for (int i=0; i < numObjects; i++) {
                objects[i] = i%numUnique;
            }
        }
        void runOnce() {
            Set<Integer> s = createSet();
            for (Integer o : objects) {
                if ( s.add(o) ) {
                    uniqueCount += o;
                }
            }
        }
        abstract Set<Integer> createSet();
        String getEndComment() { return " Unique sum is '" + uniqueCount + "'"; }
        final Integer [] objects;
        long uniqueCount = 0;
    }

    static class SmallHashSetBenchMark extends SmallMapsBenchMark
    {
        SmallHashSetBenchMark(int numWarmup, int numRepetitions, int numObjects, int numUnique) {
            super(numWarmup, numRepetitions, numObjects, numUnique);
        }
        Set<Integer> createSet() { return new HashSet<Integer>();}
    }

    static class SmallLazySetBenchMark extends SmallMapsBenchMark
    {
        SmallLazySetBenchMark(int numWarmup, int numRepetitions, int numObjects, int numUnique) {
            super(numWarmup, numRepetitions, numObjects, numUnique);
        }
        Set<Integer> createSet() { return new LazySet<Integer>() {
                @Override
                protected Set<Integer> newDelegate() {
                    return new HashSet<Integer>();
                }
            };
        }
    }

    static class SmallLazyTinyBenchMark extends SmallMapsBenchMark
    {
        SmallLazyTinyBenchMark(int numWarmup, int numRepetitions, int numObjects, int numUnique) {
            super(numWarmup, numRepetitions, numObjects, numUnique);
        }
        Set<Integer> createSet() { return new LazySet<Integer>() {
            @Override
            protected Set<Integer> newDelegate() {
                return new TinyIdentitySet<Integer>(10);
            }
        };
        }
    }

    static void benchMarkAll() {

        new MapFilterBenchMark(new HashMap<Integer, Integer>(), 100000, 10000000, 10).run();
        new MapFilterBenchMark(new IdentityHashMap<Integer, Integer>(10), 100000, 10000000, 10).run();
        new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 10).run();
        new SetFilterBenchMark(new TinyIdentitySet<Integer>(10), 100000, 10000000, 10).run();
        new SetFilterBenchMark(new TinyIdentitySet<Integer>(10), 100000, 10000000, 15).run();
        new SetFilterBenchMark(new TinyIdentitySet<Integer>(10), 100000, 10000000, 20).run();
        new SetFilterBenchMark(new TinyIdentitySet<Integer>(20), 100000, 10000000, 20).run();
        new SmallHashSetBenchMark(100000, 10000000, 10, 1).run();
        new SmallLazySetBenchMark(100000, 10000000, 10, 1).run();
        new SmallHashSetBenchMark(100000, 10000000, 10, 2).run();
        new SmallLazySetBenchMark(100000, 10000000, 10, 2).run();
        new SmallLazyTinyBenchMark(100000, 10000000, 10, 2).run();
        new SmallHashSetBenchMark(100000, 10000000, 10, 10).run();
        new SmallLazySetBenchMark(100000, 10000000, 10, 10).run();

        new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 12).run();

        new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 20).run();
        new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 25).run();
        new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 30).run();
        new SmallHashSetBenchMark(100000, 10000000, 1, 1).run();

    }

    static void benchMark() {

        //new MapFilterBenchMark(new HashMap<Integer, Integer>(), 100000, 10000000, 10).run();
        //new MapFilterBenchMark(new IdentityHashMap<Integer, Integer>(10), 100000, 10000000, 10).run();
        //new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 10).run();
        //new SetFilterBenchMark(new TinyIdentitySet<Integer>(10), 100000, 10000000, 10).run();
        //new SmallHashSetBenchMark(100000, 10000000, 10, 1).run();
        //new SmallLazySetBenchMark(100000, 10000000, 10, 1).run();
        //new SmallHashSetBenchMark(100000, 10000000, 10, 2).run();
        //new SmallLazySetBenchMark(100000, 10000000, 10, 2).run();
        new SmallLazyTinyBenchMark(100000, 10000000, 10, 2).run();
        //new SmallHashSetBenchMark(100000, 10000000, 10, 10).run();
        //new SmallLazySetBenchMark(100000, 10000000, 10, 10).run();

        //new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 12).run();

        //new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 20).run();
        //new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 25).run();
        //new SetFilterBenchMark(new HashSet<Integer>(), 100000, 10000000, 30).run();
        //new SmallHashSetBenchMark(100000, 10000000, 1, 1).run();

    }


    static public void main(String argv[]) {
        benchMarkAll();
        ExecutorService tp = Executors.newFixedThreadPool(16);

        for (int i=0; i < 16; i++) {
            tp.execute(new Runnable() {
                @Override
                public void run() {
                    benchMark();
                }
            });
        }
    }
}