1 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 ; 23 24 public abstract class AbstractPerformanceSampler extends AbstractObjectProperties implements PerformanceSampler { 25 protected long rampUpTime = 30 * 1000; protected long rampDownTime = 30 * 1000; protected long duration = 5 * 60 * 1000; protected long interval = 1000; protected PerformanceReportWriter perfReportWriter = null; 30 protected PerformanceEventListener perfEventListener = null; 31 protected final AtomicBoolean isRunning = new AtomicBoolean (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 t = new Thread (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 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 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 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 protected void onRampUpStart() {} 167 protected void onSamplerStart() {} 168 protected void onSamplerEnd() {} 169 protected void onRampDownEnd() {} 170 } 171 | Popular Tags |