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: * ProfilingPointsTestCase.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.profilingpoints;
051:
052: import org.netbeans.lib.profiler.ProfilerEngineSettings;
053: import org.netbeans.lib.profiler.TargetAppRunner;
054: import org.netbeans.lib.profiler.tests.jfluid.*;
055:
056: //import org.netbeans.lib.profiler.client.ProfilingPoint;
057: //import org.netbeans.lib.profiler.client.ProfilingPoint.HitEvent;
058: import org.netbeans.lib.profiler.tests.jfluid.utils.TestProfilerAppHandler;
059: import org.netbeans.lib.profiler.tests.jfluid.utils.Utils;
060: import org.netbeans.lib.profiler.utils.StringUtils;
061: import java.util.ArrayList;
062: import java.util.Collections;
063: import java.util.HashMap;
064:
065: /**
066: *
067: * @author ehucka
068: */
069: public abstract class ProfilingPointsTestCase extends
070: CommonProfilerTestCase {
071: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
072:
073: static boolean CHECK_ACCURACY = false;
074:
075: //~ Instance fields ----------------------------------------------------------------------------------------------------------
076:
077: double MAX_DIFF_PERC = 70;
078: int ppid = 0;
079:
080: //~ Constructors -------------------------------------------------------------------------------------------------------------
081:
082: // StartStopwatch startStopWatch = null;
083: // StopStopwatch stopStopWatch = null;
084:
085: // HashMap<StopWatchPP, ArrayList<Long>> results = new HashMap(128);
086: public ProfilingPointsTestCase(String name) {
087: super (name);
088: }
089:
090: // protected ProfilerEngineSettings initPPTest(String projectName, String mainClass, String[][] rootMethods) {
091: // ProfilerEngineSettings settings = initTest(projectName, mainClass, rootMethods);
092: // //defaults
093: // settings.setInstrumentSpawnedThreads(true);
094: // settings.setExcludeWaitTime(true);
095: // settings.setNProfiledThreadsLimit(32);
096: // //addJVMArgs(settings, "-Dorg.netbeans.lib.profiler.wireprotocol.WireIO=true");
097: //
098: // settings.setThreadCPUTimerOn(false);
099: //
100: // return settings;
101: // }
102:
103: // protected void startPPStopwatchTest(ProfilerEngineSettings settings, StopWatchPP[] stopwatches) {
104: //
105: // startStopWatch = new StartStopwatch();
106: // stopStopWatch = new StopStopwatch();
107: //
108: // ProfilingPoint[] points;
109: // ArrayList<ProfilingPoint> list=new ArrayList(64);
110: // for (StopWatchPP stopw:stopwatches) {
111: // points = stopw.getPPoints(this);
112: // for (ProfilingPoint pp:points) {
113: // list.add(pp);
114: // }
115: // }
116: // settings.setProfilingPoints(list.toArray(new ProfilingPoint[list.size()]));
117: //
118: // TargetAppRunner runner = new TargetAppRunner(settings, new TestProfilerAppHandler(this));
119: // runner.getProfilingSessionStatus().startProfilingPointsActive = true;
120: // runner.addProfilingEventListener(Utils.createProfilingListener(this));
121: // try {
122: // runner.readSavedCalibrationData();
123: // runner.getProfilerClient().initiateRecursiveCPUProfInstrumentation(settings.getInstrumentationRootMethods());
124: // Process p = startTargetVM(runner);
125: // assertNotNull("Target JVM is not started", p);
126: // bindStreams(p);
127: //
128: // runner.connectToStartedVMAndStartTA();
129: //
130: // waitForStatus(STATUS_RUNNING);
131: // assertTrue("runner is not running", runner.targetAppIsRunning());
132: //
133: // waitForStatus(STATUS_APP_FINISHED);
134: // if (runner.targetJVMIsAlive()) {
135: // log("JVM is Alive: "+System.currentTimeMillis());
136: // }
137: // setStatus(STATUS_MEASURED);
138: //
139: // ref("Stopwatches:");
140: // for (StopWatchPP stopw:stopwatches) {
141: // if (results.get(stopw) != null)
142: // stopw.measureStopwatch(results.get(stopw), this);
143: // else
144: // log("\nStopwatch "+stopw+" has null results.");
145: // }
146: //
147: // setStatus(STATUS_MEASURED);
148: // } catch (Exception ex) {
149: // log(ex);
150: // assertTrue("Exception thrown: "+ex.getMessage(), false);
151: // } finally {
152: // finalizeTest(runner);
153: // }
154: // }
155:
156: // HashMap<Integer, StopWatchPP> stopwatches = new HashMap(32);
157:
158: // protected ProfilingPoint[] createStopwatch(StopWatchPP stopwatch) {
159: // ProfilingPoint[] points=new ProfilingPoint[2];
160: // points[0] = new ProfilingPoint(ppid++, stopwatch.className, stopwatch.startLine, startStopWatch, "org.netbeans.lib.profiler.global.ProfilingPointServerHandler");
161: // points[1] = new ProfilingPoint(ppid++, stopwatch.className, -stopwatch.stopLine, stopStopWatch, "org.netbeans.lib.profiler.global.ProfilingPointServerHandler");
162: // stopwatches.put(ppid-2, stopwatch);
163: // stopwatches.put(ppid-1, stopwatch);
164: //
165: // return points;
166: // }
167: // /*
168: // protected void startPPTest(ProfilerEngineSettings settings, String crClasse,
169: // String crMethod, String crSignature, long idealTime, double diffPercent) {
170: // //create runner
171: //
172: // TargetAppRunner runner = new TargetAppRunner(settings, new TestProfilerAppHandler(this));
173: // runner.addProfilingEventListener(Utils.createProfilingListener(this));
174: // try {
175: // runner.readSavedCalibrationData();
176: // //create selection
177: // SourceCodeSelection select = new SourceCodeSelection(crClasse, crMethod, crSignature);
178: // runner.getProfilerClient().initiateCodeRegionInstrumentation(select);
179: //
180: // //measureCR(runner, idealTime, diffPercent);
181: // } catch (Exception ex) {
182: // log(ex);
183: // assertTrue("Exception thrown: "+ex.getMessage(), false);
184: // } finally {
185: // finalizeTest(runner);
186: // }
187: // }*/
188: //
189: // static int counts = 0;
190: // static int counte = 0;
191: //
192: // HashMap<StopWatchPP, HashMap<Integer, Long>> hits = new HashMap(32);
193: //
194: // class StartStopwatch implements ProfilingPoint.Executor {
195: // public void profilePointHit(HitEvent event) {
196: // System.out.println("start id "+counts++);
197: // //log("start "+event.getId()+"");
198: // StopWatchPP stopwatch = stopwatches.get(event.getId());
199: // HashMap<Integer, Long> map = hits.get(stopwatch);
200: // if (map == null) {
201: // map = new HashMap(32);
202: // hits.put(stopwatch, map);
203: // }
204: // map.put(event.getThreadId(), event.getTimestamp());
205: // }
206: // }
207: //
208: // class StopStopwatch implements ProfilingPoint.Executor {
209: // public void profilePointHit(HitEvent event) {
210: // System.out.println("end id "+counte++);
211: // StopWatchPP stopwatch = stopwatches.get(event.getId());
212: // HashMap<Integer, Long> map = hits.get(stopwatch);
213: // if (map != null && map.get(event.getThreadId()) != null) {
214: // long tms = map.remove(event.getThreadId());
215: // ArrayList<Long> res = results.get(stopwatch);
216: // if (res == null) {
217: // res = new ArrayList(128);
218: // results.put(stopwatch, res);
219: // }
220: // res.add((event.getTimestamp() - tms)/1000);
221: // }
222: // }
223: // }
224: //
225: // public static class StopWatchPP {
226: // String className;
227: // int startLine, stopLine, diffMillis;
228: //
229: // public StopWatchPP(String classname, int start, int end, int diff) {
230: // className = classname;
231: // startLine = start;
232: // stopLine = end;
233: // diffMillis = diff;
234: // }
235: //
236: // public ProfilingPoint[] getPPoints(ProfilingPointsTestCase test) {
237: // return test.createStopwatch(this);
238: // }
239: //
240: //
241: // protected void measureStopwatch(ArrayList<Long> results, ProfilingPointsTestCase test) throws Exception {
242: // ArrayList<Long> times = new ArrayList(results.size());
243: // times.addAll(results);
244: // Collections.sort(times);
245: //
246: // double all=0.0;
247: // StringBuilder sb=new StringBuilder();
248: // for (int i=0;i < times.size();i++) {
249: // all+=times.get(i);
250: // sb.append(StringUtils.mcsTimeToString(results.get(i)));
251: // sb.append(" ");
252: // }
253: //
254: // double median;
255: // if (times.size()%2 > 0)
256: // median=times.get(times.size()/2);
257: // else
258: // median=(times.get(times.size()/2)+times.get(times.size()/2-1))/2;
259: // int match=0;
260: // for (int i = 0; i < times.size(); i++) {
261: // if (Math.abs(times.get(i)-median)/1000 <= diffMillis) {
262: // match++;
263: // }
264: // }
265: // double max=times.get(times.size()-1);
266: // double min=times.get(0);
267: // test.log("\nStopWatch "+this+"\n---------------------------------------------");
268: // test.log("Values:");
269: // test.log(sb);
270: // test.log("match="+match);
271: // test.log("Invocations: "+results.size());
272: // test.log("Total="+StringUtils.mcsTimeToString((long)all)+" ms Median="+
273: // StringUtils.mcsTimeToString((long)median)+" ms Min="+
274: // StringUtils.mcsTimeToString((long)min)+
275: // " ms Max="+StringUtils.mcsTimeToString((long)max)+" ms");
276: // test.ref(this+", "+results.size()+" pass(es)");
277: // if (match < times.size()*test.MAX_DIFF_PERC/100.0) {
278: // test.log("\n!!!Results don't match: "+test.complete(String.valueOf((times.size()-match)*100.0/times.size()), 6)+
279: // " % of values are different more than "+diffMillis+" ms than median value "+StringUtils.mcsTimeToString((long)median)+"\n");
280: // if (CHECK_ACCURACY)
281: // assertTrue("Results are too different", false);
282: // }
283: // }
284: //
285: // public String toString() {
286: // return className+":"+startLine+"->"+stopLine;
287: // }
288: // }
289: }
|