1 11 12 package org.eclipse.test.internal.performance; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.test.internal.performance.data.Assert; 20 import org.eclipse.test.internal.performance.data.DataPoint; 21 import org.eclipse.test.internal.performance.data.Dim; 22 import org.eclipse.test.internal.performance.data.Sample; 23 import org.eclipse.test.internal.performance.data.Scalar; 24 25 26 public class SystemTimePerformanceMeter extends InternalPerformanceMeter { 27 28 private static final int DEFAULT_INITIAL_CAPACITY= 3; 29 30 private long fStartDate; 31 private List fStartTime; 32 private List fStopTime; 33 34 37 public SystemTimePerformanceMeter(String scenarioId) { 38 this(scenarioId, DEFAULT_INITIAL_CAPACITY); 39 fStartDate= System.currentTimeMillis(); 40 } 41 42 46 public SystemTimePerformanceMeter(String scenarioId, int initalCapacity) { 47 super(scenarioId); 48 fStartTime= new ArrayList (initalCapacity); 49 fStopTime= new ArrayList (initalCapacity); 50 } 51 52 55 public void start() { 56 fStartTime.add(new Long (System.currentTimeMillis())); 57 } 58 59 62 public void stop() { 63 fStopTime.add(new Long (System.currentTimeMillis())); 64 } 65 66 69 public void commit() { 70 Assert.isTrue(fStartTime.size() == fStopTime.size()); 71 System.out.println("Scenario: " + getScenarioName()); int maxOccurenceLength= String.valueOf(fStartTime.size()).length(); 73 for (int i= 0; i < fStartTime.size(); i++) { 74 String occurence= String.valueOf(i + 1); 75 System.out.println("Occurence " + replicate(" ", maxOccurenceLength - occurence.length()) + occurence + ": " + (((Long ) fStopTime.get(i)).longValue() - ((Long ) fStartTime.get(i)).longValue())); } 77 } 78 79 private String replicate(String s, int n) { 80 StringBuffer buf= new StringBuffer (n * s.length()); 81 for (int i= 0; i < n; i++) 82 buf.append(s); 83 return buf.toString(); 84 } 85 86 89 public void dispose() { 90 fStartTime= null; 91 fStopTime= null; 92 super.dispose(); 93 } 94 95 public Sample getSample() { 96 Assert.isTrue(fStartTime.size() == fStopTime.size()); 97 98 Map properties= new HashMap (); 99 103 104 DataPoint[] data= new DataPoint[2*fStartTime.size()]; 105 for (int i= 0; i < fStartTime.size(); i++) { 106 data[2*i]= createDataPoint(BEFORE, InternalDimensions.SYSTEM_TIME, ((Long ) fStartTime.get(i)).longValue()); 107 data[2*i+1]= createDataPoint(AFTER, InternalDimensions.SYSTEM_TIME, ((Long ) fStopTime.get(i)).longValue()); 108 } 109 110 return new Sample(getScenarioName(), fStartDate, properties, data); 111 } 112 113 private DataPoint createDataPoint(int step, Dim dimension, long value) { 114 Map scalars= new HashMap (); 115 scalars.put(dimension, new Scalar(dimension, value)); 116 return new DataPoint(step, scalars); 117 } 118 } 119 | Popular Tags |