KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > test > internal > performance > eval > StatisticsSession


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.internal.performance.eval;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.test.internal.performance.InternalPerformanceMeter;
19 import org.eclipse.test.internal.performance.data.DataPoint;
20 import org.eclipse.test.internal.performance.data.Dim;
21 import org.eclipse.test.internal.performance.data.Scalar;
22
23 import junit.framework.Assert;
24
25 /**
26  * @since 3.1
27  */

28 public class StatisticsSession {
29
30     static final class Statistics {
31         public long count;
32         public long sum;
33         public double average;
34         public double stddev;
35     }
36     
37     private final DataPoint[] fDataPoints;
38     private final Map JavaDoc fStatistics= new HashMap JavaDoc();
39
40     public StatisticsSession(DataPoint[] datapoints) {
41         fDataPoints= datapoints;
42     }
43     
44     public double getAverage(Dim dimension) {
45         return getStats(dimension).average;
46     }
47     
48     public long getSum(Dim dimension) {
49         return getStats(dimension).sum;
50     }
51     
52     public long getCount(Dim dimension) {
53         return getStats(dimension).count;
54     }
55     
56     public double getStddev(Dim dimension) {
57         return getStats(dimension).stddev;
58     }
59     
60     private Statistics getStats(Dim dimension) {
61         Statistics stats= (Statistics) fStatistics.get(dimension);
62         if (stats == null) {
63             stats= computeStats(dimension);
64             fStatistics.put(dimension, stats);
65         }
66         return stats;
67     }
68
69     private Statistics computeStats(Dim dimension) {
70                 
71         Statistics stats= new Statistics();
72         
73         Set JavaDoc set= new HashSet JavaDoc();
74         for (int j= 0; j < fDataPoints.length; j++) {
75             DataPoint dp= fDataPoints[j];
76             set.add(new Integer JavaDoc(dp.getStep()));
77         }
78         
79         long mags[]= new long[fDataPoints.length];
80         switch (set.size()) {
81         case 1:
82             // if there is only one Step, we don't calculate the delta.
83
for (int i= 0; i < fDataPoints.length; i++) {
84                 Scalar sc= fDataPoints[i].getScalar(dimension);
85                 if (sc != null) {
86                     long magnitude= sc.getMagnitude();
87                     mags[i]= magnitude;
88                     stats.sum += magnitude;
89                     stats.count++;
90                 }
91             }
92             if (stats.count > 0) {
93                 stats.average= stats.sum / stats.count;
94                 for (int i= 0; i < fDataPoints.length; i++) {
95                     stats.stddev+= (stats.average - mags[i]) * (stats.average - mags[i]);
96                 }
97             }
98             break;
99         case 2:
100             for (int i= 0; i < fDataPoints.length-1; i += 2) {
101                 DataPoint before= fDataPoints[i];
102                 Assert.assertTrue("wrong order of steps", before.getStep() == InternalPerformanceMeter.BEFORE); //$NON-NLS-1$
103
DataPoint after= fDataPoints[i + 1];
104                 Assert.assertTrue("wrong order of steps", after.getStep() == InternalPerformanceMeter.AFTER); //$NON-NLS-1$
105

106                 Scalar delta= getDelta(before, after, dimension);
107                 long magnitude= delta.getMagnitude();
108                 mags[i]= magnitude;
109                 stats.sum += magnitude;
110                 stats.count++;
111             }
112             if (stats.count > 0) {
113                 stats.average= stats.sum / stats.count;
114                 for (int i= 0; i < fDataPoints.length-1; i += 2) {
115                     stats.stddev+= (stats.average - mags[i]) * (stats.average - mags[i]);
116                 }
117             }
118             break;
119         default:
120             Assert.assertTrue("cannot handle more than two steps", false); //$NON-NLS-1$
121
break;
122         }
123         
124         stats.stddev= Math.sqrt(stats.stddev / stats.count);
125
126         return stats;
127     }
128
129     private Scalar getDelta(DataPoint before, DataPoint after, Dim dimension) {
130         Scalar one= before.getScalar(dimension);
131         Assert.assertTrue("reference has no value for dimension " + dimension, one != null); //$NON-NLS-1$
132

133         Scalar two= after.getScalar(dimension);
134         Assert.assertTrue("reference has no value for dimension " + dimension, two != null); //$NON-NLS-1$
135

136         return new Scalar(one.getDimension(), two.getMagnitude() - one.getMagnitude());
137     }
138
139     public boolean contains(Dim dimension) {
140         if (fDataPoints.length > 0)
141             return fDataPoints[0].contains(dimension);
142         return false;
143     }
144 }
145
Popular Tags