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:
parent
b4fa507e8c
commit
6df80916c4
|
@ -155,15 +155,11 @@ public class AgentResultDataCollector extends AbstractDataCollector {
|
|||
// ///////////////////////////////
|
||||
/**
|
||||
* For the failed one, only the fail count statistics will be added, Others
|
||||
* of this failed one will be ignored. And the shouldBeCountResponseTime is
|
||||
* to identify if the result is from UserBehavior
|
||||
* of this failed one will be ignored.
|
||||
*
|
||||
* @param behaviorResult
|
||||
*/
|
||||
private void addItem(BehaviorResult behaviorResult) {
|
||||
if (!behaviorResult.isShouldBeCountResponseTime()) {
|
||||
return;
|
||||
}
|
||||
if (behaviorResult.isSuccess()) {
|
||||
this.successCountOfThisCall++;
|
||||
this.totalResponseTimeOfThisCall += behaviorResult
|
||||
|
|
|
@ -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();
|
||||
}
|
|
@ -3,6 +3,7 @@ package org.bench4q.agent.plugin.basic;
|
|||
import org.bench4q.agent.plugin.Behavior;
|
||||
import org.bench4q.agent.plugin.Parameter;
|
||||
import org.bench4q.agent.plugin.Plugin;
|
||||
import org.bench4q.agent.plugin.result.TimerReturn;
|
||||
|
||||
@Plugin("ConstantTimer")
|
||||
public class ConstantTimerPlugin {
|
||||
|
@ -11,13 +12,13 @@ public class ConstantTimerPlugin {
|
|||
}
|
||||
|
||||
@Behavior("Sleep")
|
||||
public boolean sleep(@Parameter("time") int time) {
|
||||
public TimerReturn sleep(@Parameter("time") int time) {
|
||||
try {
|
||||
Thread.sleep(time);
|
||||
return true;
|
||||
return new TimerReturn(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
return new TimerReturn(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,18 @@
|
|||
package org.bench4q.agent.plugin.basic;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.bench4q.agent.plugin.Behavior;
|
||||
import org.bench4q.agent.plugin.Parameter;
|
||||
import org.bench4q.agent.plugin.Plugin;
|
||||
import org.bench4q.agent.plugin.result.HttpReturn;
|
||||
|
||||
@Plugin("Http")
|
||||
public class HttpPlugin {
|
||||
|
@ -20,42 +23,79 @@ public class HttpPlugin {
|
|||
}
|
||||
|
||||
@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 {
|
||||
URL target = new URL(url);
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) target
|
||||
.openConnection();
|
||||
httpURLConnection.setDoOutput(false);
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setUseCaches(false);
|
||||
httpURLConnection.setRequestMethod("GET");
|
||||
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) {
|
||||
this.logger.error(e.toString() + " when url is " + url);
|
||||
return false;
|
||||
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);
|
||||
httpURLConnection = (HttpURLConnection) target.openConnection();
|
||||
if (method.equals("GET")) {
|
||||
httpURLConnection.setDoOutput(false);
|
||||
} else {
|
||||
httpURLConnection.setDoOutput(true);
|
||||
}
|
||||
httpURLConnection.setDoOutput(true);
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setUseCaches(false);
|
||||
httpURLConnection.setRequestMethod(method);
|
||||
return httpURLConnection;
|
||||
}
|
||||
|
||||
private StringBuffer readConnectionInputStreamToBuffer(
|
||||
HttpURLConnection httpURLConnection) throws IOException {
|
||||
StringBuffer stringBuffer;
|
||||
BufferedReader bufferedReader = new BufferedReader(
|
||||
new InputStreamReader(httpURLConnection.getInputStream()));
|
||||
int temp = -1;
|
||||
stringBuffer = new StringBuffer();
|
||||
while ((temp = bufferedReader.read()) != -1) {
|
||||
stringBuffer.append((char) temp);
|
||||
}
|
||||
bufferedReader.close();
|
||||
return stringBuffer;
|
||||
}
|
||||
|
||||
private void writeContentToConnectOutputSteam(String content,
|
||||
HttpURLConnection httpURLConnection) throws IOException {
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
|
||||
httpURLConnection.getOutputStream());
|
||||
outputStreamWriter.write(content);
|
||||
outputStreamWriter.flush();
|
||||
outputStreamWriter.close();
|
||||
}
|
||||
|
||||
@Behavior("Post")
|
||||
public boolean post(@Parameter("url") String url,
|
||||
public HttpReturn post(@Parameter("url") String url,
|
||||
@Parameter("content") String content,
|
||||
@Parameter("contentType") String contentType,
|
||||
@Parameter("accept") String accept) {
|
||||
HttpURLConnection httpURLConnection = null;
|
||||
String requestMethod = "POST";
|
||||
StringBuffer stringBuffer = null;
|
||||
try {
|
||||
URL target = new URL(url);
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) target
|
||||
.openConnection();
|
||||
httpURLConnection.setDoOutput(true);
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setUseCaches(false);
|
||||
httpURLConnection = getRequestURLConnection(url, requestMethod);
|
||||
httpURLConnection.setRequestMethod("POST");
|
||||
if (contentType != null && contentType.length() > 0) {
|
||||
httpURLConnection.setRequestProperty("Content-Type",
|
||||
|
@ -64,85 +104,69 @@ public class HttpPlugin {
|
|||
if (accept != null && accept.length() > 0) {
|
||||
httpURLConnection.setRequestProperty("Accept", accept);
|
||||
}
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
|
||||
httpURLConnection.getOutputStream());
|
||||
outputStreamWriter.write(content);
|
||||
outputStreamWriter.flush();
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
writeContentToConnectOutputSteam(content, httpURLConnection);
|
||||
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, "");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Behavior("Put")
|
||||
public boolean put(@Parameter("url") String url,
|
||||
public HttpReturn put(@Parameter("url") String url,
|
||||
@Parameter("content") String content) {
|
||||
StringBuffer stringBuffer = null;
|
||||
String requestMethod = "PUT";
|
||||
try {
|
||||
URL target = new URL(url);
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) target
|
||||
.openConnection();
|
||||
httpURLConnection.setDoOutput(true);
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setUseCaches(false);
|
||||
httpURLConnection.setRequestMethod("PUT");
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
|
||||
httpURLConnection.getOutputStream());
|
||||
outputStreamWriter.write(content);
|
||||
outputStreamWriter.flush();
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
HttpURLConnection httpURLConnection = this.getRequestURLConnection(
|
||||
url, requestMethod);
|
||||
writeContentToConnectOutputSteam(content, httpURLConnection);
|
||||
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, "");
|
||||
}
|
||||
}
|
||||
|
||||
@Behavior("Delete")
|
||||
public boolean delete(@Parameter("url") String url,
|
||||
public HttpReturn delete(@Parameter("url") String url,
|
||||
@Parameter("content") String content) {
|
||||
StringBuffer stringBuffer = null;
|
||||
String requestMethod = "DELETE";
|
||||
int responseCode = -1;
|
||||
long responseContentLength = 0;
|
||||
String responseContentType = "";
|
||||
try {
|
||||
URL target = new URL(url);
|
||||
HttpURLConnection httpURLConnection = (HttpURLConnection) target
|
||||
.openConnection();
|
||||
httpURLConnection.setDoOutput(true);
|
||||
httpURLConnection.setDoInput(true);
|
||||
httpURLConnection.setUseCaches(false);
|
||||
httpURLConnection.setRequestMethod("DELETE");
|
||||
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
|
||||
httpURLConnection.getOutputStream());
|
||||
outputStreamWriter.write(content);
|
||||
outputStreamWriter.flush();
|
||||
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;
|
||||
HttpURLConnection httpURLConnection = getRequestURLConnection(url,
|
||||
requestMethod);
|
||||
writeContentToConnectOutputSteam(content, httpURLConnection);
|
||||
stringBuffer = readConnectionInputStreamToBuffer(httpURLConnection);
|
||||
responseCode = httpURLConnection.getResponseCode();
|
||||
responseContentLength = stringBuffer.length() * 2;
|
||||
responseContentType = httpURLConnection.getContentType();
|
||||
return new HttpReturn(true, requestMethod,
|
||||
httpURLConnection.getResponseCode(),
|
||||
stringBuffer.length() * 2,
|
||||
httpURLConnection.getContentType());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
logger.info(e);
|
||||
return new HttpReturn(false, requestMethod, responseCode,
|
||||
responseContentLength, responseContentType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package org.bench4q.agent.plugin.result;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author coderfengyun
|
||||
*
|
||||
*/
|
||||
public class TimerReturn extends PluginReturn {
|
||||
public TimerReturn(boolean success) {
|
||||
this.setSuccess(success);
|
||||
}
|
||||
}
|
|
@ -12,6 +12,10 @@ public class BehaviorResult {
|
|||
private Date endDate;
|
||||
private long responseTime;
|
||||
private boolean success;
|
||||
private long contentLength;
|
||||
private int statusCode;
|
||||
private String method;
|
||||
private String contentType;
|
||||
private boolean shouldBeCountResponseTime;
|
||||
|
||||
public UUID getId() {
|
||||
|
@ -78,6 +82,38 @@ public class BehaviorResult {
|
|||
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() {
|
||||
return shouldBeCountResponseTime;
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.concurrent.Executors;
|
|||
import org.apache.log4j.Logger;
|
||||
import org.bench4q.agent.plugin.Plugin;
|
||||
import org.bench4q.agent.plugin.PluginManager;
|
||||
import org.bench4q.agent.plugin.result.PluginReturn;
|
||||
import org.bench4q.agent.scenario.behavior.Behavior;
|
||||
import org.bench4q.agent.storage.StorageHelper;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -99,15 +100,18 @@ public class ScenarioEngine {
|
|||
Object plugin = plugins.get(behavior.getUse());
|
||||
Map<String, String> behaviorParameters = prepareBehaviorParameters(behavior);
|
||||
Date startDate = new Date(System.currentTimeMillis());
|
||||
boolean success = (Boolean) this.getPluginManager().doBehavior(
|
||||
plugin, behavior.getName(), behaviorParameters);
|
||||
PluginReturn pluginReturn = (PluginReturn) this.getPluginManager()
|
||||
.doBehavior(plugin, behavior.getName(), behaviorParameters);
|
||||
Date endDate = new Date(System.currentTimeMillis());
|
||||
if (!behavior.shouldBeCountResponseTime()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
BehaviorResult result = new BehaviorResult();
|
||||
result.setId(UUID.randomUUID());
|
||||
result.setStartDate(startDate);
|
||||
result.setEndDate(endDate);
|
||||
result.setSuccess(success);
|
||||
result.setSuccess(pluginReturn.isSuccess());
|
||||
result.setResponseTime(endDate.getTime() - startDate.getTime());
|
||||
result.setBehaviorName(behavior.getName());
|
||||
result.setPluginId(behavior.getUse());
|
||||
|
@ -116,12 +120,6 @@ public class ScenarioEngine {
|
|||
result.setShouldBeCountResponseTime(behavior
|
||||
.shouldBeCountResponseTime());
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -18,7 +18,7 @@ public class TestWithScriptFile {
|
|||
private HttpRequester httpRequester;
|
||||
private String url = "http://localhost:6565/test/run";
|
||||
private String filePath;
|
||||
private static int load = 100;
|
||||
private static int load = 1;
|
||||
|
||||
public String getFilePath() {
|
||||
return filePath;
|
||||
|
|
Loading…
Reference in New Issue