24aa14253df57bf1406035c1854fb85357355b43
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core.tests / src / org / eclipse / tracecompass / tmf / core / tests / trace / indexer / AllBench.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 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.tracecompass.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.tracecompass.internal.tmf.core.trace.indexer.BTree;
24 import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.BTreeCheckpointVisitor;
25 import org.eclipse.tracecompass.internal.tmf.core.trace.indexer.FlatArray;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
28 import org.eclipse.tracecompass.tmf.core.trace.location.TmfLongLocation;
29 import org.eclipse.tracecompass.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.dispose();
161 if (j != REPEAT - 1) {
162 file.delete();
163 }
164 if (reportProgress) {
165 System.out.print(".");
166 }
167 }
168
169 System.out.println("Write time average: " + (float) time / REPEAT);
170 }
171
172 private void writeCheckpointsArray(int checkpointsNum) {
173 FlatArray array;
174 int REPEAT = 10;
175 long time = 0;
176 for (int j = 0; j < REPEAT; j++) {
177 long old = System.currentTimeMillis();
178 array = new FlatArray(file, fTrace);
179 for (int i = 0; i < checkpointsNum; i++) {
180 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + i), new TmfLongLocation(123456L + i), i);
181 array.insert(checkpoint);
182 }
183
184 time += (System.currentTimeMillis() - old);
185 array.dispose();
186 if (j != REPEAT - 1) {
187 file.delete();
188 }
189 if (reportProgress) {
190 System.out.print(".");
191 }
192 }
193
194 System.out.println("Write time average: " + (float) time / REPEAT);
195 }
196
197
198 private void readCheckpoints(int checkpointsNum, ArrayList<Integer> list, boolean random) {
199 BTree bTree;
200 int REPEAT = 10;
201 long time = 0;
202 long cacheMisses = 0;
203 for (int j = 0; j < REPEAT; j++) {
204 long old = System.currentTimeMillis();
205 bTree = new BTree(BTREE_DEGREE, file, fTrace);
206 for (int i = 0; i < checkpointsNum; i++) {
207 Integer randomCheckpoint = list.get(i);
208 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint), new TmfLongLocation(123456L + randomCheckpoint), 0);
209 BTreeCheckpointVisitor treeVisitor = new BTreeCheckpointVisitor(checkpoint);
210 bTree.accept(treeVisitor);
211 assertEquals(randomCheckpoint.intValue(), treeVisitor.getCheckpoint().getCheckpointRank());
212 }
213 time += (System.currentTimeMillis() - old);
214 cacheMisses = bTree.getCacheMisses();
215 bTree.dispose();
216 if (reportProgress) {
217 System.out.print(".");
218 }
219 }
220
221 System.out.println("Read " + (random ? "(random)" : "(linear)") + "time average: " + (float) time / REPEAT + " (cache miss: " + cacheMisses + ")");
222 }
223
224 private void readCheckpointsArray(int checkpointsNum, ArrayList<Integer> list, boolean random) {
225 FlatArray array;
226 int REPEAT = 10;
227 long time = 0;
228 long cacheMisses = 0;
229 for (int j = 0; j < REPEAT; j++) {
230 long old = System.currentTimeMillis();
231 array = new FlatArray(file, fTrace);
232 for (int i = 0; i < checkpointsNum; i++) {
233 Integer randomCheckpoint = list.get(i);
234 TmfCheckpoint checkpoint = new TmfCheckpoint(new TmfTimestamp(12345 + randomCheckpoint), new TmfLongLocation(123456L + randomCheckpoint), 0);
235 long found = array.binarySearch(checkpoint);
236 assertEquals(randomCheckpoint.intValue(), found);
237 }
238 time += (System.currentTimeMillis() - old);
239 cacheMisses = array.getCacheMisses();
240 array.dispose();
241 if (reportProgress) {
242 System.out.print(".");
243 }
244 }
245
246 System.out.println("Read " + (random ? "(random)" : "(linear)") + "time average: " + (float) time / REPEAT + " (cache miss: " + cacheMisses + ")");
247 }
248
249 }
This page took 0.07853 seconds and 4 git commands to generate.