Commit | Line | Data |
---|---|---|
43d15218 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are made | |
5 | * 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 | |
088b4f19 | 11 | * Geneviève Bastien - Convert to JUnit performance test |
43d15218 MK |
12 | *******************************************************************************/ |
13 | ||
088b4f19 | 14 | package org.eclipse.linuxtools.tmf.ctf.core.tests.perf.experiment; |
43d15218 MK |
15 | |
16 | import java.io.File; | |
17 | import java.util.Arrays; | |
18 | import java.util.HashSet; | |
19 | import java.util.List; | |
20 | import java.util.Set; | |
21 | ||
22 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; | |
23 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
24 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
25 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
26 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType; | |
27 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
29 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace; | |
30 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; | |
088b4f19 GB |
31 | import org.eclipse.test.performance.Dimension; |
32 | import org.eclipse.test.performance.Performance; | |
33 | import org.eclipse.test.performance.PerformanceMeter; | |
34 | import org.junit.Test; | |
43d15218 MK |
35 | |
36 | /** | |
37 | * Coalescing benchmark | |
38 | * | |
39 | * @author Matthew Khouzam | |
43d15218 MK |
40 | */ |
41 | public class ExperimentBenchmark { | |
088b4f19 | 42 | |
b09d20dc | 43 | private static final String TEST_ID = "org.eclipse.linuxtools#Experiment benchmark#"; |
43d15218 MK |
44 | private static final int MAX_TRACES = 160; |
45 | private static final int BLOCK_SIZE = 100; | |
46 | private static final String TRACES_ROOT_PATH = CtfTestTrace.TRACE_EXPERIMENT.getPath(); | |
f14c2f97 MAL |
47 | private static final int SAMPLE_SIZE_SLOW = 20; |
48 | private static final int SAMPLE_SIZE = 100; | |
43d15218 | 49 | |
088b4f19 | 50 | private TmfExperimentStub fExperiment; |
43d15218 MK |
51 | |
52 | /** | |
53 | * Run the benchmark | |
54 | */ | |
088b4f19 GB |
55 | @Test |
56 | public void benchmarkExperimentSizeRequest() { | |
57 | Performance perf = Performance.getDefault(); | |
58 | ||
59 | for (int numTraces = 1; numTraces < MAX_TRACES; numTraces = (int) (1.6 * (numTraces + 1))) { | |
b09d20dc GB |
60 | PerformanceMeter pm = perf.createPerformanceMeter(TEST_ID + numTraces + " traces"); |
61 | perf.tagAsSummary(pm, "Experiment Benchmark:" + numTraces + " traces", Dimension.CPU_TIME); | |
088b4f19 | 62 | if ((int) (1.6 * (numTraces + 1)) > MAX_TRACES) { |
b09d20dc | 63 | perf.tagAsGlobalSummary(pm, "Experiment Benchmark:" + numTraces + " traces", Dimension.CPU_TIME); |
088b4f19 | 64 | } |
43d15218 | 65 | |
f14c2f97 MAL |
66 | int sampleSize = SAMPLE_SIZE; |
67 | if (numTraces > 20) { | |
68 | sampleSize = SAMPLE_SIZE_SLOW; | |
69 | } | |
70 | ||
71 | for (int s = 0; s < sampleSize; s++) { | |
43d15218 | 72 | |
088b4f19 GB |
73 | InnerEventRequest expReq = new InnerEventRequest(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND); |
74 | InnerEventRequest traceReq[] = new InnerEventRequest[numTraces]; | |
43d15218 | 75 | |
088b4f19 GB |
76 | init(numTraces); |
77 | fExperiment.sendRequest(expReq); | |
78 | ITmfTrace[] traces = fExperiment.getTraces(); | |
79 | for (int i = 0; i < numTraces; i++) { | |
80 | traceReq[i] = new InnerEventRequest(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND); | |
81 | traces[i].sendRequest(traceReq[i]); | |
82 | } | |
43d15218 | 83 | |
088b4f19 GB |
84 | pm.start(); |
85 | waitForRequest(expReq, traceReq); | |
86 | pm.stop(); | |
43d15218 | 87 | |
088b4f19 GB |
88 | for (int i = 0; i < traces.length; i++) { |
89 | if (!expReq.isTraceHandled(traces[i])) { | |
90 | System.err.println("Trace " + i + " not handled!"); | |
91 | } | |
43d15218 | 92 | } |
43d15218 | 93 | |
088b4f19 GB |
94 | fExperiment.dispose(); |
95 | } | |
96 | pm.commit(); | |
43d15218 | 97 | } |
43d15218 MK |
98 | } |
99 | ||
088b4f19 GB |
100 | /** |
101 | * Initialization | |
102 | * | |
103 | * @param maxTraces | |
104 | * maximum number of traces to open | |
105 | */ | |
106 | private void init(int maxTraces) { | |
43d15218 | 107 | try { |
088b4f19 GB |
108 | File parentDir = new File(TRACES_ROOT_PATH); |
109 | File[] traceFiles = parentDir.listFiles(); | |
110 | ITmfTrace[] traces = new CtfTmfTrace[Math.min(maxTraces, traceFiles.length)]; | |
111 | for (int i = 0; i < traces.length; i++) { | |
112 | traces[i] = new CtfTmfTrace(); | |
113 | } | |
114 | fExperiment = new TmfExperimentStub("MegaExperiment", traces, BLOCK_SIZE); | |
115 | int j = 0; | |
116 | for (int i = 0; i < (traces.length) && (j < traces.length); i++) { | |
117 | String absolutePath = traceFiles[j].getAbsolutePath(); | |
118 | if (traces[i].validate(null, absolutePath).isOK()) { | |
119 | traces[i].initTrace(null, absolutePath, ITmfEvent.class); | |
120 | } else { | |
121 | i--; | |
122 | } | |
123 | j++; | |
124 | } | |
125 | if (traces[traces.length - 1].getPath() == null) { | |
126 | throw new TmfTraceException("Insufficient valid traces in directory"); | |
127 | } | |
43d15218 MK |
128 | } catch (TmfTraceException e) { |
129 | System.out.println(e.getMessage()); | |
130 | } | |
43d15218 MK |
131 | } |
132 | ||
133 | private static void waitForRequest(InnerEventRequest expReq, InnerEventRequest[] traceReqs) { | |
43d15218 MK |
134 | try { |
135 | expReq.waitForCompletion(); | |
136 | List<InnerEventRequest> reqs = Arrays.asList(traceReqs); | |
137 | for (InnerEventRequest traceReq : reqs) { | |
138 | traceReq.waitForCompletion(); | |
139 | } | |
140 | } catch (InterruptedException e) { | |
141 | } | |
43d15218 MK |
142 | } |
143 | ||
144 | private static class InnerEventRequest extends TmfEventRequest { | |
145 | private Set<String> fTraces = new HashSet<>(); | |
146 | ||
147 | public InnerEventRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested, ExecutionType priority) { | |
148 | super(dataType, index, nbRequested, priority); | |
149 | } | |
150 | ||
151 | @Override | |
152 | public void handleData(ITmfEvent event) { | |
153 | super.handleData(event); | |
154 | if (!fTraces.contains(event.getTrace().getName())) { | |
155 | fTraces.add(event.getTrace().getName()); | |
156 | } | |
157 | } | |
158 | ||
159 | public boolean isTraceHandled(ITmfTrace trace) { | |
160 | return fTraces.contains(trace.getName()); | |
161 | } | |
162 | } | |
163 | } |