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 | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.ctf.core.tests.headless; | |
14 | ||
15 | import java.io.File; | |
16 | import java.util.Arrays; | |
17 | import java.util.HashSet; | |
18 | import java.util.List; | |
19 | import java.util.Set; | |
20 | ||
21 | import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace; | |
22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
23 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
24 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; | |
25 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest.ExecutionType; | |
26 | import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest; | |
27 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
28 | import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace; | |
29 | import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfExperimentStub; | |
30 | ||
31 | /** | |
32 | * Coalescing benchmark | |
33 | * | |
34 | * @author Matthew Khouzam | |
35 | * | |
36 | */ | |
37 | public class ExperimentBenchmark { | |
38 | private static final int MAX_TRACES = 160; | |
39 | private static final int BLOCK_SIZE = 100; | |
40 | private static final String TRACES_ROOT_PATH = CtfTestTrace.TRACE_EXPERIMENT.getPath(); | |
41 | private ITmfTrace[] traces; | |
42 | private TmfExperimentStub fExperiment; | |
43 | ||
44 | /** | |
45 | * initialization | |
46 | * | |
47 | * @param maxTraces | |
48 | * maximum number of traces to open | |
49 | * | |
50 | * @throws TmfTraceException | |
51 | * problem | |
52 | */ | |
53 | private void init(int maxTraces) throws TmfTraceException { | |
54 | File parentDir = new File(TRACES_ROOT_PATH); | |
55 | File[] traceFiles = parentDir.listFiles(); | |
56 | traces = new CtfTmfTrace[Math.min(maxTraces, traceFiles.length)]; | |
57 | for (int i = 0; i < traces.length; i++) { | |
58 | traces[i] = new CtfTmfTrace(); | |
59 | } | |
60 | fExperiment = new TmfExperimentStub("MegaExperiment", traces, BLOCK_SIZE); | |
61 | int j = 0; | |
62 | for (int i = 0; i < (traces.length) && (j < traces.length); i++) { | |
63 | String absolutePath = traceFiles[j].getAbsolutePath(); | |
64 | if (traces[i].validate(null, absolutePath).isOK()) { | |
65 | traces[i].initTrace(null, absolutePath, ITmfEvent.class); | |
66 | } else { | |
67 | i--; | |
68 | } | |
69 | j++; | |
70 | } | |
71 | if (traces[traces.length - 1].getPath() == null) { | |
72 | throw new TmfTraceException("Insufficient valid traces in directory"); | |
73 | } | |
74 | ||
75 | } | |
76 | ||
77 | /** | |
78 | * Main benchmark | |
79 | * | |
80 | * @param args | |
81 | * benchmark | |
82 | */ | |
83 | public static void main(final String[] args) { | |
84 | ExperimentBenchmark eb = new ExperimentBenchmark(); | |
85 | eb.testRun(); | |
86 | ||
87 | } | |
88 | ||
89 | /** | |
90 | * Run the benchmark | |
91 | */ | |
92 | public void testRun() { | |
93 | System.out.println("Test, init, request, dispose"); | |
94 | ||
95 | for (int numTraces = 1; numTraces < MAX_TRACES; numTraces = (int) (1.1 * (numTraces + 1))) { | |
96 | InnerEventRequest expReq = new InnerEventRequest(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND); | |
97 | ||
98 | InnerEventRequest traceReq[] = new InnerEventRequest[numTraces]; | |
99 | ||
100 | System.out.print(numTraces); | |
101 | ||
102 | waitForInit(numTraces); | |
103 | fExperiment.sendRequest(expReq); | |
104 | for (int i = 0; i < numTraces; i++) { | |
105 | traceReq[i] = new InnerEventRequest(ITmfEvent.class, 0, ITmfEventRequest.ALL_DATA, ExecutionType.BACKGROUND); | |
106 | traces[i].sendRequest(traceReq[i]); | |
107 | } | |
108 | waitForRequest(expReq, traceReq); | |
109 | ||
110 | for (int i = 0; i < traces.length; i++) { | |
111 | if (!expReq.isTraceHandled(traces[i])) { | |
112 | System.err.println("Trace " + i + " not handled!"); | |
113 | } | |
114 | } | |
115 | waitForDispose(); | |
116 | System.out.println(""); | |
117 | } | |
118 | } | |
119 | ||
120 | private void waitForDispose() { | |
121 | long start = System.nanoTime(); | |
122 | fExperiment.dispose(); | |
123 | for (int i = 0; i < traces.length; i++) { | |
124 | traces[i].dispose(); | |
125 | } | |
126 | long end = System.nanoTime(); | |
127 | printTime(start, end); | |
128 | } | |
129 | ||
130 | private void waitForInit(int numTraces) { | |
131 | long start = System.nanoTime(); | |
132 | try { | |
133 | init(numTraces); | |
134 | } catch (TmfTraceException e) { | |
135 | System.out.println(e.getMessage()); | |
136 | } | |
137 | long end = System.nanoTime(); | |
138 | printTime(start, end); | |
139 | } | |
140 | ||
141 | private static void waitForRequest(InnerEventRequest expReq, InnerEventRequest[] traceReqs) { | |
142 | long start = System.nanoTime(); | |
143 | try { | |
144 | expReq.waitForCompletion(); | |
145 | List<InnerEventRequest> reqs = Arrays.asList(traceReqs); | |
146 | for (InnerEventRequest traceReq : reqs) { | |
147 | traceReq.waitForCompletion(); | |
148 | } | |
149 | } catch (InterruptedException e) { | |
150 | } | |
151 | long end = System.nanoTime(); | |
152 | printTime(start, end); | |
153 | } | |
154 | ||
155 | private static void printTime(long start, long end) { | |
156 | /* print out the difference between the two nanosecond times in ms */ | |
157 | System.out.format(", %.3f", 0.000000001 * (end - start)); | |
158 | } | |
159 | ||
160 | private static class InnerEventRequest extends TmfEventRequest { | |
161 | private Set<String> fTraces = new HashSet<>(); | |
162 | ||
163 | public InnerEventRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested, ExecutionType priority) { | |
164 | super(dataType, index, nbRequested, priority); | |
165 | } | |
166 | ||
167 | @Override | |
168 | public void handleData(ITmfEvent event) { | |
169 | super.handleData(event); | |
170 | if (!fTraces.contains(event.getTrace().getName())) { | |
171 | fTraces.add(event.getTrace().getName()); | |
172 | } | |
173 | } | |
174 | ||
175 | public boolean isTraceHandled(ITmfTrace trace) { | |
176 | return fTraces.contains(trace.getName()); | |
177 | } | |
178 | } | |
179 | } |