1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.timing
.core
.tests
.store
;
12 import static org
.junit
.Assert
.assertNotNull
;
14 import java
.util
.Random
;
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
;
28 * Segmentstore benchmarks, tests the performance for loads and reads.
30 * NOTE : Do not add this to isTracecompassFastYet, it is not information that
31 * is interesting for users, it is for developpers.
35 * @author Matthew Khouzam
38 @FixMethodOrder(MethodSorters
.NAME_ASCENDING
)
39 public class SegmentStoreBenchmark
{
41 private ISegmentStore
<@NonNull ISegment
> fALS
= new ArrayListStore
<>();
42 private ISegmentStore
<@NonNull ISegment
> fLALS
= new LazyArrayListStore
<>();
43 private ISegmentStore
<@NonNull ISegment
> fTMS
= new TreeMapStore
<>();
46 * Add elements in order
49 public void test1AddInOrder() {
52 run(size
, fuzz
, new Object() {
53 }.getClass().getEnclosingMethod().getName());
57 * Add elements almost in order, this represents a typical degenerate use
61 public void test2AddFuzzyOrder() {
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);
68 String name
= new Object() {
69 }.getClass().getEnclosingMethod().getName();
71 run(size
, fuzz
, name
);
75 * Add elements almost in order, this represents a typical degenerate use
76 * case, then iterate over the list.
79 public void test3AddFuzzyOrderThenIterate() {
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);
86 String name
= new Object() {
87 }.getClass().getEnclosingMethod().getName();
89 runIterate(size
, fuzz
, name
);
93 * Add elements almost in order, this represents a typical degenerate use
94 * case, and iterate while building then when done.
97 public void test4AddFuzzyOrderThenIterateThenAddThenIterate() {
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);
104 String name
= new Object() {
105 }.getClass().getEnclosingMethod().getName();
107 runIterateAddIterate(size
, fuzz
, name
);
111 * Test adding elements in a random order, this is an atypical degenerate
115 public void test5AddRandomOrder() {
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();
122 String name
= new Object() {
123 }.getClass().getEnclosingMethod().getName();
125 runIterate(size
, fuzz
, name
);
129 * Test adding elements in a random order then iterate over the list, this
130 * is an atypical degenerate use case.
133 public void test6AddRandomOrderThenIterate() {
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();
140 String name
= new Object() {
141 }.getClass().getEnclosingMethod().getName();
143 runIterate(size
, fuzz
, name
);
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.
151 public void test7AddRandomOrderThenIterateThenAddThenIterate() {
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);
158 String name
= new Object() {
159 }.getClass().getEnclosingMethod().getName();
161 runIterateAddIterate(size
, fuzz
, name
);
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
);
171 private static long populate(int size
, int[] fuzz
, ISegmentStore
<@NonNull ISegment
> store
) {
173 long start
= System
.nanoTime();
174 populate(size
, fuzz
, store
, 1000000);
175 long end
= System
.nanoTime();
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
);
184 outputResults(durationA
, durationL
, durationT
, method
);
187 private static long addAndIterate(int size
, int[] fuzz
, ISegmentStore
<@NonNull ISegment
> store
) {
188 long start
= System
.nanoTime();
189 populate(size
, fuzz
, store
);
191 long end
= System
.nanoTime();
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
);
202 private static long runIterateAddIterate(int size
, int[] fuzz
, ISegmentStore
<@NonNull ISegment
> store
) {
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));
210 for (int i
= 50000; i
< 100000; i
++) {
211 long startTime
= i
+ fuzz
[i
% size
];
212 store
.add(new BasicSegment(startTime
, startTime
+ 10));
215 long end
= System
.nanoTime();
219 private static Object
iterate(ISegmentStore
<@NonNull ISegment
> store
) {
220 Object shutupCompilerWarnings
= null;
221 for (ISegment elem
: store
) {
222 shutupCompilerWarnings
= elem
;
224 return shutupCompilerWarnings
;
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));
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
));