From f073d37fe7549db62ba59a310399a2c06af5bb9f Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Wed, 21 Aug 2013 17:40:33 +0800 Subject: [PATCH] add things about controlling the test by timer --- .../org/bench4q/agent/api/HomeController.java | 2 +- .../org/bench4q/agent/api/TestController.java | 6 ++- .../agent/api/model/TestBriefStatusModel.java | 10 ---- .../agent/scenario/RunnableExecutor.java | 49 +++++++++++++++++++ .../agent/scenario/ScenarioContext.java | 10 ++++ .../agent/scenario/ScenarioEngine.java | 12 ++--- 6 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/bench4q/agent/scenario/RunnableExecutor.java diff --git a/src/main/java/org/bench4q/agent/api/HomeController.java b/src/main/java/org/bench4q/agent/api/HomeController.java index 078b3e42..4f129404 100644 --- a/src/main/java/org/bench4q/agent/api/HomeController.java +++ b/src/main/java/org/bench4q/agent/api/HomeController.java @@ -38,7 +38,7 @@ public class HomeController { getScenarioEngine().getRunningTests()); for (UUID key : contexts.keySet()) { ScenarioContext value = contexts.get(key); - if (value.getResults().size() == value.getTotalCount()) { + if (value.isFinished()) { serverStatusModel.getFinishedTests().add(key); } else { serverStatusModel.getRunningTests().add(key); diff --git a/src/main/java/org/bench4q/agent/api/TestController.java b/src/main/java/org/bench4q/agent/api/TestController.java index a373cd13..c9887f8e 100644 --- a/src/main/java/org/bench4q/agent/api/TestController.java +++ b/src/main/java/org/bench4q/agent/api/TestController.java @@ -231,15 +231,19 @@ public class TestController { return testBriefStatusModel; } - @RequestMapping(value = "/stop/{runId}", method = RequestMethod.GET) + @RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET, + RequestMethod.POST }) @ResponseBody public StopTestModel stop(@PathVariable UUID runId) { + System.out.println("stop method"); ScenarioContext scenarioContext = this.getScenarioEngine() .getRunningTests().get(runId); if (scenarioContext == null) { return null; } scenarioContext.getExecutorService().shutdownNow(); + scenarioContext.setFinished(true); + StopTestModel stopTestModel = new StopTestModel(); stopTestModel.setSuccess(true); return stopTestModel; diff --git a/src/main/java/org/bench4q/agent/api/model/TestBriefStatusModel.java b/src/main/java/org/bench4q/agent/api/model/TestBriefStatusModel.java index 5f5ece56..3eead89d 100644 --- a/src/main/java/org/bench4q/agent/api/model/TestBriefStatusModel.java +++ b/src/main/java/org/bench4q/agent/api/model/TestBriefStatusModel.java @@ -13,7 +13,6 @@ public class TestBriefStatusModel { private int failCount; private int finishedCount; private int totalCount; - private int finishedScenarioCount; private int scenarioBehaviorCount; private double averageResponseTime; @@ -71,15 +70,6 @@ public class TestBriefStatusModel { this.totalCount = totalCount; } - @XmlElement - public int getFinishedScenarioCount() { - return finishedScenarioCount; - } - - public void setFinishedScenarioCount(int finishedScenarioCount) { - this.finishedScenarioCount = finishedScenarioCount; - } - @XmlElement public double getAverageResponseTime() { return averageResponseTime; diff --git a/src/main/java/org/bench4q/agent/scenario/RunnableExecutor.java b/src/main/java/org/bench4q/agent/scenario/RunnableExecutor.java new file mode 100644 index 00000000..994a7147 --- /dev/null +++ b/src/main/java/org/bench4q/agent/scenario/RunnableExecutor.java @@ -0,0 +1,49 @@ +package org.bench4q.agent.scenario; + +import java.util.List; +import java.util.UUID; + +public class RunnableExecutor implements Runnable { + private ScenarioEngine scenarioEngine; + private UUID runId; + private List behaviorResults; + private Scenario scenario; + + private void setScenarioEngine(ScenarioEngine scenarioEngine) { + this.scenarioEngine = scenarioEngine; + } + + private void setBehaviorResults(List behaviorResults) { + this.behaviorResults = behaviorResults; + } + + private void setRunId(UUID runId) { + this.runId = runId; + } + + private void setScenario(Scenario scenario) { + this.scenario = scenario; + } + + public RunnableExecutor(ScenarioEngine scenarioEngine, UUID runId, + Scenario scenario, List behaviorResults) { + this.setScenarioEngine(scenarioEngine); + this.setRunId(runId); + this.setScenario(scenario); + this.setBehaviorResults(behaviorResults); + } + + public void run() { + this.behaviorResults.addAll(this.scenarioEngine + .doRunScenario(this.scenario)); + + ScenarioContext scenarioContext = this.scenarioEngine.getRunningTests() + .get(this.runId); + if (scenarioContext.getExecutorService().isShutdown()) { + return; + } + scenarioContext.getExecutorService().execute( + new RunnableExecutor(this.scenarioEngine, this.runId, + this.scenario, this.behaviorResults)); + } +} diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java b/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java index 7f3f9b04..3695ce91 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java @@ -12,6 +12,7 @@ public class ScenarioContext { private ExecutorService executorService; private Scenario scenario; private List results; + private boolean finished; public int getPoolSize() { return poolSize; @@ -68,4 +69,13 @@ public class ScenarioContext { public void setResults(List results) { this.results = results; } + + public boolean isFinished() { + return finished; + } + + public void setFinished(boolean finished) { + this.finished = finished; + } + } diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index bf8dcec4..033ae4f3 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -68,16 +68,10 @@ public class ScenarioEngine { poolSize, totalCount, executorService, ret); int i; this.getRunningTests().put(runId, scenarioContext); - Runnable runnable = new Runnable() { - public void run() { - ret.addAll(doRunScenario(scenario)); - } - }; - for (i = 0; i < totalCount; i++) { - scenarioContext.setFinishedCount(i - 1); + Runnable runnable = new RunnableExecutor(this, runId, scenario, ret); + for (i = 0; i < poolSize; i++) { executorService.execute(runnable); } - executorService.shutdown(); } catch (Exception e) { e.printStackTrace(); } @@ -97,7 +91,7 @@ public class ScenarioEngine { return scenarioContext; } - private List doRunScenario(Scenario scenario) { + public List doRunScenario(Scenario scenario) { Map plugins = new HashMap(); preparePlugins(scenario, plugins);