diff --git a/.gitignore b/.gitignore index 36a59301..33a60639 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /target /logs /scripts +/DetailResults diff --git a/src/main/java/org/bench4q/agent/api/TestController.java b/src/main/java/org/bench4q/agent/api/TestController.java index a98443f7..a03164f3 100644 --- a/src/main/java/org/bench4q/agent/api/TestController.java +++ b/src/main/java/org/bench4q/agent/api/TestController.java @@ -11,12 +11,12 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import org.apache.log4j.Logger; +import org.bench4q.agent.api.model.AgentBriefStatusModel; import org.bench4q.agent.api.model.CleanTestResultModel; import org.bench4q.agent.api.model.ParameterModel; import org.bench4q.agent.api.model.RunScenarioModel; import org.bench4q.agent.api.model.RunScenarioResultModel; import org.bench4q.agent.api.model.StopTestModel; -import org.bench4q.agent.api.model.TestBriefStatusModel; import org.bench4q.agent.api.model.TestDetailModel; import org.bench4q.agent.api.model.TestDetailStatusModel; import org.bench4q.agent.api.model.UsePluginModel; @@ -211,67 +211,14 @@ public class TestController { @RequestMapping(value = "/brief/{runId}", method = RequestMethod.GET) @ResponseBody - public TestBriefStatusModel brief(@PathVariable UUID runId) { + public AgentBriefStatusModel brief(@PathVariable UUID runId) { ScenarioContext scenarioContext = this.getScenarioEngine() .getRunningTests().get(runId); if (scenarioContext == null) { return null; } - TestBriefStatusModel testBriefStatusModel = new TestBriefStatusModel(); - testBriefStatusModel.setStartDate(scenarioContext.getStartDate()); - int failCount = 0; - int successCount = 0; - long totalResponseTime = 0; - long maxResponseTime = 0, minResponseTime = Long.MAX_VALUE; - List behaviorResults = new ArrayList( - scenarioContext.getResults()); - long maxDate = 0, minDate = Long.MAX_VALUE; - int validCount = 0; - for (BehaviorResult behaviorResult : behaviorResults) { - if (behaviorResult.getEndDate().getTime() > maxDate) { - maxDate = behaviorResult.getEndDate().getTime(); - } - if (behaviorResult.getStartDate().getTime() < minDate) { - minDate = behaviorResult.getStartDate().getTime(); - } - if (behaviorResult.isSuccess()) { - successCount++; - } else { - failCount++; - } - if (behaviorResult.getPluginName().contains("Timer")) { - continue; - } - if (behaviorResult.isCalculated()) { - continue; - } - if (behaviorResult.getResponseTime() > maxResponseTime) { - maxResponseTime = behaviorResult.getResponseTime(); - } - if (behaviorResult.getResponseTime() < minResponseTime) { - minResponseTime = behaviorResult.getResponseTime(); - } - totalResponseTime += behaviorResult.getResponseTime(); - validCount++; - behaviorResult.setCalculated(true); - } - if (validCount == 0) { - return null; - } - testBriefStatusModel.setAverageResponseTime((totalResponseTime + 0.0) - / validCount); - testBriefStatusModel.setElapsedTime(maxDate - - testBriefStatusModel.getStartDate().getTime()); - testBriefStatusModel.setRunningTime(maxDate - - scenarioContext.getStartDate().getTime()); - testBriefStatusModel.setFailCount(failCount); - testBriefStatusModel.setSuccessCount(successCount); - testBriefStatusModel.setFinishedCount(behaviorResults.size()); - testBriefStatusModel.setScenarioBehaviorCount(scenarioContext - .getScenario().getUserBehaviors().length); - testBriefStatusModel.setRunningTime(maxDate - minDate); - testBriefStatusModel.setFinished(scenarioContext.isFinished()); - return testBriefStatusModel; + return (AgentBriefStatusModel) scenarioContext.getDataStatistics() + .getBriefStatistics(); } @RequestMapping(value = "/stop/{runId}", method = { RequestMethod.GET, diff --git a/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java b/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java index 7f25c431..913dbe1a 100644 --- a/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java +++ b/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java @@ -7,9 +7,9 @@ import java.util.List; import org.apache.log4j.Logger; import org.bench4q.agent.api.model.TestDetailModel; import org.bench4q.agent.datacollector.interfaces.DataStatistics; +import org.bench4q.agent.helper.ApplicationContextHelper; import org.bench4q.agent.scenario.BehaviorResult; import org.bench4q.agent.storage.StorageHelper; -import org.springframework.beans.factory.annotation.Autowired; public abstract class AbstractDataCollector implements DataStatistics { protected StorageHelper storageHelper; @@ -19,11 +19,16 @@ public abstract class AbstractDataCollector implements DataStatistics { return storageHelper; } - @Autowired public void setStorageHelper(StorageHelper storageHelper) { this.storageHelper = storageHelper; } + // Each sub class should call this in their constructor + protected void mustDoWhenIniti() { + this.setStorageHelper(ApplicationContextHelper.getContext().getBean( + StorageHelper.class)); + } + protected String getHostName() { InetAddress addr; try { diff --git a/src/main/java/org/bench4q/agent/datacollector/impl/AgentResultDataCollector.java b/src/main/java/org/bench4q/agent/datacollector/impl/AgentResultDataCollector.java index 0eab580b..a0a6a538 100644 --- a/src/main/java/org/bench4q/agent/datacollector/impl/AgentResultDataCollector.java +++ b/src/main/java/org/bench4q/agent/datacollector/impl/AgentResultDataCollector.java @@ -82,11 +82,13 @@ public class AgentResultDataCollector extends AbstractDataCollector { this.testID = testID; } - public AgentResultDataCollector() { - init(); + public AgentResultDataCollector(UUID testId) { + super.mustDoWhenIniti(); + this.setTestID(testId); + mustDoWhenIniti(); } - private void init() { + public void mustDoWhenIniti() { reset(); this.setCumulativeFailCount(0); this.setCumulativeSucessfulCount(0); @@ -149,8 +151,8 @@ public class AgentResultDataCollector extends AbstractDataCollector { result.setSuccessThroughput(0); result.setFailThroughput(0); } else { - result.setSuccessThroughput(this.successCountOfThisCall - * TIME_UNIT / result.getTimeFrame()); + result.setSuccessThroughput(this.successCountOfThisCall * TIME_UNIT + / result.getTimeFrame()); result.setFailThroughput(this.failCountOfThisCall * TIME_UNIT / result.getTimeFrame()); } diff --git a/src/main/java/org/bench4q/agent/helper/ApplicationContextHelper.java b/src/main/java/org/bench4q/agent/helper/ApplicationContextHelper.java new file mode 100644 index 00000000..fcfa9dfc --- /dev/null +++ b/src/main/java/org/bench4q/agent/helper/ApplicationContextHelper.java @@ -0,0 +1,26 @@ +package org.bench4q.agent.helper; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +@Component +public class ApplicationContextHelper implements ApplicationContextAware { + + private static ApplicationContext context; + + public static ApplicationContext getContext() { + return context; + } + + private void setContext(ApplicationContext context) { + ApplicationContextHelper.context = context; + } + + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.setContext(applicationContext); + } + +} diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java b/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java index 63760cbb..ca5edfec 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioContext.java @@ -2,6 +2,7 @@ package org.bench4q.agent.scenario; import java.util.Date; import java.util.List; +import java.util.UUID; import java.util.concurrent.ExecutorService; import org.bench4q.agent.datacollector.impl.AgentResultDataCollector; @@ -66,15 +67,16 @@ public class ScenarioContext { private ScenarioContext() { } - public static ScenarioContext buildScenarioContext(final Scenario scenario, - int poolSize, ExecutorService executorService, - final List ret) { + public static ScenarioContext buildScenarioContext(UUID testId, + final Scenario scenario, int poolSize, + ExecutorService executorService, final List ret) { ScenarioContext scenarioContext = new ScenarioContext(); scenarioContext.setScenario(scenario); scenarioContext.setStartDate(new Date(System.currentTimeMillis())); scenarioContext.setExecutorService(executorService); scenarioContext.setResults(ret); - scenarioContext.setDataStatistics(new AgentResultDataCollector()); + // TODO:remove this direct dependency + scenarioContext.setDataStatistics(new AgentResultDataCollector(testId)); return scenarioContext; } } diff --git a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java index 9c71419a..42e02bf0 100644 --- a/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java +++ b/src/main/java/org/bench4q/agent/scenario/ScenarioEngine.java @@ -1,6 +1,5 @@ package org.bench4q.agent.scenario; -import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.Date; @@ -25,6 +24,7 @@ public class ScenarioEngine { private StorageHelper storageHelper; private Logger logger; + @SuppressWarnings("unused") private Logger getLogger() { return logger; } @@ -71,14 +71,16 @@ public class ScenarioEngine { final List ret = Collections .synchronizedList(new ArrayList()); final ScenarioContext scenarioContext = ScenarioContext - .buildScenarioContext(scenario, poolSize, executorService, - ret); + .buildScenarioContext(runId, scenario, poolSize, + executorService, ret); int i; this.getRunningTests().put(runId, scenarioContext); Runnable runnable = new Runnable() { public void run() { while (!scenarioContext.getExecutorService().isShutdown()) { - ret.addAll(doRunScenario(scenario)); + // ret.addAll(doRunScenario(scenario)); + scenarioContext.getDataStatistics().add( + doRunScenario(scenario)); } } }; @@ -98,8 +100,8 @@ public class ScenarioEngine { for (UserBehavior userBehavior : scenario.getUserBehaviors()) { Object plugin = plugins.get(userBehavior.getUse()); String behaviorName = userBehavior.getName(); - this.getLogger().info( - "this step's behaviorName is : " + behaviorName); + // this.getLogger().info( + // "this step's behaviorName is : " + behaviorName); Map behaviorParameters = prepareBehaviorParameters(userBehavior); Date startDate = new Date(System.currentTimeMillis()); boolean success = (Boolean) this.getPluginManager().doBehavior( @@ -153,25 +155,4 @@ public class ScenarioEngine { } } - public String getHdfsPath() { - return "hdfs://133.133.12.21:9000/home/bench4q/results"; - } - - public String getLocalPath() { - String directory = this.getClass().getProtectionDomain() - .getCodeSource().getLocation().getFile().replace("\\", "/"); - File file = new File(directory); - if (!file.isDirectory()) { - directory = directory.substring(0, directory.lastIndexOf("/")); - } - if (!directory.endsWith("/")) { - directory += "/"; - } - directory += "results"; - File toCreate = new File(directory); - if (!toCreate.exists()) { - toCreate.mkdirs(); - } - return directory; - } } diff --git a/src/test/java/org/bench4q/agent/test/DataStatisticsTest.java b/src/test/java/org/bench4q/agent/test/DataStatisticsTest.java index 92a43579..be5c2fc2 100644 --- a/src/test/java/org/bench4q/agent/test/DataStatisticsTest.java +++ b/src/test/java/org/bench4q/agent/test/DataStatisticsTest.java @@ -35,7 +35,8 @@ public class DataStatisticsTest { @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext( "classpath*:/org/bench4q/agent/config/application-context.xml"); - AgentResultDataCollector agentResultDataCollector = new AgentResultDataCollector(); + AgentResultDataCollector agentResultDataCollector = new AgentResultDataCollector( + UUID.randomUUID()); agentResultDataCollector.setStorageHelper((StorageHelper) context .getBean(StorageHelper.class)); this.setDataStatistics(agentResultDataCollector); diff --git a/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index 47a35a35..75ef3042 100644 --- a/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -38,7 +38,7 @@ public class TestWithScriptFile { public TestWithScriptFile() { this.setFilePath("scripts" + System.getProperty("file.separator") - + "scriptwithError.xml"); + + "script.xml"); this.setHttpRequester(new HttpRequester()); }