KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > tool > sampler > AbstractPerformanceSampler


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.activemq.tool.sampler;
18
19 import org.apache.activemq.tool.reports.PerformanceReportWriter;
20 import org.apache.activemq.tool.properties.AbstractObjectProperties;
21
22 import java.util.concurrent.atomic.AtomicBoolean JavaDoc;
23
24 public abstract class AbstractPerformanceSampler extends AbstractObjectProperties implements PerformanceSampler {
25     protected long rampUpTime = 30 * 1000; // 30 secs
26
protected long rampDownTime = 30 * 1000; // 30 secs
27
protected long duration = 5 * 60 * 1000; // 5 mins
28
protected long interval = 1000; // 1 sec
29
protected PerformanceReportWriter perfReportWriter = null;
30     protected PerformanceEventListener perfEventListener = null;
31     protected final AtomicBoolean JavaDoc isRunning = new AtomicBoolean JavaDoc(false);
32
33     protected long sampleIndex = 0;
34
35     public long getRampUpTime() {
36         return rampUpTime;
37     }
38
39     public void setRampUpTime(long rampUpTime) {
40         this.rampUpTime = rampUpTime;
41     }
42
43     public long getRampDownTime() {
44         return rampDownTime;
45     }
46
47     public void setRampDownTime(long rampDownTime) {
48         this.rampDownTime = rampDownTime;
49     }
50
51     public long getDuration() {
52         return duration;
53     }
54
55     public void setDuration(long duration) {
56         this.duration = duration;
57     }
58
59     public long getInterval() {
60         return interval;
61     }
62
63     public void setInterval(long interval) {
64         this.interval = interval;
65     }
66
67     public PerformanceReportWriter getPerfReportWriter() {
68         return perfReportWriter;
69     }
70
71     public void setPerfReportWriter(PerformanceReportWriter perfReportWriter) {
72         this.perfReportWriter = perfReportWriter;
73     }
74
75     public PerformanceEventListener getPerfEventListener() {
76         return perfEventListener;
77     }
78
79     public void setPerfEventListener(PerformanceEventListener perfEventListener) {
80         this.perfEventListener = perfEventListener;
81     }
82
83     public void startSampler() {
84         isRunning.set(true);
85         Thread JavaDoc t = new Thread JavaDoc(this);
86         t.start();
87     }
88
89     public void run() {
90         try {
91             onRampUpStart();
92             if (perfEventListener != null) {
93                 perfEventListener.onRampUpStart(this);
94             }
95
96             try {
97                 Thread.sleep(rampUpTime);
98             } catch (InterruptedException JavaDoc e) {
99                 e.printStackTrace();
100             }
101
102             onSamplerStart();
103             if (perfEventListener != null) {
104                 perfEventListener.onSamplerStart(this);
105             }
106
107             sample();
108
109             onSamplerEnd();
110             if (perfEventListener != null) {
111                 perfEventListener.onSamplerEnd(this);
112             }
113
114             try {
115                 Thread.sleep(rampDownTime);
116             } catch (InterruptedException JavaDoc e) {
117                 e.printStackTrace();
118             }
119
120             onRampDownEnd();
121             if (perfEventListener != null) {
122                 perfEventListener.onRampDownEnd(this);
123             }
124         } finally {
125             isRunning.set(false);
126             synchronized (isRunning) {
127                 isRunning.notifyAll();
128             }
129         }
130     }
131
132     protected void sample() {
133         // Compute for the actual duration window of the sampler
134
long endTime = System.currentTimeMillis() + duration - rampDownTime - rampUpTime;
135
136         while (System.currentTimeMillis() < endTime) {
137             try {
138                 Thread.sleep(interval);
139             } catch (InterruptedException JavaDoc e) {
140                 e.printStackTrace();
141             }
142             sampleData();
143             sampleIndex++;
144         }
145     }
146
147     public abstract void sampleData();
148
149     public boolean isRunning() {
150         return isRunning.get();
151     }
152
153     public void waitUntilDone() {
154         while (isRunning()) {
155             try {
156                 synchronized (isRunning) {
157                     isRunning.wait(0);
158                 }
159             } catch (InterruptedException JavaDoc e) {
160                 e.printStackTrace();
161             }
162         }
163     }
164
165     // Call back functions to customize behavior of thread.
166
protected void onRampUpStart() {}
167     protected void onSamplerStart() {}
168     protected void onSamplerEnd() {}
169     protected void onRampDownEnd() {}
170 }
171
Popular Tags