add DataCollector module to agent,next step i'll all use this module

This commit is contained in:
Tienan Chen 2013-11-13 15:38:56 +08:00
parent 5ffb3a6f3e
commit 0df812dae1
11 changed files with 602 additions and 230 deletions

View File

@ -15,7 +15,6 @@ 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;
@ -183,14 +182,8 @@ public class TestController {
long totalResponseTime = 0;
int validCount = 0;
for (BehaviorResult behaviorResult : behaviorResults) {
TestDetailModel testDetailModel = new TestDetailModel();
testDetailModel.setBehaviorName(behaviorResult.getBehaviorName());
testDetailModel.setEndDate(behaviorResult.getEndDate());
testDetailModel.setPluginId(behaviorResult.getPluginId());
testDetailModel.setPluginName(behaviorResult.getPluginName());
testDetailModel.setResponseTime(behaviorResult.getResponseTime());
testDetailModel.setStartDate(behaviorResult.getStartDate());
testDetailModel.setSuccess(behaviorResult.isSuccess());
TestDetailModel testDetailModel = new TestDetailModel(
behaviorResult);
testStatusModel.getTestDetailModels().add(testDetailModel);
if (testDetailModel.getEndDate().getTime() > maxDate) {
maxDate = testDetailModel.getEndDate().getTime();
@ -314,33 +307,4 @@ 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;
}
@RequestMapping(value = "/startSave/{runId}", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public SaveTestResultModel startSave(@PathVariable UUID runId) {
this.getScenarioEngine().startSaveResultsWithEdit(runId);
SaveTestResultModel saveTestResultModel = new SaveTestResultModel();
saveTestResultModel.setSuccess(true);
return saveTestResultModel;
}
@RequestMapping(value = "/stopSave/{runId}", method = { RequestMethod.GET,
RequestMethod.POST })
@ResponseBody
public SaveTestResultModel stopSave(@PathVariable UUID runId) {
this.getScenarioEngine().stopSaveResultWithEdit(runId);
SaveTestResultModel saveTestResultModel = new SaveTestResultModel();
saveTestResultModel.setSuccess(true);
return saveTestResultModel;
}
}

View File

@ -0,0 +1,139 @@
package org.bench4q.agent.api.model;
import java.lang.reflect.Field;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class AgentBriefStatusModel {
private long timeFrame;
private long averageResponseTime;
private long minResponseTime;
private long maxResponseTime;
private long totalCountFromBegin;
private long successThroughput;
private long failCountFromBegin;
private long failThroughput;
private long failRate;
private long responseTimeDeviation;
@XmlElement
public long getTimeFrame() {
return timeFrame;
}
public void setTimeFrame(long timeFrame) {
this.timeFrame = timeFrame;
}
@XmlElement
public long getAverageResponseTime() {
return averageResponseTime;
}
public void setAverageResponseTime(long averageResponseTime) {
this.averageResponseTime = averageResponseTime;
}
@XmlElement
public long getMinResponseTime() {
return minResponseTime;
}
public void setMinResponseTime(long minResponseTime) {
this.minResponseTime = minResponseTime;
}
@XmlElement
public long getMaxResponseTime() {
return maxResponseTime;
}
public void setMaxResponseTime(long maxResponseTime) {
this.maxResponseTime = maxResponseTime;
}
@XmlElement
public long getSuccessThroughput() {
return successThroughput;
}
public void setSuccessThroughput(long behaviorThroughput) {
this.successThroughput = behaviorThroughput;
}
@XmlElement
public long getTotalCountFromBegin() {
return totalCountFromBegin;
}
public void setTotalCountFromBegin(long totalCountFromBegin) {
this.totalCountFromBegin = totalCountFromBegin;
}
@XmlElement
public long getFailCountFromBegin() {
return failCountFromBegin;
}
public void setFailCountFromBegin(long failCountFromBegin) {
this.failCountFromBegin = failCountFromBegin;
}
@XmlElement
public long getFailThroughput() {
return failThroughput;
}
public void setFailThroughput(long failThroughput) {
this.failThroughput = failThroughput;
}
@XmlElement
public long getFailRate() {
return failRate;
}
public void setFailRate(long failRate) {
this.failRate = failRate;
}
@XmlElement
public long getResponseTimeDeviation() {
return responseTimeDeviation;
}
public void setResponseTimeDeviation(long responseTimeDeviation) {
this.responseTimeDeviation = responseTimeDeviation;
}
// if the all the fields of target object equals with this's except
// timeFrame,
// We call it equals.
@Override
public boolean equals(Object targetObj) {
if (!(targetObj instanceof AgentBriefStatusModel)) {
return false;
}
boolean result = true;
AgentBriefStatusModel convertedObject = (AgentBriefStatusModel) targetObj;
Field[] fs = this.getClass().getDeclaredFields();
try {
for (Field field : fs) {
field.setAccessible(true);
if (field.getLong(this) != field.getLong(convertedObject)) {
System.out.println(field.getName()
+ " not equals! and the target is "
+ field.getLong(convertedObject) + " and this is "
+ field.getLong(this));
result = false;
}
}
} catch (Exception e) {
e.printStackTrace();
result = false;
}
return result;
}
}

View File

@ -1,19 +0,0 @@
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,10 +1,16 @@
package org.bench4q.agent.api.model;
import java.io.ByteArrayOutputStream;
import java.util.Date;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.bench4q.agent.scenario.BehaviorResult;
@XmlRootElement(name = "testDetail")
public class TestDetailModel {
private String pluginId;
@ -77,4 +83,31 @@ public class TestDetailModel {
public void setSuccess(boolean success) {
this.success = success;
}
public TestDetailModel() {
}
public TestDetailModel(BehaviorResult behaviorResult) {
this.setBehaviorName(behaviorResult.getBehaviorName());
this.setEndDate(behaviorResult.getEndDate());
this.setPluginId(behaviorResult.getPluginId());
this.setPluginName(behaviorResult.getPluginName());
this.setResponseTime(behaviorResult.getResponseTime());
this.setStartDate(behaviorResult.getStartDate());
this.setSuccess(behaviorResult.isSuccess());
}
public String getModelString() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Marshaller marshaller;
try {
marshaller = JAXBContext.newInstance(TestDetailModel.class)
.createMarshaller();
marshaller.marshal(this, outputStream);
return outputStream.toString();
} catch (JAXBException e) {
e.printStackTrace();
return "";
}
}
}

View File

@ -0,0 +1,52 @@
package org.bench4q.agent.datacollector.impl;
import java.net.InetAddress;
import java.net.UnknownHostException;
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.scenario.BehaviorResult;
import org.bench4q.agent.storage.StorageHelper;
import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractDataCollector implements DataStatistics {
protected StorageHelper storageHelper;
private Logger logger = Logger.getLogger(AbstractDataCollector.class);
protected StorageHelper getStorageHelper() {
return storageHelper;
}
@Autowired
public void setStorageHelper(StorageHelper storageHelper) {
this.storageHelper = storageHelper;
}
protected String getHostName() {
InetAddress addr;
try {
addr = InetAddress.getLocalHost();
return addr.getHostAddress().toString();
} catch (UnknownHostException e) {
this.logger
.error("There is an exception when get hostName of this machine!");
return null;
}
}
public void add(List<BehaviorResult> behaviorResults) {
for (BehaviorResult behaviorResult : behaviorResults) {
TestDetailModel testDetailModel = new TestDetailModel(
behaviorResult);
this.getStorageHelper().getLocalStorage()
.writeFile(testDetailModel.getModelString(), getSavePath());
}
}
protected abstract String getSavePath();
public abstract Object getBriefStatistics();
}

View File

@ -0,0 +1,195 @@
package org.bench4q.agent.datacollector.impl;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.bench4q.agent.api.model.AgentBriefStatusModel;
import org.bench4q.agent.scenario.BehaviorResult;
public class AgentResultDataCollector extends AbstractDataCollector {
private long timeOfPreviousCall;
private long failCountOfThisCall;
private long successCountOfThisCall;
private long totalResponseTimeOfThisCall;
private long maxResponseTimeOfThisCall;
private long minResponseTimeOfThisCall;
private long totalSqureResponseTimeOfThisCall;
private long cumulativeSucessfulCount;
private long cumulativeFailCount;
private static long TIME_UNIT = 1000;
private UUID testID;
private void setTimeOfPreviousCall(long timeOfPreviousCall) {
this.timeOfPreviousCall = timeOfPreviousCall;
}
private long getFailCountOfThisCall() {
return failCountOfThisCall;
}
private void setFailCountOfThisCall(long failCountOfThisCall) {
this.failCountOfThisCall = failCountOfThisCall;
}
private long getSuccessCountOfThisCall() {
return successCountOfThisCall;
}
private void setSuccessCountOfThisCall(long successCountOfThisCall) {
this.successCountOfThisCall = successCountOfThisCall;
}
private void setTotalResponseTimeOfThisCall(long totalResponseTimeOfThisCall) {
this.totalResponseTimeOfThisCall = totalResponseTimeOfThisCall;
}
private long getMaxResponseTimeOfThisCall() {
return maxResponseTimeOfThisCall;
}
private void setMaxResponseTimeOfThisCall(long maxResponseTimeOfThisCall) {
this.maxResponseTimeOfThisCall = maxResponseTimeOfThisCall;
}
private long getMinResponseTimeOfThisCall() {
return minResponseTimeOfThisCall;
}
private void setMinResponseTimeOfThisCall(long minResponseTimeOfThisCall) {
this.minResponseTimeOfThisCall = minResponseTimeOfThisCall;
}
private void setTotalSqureResponseTimeOfThisCall(
long totalSqureResponseTimeOfThisCall) {
this.totalSqureResponseTimeOfThisCall = totalSqureResponseTimeOfThisCall;
}
private void setCumulativeSucessfulCount(long cumulativeSucessfulCount) {
this.cumulativeSucessfulCount = cumulativeSucessfulCount;
}
private void setCumulativeFailCount(long cumulativeFailCount) {
this.cumulativeFailCount = cumulativeFailCount;
}
private String getTestID() {
return testID == null ? "default" : testID.toString();
}
public void setTestID(UUID testID) {
this.testID = testID;
}
public AgentResultDataCollector() {
init();
}
private void init() {
reset();
this.setCumulativeFailCount(0);
this.setCumulativeSucessfulCount(0);
}
private void reset() {
this.setTimeOfPreviousCall(System.currentTimeMillis());
this.setFailCountOfThisCall(0);
this.setMaxResponseTimeOfThisCall(Long.MIN_VALUE);
this.setMinResponseTimeOfThisCall(Long.MAX_VALUE);
this.setSuccessCountOfThisCall(0);
this.setTotalResponseTimeOfThisCall(0);
this.setTotalSqureResponseTimeOfThisCall(0);
}
// ///////////////////////////////
// DataStatistics Interface start
// ///////////////////////////////
public void add(List<BehaviorResult> behaviorResults) {
super.add(behaviorResults);
for (BehaviorResult behaviorResult : behaviorResults) {
addItem(behaviorResult);
}
}
public AgentBriefStatusModel getBriefStatistics() {
AgentBriefStatusModel result = new AgentBriefStatusModel();
result.setTimeFrame(System.currentTimeMillis()
- this.timeOfPreviousCall);
if (this.getSuccessCountOfThisCall() == 0) {
result.setAverageResponseTime(0);
result.setMaxResponseTime(0);
result.setMinResponseTime(0);
result.setResponseTimeDeviation(0);
if (this.getFailCountOfThisCall() == 0) {
result.setFailRate(0);
} else {
result.setFailRate(100);
}
} else {
result.setAverageResponseTime(this.totalResponseTimeOfThisCall
/ this.successCountOfThisCall);
result.setFailRate(this.failCountOfThisCall
/ (this.successCountOfThisCall + this.failCountOfThisCall));
result.setMinResponseTime(this.minResponseTimeOfThisCall);
result.setMaxResponseTime(this.maxResponseTimeOfThisCall);
result.setResponseTimeDeviation(Math.round(Math
.sqrt(this.totalSqureResponseTimeOfThisCall
/ this.successCountOfThisCall
- result.getAverageResponseTime()
* result.getAverageResponseTime())));
}
this.cumulativeSucessfulCount += this.successCountOfThisCall;
result.setTotalCountFromBegin(this.cumulativeSucessfulCount);
this.cumulativeFailCount += this.failCountOfThisCall;
result.setFailCountFromBegin(this.cumulativeFailCount);
if (result.getTimeFrame() == 0) {
result.setSuccessThroughput(0);
result.setFailThroughput(0);
} else {
result.setSuccessThroughput(this.successCountOfThisCall
* TIME_UNIT / result.getTimeFrame());
result.setFailThroughput(this.failCountOfThisCall * TIME_UNIT
/ result.getTimeFrame());
}
reset();
return result;
}
// ///////////////////////////////
// DataStatistics Interface end
// ///////////////////////////////
private void addItem(BehaviorResult behaviorResult) {
if (behaviorResult.isSuccess()) {
this.successCountOfThisCall++;
this.totalResponseTimeOfThisCall += behaviorResult
.getResponseTime();
this.totalSqureResponseTimeOfThisCall += ((long) behaviorResult
.getResponseTime()) * behaviorResult.getResponseTime();
if (behaviorResult.getResponseTime() > this
.getMaxResponseTimeOfThisCall()) {
this.setMaxResponseTimeOfThisCall(behaviorResult
.getResponseTime());
}
if (behaviorResult.getResponseTime() < this
.getMinResponseTimeOfThisCall()) {
this.setMinResponseTimeOfThisCall(behaviorResult
.getResponseTime());
}
} else {
this.failCountOfThisCall++;
}
}
@Override
protected String getSavePath() {
return "DetailResults" + System.getProperty("file.separator")
+ new SimpleDateFormat("yyyyMMdd").format(new Date())
+ System.getProperty("file.separator") + this.getTestID()
+ ".txt";
}
}

View File

@ -0,0 +1,11 @@
package org.bench4q.agent.datacollector.interfaces;
import java.util.List;
import org.bench4q.agent.scenario.BehaviorResult;
public interface DataStatistics {
public void add(List<BehaviorResult> behaviorResults);
public Object getBriefStatistics();
}

View File

@ -4,47 +4,23 @@ import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import org.bench4q.agent.datacollector.impl.AgentResultDataCollector;
import org.bench4q.agent.datacollector.interfaces.DataStatistics;
public class ScenarioContext {
private int poolSize;
private int finishedCount;
private Date saveStartDate;
private Date saveStopDate;
private Date startDate;
private ExecutorService executorService;
private Scenario scenario;
private List<BehaviorResult> results;
private boolean finished;
private boolean toSave;
public int getPoolSize() {
return poolSize;
}
public void setPoolSize(int poolSize) {
this.poolSize = poolSize;
}
public int getFinishedCount() {
return finishedCount;
}
public void setFinishedCount(int finishedCount) {
this.finishedCount = finishedCount;
}
private DataStatistics dataStatistics;
public Date getStartDate() {
return saveStartDate;
return startDate;
}
public void setSaveStartDate(Date saveStartDate) {
this.saveStartDate = saveStartDate;
}
public Date getSaveStopDate() {
return saveStopDate;
}
public void setStopDate(Date saveStopDate) {
this.saveStopDate = saveStopDate;
public void setStartDate(Date saveStartDate) {
this.startDate = saveStartDate;
}
public ExecutorService getExecutorService() {
@ -79,12 +55,26 @@ public class ScenarioContext {
this.finished = finished;
}
public boolean isToSave() {
return toSave;
public DataStatistics getDataStatistics() {
return dataStatistics;
}
public void setToSave(boolean startToSave) {
this.toSave = startToSave;
private void setDataStatistics(DataStatistics dataStatistics) {
this.dataStatistics = dataStatistics;
}
private ScenarioContext() {
}
public static ScenarioContext buildScenarioContext(final Scenario scenario,
int poolSize, ExecutorService executorService,
final List<BehaviorResult> ret) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setScenario(scenario);
scenarioContext.setStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setExecutorService(executorService);
scenarioContext.setResults(ret);
scenarioContext.setDataStatistics(new AgentResultDataCollector());
return scenarioContext;
}
}

View File

@ -1,7 +1,6 @@
package org.bench4q.agent.scenario;
import java.io.File;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
@ -12,9 +11,6 @@ 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.apache.log4j.Logger;
import org.bench4q.agent.plugin.Plugin;
import org.bench4q.agent.plugin.PluginManager;
@ -27,7 +23,6 @@ public class ScenarioEngine {
private PluginManager pluginManager;
private Map<UUID, ScenarioContext> runningTests;
private StorageHelper storageHelper;
private static int saveThreshold = 10000;
private Logger logger;
private Logger getLogger() {
@ -75,8 +70,9 @@ public class ScenarioEngine {
.newFixedThreadPool(poolSize);
final List<BehaviorResult> ret = Collections
.synchronizedList(new ArrayList<BehaviorResult>());
final ScenarioContext scenarioContext = buildScenarioContext(
scenario, poolSize, executorService, ret);
final ScenarioContext scenarioContext = ScenarioContext
.buildScenarioContext(scenario, poolSize, executorService,
ret);
int i;
this.getRunningTests().put(runId, scenarioContext);
Runnable runnable = new Runnable() {
@ -94,18 +90,6 @@ public class ScenarioEngine {
}
}
private ScenarioContext buildScenarioContext(final Scenario scenario,
int poolSize, ExecutorService executorService,
final List<BehaviorResult> ret) {
ScenarioContext scenarioContext = new ScenarioContext();
scenarioContext.setScenario(scenario);
scenarioContext.setPoolSize(poolSize);
scenarioContext.setSaveStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setExecutorService(executorService);
scenarioContext.setResults(ret);
return scenarioContext;
}
public List<BehaviorResult> doRunScenario(Scenario scenario) {
Map<String, Object> plugins = new HashMap<String, Object>();
preparePlugins(scenario, plugins);
@ -190,122 +174,4 @@ public class ScenarioEngine {
}
return directory;
}
public void startSaveResultsWithEdit(UUID runId) {
ScenarioContext scenarioContext = this.getRunningTests().get(runId);
if (scenarioContext == null) {
return;
}
scenarioContext.setSaveStartDate(new Date(System.currentTimeMillis()));
scenarioContext.setToSave(true);
while (scenarioContext.isToSave()
&& scenarioContext.getResults().size() >= saveThreshold) {
// saveTestResults(runId);
}
}
public void stopSaveResultWithEdit(UUID runId) {
ScenarioContext scenarioContext = this.getRunningTests().get(runId);
if (scenarioContext == null) {
return;
}
scenarioContext.setStopDate(new Date(System.currentTimeMillis()));
scenarioContext.setToSave(false);
}
public void saveTestResults(UUID runId) {
ScenarioContext scenarioContext = this.getRunningTests().get(runId);
if (scenarioContext == null) {
return;
}
List<TestResultItem> results = new ArrayList<TestResultItem>();
List<BehaviorResult> behaviorResults = new ArrayList<BehaviorResult>(
scenarioContext.getResults());
int failCount = 0;
int successCount = 0;
long totalResponseTime = 0;
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++;
}
results.add(buildTestResultItem(behaviorResult));
}
TestResult testResult = buildTestResult(runId, scenarioContext,
results, behaviorResults, failCount, successCount,
totalResponseTime, maxDate, validCount);
doSaveTestResults(runId, testResult);
}
private void doSaveTestResults(UUID runId, TestResult testResult) {
StringWriter stringWriter = null;
try {
Marshaller marshaller = JAXBContext.newInstance(
testResult.getClass()).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
stringWriter = new StringWriter();
String fileName = this.getLocalPath() + "/" + runId.toString()
+ ".xml";
marshaller.marshal(testResult, stringWriter);
String content = stringWriter.toString();
this.getStorageHelper().getLocalStorage()
.writeFile(content, fileName);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stringWriter != null) {
try {
stringWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private TestResult buildTestResult(UUID runId,
ScenarioContext scenarioContext, List<TestResultItem> results,
List<BehaviorResult> behaviorResults, int failCount,
int successCount, long totalResponseTime, long maxDate,
int validCount) {
TestResult testResult = new TestResult();
testResult.setResults(results);
testResult.setStartDate(scenarioContext.getStartDate());
// testResult.setTotalCount(scenarioContext.getTotalCount());
testResult.setRunId(runId);
testResult.setPoolSize(scenarioContext.getPoolSize());
testResult.setAverageResponseTime((totalResponseTime + 0.0)
/ validCount);
testResult
.setElapsedTime(maxDate - testResult.getStartDate().getTime());
testResult.setFailCount(failCount);
testResult.setSuccessCount(successCount);
testResult.setFinishedCount(behaviorResults.size());
return testResult;
}
private TestResultItem buildTestResultItem(BehaviorResult behaviorResult) {
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());
return testResultItem;
}
}

View File

@ -30,6 +30,11 @@ public class LocalStorage implements Storage {
public boolean writeFile(String content, String path) {
try {
File file = new File(path.substring(path.lastIndexOf(System
.getProperty("file.separator"))));
if (!file.exists()) {
file.mkdirs();
}
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
new FileOutputStream(new File(path)), "UTF-8");
outputStreamWriter.write(content);

View File

@ -0,0 +1,136 @@
package org.bench4q.agent.test;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.bench4q.agent.api.model.AgentBriefStatusModel;
import org.bench4q.agent.datacollector.impl.AgentResultDataCollector;
import org.bench4q.agent.datacollector.interfaces.DataStatistics;
import org.bench4q.agent.scenario.BehaviorResult;
import org.bench4q.agent.storage.StorageHelper;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DataStatisticsTest {
private DataStatistics dataStatistics;
private DataStatistics getDataStatistics() {
return dataStatistics;
}
private void setDataStatistics(DataStatistics dataStatistics) {
this.dataStatistics = dataStatistics;
}
public DataStatisticsTest() {
init();
}
public void init() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:/org/bench4q/agent/config/application-context.xml");
AgentResultDataCollector agentResultDataCollector = new AgentResultDataCollector();
agentResultDataCollector.setStorageHelper((StorageHelper) context
.getBean(StorageHelper.class));
this.setDataStatistics(agentResultDataCollector);
}
@Test
public void addZeroTest() {
this.getDataStatistics().add(makeBehaviorResultList(0));
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getBriefStatistics();
AgentBriefStatusModel modelExpect = makeAllZeroModel();
modelExpect.setTimeFrame(model.getTimeFrame());
assertTrue(model.equals(modelExpect));
}
@Test
public void addOneTest() {
this.getDataStatistics().add(makeBehaviorResultList(1));
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getBriefStatistics();
AgentBriefStatusModel modelExpect = new AgentBriefStatusModel();
modelExpect.setTimeFrame(model.getTimeFrame());
makeUpStatusModelForOneBehavior(modelExpect);
assertTrue(model.equals(modelExpect));
}
@Test
public void addTwoTest() {
this.getDataStatistics().add(makeBehaviorResultList(2));
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getBriefStatistics();
AgentBriefStatusModel modelExpect = new AgentBriefStatusModel();
modelExpect.setTimeFrame(model.getTimeFrame());
makeUpStatusModelForTwoBehavior(modelExpect);
}
private void makeUpStatusModelForTwoBehavior(AgentBriefStatusModel model) {
model.setAverageResponseTime(205);
model.setSuccessThroughput(1 * 1000 / model.getTimeFrame());
model.setFailCountFromBegin(1);
model.setFailRate(50);
model.setFailThroughput(1 * 1000 / model.getTimeFrame());
model.setMaxResponseTime(210);
model.setMinResponseTime(200);
model.setResponseTimeDeviation(5);
model.setTotalCountFromBegin(2);
}
private void makeUpStatusModelForOneBehavior(AgentBriefStatusModel model) {
model.setAverageResponseTime(200);
model.setSuccessThroughput(1 * 1000 / model.getTimeFrame());
model.setFailCountFromBegin(0);
model.setFailRate(0);
model.setFailThroughput(0);
model.setMaxResponseTime(200);
model.setMinResponseTime(200);
model.setResponseTimeDeviation(0);
model.setTotalCountFromBegin(1);
}
private AgentBriefStatusModel makeAllZeroModel() {
AgentBriefStatusModel model = new AgentBriefStatusModel();
model.setAverageResponseTime(0);
model.setSuccessThroughput(0);
model.setFailCountFromBegin(0);
model.setFailRate(0);
model.setFailThroughput(0);
model.setMaxResponseTime(0);
model.setMinResponseTime(0);
model.setResponseTimeDeviation(0);
model.setTimeFrame(0);
model.setTotalCountFromBegin(0);
return model;
}
private List<BehaviorResult> makeBehaviorResultList(int count) {
List<BehaviorResult> behaviorResults = new ArrayList<BehaviorResult>();
for (int i = 0; i < count; i++) {
behaviorResults.add(buildBehaviorResult(200 + 10 * i, i / 2 == 0));
}
return behaviorResults;
}
private BehaviorResult buildBehaviorResult(long responseTime,
boolean success) {
Date date = new Date();
BehaviorResult result = new BehaviorResult();
result.setBehaviorName("");
result.setEndDate(new Date(date.getTime() + responseTime));
result.setId(UUID.randomUUID());
result.setPluginId("Get");
result.setPluginName("get");
result.setResponseTime(responseTime);
result.setStartDate(date);
result.setSuccess(success);
return result;
}
}