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 | ||
14 | import java.util.Random; | |
15 | ||
16 | import org.eclipse.jdt.annotation.NonNull; | |
17 | import org.eclipse.tracecompass.internal.analysis.timing.core.store.ArrayListStore; | |
18 | import org.eclipse.tracecompass.internal.analysis.timing.core.store.LazyArrayListStore; | |
19 | import org.eclipse.tracecompass.segmentstore.core.BasicSegment; | |
20 | import org.eclipse.tracecompass.segmentstore.core.ISegment; | |
21 | import org.eclipse.tracecompass.segmentstore.core.ISegmentStore; | |
22 | import org.eclipse.tracecompass.segmentstore.core.treemap.TreeMapStore; | |
23 | import org.junit.FixMethodOrder; | |
24 | import org.junit.Test; | |
25 | import org.junit.runners.MethodSorters; | |
26 | ||
27 | /** | |
28 | * Segmentstore benchmarks, tests the performance for loads and reads. | |
29 | * | |
30 | * NOTE : Do not add this to isTracecompassFastYet, it is not information that | |
31 | * is interesting for users, it is for developpers. | |
32 | * | |
33 | * @category benchmark | |
34 | * | |
35 | * @author Matthew Khouzam | |
36 | * | |
37 | */ | |
38 | @FixMethodOrder(MethodSorters.NAME_ASCENDING) | |
39 | public class SegmentStoreBenchmark { | |
40 | ||
41 | private ISegmentStore<@NonNull ISegment> fALS = new ArrayListStore<>(); | |
42 | private ISegmentStore<@NonNull ISegment> fLALS = new LazyArrayListStore<>(); | |
43 | private ISegmentStore<@NonNull ISegment> fTMS = new TreeMapStore<>(); | |
44 | ||
45 | /** | |
46 | * Add elements in order | |
47 | */ | |
48 | @Test | |
49 | public void test1AddInOrder() { | |
50 | int size = 1; | |
51 | int[] fuzz = { 0 }; | |
52 | run(size, fuzz, new Object() { | |
53 | }.getClass().getEnclosingMethod().getName()); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Add elements almost in order, this represents a typical degenerate use | |
58 | * case. | |
59 | */ | |
60 | @Test | |
61 | public void test2AddFuzzyOrder() { | |
62 | int size = 1000; | |
63 | int[] fuzz = new int[size]; | |
64 | Random rng = new Random(10); | |
65 | for (int i = 0; i < size; i++) { | |
66 | fuzz[i] = rng.nextInt(1000); | |
67 | } | |
68 | String name = new Object() { | |
69 | }.getClass().getEnclosingMethod().getName(); | |
70 | assertNotNull(name); | |
71 | run(size, fuzz, name); | |
72 | } | |
73 | ||
74 | /** | |
75 | * Add elements almost in order, this represents a typical degenerate use | |
76 | * case, then iterate over the list. | |
77 | */ | |
78 | @Test | |
79 | public void test3AddFuzzyOrderThenIterate() { | |
80 | int size = 1000; | |
81 | int[] fuzz = new int[size]; | |
82 | Random rng = new Random(10); | |
83 | for (int i = 0; i < size; i++) { | |
84 | fuzz[i] = rng.nextInt(1000); | |
85 | } | |
86 | String name = new Object() { | |
87 | }.getClass().getEnclosingMethod().getName(); | |
88 | assertNotNull(name); | |
89 | runIterate(size, fuzz, name); | |
90 | } | |
91 | ||
92 | /** | |
93 | * Add elements almost in order, this represents a typical degenerate use | |
94 | * case, and iterate while building then when done. | |
95 | */ | |
96 | @Test | |
97 | public void test4AddFuzzyOrderThenIterateThenAddThenIterate() { | |
98 | int size = 1000; | |
99 | int[] fuzz = new int[size]; | |
100 | Random rng = new Random(10); | |
101 | for (int i = 0; i < size; i++) { | |
102 | fuzz[i] = rng.nextInt(1000); | |
103 | } | |
104 | String name = new Object() { | |
105 | }.getClass().getEnclosingMethod().getName(); | |
106 | assertNotNull(name); | |
107 | runIterateAddIterate(size, fuzz, name); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Test adding elements in a random order, this is an atypical degenerate | |
112 | * use case. | |
113 | */ | |
114 | @Test | |
115 | public void test5AddRandomOrder() { | |
116 | int size = 1000; | |
117 | int[] fuzz = new int[size]; | |
118 | Random rng = new Random(10); | |
119 | for (int i = 0; i < size; i++) { | |
120 | fuzz[i] = rng.nextInt(); | |
121 | } | |
122 | String name = new Object() { | |
123 | }.getClass().getEnclosingMethod().getName(); | |
124 | assertNotNull(name); | |
125 | runIterate(size, fuzz, name); | |
126 | } | |
127 | ||
128 | /** | |
129 | * Test adding elements in a random order then iterate over the list, this | |
130 | * is an atypical degenerate use case. | |
131 | */ | |
132 | @Test | |
133 | public void test6AddRandomOrderThenIterate() { | |
134 | int size = 1000; | |
135 | int[] fuzz = new int[size]; | |
136 | Random rng = new Random(10); | |
137 | for (int i = 0; i < size; i++) { | |
138 | fuzz[i] = rng.nextInt(); | |
139 | } | |
140 | String name = new Object() { | |
141 | }.getClass().getEnclosingMethod().getName(); | |
142 | assertNotNull(name); | |
143 | runIterate(size, fuzz, name); | |
144 | } | |
145 | ||
146 | /** | |
147 | * Test adding elements in a random order then iterate over the list then | |
148 | * add more then iterate again, this is an atypical degenerate use case. | |
149 | */ | |
150 | @Test | |
151 | public void test7AddRandomOrderThenIterateThenAddThenIterate() { | |
152 | int size = 1000; | |
153 | int[] fuzz = new int[size]; | |
154 | Random rng = new Random(10); | |
155 | for (int i = 0; i < size; i++) { | |
156 | fuzz[i] = rng.nextInt(1000); | |
157 | } | |
158 | String name = new Object() { | |
159 | }.getClass().getEnclosingMethod().getName(); | |
160 | assertNotNull(name); | |
161 | runIterateAddIterate(size, fuzz, name); | |
162 | } | |
163 | ||
164 | private void run(int size, int[] fuzz, String method) { | |
165 | long durationA = populate(size, fuzz, fALS); | |
166 | long durationL = populate(size, fuzz, fLALS); | |
167 | long durationT = populate(size, fuzz, fTMS); | |
168 | outputResults(durationA, durationL, durationT, method); | |
169 | } | |
170 | ||
171 | private static long populate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
172 | store.clear(); | |
173 | long start = System.nanoTime(); | |
174 | populate(size, fuzz, store, 1000000); | |
175 | long end = System.nanoTime(); | |
176 | return end - start; | |
177 | } | |
178 | ||
179 | private void runIterate(int size, int[] fuzz, String method) { | |
180 | long durationA = addAndIterate(size, fuzz, fALS); | |
181 | long durationL = addAndIterate(size, fuzz, fLALS); | |
182 | long durationT = addAndIterate(size, fuzz, fTMS); | |
183 | ||
184 | outputResults(durationA, durationL, durationT, method); | |
185 | } | |
186 | ||
187 | private static long addAndIterate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
188 | long start = System.nanoTime(); | |
189 | populate(size, fuzz, store); | |
190 | iterate(store); | |
191 | long end = System.nanoTime(); | |
192 | return end - start; | |
193 | } | |
194 | ||
195 | private void runIterateAddIterate(int size, int[] fuzz, String method) { | |
196 | long durationA = runIterateAddIterate(size, fuzz, fALS); | |
197 | long durationL = runIterateAddIterate(size, fuzz, fLALS); | |
198 | long durationT = runIterateAddIterate(size, fuzz, fTMS); | |
199 | outputResults(durationA, durationL, durationT, method); | |
200 | } | |
201 | ||
202 | private static long runIterateAddIterate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store) { | |
203 | store.clear(); | |
204 | long start = System.nanoTime(); | |
205 | for (int i = 0; i < 50000; i++) { | |
206 | long startTime = i + fuzz[i % size]; | |
207 | store.add(new BasicSegment(startTime, startTime + 10)); | |
208 | } | |
209 | iterate(store); | |
210 | for (int i = 50000; i < 100000; i++) { | |
211 | long startTime = i + fuzz[i % size]; | |
212 | store.add(new BasicSegment(startTime, startTime + 10)); | |
213 | } | |
214 | iterate(store); | |
215 | long end = System.nanoTime(); | |
216 | return end - start; | |
217 | } | |
218 | ||
219 | private static Object iterate(ISegmentStore<@NonNull ISegment> store) { | |
220 | Object shutupCompilerWarnings = null; | |
221 | for (ISegment elem : store) { | |
222 | shutupCompilerWarnings = elem; | |
223 | } | |
224 | return shutupCompilerWarnings; | |
225 | } | |
226 | ||
227 | private static void populate(int size, int[] fuzz, ISegmentStore<@NonNull ISegment> store, int count) { | |
228 | for (int i = 0; i < count; i++) { | |
229 | int start = i + fuzz[i % size]; | |
230 | store.add(new BasicSegment(start, start + 10)); | |
231 | } | |
232 | } | |
233 | ||
234 | private static void outputResults(long durationA, long durationL, long durationT, String method) { | |
235 | System.out.println("Time taken for test " + method + "\n ArrayList " + String.format("%12d", durationA) + "\n LazyArrayList " + String.format("%12d", durationL) + "\n TreeMapStore " + String.format("%12d", durationT)); | |
236 | } | |
237 | } |