Commit | Line | Data |
---|---|---|
7db66c58 AM |
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.linuxtools.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.linuxtools.ctf.core.event.EventDefinition; | |
24 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; | |
25 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
26 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
27 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
28 | import org.eclipse.test.performance.Dimension; | |
29 | import org.eclipse.test.performance.Performance; | |
30 | import org.eclipse.test.performance.PerformanceMeter; | |
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. | |
36 | * <br> | |
37 | * This test runs in 3 passes. | |
38 | * <ul> | |
39 | * <li>first it opens a trace</li> | |
40 | * <li>then it reads the trace completely</li> | |
41 | * <li>then it randomly (seeded) seeks NB_SEEKS locations in the trace and reads one | |
42 | * event at each position.</li> | |
43 | * </ul> | |
44 | * | |
45 | * @author Matthew Khouzam | |
46 | * @author Alexandre Montplaisir | |
47 | */ | |
48 | public class TraceSeekBenchmark { | |
49 | ||
50 | private static final Random RND = new Random(1000); | |
51 | ||
52 | private static final int LOOP_COUNT = 25; | |
53 | private static final int NB_SEEKS = 500; | |
54 | private static final String TEST_SUITE_NAME = "CTF Read & Seek Benchmark (" + NB_SEEKS + " seeks)"; | |
55 | private static final String TEST_ID = "org.eclipse.linuxtools#" + TEST_SUITE_NAME; | |
56 | ||
57 | /** | |
58 | * Run the benchmark scenario for the trace "kernel" | |
59 | */ | |
60 | @Test | |
61 | public void testKernelTrace() { | |
62 | readAndSeekTrace(CtfTestTrace.KERNEL, "trace-kernel", true); | |
63 | } | |
64 | ||
65 | private static void readAndSeekTrace(CtfTestTrace testTrace, String testName, boolean inGlobalSummary) { | |
66 | assumeTrue(testTrace.exists()); | |
67 | ||
68 | Performance perf = Performance.getDefault(); | |
69 | PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + '#' + testName); | |
70 | perf.tagAsSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME); | |
71 | ||
72 | if (inGlobalSummary) { | |
73 | perf.tagAsGlobalSummary(pm, TEST_SUITE_NAME + ':' + testName, Dimension.CPU_TIME); | |
74 | } | |
75 | ||
76 | for (int loop = 0; loop < LOOP_COUNT; loop++) { | |
77 | try (CTFTrace trace = testTrace.getTrace(); | |
78 | CTFTraceReader traceReader = new CTFTraceReader(trace);) { | |
79 | ||
80 | /* Read the whole trace to find out the start and end times */ | |
81 | EventDefinition firstEvent = traceReader.getCurrentEventDef(); | |
82 | final long startTime = firstEvent.getTimestamp(); | |
83 | long endTime = startTime; | |
84 | while (traceReader.hasMoreEvents()) { | |
85 | EventDefinition ev = traceReader.getCurrentEventDef(); | |
86 | endTime = ev.getTimestamp(); | |
87 | traceReader.advance(); | |
88 | } | |
89 | ||
90 | /* Generate the timestamps we will seek to */ | |
91 | List<Long> seekTimestamps = new LinkedList<>(); | |
92 | final long range = endTime - startTime; | |
93 | for (int i = 0; i < NB_SEEKS; i++) { | |
94 | seekTimestamps.add(startTime + (RND.nextLong() % range)); | |
95 | } | |
96 | ||
97 | /* Benchmark seeking to the generated timestamps */ | |
98 | pm.start(); | |
99 | for (Long ts : seekTimestamps) { | |
100 | traceReader.seek(ts); | |
101 | traceReader.advance(); | |
102 | } | |
103 | pm.stop(); | |
104 | ||
105 | } catch (CTFReaderException e) { | |
106 | /* Should not happen if assumeTrue() passed above */ | |
107 | fail("Test failed at iteration " + loop + ':' + e.getMessage()); | |
108 | } | |
109 | } | |
110 | pm.commit(); | |
111 | } | |
112 | } |