add an inheritance hierarchy to let the plugin give back a plugin

return, not boolean any more. It's importance especially for http
plugin.
This commit is contained in:
Tienan Chen 2013-11-20 14:20:47 +08:00
parent b4fa507e8c
commit 6df80916c4
10 changed files with 246 additions and 123 deletions

View File

@ -155,15 +155,11 @@ public class AgentResultDataCollector extends AbstractDataCollector {
// /////////////////////////////// // ///////////////////////////////
/** /**
* For the failed one, only the fail count statistics will be added, Others * For the failed one, only the fail count statistics will be added, Others
* of this failed one will be ignored. And the shouldBeCountResponseTime is * of this failed one will be ignored.
* to identify if the result is from UserBehavior
* *
* @param behaviorResult * @param behaviorResult
*/ */
private void addItem(BehaviorResult behaviorResult) { private void addItem(BehaviorResult behaviorResult) {
if (!behaviorResult.isShouldBeCountResponseTime()) {
return;
}
if (behaviorResult.isSuccess()) { if (behaviorResult.isSuccess()) {
this.successCountOfThisCall++; this.successCountOfThisCall++;
this.totalResponseTimeOfThisCall += behaviorResult this.totalResponseTimeOfThisCall += behaviorResult

View File

@ -1,12 +0,0 @@
package org.bench4q.agent.plugin;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ShouldCountResponseTime {
String value();
}

View File

@ -3,6 +3,7 @@ package org.bench4q.agent.plugin.basic;
import org.bench4q.agent.plugin.Behavior; import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Parameter; import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.result.TimerReturn;
@Plugin("ConstantTimer") @Plugin("ConstantTimer")
public class ConstantTimerPlugin { public class ConstantTimerPlugin {
@ -11,13 +12,13 @@ public class ConstantTimerPlugin {
} }
@Behavior("Sleep") @Behavior("Sleep")
public boolean sleep(@Parameter("time") int time) { public TimerReturn sleep(@Parameter("time") int time) {
try { try {
Thread.sleep(time); Thread.sleep(time);
return true; return new TimerReturn(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false; return new TimerReturn(false);
} }
} }
} }

View File

@ -1,15 +1,18 @@
package org.bench4q.agent.plugin.basic; package org.bench4q.agent.plugin.basic;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Behavior; import org.bench4q.agent.plugin.Behavior;
import org.bench4q.agent.plugin.Parameter; import org.bench4q.agent.plugin.Parameter;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.result.HttpReturn;
@Plugin("Http") @Plugin("Http")
public class HttpPlugin { public class HttpPlugin {
@ -20,42 +23,79 @@ public class HttpPlugin {
} }
@Behavior("Get") @Behavior("Get")
public boolean get(@Parameter("url") String url) { public HttpReturn get(@Parameter("url") String url) {
HttpURLConnection httpURLConnection = null;
StringBuffer stringBuffer = null;
String requestMethod = "GET";
try { try {
httpURLConnection = getRequestURLConnection(url, requestMethod);
stringBuffer = readConnectionInputStreamToBuffer(httpURLConnection);
return new HttpReturn(true, requestMethod,
httpURLConnection.getResponseCode(),
stringBuffer.length() * 2,
httpURLConnection.getContentType());
} catch (MalformedURLException e) {
logger.info(e.getStackTrace());
return new HttpReturn(false, requestMethod, 400, -1, "");
} catch (IOException e) {
logger.info(e.getStackTrace());
return new HttpReturn(false, requestMethod, 404, -1, "");
}
}
private HttpURLConnection getRequestURLConnection(String url, String method)
throws MalformedURLException, IOException {
HttpURLConnection httpURLConnection;
URL target = new URL(url); URL target = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) target httpURLConnection = (HttpURLConnection) target.openConnection();
.openConnection(); if (method.equals("GET")) {
httpURLConnection.setDoOutput(false); httpURLConnection.setDoOutput(false);
} else {
httpURLConnection.setDoOutput(true);
}
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true); httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false); httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("GET"); httpURLConnection.setRequestMethod(method);
return httpURLConnection;
}
private StringBuffer readConnectionInputStreamToBuffer(
HttpURLConnection httpURLConnection) throws IOException {
StringBuffer stringBuffer;
BufferedReader bufferedReader = new BufferedReader( BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream())); new InputStreamReader(httpURLConnection.getInputStream()));
int temp = -1; int temp = -1;
StringBuffer stringBuffer = new StringBuffer(); stringBuffer = new StringBuffer();
while ((temp = bufferedReader.read()) != -1) { while ((temp = bufferedReader.read()) != -1) {
stringBuffer.append((char) temp); stringBuffer.append((char) temp);
} }
bufferedReader.close(); bufferedReader.close();
return true; return stringBuffer;
} catch (Exception e) {
this.logger.error(e.toString() + " when url is " + url);
return false;
} }
private void writeContentToConnectOutputSteam(String content,
HttpURLConnection httpURLConnection) throws IOException {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
httpURLConnection.getOutputStream());
outputStreamWriter.write(content);
outputStreamWriter.flush();
outputStreamWriter.close();
} }
@Behavior("Post") @Behavior("Post")
public boolean post(@Parameter("url") String url, public HttpReturn post(@Parameter("url") String url,
@Parameter("content") String content, @Parameter("content") String content,
@Parameter("contentType") String contentType, @Parameter("contentType") String contentType,
@Parameter("accept") String accept) { @Parameter("accept") String accept) {
HttpURLConnection httpURLConnection = null;
String requestMethod = "POST";
StringBuffer stringBuffer = null;
try { try {
URL target = new URL(url); httpURLConnection = getRequestURLConnection(url, requestMethod);
HttpURLConnection httpURLConnection = (HttpURLConnection) target
.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestMethod("POST");
if (contentType != null && contentType.length() > 0) { if (contentType != null && contentType.length() > 0) {
httpURLConnection.setRequestProperty("Content-Type", httpURLConnection.setRequestProperty("Content-Type",
@ -64,85 +104,69 @@ public class HttpPlugin {
if (accept != null && accept.length() > 0) { if (accept != null && accept.length() > 0) {
httpURLConnection.setRequestProperty("Accept", accept); httpURLConnection.setRequestProperty("Accept", accept);
} }
OutputStreamWriter outputStreamWriter = new OutputStreamWriter( writeContentToConnectOutputSteam(content, httpURLConnection);
httpURLConnection.getOutputStream()); stringBuffer = readConnectionInputStreamToBuffer(httpURLConnection);
outputStreamWriter.write(content); return new HttpReturn(true, requestMethod,
outputStreamWriter.flush(); httpURLConnection.getResponseCode(),
outputStreamWriter.close(); stringBuffer.length() * 2,
BufferedReader bufferedReader = new BufferedReader( httpURLConnection.getContentType());
new InputStreamReader(httpURLConnection.getInputStream())); } catch (MalformedURLException e) {
int temp = -1; logger.info(e.getStackTrace());
StringBuffer stringBuffer = new StringBuffer(); return new HttpReturn(false, requestMethod, 400, -1, "");
while ((temp = bufferedReader.read()) != -1) { } catch (IOException e) {
stringBuffer.append((char) temp); logger.info(e.getStackTrace());
} return new HttpReturn(false, requestMethod, 404, -1, "");
bufferedReader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} }
} }
@Behavior("Put") @Behavior("Put")
public boolean put(@Parameter("url") String url, public HttpReturn put(@Parameter("url") String url,
@Parameter("content") String content) { @Parameter("content") String content) {
StringBuffer stringBuffer = null;
String requestMethod = "PUT";
try { try {
URL target = new URL(url); HttpURLConnection httpURLConnection = this.getRequestURLConnection(
HttpURLConnection httpURLConnection = (HttpURLConnection) target url, requestMethod);
.openConnection(); writeContentToConnectOutputSteam(content, httpURLConnection);
httpURLConnection.setDoOutput(true); stringBuffer = readConnectionInputStreamToBuffer(httpURLConnection);
httpURLConnection.setDoInput(true); return new HttpReturn(true, requestMethod,
httpURLConnection.setUseCaches(false); httpURLConnection.getResponseCode(),
httpURLConnection.setRequestMethod("PUT"); stringBuffer.length() * 2,
OutputStreamWriter outputStreamWriter = new OutputStreamWriter( httpURLConnection.getContentType());
httpURLConnection.getOutputStream()); } catch (MalformedURLException e) {
outputStreamWriter.write(content); logger.info(e.getStackTrace());
outputStreamWriter.flush(); return new HttpReturn(false, requestMethod, 400, -1, "");
outputStreamWriter.close(); } catch (IOException e) {
BufferedReader bufferedReader = new BufferedReader( logger.info(e.getStackTrace());
new InputStreamReader(httpURLConnection.getInputStream())); return new HttpReturn(false, requestMethod, 404, -1, "");
int temp = -1;
StringBuffer stringBuffer = new StringBuffer();
while ((temp = bufferedReader.read()) != -1) {
stringBuffer.append((char) temp);
}
bufferedReader.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} }
} }
@Behavior("Delete") @Behavior("Delete")
public boolean delete(@Parameter("url") String url, public HttpReturn delete(@Parameter("url") String url,
@Parameter("content") String content) { @Parameter("content") String content) {
StringBuffer stringBuffer = null;
String requestMethod = "DELETE";
int responseCode = -1;
long responseContentLength = 0;
String responseContentType = "";
try { try {
URL target = new URL(url); HttpURLConnection httpURLConnection = getRequestURLConnection(url,
HttpURLConnection httpURLConnection = (HttpURLConnection) target requestMethod);
.openConnection(); writeContentToConnectOutputSteam(content, httpURLConnection);
httpURLConnection.setDoOutput(true); stringBuffer = readConnectionInputStreamToBuffer(httpURLConnection);
httpURLConnection.setDoInput(true); responseCode = httpURLConnection.getResponseCode();
httpURLConnection.setUseCaches(false); responseContentLength = stringBuffer.length() * 2;
httpURLConnection.setRequestMethod("DELETE"); responseContentType = httpURLConnection.getContentType();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter( return new HttpReturn(true, requestMethod,
httpURLConnection.getOutputStream()); httpURLConnection.getResponseCode(),
outputStreamWriter.write(content); stringBuffer.length() * 2,
outputStreamWriter.flush(); httpURLConnection.getContentType());
outputStreamWriter.close();
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream()));
int temp = -1;
StringBuffer stringBuffer = new StringBuffer();
while ((temp = bufferedReader.read()) != -1) {
stringBuffer.append((char) temp);
}
bufferedReader.close();
return true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.info(e);
return false; return new HttpReturn(false, requestMethod, responseCode,
responseContentLength, responseContentType);
} }
} }
} }

View File

@ -0,0 +1,55 @@
package org.bench4q.agent.plugin.result;
/***
* the contentLength's unit is Byte
*
* @author coderfengyun
*
*/
public class HttpReturn extends PluginReturn {
private String method;
private int statusCode;
private long contentLength;
private String contentType;
public String getMethod() {
return method;
}
private void setMethod(String method) {
this.method = method;
}
public int getStatusCode() {
return statusCode;
}
private void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public long getContentLength() {
return contentLength;
}
private void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public String getContentType() {
return contentType;
}
private void setContentType(String contentType) {
this.contentType = contentType;
}
public HttpReturn(boolean success, String method, int statusCode,
long contentLength, String contentType) {
this.setSuccess(success);
this.setMethod(method);
this.setStatusCode(statusCode);
this.setContentLength(contentLength);
this.setContentType(contentType);
}
}

View File

@ -0,0 +1,13 @@
package org.bench4q.agent.plugin.result;
public abstract class PluginReturn {
private boolean success;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}

View File

@ -0,0 +1,12 @@
package org.bench4q.agent.plugin.result;
/**
*
* @author coderfengyun
*
*/
public class TimerReturn extends PluginReturn {
public TimerReturn(boolean success) {
this.setSuccess(success);
}
}

View File

@ -12,6 +12,10 @@ public class BehaviorResult {
private Date endDate; private Date endDate;
private long responseTime; private long responseTime;
private boolean success; private boolean success;
private long contentLength;
private int statusCode;
private String method;
private String contentType;
private boolean shouldBeCountResponseTime; private boolean shouldBeCountResponseTime;
public UUID getId() { public UUID getId() {
@ -78,6 +82,38 @@ public class BehaviorResult {
this.success = success; this.success = success;
} }
public long getContentLength() {
return contentLength;
}
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public boolean isShouldBeCountResponseTime() { public boolean isShouldBeCountResponseTime() {
return shouldBeCountResponseTime; return shouldBeCountResponseTime;
} }

View File

@ -12,6 +12,7 @@ import java.util.concurrent.Executors;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.Plugin; import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.plugin.result.PluginReturn;
import org.bench4q.agent.scenario.behavior.Behavior; import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.storage.StorageHelper; import org.bench4q.agent.storage.StorageHelper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -99,15 +100,18 @@ public class ScenarioEngine {
Object plugin = plugins.get(behavior.getUse()); Object plugin = plugins.get(behavior.getUse());
Map<String, String> behaviorParameters = prepareBehaviorParameters(behavior); Map<String, String> behaviorParameters = prepareBehaviorParameters(behavior);
Date startDate = new Date(System.currentTimeMillis()); Date startDate = new Date(System.currentTimeMillis());
boolean success = (Boolean) this.getPluginManager().doBehavior( PluginReturn pluginReturn = (PluginReturn) this.getPluginManager()
plugin, behavior.getName(), behaviorParameters); .doBehavior(plugin, behavior.getName(), behaviorParameters);
Date endDate = new Date(System.currentTimeMillis()); Date endDate = new Date(System.currentTimeMillis());
if (!behavior.shouldBeCountResponseTime()) {
continue;
}
BehaviorResult result = new BehaviorResult(); BehaviorResult result = new BehaviorResult();
result.setId(UUID.randomUUID()); result.setId(UUID.randomUUID());
result.setStartDate(startDate); result.setStartDate(startDate);
result.setEndDate(endDate); result.setEndDate(endDate);
result.setSuccess(success); result.setSuccess(pluginReturn.isSuccess());
result.setResponseTime(endDate.getTime() - startDate.getTime()); result.setResponseTime(endDate.getTime() - startDate.getTime());
result.setBehaviorName(behavior.getName()); result.setBehaviorName(behavior.getName());
result.setPluginId(behavior.getUse()); result.setPluginId(behavior.getUse());
@ -116,12 +120,6 @@ public class ScenarioEngine {
result.setShouldBeCountResponseTime(behavior result.setShouldBeCountResponseTime(behavior
.shouldBeCountResponseTime()); .shouldBeCountResponseTime());
ret.add(result); ret.add(result);
// TODO: refactor, collect result here, and judge if to calculate
// that
// behavior's result,because it may be a userBehavior or
// timerBehavior
// behavior.buildBehaviorResultAndAdd(plugin, startDate, success,
// endDate, ret);
} }
return ret; return ret;
} }

View File

@ -18,7 +18,7 @@ public class TestWithScriptFile {
private HttpRequester httpRequester; private HttpRequester httpRequester;
private String url = "http://localhost:6565/test/run"; private String url = "http://localhost:6565/test/run";
private String filePath; private String filePath;
private static int load = 100; private static int load = 1;
public String getFilePath() { public String getFilePath() {
return filePath; return filePath;