- Redesigned the sampler and added the cpu sampler.

git-svn-id: https://svn.apache.org/repos/asf/incubator/activemq/trunk@416547 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Adrian T. Co 2006-06-23 02:26:20 +00:00
parent f1c6e4c663
commit 80bf2417f1
8 changed files with 345 additions and 0 deletions

View File

@ -0,0 +1,150 @@
package org.apache.activemq.tool.sampler;
import org.apache.activemq.tool.reports.PerformanceReportWriter;
import org.apache.activemq.tool.properties.AbstractObjectProperties;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicBoolean;
public abstract class AbstractPerformanceSampler extends AbstractObjectProperties implements PerformanceSampler {
protected long rampUpTime = 30 * 1000; // 30 secs
protected long rampDownTime = 30 * 1000; // 30 secs
protected long duration = 5 * 60 * 1000; // 5 mins
protected long interval = 1000; // 1 sec
protected PerformanceReportWriter perfReportWriter = null;
protected PerformanceEventListener perfEventListener = null;
protected final AtomicBoolean isRunning = new AtomicBoolean(false);
protected long sampleIndex = 0;
public long getRampUpTime() {
return rampUpTime;
}
public void setRampUpTime(long rampUpTime) {
this.rampUpTime = rampUpTime;
}
public long getRampDownTime() {
return rampDownTime;
}
public void setRampDownTime(long rampDownTime) {
this.rampDownTime = rampDownTime;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getInterval() {
return interval;
}
public void setInterval(long interval) {
this.interval = interval;
}
public PerformanceReportWriter getPerfReportWriter() {
return perfReportWriter;
}
public void setPerfReportWriter(PerformanceReportWriter perfReportWriter) {
this.perfReportWriter = perfReportWriter;
}
public PerformanceEventListener getPerfEventListener() {
return perfEventListener;
}
public void setPerfEventListener(PerformanceEventListener perfEventListener) {
this.perfEventListener = perfEventListener;
}
public void startSampler() {
isRunning.set(true);
Thread t = new Thread(this);
t.start();
}
public void run() {
try {
// Compute for the actual duration window of the sampler
long endTime = System.currentTimeMillis() + duration - rampDownTime;
onRampUpStart();
if (perfEventListener != null) {
perfEventListener.onRampUpStart(this);
}
try {
Thread.sleep(rampUpTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
onSamplerStart();
if (perfEventListener != null) {
perfEventListener.onSamplerStart(this);
}
while (System.currentTimeMillis() < endTime) {
try {
Thread.sleep(interval);
} catch (InterruptedException e) {
e.printStackTrace();
}
sampleData();
sampleIndex++;
}
onSamplerEnd();
if (perfEventListener != null) {
perfEventListener.onSamplerEnd(this);
}
try {
Thread.sleep(rampDownTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
onRampDownEnd();
if (perfEventListener != null) {
perfEventListener.onRampDownEnd(this);
}
} finally {
isRunning.set(false);
synchronized (isRunning) {
isRunning.notifyAll();
}
}
}
public abstract void sampleData();
public boolean isRunning() {
return isRunning.get();
}
public void waitUntilDone() {
while (isRunning()) {
try {
synchronized (isRunning) {
isRunning.wait(0);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// Call back functions to customize behavior of thread.
protected void onRampUpStart() {}
protected void onSamplerStart() {}
protected void onSamplerEnd() {}
protected void onRampDownEnd() {}
}

View File

@ -0,0 +1,34 @@
package org.apache.activemq.tool.sampler;
import org.apache.activemq.tool.sampler.plugins.CpuSamplerPlugin;
import org.apache.activemq.tool.sampler.plugins.LinuxCpuSamplerPlugin;
import org.apache.activemq.tool.reports.plugins.ReportPlugin;
import java.io.IOException;
public class CpuSamplerTask extends AbstractPerformanceSampler {
private CpuSamplerPlugin plugin = null;
public void createPlugin() throws IOException {
createPlugin(System.getProperty("os.name"));
}
public void createPlugin(String osName) throws IOException {
if (osName == null) {
throw new IOException("No defined OS name found. Foound: " + osName);
}
if (osName.equalsIgnoreCase(CpuSamplerPlugin.LINUX)) {
plugin = new LinuxCpuSamplerPlugin();
} else {
throw new IOException("No CPU Sampler Plugin found for OS: " + osName + ". CPU Sampler will not be started.");
}
}
public void sampleData() {
if (plugin != null && perfReportWriter != null) {
perfReportWriter.writeCsvData(ReportPlugin.REPORT_PLUGIN_CPU, plugin.getCpuUtilizationStats());
}
}
}

View File

@ -0,0 +1,23 @@
/**
*
* Copyright 2005-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.activemq.tool.sampler;
public interface MeasurableClient {
public void reset();
public String getClientName();
public long getThroughput();
}

View File

@ -0,0 +1,8 @@
package org.apache.activemq.tool.sampler;
public interface PerformanceEventListener {
public void onRampUpStart(PerformanceSampler sampler);
public void onSamplerStart(PerformanceSampler sampler);
public void onSamplerEnd(PerformanceSampler sampler);
public void onRampDownEnd(PerformanceSampler sampler);
}

View File

@ -0,0 +1,22 @@
package org.apache.activemq.tool.sampler;
import org.apache.activemq.tool.reports.PerformanceReportWriter;
public interface PerformanceSampler extends Runnable {
public long getRampUpTime();
public void setRampUpTime(long rampUpTime);
public long getRampDownTime();
public void setRampDownTime(long rampDownTime);
public long getDuration();
public void setDuration(long duration);
public long getInterval();
public void setInterval(long interval);
public PerformanceReportWriter getPerfReportWriter();
public void setPerfReportWriter(PerformanceReportWriter writer);
public PerformanceEventListener getPerfEventListener();
public void setPerfEventListener(PerformanceEventListener listener);
public void sampleData();
public boolean isRunning();
public void waitUntilDone();
}

View File

@ -0,0 +1,39 @@
package org.apache.activemq.tool.sampler;
import org.apache.activemq.tool.reports.plugins.ReportPlugin;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ThroughputSamplerTask extends AbstractPerformanceSampler {
private final Object mutex = new Object();
private List clients = new ArrayList();
public void sampleData() {
for (Iterator i = clients.iterator(); i.hasNext();) {
MeasurableClient client = (MeasurableClient) i.next();
if (perfReportWriter != null) {
perfReportWriter.writeCsvData(ReportPlugin.REPORT_PLUGIN_THROUGHPUT,
"index=" + sampleIndex + ",clientName=" + client.getClientName() +
",throughput=" + client.getThroughput());
}
client.reset();
}
}
public void registerClient(MeasurableClient client) {
synchronized (mutex) {
clients.add(client);
}
}
protected void onSamplerStart() {
// Reset the throughput of the clients
for (Iterator i = clients.iterator(); i.hasNext();) {
MeasurableClient client = (MeasurableClient) i.next();
client.reset();
}
}
}

View File

@ -0,0 +1,19 @@
package org.apache.activemq.tool.sampler.plugins;
public interface CpuSamplerPlugin {
public final static String WINDOWS_2000 = "Windows 2000";
public final static String WINDOWS_NT = "Windows NT";
public final static String WINDOWS_XP = "Windows XP";
public final static String WINDOWS_95 = "Windows 95";
public final static String WINDOWS_CE = "Windows CE";
public final static String LINUX = "Linux";
public final static String SOLARIS = "Solaris";
public final static String AIX = "AIX";
public final static String FREEBSD = "FreeBSD";
public final static String MAC_OS = "Mac OS";
public final static String MAC_OS_X = "Mac OS X";
public final static String POWERPC = "PowerPC";
public final static String OS_2 = "OS/2";
public String getCpuUtilizationStats();
}

View File

@ -0,0 +1,50 @@
package org.apache.activemq.tool.sampler.plugins;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LinuxCpuSamplerPlugin implements CpuSamplerPlugin {
private String vmstat = "vmstat";
public String getCpuUtilizationStats() {
try {
Process p = Runtime.getRuntime().exec(vmstat);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
br.readLine(); // throw away the first line
String header = br.readLine();
String data = br.readLine();
br.close();
// Convert to CSV of key=value pair
return convertToCSV(header, data);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
public String getVmstat() {
return vmstat;
}
public void setVmstat(String vmstat) {
this.vmstat = vmstat;
}
protected String convertToCSV(String header, String data) {
StringTokenizer headerTokens = new StringTokenizer(header, " ");
StringTokenizer dataTokens = new StringTokenizer(data, " ");
String csv = "";
while (headerTokens.hasMoreTokens()) {
csv += (headerTokens.nextToken() + "=" + dataTokens.nextToken() + ";");
}
return csv;
}
}