From 147c40166d13366d7d7a579ae4b9b8ef3799f364 Mon Sep 17 00:00:00 2001 From: Zhen Tang Date: Wed, 26 Jun 2013 17:08:00 +0800 Subject: [PATCH] Thread pool added. --- .../org/bench4q/agent/api/HomeController.java | 2 +- .../bench4q/agent/plugin/http/HttpPlugin.java | 7 +++--- .../plugin/timer/ConstantTimerPlugin.java | 4 ++-- .../agent/scenario/ScenarioEngine.java | 24 +++++++++++++++++++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/bench4q/agent/api/HomeController.java b/src/main/java/org/bench4q/agent/api/HomeController.java index 2ef18060..cb4fedb0 100644 --- a/src/main/java/org/bench4q/agent/api/HomeController.java +++ b/src/main/java/org/bench4q/agent/api/HomeController.java @@ -80,7 +80,7 @@ public class HomeController { scenario.getUserBehaviors()[2].getParameters()[0].setKey("time"); scenario.getUserBehaviors()[2].getParameters()[0].setValue("1000"); - this.getScenarioEngine().runScenario(scenario); + this.getScenarioEngine().runScenario(scenario, 1000); return "It works!"; } } \ No newline at end of file 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 aff35df4..27467cb4 100644 --- a/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java +++ b/src/main/java/org/bench4q/agent/plugin/http/HttpPlugin.java @@ -6,17 +6,16 @@ import org.bench4q.agent.plugin.Plugin; @Plugin("Http") public class HttpPlugin { public HttpPlugin() { - System.out.println("Http Plugin init..."); + } @Behavior("Get") public void get(String url, String content) { - System.out.println("HTTP GET, url=" + url + ", content=" + content); + } @Behavior("Post") public void post(String url, String content, int code) { - System.out.println("HTTP POST, url=" + url + ", content=" + content - + ", code=" + code); + } } 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 5fe3a951..010b021c 100644 --- a/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java +++ b/src/main/java/org/bench4q/agent/plugin/timer/ConstantTimerPlugin.java @@ -6,11 +6,11 @@ import org.bench4q.agent.plugin.Plugin; @Plugin("ConstantTimer") public class ConstantTimerPlugin { public ConstantTimerPlugin(int time) { - System.out.println("Constant Plugin init... time=" + time); + } @Behavior("Sleep") public void sleep(int time) { - System.out.println("Sleeping for " + time + "..."); + } } diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index 6f344d49..f6b51eb2 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -1,7 +1,10 @@ package org.bench4q.agent.scenario; +import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import org.bench4q.agent.plugin.PluginManager; import org.springframework.beans.factory.annotation.Autowired; @@ -20,7 +23,28 @@ public class ScenarioEngine { this.pluginManager = pluginManager; } + public void runScenario(final Scenario scenario, int threadCount) { + System.out.println("Start at:" + new Date(System.currentTimeMillis())); + ExecutorService executorService = Executors + .newFixedThreadPool(threadCount); + int i; + Runnable runnable = new Runnable() { + public void run() { + doRunScenario(scenario); + } + }; + for (i = 0; i < threadCount; i++) { + executorService.execute(runnable); + } + executorService.shutdown(); + System.out.println("End at:" + new Date(System.currentTimeMillis())); + } + public void runScenario(Scenario scenario) { + this.doRunScenario(scenario); + } + + private void doRunScenario(Scenario scenario) { ScenarioContext scenarioContext = new ScenarioContext(); scenarioContext.setPlugins(new HashMap()); for (UsePlugin usePlugin : scenario.getUsePlugins()) {