KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > InternalPerformanceMeter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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
12 package org.eclipse.test.internal.performance;
13
14 import java.io.PrintStream JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17
18 import org.eclipse.test.internal.performance.data.DataPoint;
19 import org.eclipse.test.internal.performance.data.Dim;
20 import org.eclipse.test.internal.performance.data.Sample;
21 import org.eclipse.test.internal.performance.db.DB;
22 import org.eclipse.test.internal.performance.db.Variations;
23 import org.eclipse.test.internal.performance.eval.StatisticsSession;
24 import org.eclipse.test.performance.Dimension;
25 import org.eclipse.test.performance.PerformanceMeter;
26
27
28 public abstract class InternalPerformanceMeter extends PerformanceMeter {
29
30     
31     private static class DimensionComparator implements Comparator JavaDoc {
32
33         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
34             return ((Dim) o1).getId() - ((Dim) o2).getId();
35         }
36
37     }
38
39     public static final int AVERAGE= -3;
40     public static final int BEFORE= 0;
41     public static final int AFTER= 1;
42     
43     protected static final String JavaDoc VERBOSE_PERFORMANCE_METER_PROPERTY= "InternalPrintPerformanceResults"; //$NON-NLS-1$
44

45     private String JavaDoc fScenarioId;
46     
47     private String JavaDoc fShortName;
48     private Dimension[] fSummaryDimensions;
49     private boolean fSummaryIsGlobal;
50     private int fCommentType;
51     private String JavaDoc fComment;
52
53     
54     public InternalPerformanceMeter(String JavaDoc scenarioId) {
55         fScenarioId= scenarioId;
56     }
57
58     public void dispose() {
59         fScenarioId= null;
60     }
61
62     public abstract Sample getSample();
63
64     /**
65      * Answer the scenario ID.
66      * @return the scenario ID
67      */

68     public String JavaDoc getScenarioName() {
69         return fScenarioId;
70     }
71
72     /*
73      * @see org.eclipse.test.performance.PerformanceMeter#commit()
74      */

75     public void commit() {
76         Sample sample= getSample();
77         if (sample != null) {
78             if (fSummaryDimensions != null)
79                 sample.tagAsSummary(fSummaryIsGlobal, fShortName, fSummaryDimensions, fCommentType, fComment);
80             Variations variations= PerformanceTestPlugin.getVariations();
81             if (variations != null)
82                 DB.store(variations, sample);
83             if (!DB.isActive() || System.getProperty(VERBOSE_PERFORMANCE_METER_PROPERTY) != null)
84                 printSample(System.out, sample);
85         }
86     }
87
88     private void printSample(PrintStream JavaDoc ps, Sample sample) {
89         ps.print("Scenario '" + getScenarioName() + "' "); //$NON-NLS-1$ //$NON-NLS-2$
90
DataPoint[] dataPoints= sample.getDataPoints();
91         if (dataPoints.length > 0) {
92             StatisticsSession s= new StatisticsSession(dataPoints);
93             Dim[] dimensions= dataPoints[0].getDimensions();
94             Arrays.sort(dimensions, new DimensionComparator());
95             if (dimensions.length > 0) {
96                 ps.println("(average over " + s.getCount(dimensions[0]) + " samples):"); //$NON-NLS-1$ //$NON-NLS-2$
97
for (int i= 0; i < dimensions.length; i++) {
98                     Dim dimension= dimensions[i];
99                     ps.println(" " + dimension.getName() + ": " + dimension.getDisplayValue(s.getAverage(dimension))); //$NON-NLS-1$ //$NON-NLS-2$
100
}
101             }
102         }
103         ps.println();
104     }
105
106     public void tagAsSummary(boolean global, String JavaDoc shortName, Dimension[] dims) {
107         fSummaryIsGlobal= global;
108         fShortName= shortName;
109         fSummaryDimensions= dims;
110      }
111
112     public void setComment(int commentType, String JavaDoc comment) {
113         fCommentType= commentType;
114         fComment= comment;
115     }
116 }
117
Popular Tags