remove the bug when brief

remove the bug when brief
This commit is contained in:
coderfengyun 2014-09-12 17:36:51 +08:00
parent 513cbf3a4f
commit 37579cbcfb
9 changed files with 21 additions and 572 deletions

View File

@ -8,6 +8,7 @@ import org.apache.log4j.Logger;
import org.bench4q.agent.plugin.ParameterFileCollector;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.scenario.behavior.UserBehavior;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine;
import org.bench4q.agent.scenario.engine.Schedule;
@ -155,6 +156,9 @@ public class TestController {
}
for (Behavior behavior : scenarioContext.getScenario()
.getAllBehaviors()) {
if (!(behavior instanceof UserBehavior)) {
continue;
}
BehaviorBriefModel briefModel = behavior
.getBehaviorBriefResult(scenarioContext.getDataCollector());
briefModel.setBehaviorUrl(behavior.getSpecificParamValue("url"));

View File

@ -1,82 +0,0 @@
package org.bench4q.agent.datacollector.impl;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.bench4q.agent.Main;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.helper.ApplicationContextHelper;
import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.agent.storage.StorageHelper;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.BehaviorResultModel;
public abstract class AbstractDataCollector implements DataCollector {
protected StorageHelper storageHelper;
protected StorageHelper getStorageHelper() {
return storageHelper;
}
public void setStorageHelper(StorageHelper storageHelper) {
this.storageHelper = storageHelper;
}
public AbstractDataCollector() {
mustDoWhenIniti();
}
protected void mustDoWhenIniti() {
this.setStorageHelper(ApplicationContextHelper.getContext().getBean(
StorageHelper.class));
}
public void add(final BehaviorResult behaviorResult) {
if (!Main.IS_TO_SAVE_DETAIL) {
return;
}
if (behaviorResult == null) {
return;
}
Runnable runnable = new Runnable() {
public void run() {
storageHelper.getLocalStorage().writeFile(
buildBehaviorResultModel(behaviorResult)
.getModelString(),
calculateSavePath(behaviorResult));
}
};
ExecutorService executorService = Executors
.newSingleThreadScheduledExecutor();
executorService.execute(runnable);
executorService.shutdown();
}
private BehaviorResultModel buildBehaviorResultModel(
BehaviorResult behaviorResult) {
BehaviorResultModel resultModel = new BehaviorResultModel();
resultModel.setBehaviorId(behaviorResult.getBehaviorId());
resultModel.setBehaviorName(behaviorResult.getBehaviorName());
resultModel.setContentLength(behaviorResult.getContentLength());
resultModel.setContentType(behaviorResult.getContentType());
resultModel.setEndDate(behaviorResult.getEndDate());
resultModel.setPluginId(behaviorResult.getPluginId());
resultModel.setPluginName(behaviorResult.getPluginName());
resultModel.setResponseTime(behaviorResult.getResponseTime());
resultModel.setShouldBeCountResponseTime(behaviorResult
.isShouldBeCountResponseTime());
resultModel.setStartDate(behaviorResult.getStartDate());
resultModel.setStatusCode(behaviorResult.getStatusCode());
resultModel.setSuccessCount(behaviorResult.getSuccessCount());
resultModel.setFailCount(behaviorResult.getFailCount());
return resultModel;
}
protected abstract String calculateSavePath(BehaviorResult behaviorResult);
public abstract Object getScenarioBriefStatistics();
public abstract BehaviorBriefModel getBehaviorBriefStatistics(
int id);
}

View File

@ -1,62 +0,0 @@
package org.bench4q.agent.datacollector.impl;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel;
public class BehaviorResultCollector {
private int behaviorId;
private long successCountFromBegin;
private long failCountFromBegin;
// statusCode - BehaviorStatusCodeResultCollector
private final Map<Integer, BehaviorStatusCodeResultCollector> detailStatusMap;
public BehaviorResultCollector(int behaviorId) {
this.behaviorId = behaviorId;
this.detailStatusMap = new HashMap<Integer, BehaviorStatusCodeResultCollector>();
}
public void add(BehaviorResult behaviorResult) {
if (behaviorResult == null
|| behaviorResult.getBehaviorId() != this.behaviorId) {
return;
}
if (!detailStatusMap.containsKey(behaviorResult.getStatusCode())) {
guardExist(behaviorResult.getStatusCode(),
behaviorResult.getContentType());
}
this.successCountFromBegin += behaviorResult.getSuccessCount();
this.failCountFromBegin += behaviorResult.getFailCount();
BehaviorStatusCodeResultCollector statusCodeResult = detailStatusMap
.get(behaviorResult.getStatusCode());
statusCodeResult.add(behaviorResult);
}
private synchronized void guardExist(int statusCode, String contentType) {
if (!this.detailStatusMap.containsKey(statusCode)) {
this.detailStatusMap.put(statusCode,
new BehaviorStatusCodeResultCollector(this.behaviorId,
statusCode, contentType));
}
}
public BehaviorBriefModel getStatistics() {
BehaviorBriefModel result = new BehaviorBriefModel();
List<BehaviorStatusCodeResultModel> list = new LinkedList<BehaviorStatusCodeResultModel>();
for (BehaviorStatusCodeResultCollector codeResultCollector : this.detailStatusMap
.values()) {
list.add(codeResultCollector.getStatistics());
}
result.setDetailStatusCodeResultModels(list);
result.setBehaviorId(this.behaviorId);
result.setTotalCount(this.successCountFromBegin
+ this.failCountFromBegin);
result.setSuccessfulCount(this.successCountFromBegin);
return result;
}
}

View File

@ -1,109 +0,0 @@
package org.bench4q.agent.datacollector.impl;
import java.lang.reflect.Field;
import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel;
public class BehaviorStatusCodeResultCollector {
private int behaviorId;
private int statusCode;
public long count;
public long countThisTime;
public long contentLength;
public long minResponseTime;
public long maxResponseTime;
public long totalResponseTimeThisTime;
public String contentType;
public BehaviorStatusCodeResultCollector(int behaviorId, int statusCode,
String contentType) {
this.behaviorId = behaviorId;
this.statusCode = statusCode;
this.contentType = contentType;
this.count = 0;
this.contentLength = 0;
this.minResponseTime = Long.MAX_VALUE;
this.maxResponseTime = Long.MIN_VALUE;
resetTempraryField();
}
public void resetTempraryField() {
this.countThisTime = 0;
this.totalResponseTimeThisTime = 0;
}
public static boolean isSuccess(int statusCode) {
return statusCode == 200;
}
public void add(BehaviorResult behaviorResult) {
if (behaviorResult == null
|| behaviorResult.getBehaviorId() != this.behaviorId
|| behaviorResult.getStatusCode() != this.statusCode) {
return;
}
this.count++;
this.countThisTime++;
if (behaviorResult.getSuccessCount() == 0) {
this.contentLength += 0;
this.totalResponseTimeThisTime = 0;
return;
}
this.contentLength += behaviorResult.getContentLength();
this.totalResponseTimeThisTime += behaviorResult.getResponseTime();
if (behaviorResult.getResponseTime() > this.maxResponseTime) {
this.maxResponseTime = behaviorResult.getResponseTime();
}
if (behaviorResult.getResponseTime() < this.minResponseTime) {
this.minResponseTime = behaviorResult.getResponseTime();
}
}
private BehaviorStatusCodeResultModel buildModel() {
BehaviorStatusCodeResultModel result = new BehaviorStatusCodeResultModel();
result.setBehaviorId(this.behaviorId);
result.setContentLength(this.contentLength);
result.setContentType(this.contentType);
result.setCount(this.count);
result.setCountThisTime(this.countThisTime);
result.setMaxResponseTime(this.maxResponseTime);
result.setMinResponseTime(this.minResponseTime);
result.setStatusCode(this.statusCode);
result.setTotalResponseTimeThisTime(this.totalResponseTimeThisTime);
return result;
}
public BehaviorStatusCodeResultModel getStatistics() {
BehaviorStatusCodeResultModel result = buildModel();
this.resetTempraryField();
return result;
}
public boolean equals(Object expectedObj) {
Field[] fields = this.getClass().getDeclaredFields();
boolean equal = true;
try {
for (Field field : fields) {
field.setAccessible(true);
if (field.getName().equals("contentType")) {
field.get(expectedObj).equals(field.get(this));
continue;
}
if (field.getLong(this) != field.getLong(expectedObj)) {
System.out.println(field.getName()
+ " is diferent, this is " + field.getLong(this)
+ ", and the expected is "
+ field.getLong(expectedObj));
equal = false;
}
}
} catch (Exception e) {
e.printStackTrace();
equal = false;
}
return equal;
}
}

View File

@ -26,10 +26,10 @@ public class BehaviorsResultCollector {
new BehaviorResultCollector(behaviorResult
.getBehaviorId()));
}
}
this.behaviorResultMap.get(behaviorResult.getBehaviorId()).add(
behaviorResult);
}
}
public Object getStatistics(int behaviorId) {
if (!this.behaviorResultMap.containsKey(behaviorId)) {

View File

@ -29,7 +29,6 @@ public class DataCollectorImpl implements DataCollector {
}
public DataCollectorImpl(UUID testId, StorageHelper storageHelper) {
// mustDoWhenIniti();
this.storageHelper = storageHelper;
this.testId = testId;
this.pagesResultCollector = new PagesResultCollector();
@ -37,15 +36,7 @@ public class DataCollectorImpl implements DataCollector {
this.scenarioResultCollector = new ScenarioResultCollector();
}
// protected void mustDoWhenIniti() {
// this.setStorageHelper(ApplicationContextHelper.getContext().getBean(
// StorageHelper.class));
// }
public void add(final BehaviorResult behaviorResult) {
if (!Main.IS_TO_SAVE_DETAIL) {
return;
}
if (behaviorResult == null) {
return;
}
@ -55,6 +46,9 @@ public class DataCollectorImpl implements DataCollector {
}
private void saveItAsync(final BehaviorResult behaviorResult) {
if (!Main.IS_TO_SAVE_DETAIL) {
return;
}
Runnable runnable = new Runnable() {
public void run() {
storageHelper.getLocalStorage().writeFile(

View File

@ -1,110 +0,0 @@
package org.bench4q.agent.datacollector.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.agent.scenario.engine.PageResult;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
public class PageResultCollector extends AbstractDataCollector {
Map<Integer, PageBrief> pageBriefMap;
private Map<Integer, PageBrief> getPageBriefMap() {
return pageBriefMap;
}
private void setPageBriefMap(Map<Integer, PageBrief> pageBriefMap) {
this.pageBriefMap = pageBriefMap;
}
public PageResultCollector() {
this.setPageBriefMap(new HashMap<Integer, PageBrief>());
}
public void add(PageResult pageResult) {
if (pageResult == null || pageResult.getPageId() < 0) {
return;
}
PageBrief pageBrief = guardTheValueOfThePageIdExists(pageResult
.getPageId());
pageBrief.countThisTime++;
pageBrief.countFromBegin++;
pageBrief.totalResponseTimeThisTime += pageResult.getExecuteRange();
pageBrief.latesTimeResponseTime = pageResult.getExecuteRange();
if (pageResult.getExecuteRange() > pageBrief.maxResponseTimeFromBegin) {
pageBrief.maxResponseTimeFromBegin = pageResult.getExecuteRange();
}
if (pageResult.getExecuteRange() < pageBrief.minResponseTimeFromBegin) {
pageBrief.minResponseTimeFromBegin = pageResult.getExecuteRange();
}
}
private synchronized PageBrief guardTheValueOfThePageIdExists(int pageId) {
if (!this.getPageBriefMap().containsKey(pageId)) {
this.getPageBriefMap().put(pageId, new PageBrief());
}
return this.getPageBriefMap().get(pageId);
}
public Object getPageBriefStatistics(int pageId) {
PageBrief pageBrief = guardTheValueOfThePageIdExists(pageId);
AgentPageBriefModel result = new AgentPageBriefModel();
result.setCountFromBegin(pageBrief.countFromBegin);
result.setCountThisTime(pageBrief.countThisTime);
result.setMaxResponseTimeFromBegin(pageBrief.maxResponseTimeFromBegin);
result.setMinResponseTimeFromBegin(pageBrief.minResponseTimeFromBegin);
result.setTotalResponseTimeThisTime(pageBrief.totalResponseTimeThisTime);
result.setPageId(pageId);
long nowTime = new Date().getTime();
result.setTimeFrame(nowTime - pageBrief.lastSampleTime);
result.setLatestResponseTime(pageBrief.latesTimeResponseTime);
pageBrief.resetTemperatyField();
return result;
}
@Override
public void add(BehaviorResult behaviorResult) {
}
@Override
protected String calculateSavePath(BehaviorResult behaviorResult) {
return null;
}
@Override
public Object getScenarioBriefStatistics() {
return null;
}
@Override
public BehaviorBriefModel getBehaviorBriefStatistics(int id) {
return null;
}
public class PageBrief {
public long lastSampleTime;
public long countThisTime;
public long totalResponseTimeThisTime;
public long maxResponseTimeFromBegin;
public long minResponseTimeFromBegin;
public long countFromBegin;
public long latesTimeResponseTime;
public PageBrief() {
resetTemperatyField();
this.maxResponseTimeFromBegin = Long.MIN_VALUE;
this.minResponseTimeFromBegin = Long.MAX_VALUE;
this.countFromBegin = 0;
this.latesTimeResponseTime = 0;
}
public void resetTemperatyField() {
this.lastSampleTime = new Date().getTime();
this.countThisTime = 0;
this.totalResponseTimeThisTime = 0;
}
}
}

View File

@ -1,197 +0,0 @@
package org.bench4q.agent.test.datastatistics;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.ScenarioResultCollector;
import org.bench4q.agent.scenario.engine.BehaviorResult;
import org.bench4q.agent.scenario.engine.PageResult;
import org.bench4q.agent.storage.StorageHelper;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ScenarioStatisticsTest {
private DataCollector dataStatistics;
protected DataCollector getDataStatistics() {
return dataStatistics;
}
private void setDataStatistics(DataCollector dataStatistics) {
this.dataStatistics = dataStatistics;
}
public ScenarioStatisticsTest() {
init();
}
public void init() {
@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath*:/org/bench4q/agent/config/application-context.xml");
ScenarioResultCollector agentResultDataCollector = new ScenarioResultCollector(
UUID.randomUUID());
agentResultDataCollector.setStorageHelper((StorageHelper) context
.getBean(StorageHelper.class));
this.setDataStatistics(agentResultDataCollector);
}
@Test
public void addZeroBriefTest() {
for (BehaviorResult behaviorResult : makeBehaviorList(0, 1)) {
this.getDataStatistics().add(behaviorResult);
}
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getScenarioBriefStatistics();
AgentBriefStatusModel modelExpect = makeAllZeroModel();
modelExpect.setTimeFrame(model.getTimeFrame());
assertTrue(model.equals(modelExpect));
}
@Test
public void addOneBriefTest() throws InterruptedException {
List<BehaviorResult> generatedBehaviorResults = makeBehaviorList(1, 1);
for (BehaviorResult behaviorResult : generatedBehaviorResults) {
this.getDataStatistics().add(behaviorResult);
}
Thread.sleep(100);
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getScenarioBriefStatistics();
AgentBriefStatusModel modelExpect = new AgentBriefStatusModel();
modelExpect.setTimeFrame(model.getTimeFrame());
makeUpStatusModelForOneBehavior(modelExpect);
assertTrue(model.equals(modelExpect));
judgeBehaviorTotalAndScenarioTotal(generatedBehaviorResults, model);
}
private void judgeBehaviorTotalAndScenarioTotal(
List<BehaviorResult> generatedBehaviorResults,
AgentBriefStatusModel model) {
int successCount = 0;
HashSet<Integer> set = new HashSet<Integer>();
for (BehaviorResult unit : generatedBehaviorResults) {
BehaviorBriefModel briefModel = this.getDataStatistics()
.getBehaviorBriefStatistics(unit.getBehaviorId());
if (set.contains(unit.getBehaviorId()) || briefModel == null) {
continue;
}
set.add(unit.getBehaviorId());
successCount += briefModel.getSuccessfulCount();
}
assertEquals(successCount, model.getSuccessCountFromBegin());
}
@Test
public void addTwoBriefTest() throws InterruptedException {
List<BehaviorResult> behaviorResults = makeBehaviorList(2, 1);
for (BehaviorResult behaviorResult : behaviorResults) {
this.getDataStatistics().add(behaviorResult);
}
Thread.sleep(100);
AgentBriefStatusModel model = (AgentBriefStatusModel) this
.getDataStatistics().getScenarioBriefStatistics();
AgentBriefStatusModel modelExpect = makeUpStatusModelForTwoBehavior(model
.getTimeFrame());
assertTrue(model.equals(modelExpect));
judgeBehaviorTotalAndScenarioTotal(behaviorResults, model);
}
public static AgentBriefStatusModel makeUpStatusModelForTwoBehavior(
long timeFrame) {
AgentBriefStatusModel model = new AgentBriefStatusModel();
model.setTimeFrame(timeFrame);
model.setSuccessThroughputThisTime(1 * 1000 / timeFrame);
model.setFailCountFromBegin(1);
model.setFailThroughputThisTime(1 * 1000 / timeFrame);
model.setMaxResponseTime(200);
model.setMinResponseTime(200);
model.setSuccessCountFromBegin(1);
model.setSuccessCountThisTime(1);
model.setFailCountThisTime(1);
model.setTotalResponseTimeThisTime(200);
model.setTotalSqureResponseTimeThisTime(40000);
return model;
}
public static void makeUpStatusModelForOneBehavior(
AgentBriefStatusModel model) {
model.setSuccessThroughputThisTime(1 * 1000 / model.getTimeFrame());
model.setFailCountFromBegin(0);
model.setFailThroughputThisTime(0);
model.setMaxResponseTime(200);
model.setMinResponseTime(200);
model.setSuccessCountFromBegin(1);
model.setSuccessCountThisTime(1);
model.setFailCountThisTime(0);
model.setTotalResponseTimeThisTime(200);
model.setTotalSqureResponseTimeThisTime(40000);
}
public static AgentBriefStatusModel makeAllZeroModel() {
AgentBriefStatusModel model = new AgentBriefStatusModel();
model.setSuccessThroughputThisTime(0);
model.setFailCountFromBegin(0);
model.setFailThroughputThisTime(0);
model.setMaxResponseTime(0);
model.setMinResponseTime(0);
model.setTimeFrame(0);
model.setSuccessCountFromBegin(0);
model.setTotalResponseTimeThisTime(0);
model.setSuccessCountThisTime(0);
model.setFailCountThisTime(0);
model.setTotalSqureResponseTimeThisTime(0);
return model;
}
public static PageResult makePageResultWithBehaviorResult(int count) {
List<BehaviorResult> behaviorResults = makeBehaviorList(count, 1);
return PageResult.buildPageResult(2, behaviorResults);
}
public static List<BehaviorResult> makeBehaviorList(int count,
int behaviorId) {
List<BehaviorResult> behaviorResults = new ArrayList<BehaviorResult>();
for (int i = 0; i < count; i++) {
int statusCode = i % 2 == 0 ? 200 : 400;
behaviorResults.add(buildBehaviorResult(200 + 10 * i, i % 2 == 0,
statusCode, behaviorId, 200 + 10 * i));
}
return behaviorResults;
}
public static BehaviorResult buildBehaviorResult(long responseTime,
boolean success, int statusCode, int behaviorId, long startDateTime) {
Date date = new Date(startDateTime);
BehaviorResult result = new BehaviorResult();
result.setBehaviorName("");
result.setEndDate(new Date(date.getTime() + responseTime));
result.setPluginId("Get");
result.setPluginName("get");
result.setResponseTime(responseTime);
result.setStartDate(date);
result.setSuccessCount(success ? 1 : 0);
result.setFailCount(success ? 0 : 1);
result.setShouldBeCountResponseTime(true);
result.setBehaviorId(behaviorId);
if (success) {
result.setContentLength(20);
result.setContentType("image");
} else {
result.setContentLength(0);
result.setContentType("");
}
result.setStatusCode(statusCode);
return result;
}
}

View File

@ -51,12 +51,18 @@ public class Test_DataCollectorImpl {
.getScenarioBriefStatistics();
judgeBehaviorTotalAndScenarioTotal(
Arrays.asList(buildBehaviorResult, buildBehaviorResult2), model);
this.dataCollectorImpl.add(Test_ScenarioBriefCollector
.buildBehaviorResult(230, true, 200, 1, new Date().getTime()));
model = (AgentBriefStatusModel) this.dataCollectorImpl
.getScenarioBriefStatistics();
judgeBehaviorTotalAndScenarioTotal(
Arrays.asList(buildBehaviorResult, buildBehaviorResult2), model);
}
private void judgeBehaviorTotalAndScenarioTotal(
List<BehaviorResult> generatedBehaviorResults,
AgentBriefStatusModel model) {
int successCount = 0;
int successCount = 0, totalCount = 0;
HashSet<Integer> set = new HashSet<Integer>();
for (BehaviorResult unit : generatedBehaviorResults) {
BehaviorBriefModel briefModel = (BehaviorBriefModel) this.dataCollectorImpl
@ -66,7 +72,12 @@ public class Test_DataCollectorImpl {
}
set.add(unit.getBehaviorId());
successCount += briefModel.getSuccessfulCount();
totalCount += briefModel.getTotalCount();
}
assertEquals((long) successCount, model.getSuccessCountFromBegin());
assertEquals(
totalCount,
model.getSuccessCountFromBegin()
+ model.getFailCountFromBegin());
}
}