KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > performance > PerformanceTestCase


1 /*******************************************************************************
2  * Copyright (c) 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.test.performance;
12
13 import junit.framework.TestCase;
14
15 /**
16  * A PerformanceTestCase is a convenience class that takes care of managing
17  * a <code>PerformanceMeter</code>.
18  * <p>
19  * Here is an example:
20  * <pre>
21  * public class MyPerformanceTestCase extends PeformanceTestCase {
22  *
23  * public void testMyOperation() {
24  * for (int i= 0; i < 10; i++) {
25  * startMeasuring();
26  * // my operation
27  * stopMeasuring();
28  * }
29  * commitMeasurements();
30  * assertPerformance();
31  * }
32  * }
33  */

34 public class PerformanceTestCase extends TestCase {
35
36     protected PerformanceMeter fPerformanceMeter;
37
38     /**
39      * Constructs a performance test case.
40      */

41     public PerformanceTestCase() {
42         super();
43     }
44
45     /**
46      * Constructs a performance test case with the given name.
47      * @param name the name of the performance test case
48      */

49     public PerformanceTestCase(String JavaDoc name) {
50         super(name);
51     }
52     
53     /**
54      * Overridden to create a default performance meter for this test case.
55      * @throws Exception
56      */

57     protected void setUp() throws Exception JavaDoc {
58         Performance performance= Performance.getDefault();
59         fPerformanceMeter= performance.createPerformanceMeter(performance.getDefaultScenarioId(this));
60     }
61
62     /**
63      * Overridden to dispose of the performance meter.
64      * @throws Exception
65      */

66     protected void tearDown() throws Exception JavaDoc {
67         fPerformanceMeter.dispose();
68     }
69
70     /**
71      * Mark the scenario of this test case
72      * to be included into the global and the component performance summary. The summary shows
73      * the given dimension of the scenario and labels the scenario with the short name.
74      *
75      * @param shortName a short (shorter than 40 characters) descritive name of the scenario
76      * @param dimension the dimension to show in the summary
77      */

78     public void tagAsGlobalSummary(String JavaDoc shortName, Dimension dimension) {
79         Performance performance= Performance.getDefault();
80         performance.tagAsGlobalSummary(fPerformanceMeter, shortName, new Dimension[] { dimension } );
81     }
82
83     /**
84      * Mark the scenario represented by the given PerformanceMeter
85      * to be included into the global and the component performance summary. The summary shows
86      * the given dimensions of the scenario and labels the scenario with the short name.
87      *
88      * @param shortName a short (shorter than 40 characters) descritive name of the scenario
89      * @param dimensions an array of dimensions to show in the summary
90      */

91     public void tagAsGlobalSummary(String JavaDoc shortName, Dimension[] dimensions) {
92         Performance performance= Performance.getDefault();
93         performance.tagAsGlobalSummary(fPerformanceMeter, shortName, dimensions );
94     }
95     
96     /**
97      * Mark the scenario of this test case
98      * to be included into the component performance summary. The summary shows
99      * the given dimension of the scenario and labels the scenario with the short name.
100      *
101      * @param shortName a short (shorter than 40 characters) descritive name of the scenario
102      * @param dimension the dimension to show in the summary
103      */

104     public void tagAsSummary(String JavaDoc shortName, Dimension dimension) {
105         Performance performance= Performance.getDefault();
106         performance.tagAsSummary(fPerformanceMeter, shortName, new Dimension[] { dimension } );
107     }
108
109     /**
110      * Mark the scenario represented by the given PerformanceMeter
111      * to be included into the component performance summary. The summary shows
112      * the given dimensions of the scenario and labels the scenario with the short name.
113      *
114      * @param shortName a short (shorter than 40 characters) descritive name of the scenario
115      * @param dimensions an array of dimensions to show in the summary
116      */

117     public void tagAsSummary(String JavaDoc shortName, Dimension[] dimensions) {
118         Performance performance= Performance.getDefault();
119         performance.tagAsSummary(fPerformanceMeter, shortName, dimensions );
120     }
121     
122     /**
123      * Set a comment for the scenario represented by this TestCase.
124      * Currently only comments with a commentKind of EXPLAINS_DEGRADATION_COMMENT are used.
125      * Their commentText is shown in a hover of the performance summaries graph if a performance
126      * degradation exists.
127      *
128      * @param commentKind kind of comment. Must be EXPLAINS_DEGRADATION_COMMENT to have an effect.
129      * @param commentText the comment (shorter than 400 characters)
130      */

131     public void setComment(int commentKind, String JavaDoc commentText) {
132         Performance performance= Performance.getDefault();
133         performance.setComment(fPerformanceMeter, commentKind, commentText);
134     }
135     
136     /**
137      * Called from within a test case immediately before the code to measure is run.
138      * It starts capturing of performance data.
139      * Must be followed by a call to {@link PerformanceTestCase#stopMeasuring()} before subsequent calls
140      * to this method or {@link PerformanceTestCase#commitMeasurements()}.
141      */

142     protected void startMeasuring() {
143         fPerformanceMeter.start();
144     }
145     
146     protected void stopMeasuring() {
147         fPerformanceMeter.stop();
148     }
149     
150     protected void commitMeasurements() {
151         fPerformanceMeter.commit();
152     }
153
154     /**
155      * Asserts default properties of the measurements captured for this test case.
156      *
157      * @throws RuntimeException if the properties do not hold
158      */

159     protected void assertPerformance() {
160         Performance.getDefault().assertPerformance(fPerformanceMeter);
161     }
162
163     /**
164      * Asserts that the measurement specified by the given dimension
165      * is within a certain range with respect to some reference value.
166      * If the specified dimension isn't available, the call has no effect.
167      *
168      * @param dim the Dimension to check
169      * @param lowerPercentage a negative number indicating the percentage the measured value is allowed to be smaller than some reference value
170      * @param upperPercentage a positive number indicating the percentage the measured value is allowed to be greater than some reference value
171      * @throws RuntimeException if the properties do not hold
172      */

173     protected void assertPerformanceInRelativeBand(Dimension dim, int lowerPercentage, int upperPercentage) {
174         Performance.getDefault().assertPerformanceInRelativeBand(fPerformanceMeter, dim, lowerPercentage, upperPercentage);
175     }
176 }
177
Popular Tags