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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package gui.action;
043:
044: import java.util.LinkedList;
045: import org.netbeans.junit.NbPerformanceTest;
046: import org.netbeans.performance.test.guitracker.ActionTracker;
047: import org.netbeans.performance.test.utilities.PerformanceTestCase;
048:
049: /**
050: *
051: * @author mkhramov@netbeans.org
052: */
053: public class PerformanceCounters {
054:
055: private static int COUNTER_START = -20;
056: private static int COUNTER_STOP = -30;
057: private static int testAttempt = 0;
058: private static String testName;
059: private static ActionTracker tr;
060: private static LinkedList<String> countersList;
061: private static PerformanceTestCase testCase;
062:
063: public static void initPerformanceCounters(PerformanceTestCase test) {
064: testCase = test;
065: testName = test.getName();
066: testCase.log("Test name = " + test.getName());
067: tr = ActionTracker.getInstance();
068: countersList = new LinkedList<String>();
069: testAttempt = 0;
070: }
071:
072: public static void addPerformanceCounter(String counterName) {
073:
074: countersList.add(counterName);
075: //long lastEventTime = tr.getCurrentEvents().getLast().getTimeMillis();
076: long ctm = System.currentTimeMillis();
077: //testCase.log("add: last event = "+lastEventTime+" system Time = "+ctm);
078: tr.add(COUNTER_START, counterName + "[ " + getPass() + " ]");
079: }
080:
081: public static void endPerformanceCounter(String counterName) {
082: //long lastEventTime = tr.getCurrentEvents().getLast().getTimeMillis();
083: long ctm = System.currentTimeMillis();
084: //testCase.log("end: last event = "+lastEventTime+" system Time = "+ctm);
085: tr.add(COUNTER_STOP, counterName + "[ " + getPass() + " ]");
086: }
087:
088: private static void clearPerformanceCounters() {
089: countersList = new LinkedList<String>();
090:
091: }
092:
093: private static void nextPass() {
094: testAttempt++;
095: }
096:
097: private static int getPass() {
098: return testAttempt;
099: }
100:
101: public static void reportPerformanceCounters() {
102: for (String counter : countersList) {
103: reportPerformanceCounter(counter);
104: }
105: nextPass();
106: }
107:
108: private static void reportPerformanceCounter(String counterName) {
109: long counterResult = measurePerformanceCounter(counterName);
110: //testCase.log("Report for "+counterName+" = "+counterResult);
111: testCase.reportPerformance(testName + " at step: "
112: + counterName, counterResult, "ms",
113: NbPerformanceTest.PerformanceData.NO_ORDER,
114: NbPerformanceTest.PerformanceData.NO_THRESHOLD);
115:
116: }
117:
118: private static long measurePerformanceCounter(String name) {
119: String attemptName = name + "[ " + getPass() + " ]";
120:
121: ActionTracker.Tuple start = tr.getCurrentEvents().getFirst();
122: ActionTracker.Tuple end = tr.getCurrentEvents().getFirst();
123:
124: for (ActionTracker.Tuple tuple : tr.getCurrentEvents()) {
125: int code = tuple.getCode();
126: String counter = tuple.getName();
127:
128: if (code == COUNTER_START && counter.equals(attemptName)) {
129: start = tuple;
130: } else if (code == COUNTER_STOP
131: && counter.equals(attemptName)) {
132: end = tuple;
133: }
134: }
135: testCase.log("Start event code " + start.getCode() + " name "
136: + start.getName() + " time " + start.getTimeMillis()
137: + " diff " + start.getTimeDifference());
138: testCase.log("End event code " + end.getCode() + " name "
139: + end.getName() + " time " + end.getTimeMillis()
140: + " diff " + end.getTimeDifference());
141:
142: start.setMeasured(true);
143: end.setMeasured(true);
144:
145: long result = end.getTimeMillis() - start.getTimeMillis();
146: if (result < 0 || start.getTimeMillis() == 0) {
147: throw new IllegalStateException(
148: "Measuring failed, because we start["
149: + start.getTimeMillis() + "] > end["
150: + end.getTimeMillis() + "] or start=0");
151: }
152: return result;
153: }
154: }
|