ctf: rename CTFReaderException to CTFException
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core.tests / perf / org / eclipse / tracecompass / ctf / core / tests / perf / trace / TraceSeekBenchmark.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 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 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Convert to a org.eclipse.test.performance test
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.ctf.core.tests.perf.trace;
15
16 import static org.junit.Assert.fail;
17 import static org.junit.Assume.assumeTrue;
18
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Random;
22
23 import org.eclipse.test.performance.Dimension;
24 import org.eclipse.test.performance.Performance;
25 import org.eclipse.test.performance.PerformanceMeter;
26 import org.eclipse.tracecompass.ctf.core.CTFException;
27 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
28 import org.eclipse.tracecompass.ctf.core.tests.shared.CtfTestTrace;
29 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
30 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
31 import org.junit.Test;
32
33 /**
34 * Tests for performance regressions of the ctf reader. It only tests the ctf
35 * reader, not tmf. <br>
36 * This test runs in 3 passes.
37 * <ul>
38 * <li>first it opens a trace</li>
39 * <li>then it reads the trace completely</li>
40 * <li>then it randomly (seeded) seeks NB_SEEKS locations in the trace and reads
41 * one event at each position.</li>
42 * </ul>
43 *
44 * @author Matthew Khouzam
45 * @author Alexandre Montplaisir
46 */
47 public class TraceSeekBenchmark {
48
49 private static final Random RND = new Random(1000);
50
51 private static final int LOOP_COUNT = 25;
52 private static final int NB_SEEKS = 500;
53 private static final String TEST_SUITE_NAME = "CTF Read & Seek Benchmark (" + NB_SEEKS + " seeks)";
54 private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME;
55
56 /**
57 * Run the benchmark scenario for the trace "kernel"
58 *
59 * @throws CTFException
60 * Should not happen
61 */
62 @Test
63 public void testKernelTrace() throws CTFException {
64 readAndSeekTrace(CtfTestTrace.KERNEL, "trace-kernel", true);
65 }
66
67 private static void readAndSeekTrace(CtfTestTrace testTrace, String testName, boolean inGlobalSummary) throws CTFException {
68 assumeTrue(testTrace.exists());
69
70 Performance perf = Performance.getDefault();
71 PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName);
72 perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
73
74 if (inGlobalSummary) {
75 perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME);
76 }
77
78 for (int loop = 0; loop < LOOP_COUNT; loop++) {
79 CTFTrace trace = testTrace.getTrace();
80 try (CTFTraceReader traceReader = new CTFTraceReader(trace);) {
81
82 /* Read the whole trace to find out the start and end times */
83 EventDefinition firstEvent = traceReader.getCurrentEventDef();
84 final long startTime = firstEvent.getTimestamp();
85 long endTime = startTime;
86 while (traceReader.hasMoreEvents()) {
87 EventDefinition ev = traceReader.getCurrentEventDef();
88 endTime = ev.getTimestamp();
89 traceReader.advance();
90 }
91
92 /* Generate the timestamps we will seek to */
93 List<Long> seekTimestamps = new LinkedList<>();
94 final long range = endTime - startTime;
95 for (int i = 0; i < NB_SEEKS; i++) {
96 long delta = (RND.nextLong() % range);
97 if (delta < 0) {
98 delta += range;
99 }
100 seekTimestamps.add(startTime + delta);
101 }
102
103 /* Benchmark seeking to the generated timestamps */
104 pm.start();
105 for (Long ts : seekTimestamps) {
106 traceReader.seek(ts);
107 traceReader.advance();
108 }
109 pm.stop();
110
111 } catch (CTFException e) {
112 /* Should not happen if assumeTrue() passed above */
113 fail("Test failed at iteration " + loop + ':' + e.getMessage());
114 }
115 }
116 pm.commit();
117 }
118 }
This page took 0.034579 seconds and 6 git commands to generate.