Commit | Line | Data |
---|---|---|
ab74ca14 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | *******************************************************************************/ | |
9 | ||
10 | package org.eclipse.tracecompass.analysis.timing.core.tests.store; | |
11 | ||
12 | import static org.junit.Assert.assertNotNull; | |
13 | ||
b3cf0859 GB |
14 | import java.text.DecimalFormat; |
15 | import java.text.Format; | |
16 | import java.util.Arrays; | |
ab74ca14 MK |
17 | import java.util.Random; |
18 | ||
19 | import org.eclipse.jdt.annotation.NonNull; | |
664a3a81 LPD |
20 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.ArrayListStore; |
21 | import org.eclipse.tracecompass.internal.segmentstore.core.arraylist.LazyArrayListStore; | |
22 | import org.eclipse.tracecompass.internal.segmentstore.core.treemap.TreeMapStore; | |
ab74ca14 MK |
23 | import org.eclipse.tracecompass.segmentstore.core.BasicSegment; |
24 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
25 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
ab74ca14 MK |
26 | import org.junit.FixMethodOrder; |
27 | import org.junit.Test; | |
b3cf0859 | 28 | import org.junit.runner.RunWith; |
ab74ca14 | 29 | import org.junit.runners.MethodSorters; |
b3cf0859 GB |
30 | import org.junit.runners.Parameterized; |
31 | import org.junit.runners.Parameterized.Parameters; | |
ab74ca14 MK |
32 | |
33 | /** | |
34 | * Segmentstore benchmarks, tests the performance for loads and reads. | |
35 | * | |
36 | * NOTE : Do not add this to isTracecompassFastYet, it is not information that | |
37 | * is interesting for users, it is for developpers. | |
38 | * | |
39 | * @category benchmark | |
40 | * | |
41 | * @author Matthew Khouzam | |
42 | * | |
43 | */ | |
44 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) | |
b3cf0859 | 45 | @RunWith(Parameterized.class) |
ab74ca14 MK |
46 | public class SegmentStoreBenchmark { |
47 | ||
b3cf0859 GB |
48 | private final ISegmentStore<@NonNull ISegment> fSegStore; |
49 | private final String fName; | |
50 | private static final Format FORMAT = new DecimalFormat("###,###.##"); //$NON-NLS-1$ | |
51 | ||
52 | /** | |
53 | * @return The arrays of parameters | |
54 | */ | |
55 | @Parameters(name = "{index}: {0}") | |
56 | public static Iterable<Object[]> getParameters() { | |
57 | return Arrays.asList(new Object[][] { | |
58 | { "Array list store", new ArrayListStore<>() }, | |
59 | { "Lazy array list store", new LazyArrayListStore<>() }, | |
60 | { "Treemap store", new TreeMapStore<>() }, | |
61 | }); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Constructor | |
66 | * | |
67 | * @param name | |
68 | * The name of this test | |
69 | * @param segStore | |
70 | * The segment store to fill for the benchmarks | |
71 | */ | |
72 | public SegmentStoreBenchmark(String name, ISegmentStore<@NonNull ISegment> segStore) { | |
73 | fSegStore = segStore; | |
74 | fName = name; | |
75 | } | |
ab74ca14 MK |
76 | |
77 | /** | |
78 | * Add elements in order | |
79 | */ | |
80 | @Test | |
81 | public void test1AddInOrder() { | |
82 | int size = 1; | |
83 | int[] fuzz = { 0 }; | |
84 | run(size, fuzz, new Object() { | |
85 | }.getClass().getEnclosingMethod().getName()); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Add elements almost in order, this represents a typical degenerate use | |
90 | * case. | |
91 | */ | |
92 | @Test | |
93 | public void test2AddFuzzyOrder() { | |
94 | int size = 1000; | |
95 | int[] fuzz = new int[size]; | |
96 | Random rng = new Random(10); | |
97 | for (int i = 0; i < size; i++) { | |
98 | fuzz[i] = rng.nextInt(1000); | |
99 | } | |
100 | String name = new Object() { | |
101 | }.getClass().getEnclosingMethod().getName(); | |
102 | assertNotNull(name); | |
103 | run(size, fuzz, name); | |
104 | } | |
105 | ||
106 | /** | |
107 | * Add elements almost in order, this represents a typical degenerate use | |
108 | * case, then iterate over the list. | |
109 | */ | |
110 | @Test | |
111 | public void test3AddFuzzyOrderThenIterate() { | |
112 | int size = 1000; | |
113 | int[] fuzz = new int[size]; | |
114 | Random rng = new Random(10); | |
115 | for (int i = 0; i < size; i++) { | |
116 | fuzz[i] = rng.nextInt(1000); | |
117 | } | |
118 | String name = new Object() { | |
119 | }.getClass().getEnclosingMethod().getName(); | |
120 | assertNotNull(name); | |
121 | runIterate(size, fuzz, name); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Add elements almost in order, this represents a typical degenerate use | |
126 | * case, and iterate while building then when done. | |
127 | */ | |
128 | @Test | |
129 | public void test4AddFuzzyOrderThenIterateThenAddThenIterate() { | |
130 | int size = 1000; | |
131 | int[] fuzz = new int[size]; | |
132 | Random rng = new Random(10); | |
133 | for (int i = 0; i < size; i++) { | |
134 | fuzz[i] = rng.nextInt(1000); | |
135 | } | |
136 | String name = new Object() { | |
137 | }.getClass().getEnclosingMethod().getName(); | |
138 | assertNotNull(name); | |
139 | runIterateAddIterate(size, fuzz, name); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Test adding elements in a random order, this is an atypical degenerate | |
144 | * use case. | |
145 | */ | |
146 | @Test | |
147 | public void test5AddRandomOrder() { | |
148 | int size = 1000; | |
149 | int[] fuzz = new int[size]; | |
150 | Random rng = new Random(10); | |
151 | for (int i = 0; i < size; i++) { | |
b3cf0859 | 152 | fuzz[i] = Math.abs(rng.nextInt()); |
ab74ca14 MK |
153 | } |
154 | String name = new Object() { | |
155 | }.getClass().getEnclosingMethod().getName(); | |
156 | assertNotNull(name); | |
157 | runIterate(size, fuzz, name); | |
158 | } | |
159 | ||
160 | /** | |
161 | * Test adding elements in a random order then iterate over the list, this | |
162 | * is an atypical degenerate use case. | |
163 | */ | |
164 | @Test | |
165 | public void test6AddRandomOrderThenIterate() { | |
166 | int size = 1000; | |
167 | int[] fuzz = new int[size]; | |
168 | Random rng = new Random(10); | |
169 | for (int i = 0; i < size; i++) { | |
b3cf0859 | 170 | fuzz[i] = Math.abs(rng.nextInt()); |
ab74ca14 MK |
171 | } |
172 | String name = new Object() { | |
173 | }.getClass().getEnclosingMethod().getName(); | |
174 | assertNotNull(name); | |
175 | runIterate(size, fuzz, name); | |
176 | } | |
177 | ||
178 | /** | |
179 | * Test adding elements in a random order then iterate over the list then | |
180 | * add more then iterate again, this is an atypical degenerate use case. | |
181 | */ | |
182 | @Test | |
183 | public void test7AddRandomOrderThenIterateThenAddThenIterate() { | |
184 | int size = 1000; | |
185 | int[] fuzz = new int[size]; | |
186 | Random rng = new Random(10); | |
187 | for (int i = 0; i < size; i++) { | |
188 | fuzz[i] = rng.nextInt(1000); | |
189 | } | |
190 | String name = new Object() { | |
191 | }.getClass().getEnclosingMethod().getName(); | |
192 | assertNotNull(name); | |
193 | runIterateAddIterate(size, fuzz, name); | |
194 | } | |
195 | ||
196 | private void run(int size, int[] fuzz, String method) { | |
b3cf0859 GB |
197 | long duration = populate(size, fuzz, fSegStore); |
198 | outputResults(duration, method); | |
ab74ca14 MK |
199 | } |
200 | ||
201 | private static long populate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
202 | store.clear(); | |
203 | long start = System.nanoTime(); | |
204 | populate(size, fuzz, store, 1000000); | |
205 | long end = System.nanoTime(); | |
206 | return end - start; | |
207 | } | |
208 | ||
209 | private void runIterate(int size, int[] fuzz, String method) { | |
b3cf0859 | 210 | long duration = addAndIterate(size, fuzz, fSegStore); |
ab74ca14 | 211 | |
b3cf0859 | 212 | outputResults(duration, method); |
ab74ca14 MK |
213 | } |
214 | ||
215 | private static long addAndIterate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
216 | long start = System.nanoTime(); | |
217 | populate(size, fuzz, store); | |
218 | iterate(store); | |
219 | long end = System.nanoTime(); | |
220 | return end - start; | |
221 | } | |
222 | ||
223 | private void runIterateAddIterate(int size, int[] fuzz, String method) { | |
b3cf0859 GB |
224 | long duration = runIterateAddIterate(size, fuzz, fSegStore); |
225 | outputResults(duration, method); | |
ab74ca14 MK |
226 | } |
227 | ||
228 | private static long runIterateAddIterate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
229 | store.clear(); | |
230 | long start = System.nanoTime(); | |
231 | for (int i = 0; i < 50000; i++) { | |
232 | long startTime = i + fuzz[i % size]; | |
233 | store.add(new BasicSegment(startTime, startTime + 10)); | |
234 | } | |
235 | iterate(store); | |
236 | for (int i = 50000; i < 100000; i++) { | |
237 | long startTime = i + fuzz[i % size]; | |
238 | store.add(new BasicSegment(startTime, startTime + 10)); | |
239 | } | |
240 | iterate(store); | |
241 | long end = System.nanoTime(); | |
242 | return end - start; | |
243 | } | |
244 | ||
245 | private static Object iterate(ISegmentStore<@NonNull ISegment> store) { | |
246 | Object shutupCompilerWarnings = null; | |
247 | for (ISegment elem : store) { | |
248 | shutupCompilerWarnings = elem; | |
249 | } | |
250 | return shutupCompilerWarnings; | |
251 | } | |
252 | ||
253 | private static void populate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store, int count) { | |
254 | for (int i = 0; i < count; i++) { | |
b3cf0859 | 255 | long start = i + fuzz[i % size]; |
ab74ca14 MK |
256 | store.add(new BasicSegment(start, start + 10)); |
257 | } | |
258 | } | |
259 | ||
b3cf0859 GB |
260 | private void outputResults(long duration, String method) { |
261 | System.out.println(fName + ": Time taken for test " + method + ": " + FORMAT.format(duration)); | |
ab74ca14 MK |
262 | } |
263 | } |