From d0a14e6ea4ae133af1f2e8c782ae0ac7dc4befe9 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Fri, 28 Jun 2013 15:24:38 +0800 Subject: [PATCH] Constant timer and Http plugin added. --- .../org/bench4q/agent/api/HomeController.java | 2 +- .../bench4q/agent/api/PluginController.java | 4 +- .../org/bench4q/agent/api/TestController.java | 17 ++--- .../plugin/{annotation => }/Behavior.java | 2 +- .../plugin/{metadata => }/BehaviorInfo.java | 2 +- .../agent/plugin/{annotation => }/Plugin.java | 2 +- .../plugin/{metadata => }/PluginInfo.java | 2 +- .../bench4q/agent/plugin/PluginManager.java | 4 -- .../bench4q/agent/plugin/http/HttpPlugin.java | 71 ++++++++++++++----- .../plugin/timer/ConstantTimerPlugin.java | 9 ++- 10 files changed, 72 insertions(+), 43 deletions(-) rename src/main/java/org/bench4q/agent/plugin/{annotation => }/Behavior.java (82%) rename src/main/java/org/bench4q/agent/plugin/{metadata => }/BehaviorInfo.java (83%) rename src/main/java/org/bench4q/agent/plugin/{annotation => }/Plugin.java (82%) rename src/main/java/org/bench4q/agent/plugin/{metadata => }/PluginInfo.java (87%) diff --git a/src/main/java/org/bench4q/agent/api/HomeController.java b/src/main/java/org/bench4q/agent/api/HomeController.java index 8f5c131b..46a0f120 100644 --- a/src/main/java/org/bench4q/agent/api/HomeController.java +++ b/src/main/java/org/bench4q/agent/api/HomeController.java @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/") public class HomeController { - @RequestMapping(method = RequestMethod.GET) + @RequestMapping(method = { RequestMethod.GET, RequestMethod.POST }) @ResponseBody public String index() { diff --git a/src/main/java/org/bench4q/agent/api/PluginController.java b/src/main/java/org/bench4q/agent/api/PluginController.java index 620e1b1b..a701b443 100644 --- a/src/main/java/org/bench4q/agent/api/PluginController.java +++ b/src/main/java/org/bench4q/agent/api/PluginController.java @@ -6,9 +6,9 @@ import java.util.List; import org.bench4q.agent.api.model.BehaviorInfoModel; import org.bench4q.agent.api.model.PluginInfoListModel; import org.bench4q.agent.api.model.PluginInfoModel; +import org.bench4q.agent.plugin.BehaviorInfo; +import org.bench4q.agent.plugin.PluginInfo; import org.bench4q.agent.plugin.PluginManager; -import org.bench4q.agent.plugin.metadata.BehaviorInfo; -import org.bench4q.agent.plugin.metadata.PluginInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/org/bench4q/agent/api/TestController.java b/src/main/java/org/bench4q/agent/api/TestController.java index ca46e761..1cb78ca2 100644 --- a/src/main/java/org/bench4q/agent/api/TestController.java +++ b/src/main/java/org/bench4q/agent/api/TestController.java @@ -49,29 +49,24 @@ public class TestController { scenario.getUserBehaviors()[0] = new UserBehavior(); scenario.getUserBehaviors()[0].setUse("http"); scenario.getUserBehaviors()[0].setName("Get"); - scenario.getUserBehaviors()[0].setParameters(new Parameter[2]); + scenario.getUserBehaviors()[0].setParameters(new Parameter[1]); scenario.getUserBehaviors()[0].getParameters()[0] = new Parameter(); scenario.getUserBehaviors()[0].getParameters()[0].setKey("url"); - scenario.getUserBehaviors()[0].getParameters()[0].setValue("localhost"); - scenario.getUserBehaviors()[0].getParameters()[1] = new Parameter(); - scenario.getUserBehaviors()[0].getParameters()[1].setKey("content"); - scenario.getUserBehaviors()[0].getParameters()[1] - .setValue("Hello,world!"); + scenario.getUserBehaviors()[0].getParameters()[0] + .setValue("http://www.baidu.com"); scenario.getUserBehaviors()[1] = new UserBehavior(); scenario.getUserBehaviors()[1].setUse("http"); scenario.getUserBehaviors()[1].setName("Post"); - scenario.getUserBehaviors()[1].setParameters(new Parameter[3]); + scenario.getUserBehaviors()[1].setParameters(new Parameter[2]); scenario.getUserBehaviors()[1].getParameters()[0] = new Parameter(); scenario.getUserBehaviors()[1].getParameters()[0].setKey("url"); - scenario.getUserBehaviors()[1].getParameters()[0].setValue("localhost"); + scenario.getUserBehaviors()[1].getParameters()[0] + .setValue("http://localhost:6565"); scenario.getUserBehaviors()[1].getParameters()[1] = new Parameter(); scenario.getUserBehaviors()[1].getParameters()[1].setKey("content"); scenario.getUserBehaviors()[1].getParameters()[1] .setValue("Hello,world!"); - scenario.getUserBehaviors()[1].getParameters()[2] = new Parameter(); - scenario.getUserBehaviors()[1].getParameters()[2].setKey("code"); - scenario.getUserBehaviors()[1].getParameters()[2].setValue("404"); scenario.getUserBehaviors()[2] = new UserBehavior(); scenario.getUserBehaviors()[2].setUse("timer"); diff --git a/src/main/java/org/bench4q/agent/plugin/annotation/Behavior.java b/src/main/java/org/bench4q/agent/plugin/Behavior.java similarity index 82% rename from src/main/java/org/bench4q/agent/plugin/annotation/Behavior.java rename to src/main/java/org/bench4q/agent/plugin/Behavior.java index 6e7d4e60..3fab5f99 100644 --- a/src/main/java/org/bench4q/agent/plugin/annotation/Behavior.java +++ b/src/main/java/org/bench4q/agent/plugin/Behavior.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin.annotation; +package org.bench4q.agent.plugin; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/bench4q/agent/plugin/metadata/BehaviorInfo.java b/src/main/java/org/bench4q/agent/plugin/BehaviorInfo.java similarity index 83% rename from src/main/java/org/bench4q/agent/plugin/metadata/BehaviorInfo.java rename to src/main/java/org/bench4q/agent/plugin/BehaviorInfo.java index 547ff43f..96317ac3 100644 --- a/src/main/java/org/bench4q/agent/plugin/metadata/BehaviorInfo.java +++ b/src/main/java/org/bench4q/agent/plugin/BehaviorInfo.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin.metadata; +package org.bench4q.agent.plugin; public class BehaviorInfo { private String name; diff --git a/src/main/java/org/bench4q/agent/plugin/annotation/Plugin.java b/src/main/java/org/bench4q/agent/plugin/Plugin.java similarity index 82% rename from src/main/java/org/bench4q/agent/plugin/annotation/Plugin.java rename to src/main/java/org/bench4q/agent/plugin/Plugin.java index f35ca906..7841b547 100644 --- a/src/main/java/org/bench4q/agent/plugin/annotation/Plugin.java +++ b/src/main/java/org/bench4q/agent/plugin/Plugin.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin.annotation; +package org.bench4q.agent.plugin; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/src/main/java/org/bench4q/agent/plugin/metadata/PluginInfo.java b/src/main/java/org/bench4q/agent/plugin/PluginInfo.java similarity index 87% rename from src/main/java/org/bench4q/agent/plugin/metadata/PluginInfo.java rename to src/main/java/org/bench4q/agent/plugin/PluginInfo.java index 8742fb59..d3c8aa39 100644 --- a/src/main/java/org/bench4q/agent/plugin/metadata/PluginInfo.java +++ b/src/main/java/org/bench4q/agent/plugin/PluginInfo.java @@ -1,4 +1,4 @@ -package org.bench4q.agent.plugin.metadata; +package org.bench4q.agent.plugin; public class PluginInfo { private String name; diff --git a/src/main/java/org/bench4q/agent/plugin/PluginManager.java b/src/main/java/org/bench4q/agent/plugin/PluginManager.java index 28df9235..ce71b093 100644 --- a/src/main/java/org/bench4q/agent/plugin/PluginManager.java +++ b/src/main/java/org/bench4q/agent/plugin/PluginManager.java @@ -16,10 +16,6 @@ import javassist.bytecode.CodeAttribute; import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.MethodInfo; -import org.bench4q.agent.plugin.annotation.Behavior; -import org.bench4q.agent.plugin.annotation.Plugin; -import org.bench4q.agent.plugin.metadata.BehaviorInfo; -import org.bench4q.agent.plugin.metadata.PluginInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java b/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java index 96b00fe5..6e10b93f 100644 --- a/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java +++ b/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java @@ -1,45 +1,84 @@ package org.bench4q.agent.plugin.http; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.HttpURLConnection; +import java.net.URL; import java.util.Date; +import org.bench4q.agent.plugin.Behavior; import org.bench4q.agent.plugin.BehaviorResult; -import org.bench4q.agent.plugin.annotation.Behavior; -import org.bench4q.agent.plugin.annotation.Plugin; +import org.bench4q.agent.plugin.Plugin; @Plugin("Http") public class HttpPlugin { public HttpPlugin() { - System.out.println("init http plugin"); + } @Behavior("Get") - public BehaviorResult get(String url, String content) { + public BehaviorResult get(String url) { BehaviorResult behaviorResult = new BehaviorResult(); behaviorResult.setStartDate(new Date(System.currentTimeMillis())); try { - System.out.println("get"); - System.out.println("url:" + url); - System.out.println("content:" + content); - behaviorResult.setEndDate(new Date(System.currentTimeMillis())); + 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(); behaviorResult.setSuccess(true); } catch (Exception e) { e.printStackTrace(); + behaviorResult.setSuccess(false); + } finally { behaviorResult.setEndDate(new Date(System.currentTimeMillis())); - behaviorResult.setSuccess(true); } return behaviorResult; } @Behavior("Post") - public BehaviorResult post(String url, String content, int code) { + public BehaviorResult post(String url, String content) { BehaviorResult behaviorResult = new BehaviorResult(); behaviorResult.setStartDate(new Date(System.currentTimeMillis())); - System.out.println("get"); - System.out.println("url:" + url); - System.out.println("content:" + content); - System.out.println("code:" + code); - behaviorResult.setEndDate(new Date(System.currentTimeMillis())); - behaviorResult.setSuccess(true); + try { + URL target = new URL(url); + HttpURLConnection httpURLConnection = (HttpURLConnection) target + .openConnection(); + httpURLConnection.setDoOutput(true); + httpURLConnection.setDoInput(true); + httpURLConnection.setUseCaches(false); + httpURLConnection.setRequestMethod("POST"); + 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(); + behaviorResult.setSuccess(true); + } catch (Exception e) { + e.printStackTrace(); + behaviorResult.setSuccess(false); + } finally { + behaviorResult.setEndDate(new Date(System.currentTimeMillis())); + } return behaviorResult; } } diff --git a/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java b/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java index 095fa656..989d39bb 100644 --- a/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java +++ b/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java @@ -2,9 +2,9 @@ package org.bench4q.agent.plugin.timer; import java.util.Date; +import org.bench4q.agent.plugin.Behavior; import org.bench4q.agent.plugin.BehaviorResult; -import org.bench4q.agent.plugin.annotation.Behavior; -import org.bench4q.agent.plugin.annotation.Plugin; +import org.bench4q.agent.plugin.Plugin; @Plugin("ConstantTimer") public class ConstantTimerPlugin { @@ -17,14 +17,13 @@ public class ConstantTimerPlugin { BehaviorResult behaviorResult = new BehaviorResult(); behaviorResult.setStartDate(new Date(System.currentTimeMillis())); try { - System.out.println("sleep:" + time); Thread.sleep(time); - behaviorResult.setEndDate(new Date(System.currentTimeMillis())); behaviorResult.setSuccess(true); } catch (Exception e) { e.printStackTrace(); - behaviorResult.setEndDate(new Date(System.currentTimeMillis())); behaviorResult.setSuccess(false); + } finally { + behaviorResult.setEndDate(new Date(System.currentTimeMillis())); } return behaviorResult; }