Thread pool added.

This commit is contained in:
Zhen Tang 2013-06-26 17:08:00 +08:00
parent ad99f1aaa3
commit 147c40166d
4 changed files with 30 additions and 7 deletions

View File

@ -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!";
}
}

View File

@ -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);
}
}

View File

@ -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 + "...");
}
}

View File

@ -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<String, Object>());
for (UsePlugin usePlugin : scenario.getUsePlugins()) {