001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.activemq.tool.sampler;
018    
019    import java.util.concurrent.atomic.AtomicBoolean;
020    
021    import org.apache.activemq.tool.properties.AbstractObjectProperties;
022    import org.apache.activemq.tool.reports.PerformanceReportWriter;
023    
024    public abstract class AbstractPerformanceSampler extends AbstractObjectProperties implements PerformanceSampler {
025        
026        protected long rampUpTime = 30 * 1000; // 30 secs
027        protected long rampDownTime = 30 * 1000; // 30 secs
028        protected long duration = 5 * 60 * 1000; // 5 mins
029        protected long interval = 1000; // 1 sec
030        protected PerformanceReportWriter perfReportWriter;
031        protected PerformanceEventListener perfEventListener;
032        protected final AtomicBoolean isRunning = new AtomicBoolean(false);
033        protected long sampleIndex;
034    
035        public long getRampUpTime() {
036            return rampUpTime;
037        }
038    
039        public void setRampUpTime(long rampUpTime) {
040            this.rampUpTime = rampUpTime;
041        }
042    
043        public long getRampDownTime() {
044            return rampDownTime;
045        }
046    
047        public void setRampDownTime(long rampDownTime) {
048            this.rampDownTime = rampDownTime;
049        }
050    
051        public long getDuration() {
052            return duration;
053        }
054    
055        public void setDuration(long duration) {
056            this.duration = duration;
057        }
058    
059        public long getInterval() {
060            return interval;
061        }
062    
063        public void setInterval(long interval) {
064            this.interval = interval;
065        }
066    
067        public PerformanceReportWriter getPerfReportWriter() {
068            return perfReportWriter;
069        }
070    
071        public void setPerfReportWriter(PerformanceReportWriter perfReportWriter) {
072            this.perfReportWriter = perfReportWriter;
073        }
074    
075        public PerformanceEventListener getPerfEventListener() {
076            return perfEventListener;
077        }
078    
079        public void setPerfEventListener(PerformanceEventListener perfEventListener) {
080            this.perfEventListener = perfEventListener;
081        }
082    
083        public void startSampler() {
084            isRunning.set(true);
085            Thread t = new Thread(this);
086            t.start();
087        }
088    
089        public void run() {
090            try {
091                onRampUpStart();
092                if (perfEventListener != null) {
093                    perfEventListener.onRampUpStart(this);
094                }
095    
096                try {
097                    Thread.sleep(rampUpTime);
098                } catch (InterruptedException e) {
099                    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 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 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 e) {
160                    e.printStackTrace();
161                }
162            }
163        }
164    
165        // Call back functions to customize behavior of thread.
166        protected void onRampUpStart() {
167        }
168    
169        protected void onSamplerStart() {
170        }
171    
172        protected void onSamplerEnd() {
173        }
174    
175        protected void onRampDownEnd() {
176        }
177    }