1 11 package org.eclipse.test.internal.performance.eval; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Map ; 16 import java.util.Set ; 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 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 fStatistics= new HashMap (); 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 set= new HashSet (); 74 for (int j= 0; j < fDataPoints.length; j++) { 75 DataPoint dp= fDataPoints[j]; 76 set.add(new Integer (dp.getStep())); 77 } 78 79 long mags[]= new long[fDataPoints.length]; 80 switch (set.size()) { 81 case 1: 82 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); DataPoint after= fDataPoints[i + 1]; 104 Assert.assertTrue("wrong order of steps", after.getStep() == InternalPerformanceMeter.AFTER); 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); 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); 133 Scalar two= after.getScalar(dimension); 134 Assert.assertTrue("reference has no value for dimension " + dimension, two != null); 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 |