KickJava   Java API By Example, From Geeks To Geeks.

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


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
12 package org.eclipse.test.internal.performance;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 JavaDoc fStartTime;
32     private List JavaDoc fStopTime;
33     
34     /**
35      * @param scenarioId the scenario id
36      */

37     public SystemTimePerformanceMeter(String JavaDoc scenarioId) {
38         this(scenarioId, DEFAULT_INITIAL_CAPACITY);
39         fStartDate= System.currentTimeMillis();
40     }
41     
42     /**
43      * @param scenarioId the scenario id
44      * @param initalCapacity the initial capacity in the number of measurments
45      */

46     public SystemTimePerformanceMeter(String JavaDoc scenarioId, int initalCapacity) {
47         super(scenarioId);
48         fStartTime= new ArrayList JavaDoc(initalCapacity);
49         fStopTime= new ArrayList JavaDoc(initalCapacity);
50     }
51     
52     /*
53      * @see org.eclipse.test.performance.PerformanceMeter#start()
54      */

55     public void start() {
56         fStartTime.add(new Long JavaDoc(System.currentTimeMillis()));
57     }
58     
59     /*
60      * @see org.eclipse.test.performance.PerformanceMeter#stop()
61      */

62     public void stop() {
63         fStopTime.add(new Long JavaDoc(System.currentTimeMillis()));
64     }
65     
66     /*
67      * @see org.eclipse.test.performance.PerformanceMeter#commit()
68      */

69     public void commit() {
70         Assert.isTrue(fStartTime.size() == fStopTime.size());
71         System.out.println("Scenario: " + getScenarioName()); //$NON-NLS-1$
72
int maxOccurenceLength= String.valueOf(fStartTime.size()).length();
73         for (int i= 0; i < fStartTime.size(); i++) {
74             String JavaDoc occurence= String.valueOf(i + 1);
75             System.out.println("Occurence " + replicate(" ", maxOccurenceLength - occurence.length()) + occurence + ": " + (((Long JavaDoc) fStopTime.get(i)).longValue() - ((Long JavaDoc) fStartTime.get(i)).longValue())); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
76
}
77     }
78     
79     private String JavaDoc replicate(String JavaDoc s, int n) {
80         StringBuffer JavaDoc buf= new StringBuffer JavaDoc(n * s.length());
81         for (int i= 0; i < n; i++)
82             buf.append(s);
83         return buf.toString();
84     }
85
86     /*
87      * @see org.eclipse.test.performance.PerformanceMeter#dispose()
88      */

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 JavaDoc properties= new HashMap JavaDoc();
99             /*
100             properties.put(DRIVER_PROPERTY, PerformanceTestPlugin.getBuildId());
101             properties.put(HOSTNAME_PROPERTY, getHostName());
102             */

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 JavaDoc) fStartTime.get(i)).longValue());
107                 data[2*i+1]= createDataPoint(AFTER, InternalDimensions.SYSTEM_TIME, ((Long JavaDoc) 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 JavaDoc scalars= new HashMap JavaDoc();
115         scalars.put(dimension, new Scalar(dimension, value));
116         return new DataPoint(step, scalars);
117     }
118 }
119
Popular Tags