tmf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / trace / indexer / AllBench.java
1 /*******************************************************************************
2 * Copyright (c) 2013 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 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.tests.trace.indexer;
14
15 import static org.junit.Assert.assertEquals;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.RandomAccessFile;
20 import java.util.ArrayList;
21 import java.util.Random;
22
23 import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.BTree;
24 import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.BTreeCheckpointVisitor;
25 import org.eclipse.linuxtools.internal.tmf.core.trace.indexer.FlatArray;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
28 import org.eclipse.linuxtools.tmf.core.trace.location.TmfLongLocation;
29 import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
30
31 /**
32 * A class to benchmark different algoritms for storing the
33 * checkpoint index on disk
34 *
35 * @author Marc-Andre Laperle
36 */
37 public class AllBench {
38
39 private static final boolean reportProgress = true;
40 private static ArrayList<ArrayList<Integer>> nums;
41 private TmfTraceStub fTrace;
42 private File file = new File("index.idx");
43
44 static int BTREE_DEGREE = 10;
45
46 private void setUp() {
47 fTrace = new TmfTraceStub();
48 if (file.exists()) {
49 file.delete();
50 }
51 }
52
53 private void tearDown() {
54 fTrace.dispose();
55 fTrace = null;
56 if (file.exists()) {
57 file.delete();
58 }
59 }
60
61 private static void generateDataFile(ArrayList<Integer> list, int checkpointsNums) throws IOException {
62 File randomDataFile = new File("data" + checkpointsNums);
63 try (RandomAccessFile f = new RandomAccessFile(randomDataFile, "rw");) {
64 if (randomDataFile.exists()) {
65 for (int i = 0; i < checkpointsNums; i++) {
66 Random rand = new Random();
67 int nextInt = rand.nextInt(checkpointsNums);
68 list.add(nextInt);
69 f.writeInt(nextInt);
70 }
71 } else {
72 for (int i = 0; i < checkpointsNums; i++) {
73 list.add(f.readInt());
74 }
75 }
76 }
77 }
78
79 @SuppressWarnings("javadoc")
80 public static void main(String[] args) throws IOException {
81 int checkpointsNums [] = new int [] { 5000, 50000, 500000, 1000000 };
82 nums = new ArrayList<>(checkpointsNums.length);
83
84 System.out.println("DEGREE: " + BTREE_DEGREE);
85
86 AllBench b = new AllBench();
87 b.setUp();
88 for (int i = 0; i < checkpointsNums.length; i++) {
89 ArrayList<Integer> list = new ArrayList<>();
90 generateDataFile(list, checkpointsNums[i]);
91 nums.add(list);
92
93 System.out.println("*** " + checkpointsNums[i] + " checkpoints ***\n");
94
95 b.benchIt(list);
96 }
97 b.tearDown();
98 }
99
100 private void benchIt(ArrayList<Integer> list) {
101
102 System.out.println("Testing BTree\n");
103
104 testInsertAlot(list);
105
106 System.out.println("Testing Array\n");
107
108 testInsertAlotArray(list);
109 }
110
111 private void testInsertAlot(ArrayList<Integer> list2) {
112 int checkpointsNum = list2.size();
113
114 writeCheckpoints(checkpointsNum);
115
116 ArrayList<Integer> list = new ArrayList<>();
117 for (int i = 0; i < checkpointsNum; i++) {
118 list.add(i);
119 }
120
121 readCheckpoints(checkpointsNum, list, false);
122 readCheckpoints(checkpointsNum, list2, true);
123
124 file.delete();
125
126 System.out.println();
127 }
128
129 private void testInsertAlotArray(ArrayList<Integer> list2) {
130 int checkpointsNum = list2.size();
131
132 writeCheckpointsArray(checkpointsNum);
133
134 ArrayList<Integer> list = new ArrayList<>();
135 for (int i = 0; i < checkpointsNum; i++) {
136 list.add(i);
137 }
138
139 readCheckpointsArray(checkpointsNum, list, false);
140 readCheckpointsArray(checkpointsNum, list2, true);
141
142 file.delete();
143
144 System.out.println();
145 }
146
147 private void writeCheckpoints(int checkpointsNum) {
148 BTree bTree;
149 int REPEAT = 10;
150 long time = 0;
151 for (int j = 0; j < REPEAT; j++) {
152 long old = System.currentTimeMillis();
153 bTree = new BTree(BTREE_DEGREE, file, fTrace);
154 for (int i = 0; i < checkpointsNum; i++) {
155 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + i), new TmfLongLocation(123456L + i), i);
156 bTree.insert(checkpoint);
157 }
158
159 time += (System.currentTimeMillis() - old);
160 bTree.setIndexComplete();
161 bTree.dispose();
162 if (j != REPEAT - 1) {
163 file.delete();
164 }
165 if (reportProgress) {
166 System.out.print(".");
167 }
168 }
169
170 System.out.println("Write time average: " + (float) time / REPEAT);
171 }
172
173 private void writeCheckpointsArray(int checkpointsNum) {
174 FlatArray array;
175 int REPEAT = 10;
176 long time = 0;
177 for (int j = 0; j < REPEAT; j++) {
178 long old = System.currentTimeMillis();
179 array = new FlatArray(file, fTrace);
180 for (int i = 0; i < checkpointsNum; i++) {
181 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + i), new TmfLongLocation(123456L + i), i);
182 array.insert(checkpoint);
183 }
184
185 time += (System.currentTimeMillis() - old);
186 array.setIndexComplete();
187 array.dispose();
188 if (j != REPEAT - 1) {
189 file.delete();
190 }
191 if (reportProgress) {
192 System.out.print(".");
193 }
194 }
195
196 System.out.println("Write time average: " + (float) time / REPEAT);
197 }
198
199
200 private void readCheckpoints(int checkpointsNum, ArrayList<Integer> list, boolean random) {
201 BTree bTree;
202 int REPEAT = 10;
203 long time = 0;
204 long cacheMisses = 0;
205 for (int j = 0; j < REPEAT; j++) {
206 long old = System.currentTimeMillis();
207 bTree = new BTree(BTREE_DEGREE, file, fTrace);
208 for (int i = 0; i < checkpointsNum; i++) {
209 Integer randomCheckpoint = list.get(i);
210 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint), new TmfLongLocation(123456L + randomCheckpoint), 0);
211 BTreeCheckpointVisitor treeVisitor = new BTreeCheckpointVisitor(checkpoint);
212 bTree.accept(treeVisitor);
213 assertEquals(randomCheckpoint.intValue(), treeVisitor.getCheckpoint().getCheckpointRank());
214 }
215 time += (System.currentTimeMillis() - old);
216 cacheMisses = bTree.getCacheMisses();
217 bTree.dispose();
218 if (reportProgress) {
219 System.out.print(".");
220 }
221 }
222
223 System.out.println("Read " + (random ? "(random)" : "(linear)") + "time average: " + (float) time / REPEAT + " (cache miss: " + cacheMisses + ")");
224 }
225
226 private void readCheckpointsArray(int checkpointsNum, ArrayList<Integer> list, boolean random) {
227 FlatArray array;
228 int REPEAT = 10;
229 long time = 0;
230 long cacheMisses = 0;
231 for (int j = 0; j < REPEAT; j++) {
232 long old = System.currentTimeMillis();
233 array = new FlatArray(file, fTrace);
234 for (int i = 0; i < checkpointsNum; i++) {
235 Integer randomCheckpoint = list.get(i);
236 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint), new TmfLongLocation(123456L + randomCheckpoint), 0);
237 long found = array.binarySearch(checkpoint);
238 assertEquals(randomCheckpoint.intValue(), found);
239 }
240 time += (System.currentTimeMillis() - old);
241 cacheMisses = array.getCacheMisses();
242 array.dispose();
243 if (reportProgress) {
244 System.out.print(".");
245 }
246 }
247
248 System.out.println("Read " + (random ? "(random)" : "(linear)") + "time average: " + (float) time / REPEAT + " (cache miss: " + cacheMisses + ")");
249 }
250
251 }
This page took 0.036278 seconds and 5 git commands to generate.