001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: /*
042: * MeasureDiffsTestCase.java
043: *
044: * Created on July 19, 2005, 5:20 PM
045: *
046: * To change this template, choose Tools | Options and locate the template under
047: * the Source Creation and Management node. Right-click the template and choose
048: * Open. You can then make changes to the template in the Source Editor.
049: */
050: package org.netbeans.lib.profiler.tests.jfluid.others;
051:
052: import org.netbeans.lib.profiler.ProfilerEngineSettings;
053: import org.netbeans.lib.profiler.TargetAppRunner;
054: import org.netbeans.lib.profiler.global.CommonConstants;
055: import org.netbeans.lib.profiler.results.ProfilingResultsDispatcher;
056: import org.netbeans.lib.profiler.results.RuntimeCCTNode;
057: import org.netbeans.lib.profiler.results.cpu.CPUCCTProvider;
058: import org.netbeans.lib.profiler.results.cpu.CPUCallGraphBuilder;
059: import org.netbeans.lib.profiler.results.cpu.FlatProfileBuilder;
060: import org.netbeans.lib.profiler.results.cpu.FlatProfileContainer;
061: import org.netbeans.lib.profiler.results.cpu.FlatProfileContainerFree;
062: import org.netbeans.lib.profiler.tests.jfluid.*;
063: import org.netbeans.lib.profiler.tests.jfluid.utils.*;
064: import java.io.BufferedReader;
065: import java.io.File;
066: import java.io.FileReader;
067: import java.io.IOException;
068: import java.util.*;
069:
070: /**
071: *
072: * @author ehucka
073: */
074: public abstract class MeasureDiffsTestCase extends
075: CommonProfilerTestCase {
076: //~ Inner Classes ------------------------------------------------------------------------------------------------------------
077:
078: class Results {
079: //~ Instance fields ------------------------------------------------------------------------------------------------------
080:
081: double ideal;
082: double measalone;
083: double measprofiler;
084: double profiled;
085:
086: //~ Methods --------------------------------------------------------------------------------------------------------------
087:
088: public void setIdeal(String val) {
089: try {
090: ideal = Double.parseDouble(val);
091: } catch (NumberFormatException ex) {
092: }
093: }
094:
095: public void setMeasuredAlone(String val) {
096: try {
097: measalone = Double.parseDouble(val);
098: } catch (NumberFormatException ex) {
099: }
100: }
101:
102: public void setMeasuredProfiled(String val) {
103: try {
104: measprofiler = Double.parseDouble(val);
105: } catch (NumberFormatException ex) {
106: }
107: }
108:
109: public void setProfiled(double val) {
110: profiled = val;
111: }
112:
113: public String toString() {
114: return String.valueOf(ideal) + ";"
115: + String.valueOf(measalone) + ";"
116: + String.valueOf(measprofiler) + ";"
117: + String.valueOf(profiled);
118: }
119: }
120:
121: private static class CPUResultListener implements
122: CPUCCTProvider.Listener {
123: //~ Instance fields ------------------------------------------------------------------------------------------------------
124:
125: private final Object resultsLock = new Object();
126: private boolean hasResults = false;
127:
128: //~ Methods --------------------------------------------------------------------------------------------------------------
129:
130: public void cctEstablished(RuntimeCCTNode appRootNode) {
131: synchronized (resultsLock) {
132: hasResults = true;
133: resultsLock.notify();
134: }
135: }
136:
137: public void cctReset() {
138: synchronized (resultsLock) {
139: hasResults = false;
140: resultsLock.notify();
141: }
142: }
143:
144: public boolean wait4results(long timeout) {
145: synchronized (resultsLock) {
146: if (!hasResults) {
147: try {
148: resultsLock.wait(timeout);
149: } catch (InterruptedException e) {
150: }
151: }
152:
153: return hasResults;
154: }
155: }
156: }
157:
158: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
159:
160: static int ALL_INV_ERROR_METHOD = 0;
161: static int LAST_INV_ERROR_METHOD = 1;
162:
163: //~ Instance fields ----------------------------------------------------------------------------------------------------------
164:
165: File outFile;
166: private CPUCallGraphBuilder builder;
167:
168: //~ Constructors -------------------------------------------------------------------------------------------------------------
169:
170: /**
171: * Creates a new instance of MeasureDiffsTestCase
172: */
173: public MeasureDiffsTestCase(String name) {
174: super (name);
175: }
176:
177: //~ Methods ------------------------------------------------------------------------------------------------------------------
178:
179: public void startAppAlone(ProfilerEngineSettings settings) {
180: ArrayList commands = new ArrayList(10);
181: commands.add(settings.getTargetJVMExeFile());
182: commands.add("-classpath");
183: commands.add(settings.getMainClassPath());
184: commands.add(settings.getMainClassName());
185: commands.add(outFile.getAbsolutePath());
186:
187: String[] cmds = (String[]) commands.toArray(new String[commands
188: .size()]);
189:
190: try {
191: Runtime.getRuntime().exec(cmds, null,
192: new File(settings.getWorkingDir())).waitFor();
193: } catch (Exception ex) {
194: ex.printStackTrace();
195: }
196: }
197:
198: protected ProfilerEngineSettings initCpuTest(String projectName,
199: String mainClass) {
200: ProfilerEngineSettings settings = initTest(projectName,
201: mainClass, null);
202: //defaults
203: settings.setCPUProfilingType(CommonConstants.CPU_INSTR_FULL);
204: settings.setInstrScheme(CommonConstants.INSTRSCHEME_TOTAL);
205: settings.setInstrumentEmptyMethods(false);
206: settings.setInstrumentGetterSetterMethods(false);
207: settings.setInstrumentMethodInvoke(true);
208: settings.setInstrumentSpawnedThreads(true);
209: settings.setExcludeWaitTime(true);
210:
211: //addJVMArgs(settings, "-Dorg.netbeans.lib.profiler.wireprotocol.WireIO=true");
212: settings.setThreadCPUTimerOn(false);
213:
214: return settings;
215: }
216:
217: protected ProfilerEngineSettings initCpuTest(String projectName,
218: String mainClass, String[][] rootMethods) {
219: ProfilerEngineSettings settings = initTest(projectName,
220: mainClass, rootMethods);
221: //defaults
222: settings.setCPUProfilingType(CommonConstants.CPU_INSTR_FULL);
223: settings.setInstrScheme(CommonConstants.INSTRSCHEME_LAZY);
224: settings.setInstrumentEmptyMethods(false);
225: settings.setInstrumentGetterSetterMethods(false);
226: settings.setInstrumentMethodInvoke(true);
227: settings.setInstrumentSpawnedThreads(false);
228: settings.setExcludeWaitTime(true);
229:
230: //addJVMArgs(settings, "-Dorg.netbeans.lib.profiler.wireprotocol.WireIO=true");
231: settings.setThreadCPUTimerOn(false);
232:
233: return settings;
234: }
235:
236: protected void readIdealTimes(HashMap results) {
237: try {
238: BufferedReader br = new BufferedReader(new FileReader(
239: outFile));
240: String line;
241: boolean first = true;
242:
243: while ((line = br.readLine()) != null) {
244: if (first) {
245: first = false;
246: } else {
247: StringTokenizer st = new StringTokenizer(line, ";");
248: String met = st.nextToken();
249: Results res = new Results();
250: res.setIdeal(st.nextToken());
251: res.setMeasuredAlone(st.nextToken());
252: results.put(met, res);
253: }
254: }
255:
256: br.close();
257: } catch (IOException ex) {
258: ex.printStackTrace();
259: }
260: }
261:
262: protected void readProfiledTimes(HashMap results) {
263: try {
264: BufferedReader br = new BufferedReader(new FileReader(
265: outFile));
266: String line;
267: boolean first = true;
268:
269: while ((line = br.readLine()) != null) {
270: if (first) {
271: first = false;
272: } else {
273: StringTokenizer st = new StringTokenizer(line, ";");
274: String met = st.nextToken();
275: st.nextToken(); //skip ideal
276:
277: Results res = (Results) (results.get(met));
278:
279: if (res != null) {
280: res.setMeasuredProfiled(st.nextToken());
281: }
282: }
283: }
284:
285: br.close();
286: } catch (IOException ex) {
287: ex.printStackTrace();
288: }
289: }
290:
291: protected void refCPUResults(FlatProfileContainer fpc,
292: String[] methodsOrder, HashMap results) {
293: for (int row = 0; row < fpc.getNRows(); row++) {
294: for (int mets = 0; mets < methodsOrder.length; mets++) {
295: if (fpc.getMethodNameAtRow(row).startsWith(
296: methodsOrder[mets])) {
297: double tm = fpc.getTimeInMcs0AtRow(row)
298: / (fpc.getNInvocationsAtRow(row) * 1000.0);
299: String name = fpc.getMethodNameAtRow(row);
300:
301: if (name.indexOf('.') > -1) {
302: name = name
303: .substring(name.lastIndexOf('.') + 1);
304: }
305:
306: name = name.substring(0, name.indexOf('('));
307:
308: Results res = (Results) (results.get(name));
309:
310: if (res != null) {
311: res.setProfiled(tm);
312: }
313: }
314: }
315: }
316: }
317:
318: protected void startCPUTest(ProfilerEngineSettings settings,
319: String[] methodsOrder) {
320: HashMap results = new HashMap(64);
321:
322: assertTrue(builder != null);
323:
324: //get results with alone run
325: try {
326: outFile = File.createTempFile("profiler", "test");
327: startAppAlone(settings);
328: } catch (IOException ex) {
329: ex.printStackTrace();
330: }
331:
332: readIdealTimes(results);
333:
334: settings.setMainArgs(outFile.getAbsolutePath());
335:
336: //create runner
337: TargetAppRunner runner = new TargetAppRunner(settings,
338: new TestProfilerAppHandler(this ),
339: new TestProfilingPointsProcessor());
340: runner.addProfilingEventListener(Utils
341: .createProfilingListener(this ));
342:
343: ProfilingResultsDispatcher.getDefault().addListener(builder);
344:
345: CPUResultListener resultListener = new CPUResultListener();
346: builder.addListener(resultListener);
347:
348: FlatProfileBuilder flattener = new FlatProfileBuilder();
349: builder.addListener(flattener);
350: flattener.setContext(runner.getProfilerClient());
351:
352: try {
353: runner.readSavedCalibrationData();
354:
355: Process p = startTargetVM(runner);
356: assertNotNull("Target JVM is not started", p);
357: bindStreams(p);
358:
359: runner.connectToStartedVMAndStartTA();
360:
361: runner.getProfilerClient()
362: .initiateRecursiveCPUProfInstrumentation(
363: settings.getInstrumentationRootMethods());
364:
365: waitForStatus(STATUS_RUNNING);
366: assertTrue("runner is not running", runner
367: .targetAppIsRunning());
368:
369: ArrayList methods = new ArrayList();
370:
371: waitForStatus(STATUS_APP_FINISHED);
372:
373: if (runner.targetJVMIsAlive()) {
374: log("Get results: " + System.currentTimeMillis());
375: assertTrue("Results do not exist - issue 65185.",
376: runner.getProfilerClient().cpuResultsExist());
377:
378: runner.getProfilerClient().forceObtainedResultsDump();
379:
380: boolean gotResults = resultListener.wait4results(10000);
381: assertTrue(
382: "Results are not available after 10 seconds.",
383: gotResults);
384: log("obtaining results "
385: + String.valueOf(System.currentTimeMillis()));
386:
387: //logInstrumented(runner);
388: FlatProfileContainerFree fpc = (FlatProfileContainerFree) flattener
389: .createFlatProfile();
390: fpc.filterOriginalData(new String[0], 0, 0.0D);
391: refCPUResults(fpc, methodsOrder, results);
392: }
393:
394: setStatus(STATUS_MEASURED);
395: readProfiledTimes(results);
396: writeResults(results);
397: } catch (Exception ex) {
398: log(ex);
399: assertTrue("Exception thrown: " + ex.getMessage(), false);
400: } finally {
401: finalizeTest(runner);
402: }
403: }
404:
405: protected void writeResults(HashMap results) {
406: Object[] keys = results.keySet().toArray();
407: Arrays.sort(keys);
408: log("\nMethod Name;Ideal Time;Measured Time;Measured during profiling;Measured by profiler");
409:
410: for (int i = 0; i < keys.length; i++) {
411: Results res = (Results) (results.get(keys[i]));
412: log(keys[i] + ";" + res.toString());
413: }
414:
415: log("\n");
416: }
417: }
|