test results can be saved to file now.

This commit is contained in:
Zhen Tang 2013-07-02 22:50:51 +08:00
parent 607d1cc8e2
commit 00a2c4341b
7 changed files with 318 additions and 6 deletions

View File

@ -8,6 +8,7 @@ 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.SaveTestResultModel;
import org.bench4q.agent.api.model.StopTestModel;
import org.bench4q.agent.api.model.TestBriefStatusModel;
import org.bench4q.agent.api.model.TestDetailModel;
@ -223,4 +224,13 @@ public class TestController {
cleanTestResultModel.setSuccess(true);
return cleanTestResultModel;
}
@RequestMapping(value = "/save/{runId}", method = RequestMethod.GET)
@ResponseBody
public SaveTestResultModel save(@PathVariable UUID runId) {
this.getScenarioEngine().saveTestResults(runId);
SaveTestResultModel saveTestResultModel = new SaveTestResultModel();
saveTestResultModel.setSuccess(true);
return saveTestResultModel;
}
}

View File

@ -0,0 +1,19 @@
package org.bench4q.agent.api.model;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "saveTestResult")
public class SaveTestResultModel {
private boolean success;
@XmlElement
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}

View File

@ -1,8 +1,10 @@
package org.bench4q.agent.plugin;
import java.util.Date;
import java.util.UUID;
public class BehaviorResult {
private UUID id;
private String pluginId;
private String pluginName;
private String behaviorName;
@ -11,6 +13,14 @@ public class BehaviorResult {
private long responseTime;
private boolean success;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getPluginId() {
return pluginId;
}

View File

@ -1,5 +1,7 @@
package org.bench4q.agent.scenario;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@ -10,6 +12,9 @@ import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import org.bench4q.agent.plugin.BehaviorResult;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager;
@ -42,10 +47,6 @@ public class ScenarioEngine {
this.runningTests = runningTests;
}
public ScenarioContext getState(UUID uuid) {
return this.getRunningTests().get(uuid);
}
public void runScenario(UUID runId, final Scenario scenario, int poolSize,
int totalCount) {
try {
@ -102,6 +103,7 @@ public class ScenarioEngine {
.put(parameter.getKey(), parameter.getValue());
}
BehaviorResult result = new BehaviorResult();
result.setId(UUID.randomUUID());
result.setStartDate(new Date(System.currentTimeMillis()));
boolean success = (Boolean) this.getPluginManager().doBehavior(
plugin, behaviorName, behaviorParameters);
@ -117,4 +119,72 @@ public class ScenarioEngine {
}
return ret;
}
public String getPath() {
return System.getProperty("user.dir");
}
public void saveTestResults(UUID runId) {
try {
ScenarioContext scenarioContext = this.getRunningTests().get(runId);
if (scenarioContext == null) {
return;
}
TestResult testResult = new TestResult();
testResult.setRunId(runId);
testResult.setPoolSize(scenarioContext.getPoolSize());
testResult.setResults(new ArrayList<TestResultItem>());
testResult.setStartDate(scenarioContext.getStartDate());
testResult.setTotalCount(scenarioContext.getTotalCount());
int failCount = 0;
int successCount = 0;
long totalResponseTime = 0;
List<BehaviorResult> behaviorResults = new ArrayList<BehaviorResult>(
scenarioContext.getResults());
long maxDate = 0;
int validCount = 0;
for (BehaviorResult behaviorResult : behaviorResults) {
if (behaviorResult.getEndDate().getTime() > maxDate) {
maxDate = behaviorResult.getEndDate().getTime();
}
if (behaviorResult.isSuccess()) {
successCount++;
} else {
failCount++;
}
if (!behaviorResult.getPluginName().contains("Timer")) {
totalResponseTime += behaviorResult.getResponseTime();
validCount++;
}
TestResultItem testResultItem = new TestResultItem();
testResultItem
.setBehaviorName(behaviorResult.getBehaviorName());
testResultItem.setEndDate(behaviorResult.getEndDate());
testResultItem.setId(behaviorResult.getId());
testResultItem.setPluginId(behaviorResult.getPluginId());
testResultItem.setPluginName(behaviorResult.getPluginName());
testResultItem
.setResponseTime(behaviorResult.getResponseTime());
testResultItem.setStartDate(behaviorResult.getStartDate());
testResultItem.setSuccess(behaviorResult.isSuccess());
testResult.getResults().add(testResultItem);
}
testResult.setAverageResponseTime((totalResponseTime + 0.0)
/ validCount);
testResult.setElapsedTime(maxDate
- testResult.getStartDate().getTime());
testResult.setFailCount(failCount);
testResult.setSuccessCount(successCount);
testResult.setFinishedCount(behaviorResults.size());
Marshaller marshaller = JAXBContext.newInstance(
testResult.getClass()).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
FileWriter fileWriter = new FileWriter(new File(this.getPath()
+ "/" + runId.toString() + ".xml"));
marshaller.marshal(testResult, fileWriter);
fileWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,111 @@
package org.bench4q.agent.scenario;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "testResult")
public class TestResult implements Serializable {
private static final long serialVersionUID = -370091935554266546L;
private UUID runId;
private int poolSize;
private int totalCount;
private Date startDate;
private long elapsedTime;
private int successCount;
private int failCount;
private int finishedCount;
private double averageResponseTime;
private List<TestResultItem> results;
@XmlElement
public UUID getRunId() {
return runId;
}
public void setRunId(UUID runId) {
this.runId = runId;
}
@XmlElement
public int getPoolSize() {
return poolSize;
}
public void setPoolSize(int poolSize) {
this.poolSize = poolSize;
}
@XmlElement
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
@XmlElement
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public long getElapsedTime() {
return elapsedTime;
}
public void setElapsedTime(long elapsedTime) {
this.elapsedTime = elapsedTime;
}
public int getSuccessCount() {
return successCount;
}
public void setSuccessCount(int successCount) {
this.successCount = successCount;
}
public int getFailCount() {
return failCount;
}
public void setFailCount(int failCount) {
this.failCount = failCount;
}
public int getFinishedCount() {
return finishedCount;
}
public void setFinishedCount(int finishedCount) {
this.finishedCount = finishedCount;
}
public double getAverageResponseTime() {
return averageResponseTime;
}
public void setAverageResponseTime(double averageResponseTime) {
this.averageResponseTime = averageResponseTime;
}
@XmlElementWrapper(name = "results")
@XmlElement(name = "result")
public List<TestResultItem> getResults() {
return results;
}
public void setResults(List<TestResultItem> results) {
this.results = results;
}
}

View File

@ -0,0 +1,93 @@
package org.bench4q.agent.scenario;
import java.io.Serializable;
import java.util.Date;
import java.util.UUID;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "testResultItem")
public class TestResultItem implements Serializable {
private static final long serialVersionUID = 3307951299814477213L;
private UUID id;
private String pluginId;
private String pluginName;
private String behaviorName;
private Date startDate;
private Date endDate;
private long responseTime;
private boolean success;
@XmlElement
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
@XmlElement
public String getPluginId() {
return pluginId;
}
public void setPluginId(String pluginId) {
this.pluginId = pluginId;
}
@XmlElement
public String getPluginName() {
return pluginName;
}
public void setPluginName(String pluginName) {
this.pluginName = pluginName;
}
@XmlElement
public String getBehaviorName() {
return behaviorName;
}
public void setBehaviorName(String behaviorName) {
this.behaviorName = behaviorName;
}
@XmlElement
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
@XmlElement
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
@XmlElement
public long getResponseTime() {
return responseTime;
}
public void setResponseTime(long responseTime) {
this.responseTime = responseTime;
}
@XmlElement
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}

View File

@ -23,7 +23,7 @@ public class RunScenarioTest {
public void testRunScenario() {
try {
RunScenarioModel runScenarioModel = new RunScenarioModel();
runScenarioModel.setTotalCount(10000);
runScenarioModel.setTotalCount(1000);
runScenarioModel.setPoolSize(100);
runScenarioModel.setUsePlugins(new ArrayList<UsePluginModel>());
runScenarioModel
@ -73,7 +73,6 @@ public class RunScenarioTest {
runScenarioModel.getUserBehaviors().add(postUserBehaviorModel);
Marshaller marshaller = JAXBContext.newInstance(
runScenarioModel.getClass()).createMarshaller();
;
StringWriter stringWriter = new StringWriter();
marshaller.marshal(runScenarioModel, stringWriter);
String url = "http://localhost:6565/test/run";