1 /*******************************************************************************
2 * Copyright (c) 2013 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
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.tests
.trace
.indexer
;
15 import static org
.junit
.Assert
.assertEquals
;
18 import java
.io
.IOException
;
19 import java
.io
.RandomAccessFile
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Random
;
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
;
32 * A class to benchmark different algoritms for storing the
33 * checkpoint index on disk
35 * @author Marc-Andre Laperle
37 public class AllBench
{
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");
44 static int BTREE_DEGREE
= 10;
46 private void setUp() {
47 fTrace
= new TmfTraceStub();
53 private void tearDown() {
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
);
72 for (int i
= 0; i
< checkpointsNums
; i
++) {
73 list
.add(f
.readInt());
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
);
84 System
.out
.println("DEGREE: " + BTREE_DEGREE
);
86 AllBench b
= new AllBench();
88 for (int i
= 0; i
< checkpointsNums
.length
; i
++) {
89 ArrayList
<Integer
> list
= new ArrayList
<>();
90 generateDataFile(list
, checkpointsNums
[i
]);
93 System
.out
.println("*** " + checkpointsNums
[i
] + " checkpoints ***\n");
100 private void benchIt(ArrayList
<Integer
> list
) {
102 System
.out
.println("Testing BTree\n");
104 testInsertAlot(list
);
106 System
.out
.println("Testing Array\n");
108 testInsertAlotArray(list
);
111 private void testInsertAlot(ArrayList
<Integer
> list2
) {
112 int checkpointsNum
= list2
.size();
114 writeCheckpoints(checkpointsNum
);
116 ArrayList
<Integer
> list
= new ArrayList
<>();
117 for (int i
= 0; i
< checkpointsNum
; i
++) {
121 readCheckpoints(checkpointsNum
, list
, false);
122 readCheckpoints(checkpointsNum
, list2
, true);
126 System
.out
.println();
129 private void testInsertAlotArray(ArrayList
<Integer
> list2
) {
130 int checkpointsNum
= list2
.size();
132 writeCheckpointsArray(checkpointsNum
);
134 ArrayList
<Integer
> list
= new ArrayList
<>();
135 for (int i
= 0; i
< checkpointsNum
; i
++) {
139 readCheckpointsArray(checkpointsNum
, list
, false);
140 readCheckpointsArray(checkpointsNum
, list2
, true);
144 System
.out
.println();
147 private void writeCheckpoints(int checkpointsNum
) {
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
);
159 time
+= (System
.currentTimeMillis() - old
);
160 bTree
.setIndexComplete();
162 if (j
!= REPEAT
- 1) {
165 if (reportProgress
) {
166 System
.out
.print(".");
170 System
.out
.println("Write time average: " + (float) time
/ REPEAT
);
173 private void writeCheckpointsArray(int checkpointsNum
) {
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
);
185 time
+= (System
.currentTimeMillis() - old
);
186 array
.setIndexComplete();
188 if (j
!= REPEAT
- 1) {
191 if (reportProgress
) {
192 System
.out
.print(".");
196 System
.out
.println("Write time average: " + (float) time
/ REPEAT
);
200 private void readCheckpoints(int checkpointsNum
, ArrayList
<Integer
> list
, boolean random
) {
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());
215 time
+= (System
.currentTimeMillis() - old
);
216 cacheMisses
= bTree
.getCacheMisses();
218 if (reportProgress
) {
219 System
.out
.print(".");
223 System
.out
.println("Read " + (random ?
"(random)" : "(linear)") + "time average: " + (float) time
/ REPEAT
+ " (cache miss: " + cacheMisses
+ ")");
226 private void readCheckpointsArray(int checkpointsNum
, ArrayList
<Integer
> list
, boolean random
) {
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
);
240 time
+= (System
.currentTimeMillis() - old
);
241 cacheMisses
= array
.getCacheMisses();
243 if (reportProgress
) {
244 System
.out
.print(".");
248 System
.out
.println("Read " + (random ?
"(random)" : "(linear)") + "time average: " + (float) time
/ REPEAT
+ " (cache miss: " + cacheMisses
+ ")");