From 19170fd438128cd9cd13e45a69e83ed84602c23a Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Thu, 11 Sep 2014 17:42:55 +0800 Subject: [PATCH 1/8] add tests for behaviorsBrief add tests for behaviorsBrief --- .../org/bench4q/agent/api/TestController.java | 2 +- .../datastatistics/DetailStatisticsTest.java | 1 - .../BehaviorsBriefStatistics.java | 48 +++++-------------- .../BehaviorsBriefStatisticsTest.java | 9 +++- .../models/agent/BehaviorBriefModel.java | 5 ++ .../agent/BehaviorStatusCodeResultModel.java | 35 ++++++++++++++ .../statistics/AgentBehaviorsBriefModel.java | 4 ++ 7 files changed, 64 insertions(+), 40 deletions(-) diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index c9f76cd4..5a1682cd 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -179,7 +179,7 @@ public class TestController { AgentPagesBriefModel result = new AgentPagesBriefModel(); List pageBrieves = new ArrayList(); - if (context == null || context.isFinished()) { + if (context == null) { return null; } for (int i = 0; i < context.getScenario().getPages().length; i++) { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java index f892c05d..4a2893ab 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java @@ -161,6 +161,5 @@ public class DetailStatisticsTest { @Test public void test_addSomeSameUrlResult() { - } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java index 33bdef05..1b4a3928 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java @@ -1,9 +1,10 @@ package org.bench4q.master.domain.valueobject.datastatistics; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.agent.statistics.DataStatisticsModel; @@ -32,8 +33,8 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { } public BehaviorsBriefStatistics() { - this.setMap(new HashMap>()); - this.setIdUrlMap(new HashMap()); + this.setMap(new ConcurrentHashMap>()); + this.setIdUrlMap(new ConcurrentHashMap()); } public ScriptBehaviorsBriefModel getStatistics() { @@ -47,7 +48,8 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { .get(behaviorId)); List statusList = new ArrayList(); for (int statusCode : this.getMap().get(behaviorId).keySet()) { - statusList.add(getCopy(behaviorId, statusCode)); + statusList.add(this.getMap().get(behaviorId).get(statusCode) + .getCopy()); } behaviorBriefModel.setDetailStatusCodeResultModels(statusList); this.setBehaviorCount(behaviorBriefModel); @@ -62,13 +64,10 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { private void setBehaviorCount(BehaviorBriefModel behaviorBriefModel) { long totalCount = 0, successfulCount = 0; - if (behaviorBriefModel.getDetailStatusCodeResultModels() == null) { - return; - } for (BehaviorStatusCodeResultModel behaviorStatusCodeResultModel : behaviorBriefModel .getDetailStatusCodeResultModels()) { totalCount += behaviorStatusCodeResultModel.getCount(); - if (behaviorStatusCodeResultModel.getStatusCode() == 200) { + if (behaviorStatusCodeResultModel.isSuccess()) { successfulCount += behaviorStatusCodeResultModel.getCount(); } } @@ -76,21 +75,6 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { behaviorBriefModel.setSuccessfulCount(successfulCount); } - private BehaviorStatusCodeResultModel getCopy(int behaviorId, int statusCode) { - BehaviorStatusCodeResultModel statusInMap = this.getMap() - .get(behaviorId).get(statusCode); - BehaviorStatusCodeResultModel ret = new BehaviorStatusCodeResultModel(); - ret.setStatusCode(statusCode); - ret.setContentLength(statusInMap.getContentLength()); - ret.setContentType(statusInMap.getContentType()); - ret.setCount(statusInMap.getCount()); - ret.setMaxResponseTime(statusInMap.getMaxResponseTime()); - ret.setMinResponseTime(statusInMap.getMinResponseTime()); - ret.setTotalResponseTimeThisTime(statusInMap - .getTotalResponseTimeThisTime()); - return ret; - } - public void add(DataStatisticsModel dataUnit) { if (!(dataUnit instanceof AgentBehaviorsBriefModel)) { return; @@ -109,18 +93,7 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { } BehaviorStatusCodeResultModel origin = behaviorStatusMap .get(newOne.getStatusCode()); - origin.setContentLength(origin.getContentLength() - + newOne.getContentLength()); - origin.setCount(origin.getCount() + newOne.getCount()); - origin.setTotalResponseTimeThisTime(origin - .getTotalResponseTimeThisTime() - + newOne.getTotalResponseTimeThisTime()); - if (newOne.getMaxResponseTime() > origin.getMaxResponseTime()) { - origin.setMaxResponseTime(newOne.getMaxResponseTime()); - } - if (newOne.getMinResponseTime() < origin.getMinResponseTime()) { - origin.setMinResponseTime(newOne.getMinResponseTime()); - } + origin.plus(newOne); } } } @@ -128,8 +101,9 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { private synchronized void guardAllMapsKeyExists( BehaviorBriefModel behaviorBriefModel) { if (!this.getMap().containsKey(behaviorBriefModel.getBehaviorId())) { - this.getMap().put(behaviorBriefModel.getBehaviorId(), - new HashMap()); + this.getMap() + .put(behaviorBriefModel.getBehaviorId(), + new ConcurrentHashMap()); } if (!this.getIdUrlMap().containsKey(behaviorBriefModel.getBehaviorId())) { this.getIdUrlMap().put(behaviorBriefModel.getBehaviorId(), diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java index 009ae8f7..8c86dec9 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java @@ -1,10 +1,17 @@ package org.bench4q.master.unitTest.datastatistics; +import org.bench4q.master.domain.valueobject.datastatistics.BehaviorsBriefStatistics; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.junit.Test; public class BehaviorsBriefStatisticsTest { @Test public void testAddNull() { - + BehaviorsBriefStatistics behaviorsBriefStatistics = new BehaviorsBriefStatistics(); + AgentBehaviorsBriefModel dataUnit = new AgentBehaviorsBriefModel(); + behaviorsBriefStatistics.add(dataUnit); + System.out.println(MarshalHelper.tryMarshal(behaviorsBriefStatistics + .getStatistics())); } } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java index 1b723bae..e4bf9386 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java @@ -1,6 +1,8 @@ package org.bench4q.share.models.agent; +import java.util.LinkedList; import java.util.List; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @@ -60,4 +62,7 @@ public class BehaviorBriefModel { this.successfulCount = successfulCount; } + public BehaviorBriefModel() { + this.setDetailStatusCodeResultModels(new LinkedList()); + } } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java index 983c438d..cd35e7cb 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java @@ -76,4 +76,39 @@ public class BehaviorStatusCodeResultModel { this.totalResponseTimeThisTime = totalResponseTimeThisTime; } + /** + * It's just the http's success judgement, if we'll turn to other protocal, + * we should build a inheritance system + * + * @return + */ + public boolean isSuccess() { + return this.getStatusCode() == 200; + } + + public BehaviorStatusCodeResultModel getCopy() { + BehaviorStatusCodeResultModel ret = new BehaviorStatusCodeResultModel(); + ret.setStatusCode(getStatusCode()); + ret.setContentLength(getContentLength()); + ret.setContentType(getContentType()); + ret.setCount(getCount()); + ret.setMaxResponseTime(getMaxResponseTime()); + ret.setMinResponseTime(getMinResponseTime()); + ret.setTotalResponseTimeThisTime(getTotalResponseTimeThisTime()); + return ret; + } + + public synchronized void plus(BehaviorStatusCodeResultModel newOne) { + this.setContentLength(this.getContentLength() + + newOne.getContentLength()); + this.setCount(this.getCount() + newOne.getCount()); + this.setTotalResponseTimeThisTime(this.getTotalResponseTimeThisTime() + + newOne.getTotalResponseTimeThisTime()); + if (newOne.getMaxResponseTime() > this.getMaxResponseTime()) { + this.setMaxResponseTime(newOne.getMaxResponseTime()); + } + if (newOne.getMinResponseTime() < this.getMinResponseTime()) { + this.setMinResponseTime(newOne.getMinResponseTime()); + } + } } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java index 9c385f1b..27c55265 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java @@ -1,5 +1,6 @@ package org.bench4q.share.models.agent.statistics; +import java.util.LinkedList; import java.util.List; import javax.xml.bind.annotation.XmlElement; @@ -23,4 +24,7 @@ public class AgentBehaviorsBriefModel extends DataStatisticsModel { this.behaviorBriefModels = behaviorBriefModels; } + public AgentBehaviorsBriefModel() { + this.setBehaviorBriefModels(new LinkedList()); + } } From 2b1fdb9f17dbf133a86c6a82781e26a57d837d70 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Fri, 12 Sep 2014 15:08:59 +0800 Subject: [PATCH 2/8] add behaviorResultCollector and behaviorStatusResultCollector add behaviorResultCollector and behaviorStatusResultCollector --- .../org/bench4q/agent/api/TestController.java | 70 +----- .../agent/datacollector/DataCollector.java | 6 +- .../impl/AbstractDataCollector.java | 4 +- .../impl/BehaviorResultCollector.java | 59 ++++- .../BehaviorStatusCodeResultCollector.java | 109 +++++++++ .../impl/PageResultCollector.java | 4 +- .../impl/ScenarioResultCollector.java | 71 ++---- .../agent/scenario/behavior/Behavior.java | 6 +- .../scenario/behavior/ControlBehavior.java | 6 +- .../agent/scenario/behavior/TestBehavior.java | 6 +- .../scenario/behavior/TimerBehavior.java | 6 +- .../behavior/TransactionBehavior.java | 6 +- .../agent/scenario/behavior/UserBehavior.java | 6 +- .../PageResultStatisticsTest.java | 14 +- .../ScenarioStatisticsTest.java | 29 +-- .../Test_BehaviorResultCollector.java | 117 ++++++++++ .../Test_BehaviorStatusResultStatistics.java | 75 ++++++ .../master/domain/entity/TestPlanScript.java | 2 +- .../datastatistics/RunningScriptSampler.java | 6 +- .../impl/BehaviorBriefStatistics.java | 70 ++++++ .../impl/BehaviorsBriefStatistics.java | 134 +++++++++++ .../impl/PageBriefStatistics.java | 79 +++++++ .../impl/PagesBriefStatistics.java | 60 +++++ .../impl/ScriptBriefStatistics.java | 214 ++++++++++++++++++ .../datastatistics/impl/ScriptStatistics.java | 14 ++ .../integrated/Test_RunningScriptSampler.java | 2 +- .../BehaviorsBriefStatisticsTest.java | 15 +- .../ScriptBriefStatisticsTest.java | 2 +- .../datastatistics/TestPageBrief.java | 2 +- .../datastatistics/Test_PagesBrief.java | 2 +- .../datastatistics/Test_ScriptSample.java | 4 +- .../models/agent/BehaviorBriefModel.java | 4 +- .../agent/BehaviorStatusCodeResultModel.java | 24 ++ .../statistics/ScriptBehaviorBriefModel.java | 70 ++++++ .../ScriptBehaviorStatusCodeModel.java | 59 +++++ 35 files changed, 1172 insertions(+), 185 deletions(-) create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java create mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java create mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PageBriefStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PagesBriefStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptStatistics.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index 5a1682cd..dd752b6e 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -2,11 +2,9 @@ package org.bench4q.agent.api; import java.util.ArrayList; import java.util.List; -import java.util.Map; import java.util.UUID; import org.apache.log4j.Logger; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; import org.bench4q.agent.plugin.ParameterFileCollector; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.behavior.Behavior; @@ -15,7 +13,6 @@ import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.agent.scenario.engine.Schedule; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.agent.CleanTestResultModel; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; @@ -127,50 +124,6 @@ public class TestController { return result; } - @RequestMapping(value = "/brief/{runId}/{behaviorId}", method = RequestMethod.GET) - @ResponseBody - public BehaviorBriefModel behaviorBrief(@PathVariable UUID runId, - @PathVariable int behaviorId) { - ScenarioContext scenarioContext = this.getScenarioEngine() - .getRunningTests().get(runId); - if (scenarioContext == null) { - return null; - } - Map map = scenarioContext - .getDataStatistics().getBehaviorBriefStatistics(behaviorId); - return buildBehaviorBrief(runId, behaviorId, "", map); - } - - private BehaviorBriefModel buildBehaviorBrief(UUID runId, int behaviorId, - String behaviorUrl, Map map) { - List detailStatusCodeResultModels = new ArrayList(); - for (int statusCode : map.keySet()) { - BehaviorStatusCodeResultModel behaviorStatusCodeResultModel = new BehaviorStatusCodeResultModel(); - BehaviorStatusCodeResult detailStatusCodeResult = map - .get(statusCode); - behaviorStatusCodeResultModel.setStatusCode(statusCode); - behaviorStatusCodeResultModel - .setCount(detailStatusCodeResult.count); - behaviorStatusCodeResultModel - .setContentLength(detailStatusCodeResult.contentLength); - behaviorStatusCodeResultModel - .setMinResponseTime(detailStatusCodeResult.minResponseTime); - behaviorStatusCodeResultModel - .setMaxResponseTime(detailStatusCodeResult.maxResponseTime); - behaviorStatusCodeResultModel - .setContentType(detailStatusCodeResult.contentType); - behaviorStatusCodeResultModel - .setTotalResponseTimeThisTime(detailStatusCodeResult.totalResponseTimeThisTime); - detailStatusCodeResultModels.add(behaviorStatusCodeResultModel); - } - BehaviorBriefModel behaviorBriefModel = new BehaviorBriefModel(); - behaviorBriefModel.setBehaviorId(behaviorId); - behaviorBriefModel - .setDetailStatusCodeResultModels(detailStatusCodeResultModels); - behaviorBriefModel.setBehaviorUrl(behaviorUrl); - return behaviorBriefModel; - } - @RequestMapping(value = "/pagesBrief/{runId}") @ResponseBody public AgentPagesBriefModel pagesBrief(@PathVariable UUID runId) { @@ -190,19 +143,6 @@ public class TestController { return result; } - @RequestMapping(value = "/pageBrief/{runId}/{pageId}") - @ResponseBody - public AgentPageBriefModel pageBrief(@PathVariable UUID runId, - @PathVariable int pageId) { - ScenarioContext context = this.getScenarioEngine().getRunningTests() - .get(runId); - if (context == null) { - return null; - } - return (AgentPageBriefModel) context.getDataStatistics() - .getPageBriefStatistics(pageId); - } - @RequestMapping(value = "/behaviorsBrief/{runId}") @ResponseBody public AgentBehaviorsBriefModel behaviorsBrief(@PathVariable UUID runId) { @@ -215,14 +155,10 @@ public class TestController { } for (Behavior behavior : scenarioContext.getScenario() .getAllBehaviors()) { - int behaviorId = behavior.getId(); - Map map = behavior + BehaviorBriefModel briefModel = behavior .getBehaviorBriefResult(scenarioContext.getDataStatistics()); - if (map == null) { - continue; - } - behaviorBriefModels.add(buildBehaviorBrief(runId, behaviorId, - behavior.getSpecificParamValue("url"), map)); + briefModel.setBehaviorUrl(behavior.getSpecificParamValue("url")); + behaviorBriefModels.add(briefModel); } ret.setBehaviorBriefModels(behaviorBriefModels); return ret; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java index 1e1a073a..7c11589d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java @@ -1,10 +1,8 @@ package org.bench4q.agent.datacollector; -import java.util.Map; - -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.agent.scenario.engine.PageResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public interface DataCollector { public void add(BehaviorResult behaviorResult); @@ -13,7 +11,7 @@ public interface DataCollector { public Object getScenarioBriefStatistics(); - public Map getBehaviorBriefStatistics( + public BehaviorBriefModel getBehaviorBriefStatistics( int behaviorId); public Object getPageBriefStatistics(int pageId); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java index 1ab0ed1d..682e17bc 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java @@ -1,6 +1,5 @@ package org.bench4q.agent.datacollector.impl; -import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -9,6 +8,7 @@ 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 { @@ -76,7 +76,7 @@ public abstract class AbstractDataCollector implements DataCollector { public abstract Object getScenarioBriefStatistics(); - public abstract Map getBehaviorBriefStatistics( + public abstract BehaviorBriefModel getBehaviorBriefStatistics( int id); } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java index 09cd8dab..ddaf3757 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java @@ -1,5 +1,62 @@ package org.bench4q.agent.datacollector.impl; -public class BehaviorResultCollector { +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 detailStatusMap; + + public BehaviorResultCollector(int behaviorId) { + this.behaviorId = behaviorId; + this.detailStatusMap = new HashMap(); + } + + 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 list = new LinkedList(); + 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; + } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java new file mode 100644 index 00000000..ea6d11b0 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java @@ -0,0 +1,109 @@ +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; + } +} \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java index e75b5a30..fa841d10 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java @@ -6,6 +6,7 @@ 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 { @@ -79,8 +80,7 @@ public class PageResultCollector extends AbstractDataCollector { } @Override - public Map getBehaviorBriefStatistics( - int id) { + public BehaviorBriefModel getBehaviorBriefStatistics(int id) { return null; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java index 4491cd45..88069b4d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java @@ -1,15 +1,14 @@ package org.bench4q.agent.datacollector.impl; import java.text.SimpleDateFormat; -import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.UUID; -import java.util.concurrent.ConcurrentHashMap; 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.AgentBriefStatusModel; /** @@ -31,9 +30,8 @@ public class ScenarioResultCollector extends AbstractDataCollector { private static long TIME_UNIT = 1000; private UUID testID; private PageResultCollector pageResultCollector; - // The first integer is the behavior's id, and the second integer is - // the StatusCode of this behaviorResult. - private Map> detailMap; + // behaviorId -- BehaviorResultCollector + private final Map behaviorCollectors; private void setTimeOfPreviousCall(long timeOfPreviousCall) { this.timeOfPreviousCall = timeOfPreviousCall; @@ -92,11 +90,6 @@ public class ScenarioResultCollector extends AbstractDataCollector { this.testID = testID; } - private void setDetailMap( - Map> detailMap) { - this.detailMap = detailMap; - } - private PageResultCollector getPageResultCollector() { return pageResultCollector; } @@ -106,6 +99,7 @@ public class ScenarioResultCollector extends AbstractDataCollector { } public ScenarioResultCollector(UUID testId) { + this.behaviorCollectors = new HashMap(); this.setTestID(testId); this.setPageResultCollector(new PageResultCollector()); init(); @@ -115,7 +109,6 @@ public class ScenarioResultCollector extends AbstractDataCollector { reset(); this.setCumulativeFailCount(0); this.setCumulativeSucessfulCount(0); - this.setDetailMap(new ConcurrentHashMap>()); } private void reset() { @@ -193,46 +186,9 @@ public class ScenarioResultCollector extends AbstractDataCollector { } private void statisticBehaviorBriefResult(BehaviorResult behaviorResult) { - insertWhenNotExist(behaviorResult); - Map detailStatusMap = this.detailMap + BehaviorResultCollector collector = this.behaviorCollectors .get(behaviorResult.getBehaviorId()); - // TODO: there's a problem about concurrency - guardStatusMapExists(behaviorResult, detailStatusMap); - BehaviorStatusCodeResult statusCodeResult = detailStatusMap - .get(behaviorResult.getStatusCode()); - statusCodeResult.count++; - if (behaviorResult.getSuccessCount() == 0) { - statusCodeResult.contentLength += 0; - statusCodeResult.totalResponseTimeThisTime = 0; - return; - } - statusCodeResult.contentLength += behaviorResult.getContentLength(); - statusCodeResult.totalResponseTimeThisTime += behaviorResult - .getResponseTime(); - if (behaviorResult.getResponseTime() > statusCodeResult.maxResponseTime) { - statusCodeResult.maxResponseTime = behaviorResult.getResponseTime(); - } - if (behaviorResult.getResponseTime() < statusCodeResult.minResponseTime) { - statusCodeResult.minResponseTime = behaviorResult.getResponseTime(); - } - } - - private synchronized void guardStatusMapExists( - BehaviorResult behaviorResult, - Map detailStatusMap) { - if (!detailStatusMap.containsKey(behaviorResult.getStatusCode())) { - detailStatusMap.put( - new Integer(behaviorResult.getStatusCode()), - new BehaviorStatusCodeResult(behaviorResult - .getContentType())); - } - } - - private synchronized void insertWhenNotExist(BehaviorResult behaviorResult) { - if (!this.detailMap.containsKey(behaviorResult.getBehaviorId())) { - this.detailMap.put(new Integer(behaviorResult.getBehaviorId()), - new HashMap()); - } + collector.add(behaviorResult); } @Override @@ -253,17 +209,24 @@ public class ScenarioResultCollector extends AbstractDataCollector { @Override public synchronized void add(BehaviorResult behaviorResult) { super.add(behaviorResult); + if (!this.behaviorCollectors + .containsKey(behaviorResult.getBehaviorId())) { + this.behaviorCollectors + .put(behaviorResult.getBehaviorId(), + new BehaviorResultCollector(behaviorResult + .getBehaviorId())); + } statisticScenarioBriefResult(behaviorResult); statisticBehaviorBriefResult(behaviorResult); } @Override - public Map getBehaviorBriefStatistics( - int behaviorId) { - if (!this.detailMap.containsKey(behaviorId)) { + public BehaviorBriefModel getBehaviorBriefStatistics(int behaviorId) { + if (!this.behaviorCollectors.containsKey(behaviorId)) { return null; } - return Collections.unmodifiableMap(this.detailMap.get(behaviorId)); + BehaviorResultCollector m = this.behaviorCollectors.get(behaviorId); + return m.getStatistics(); } public Object getPageBriefStatistics(int pageId) { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java index a8c3656a..b0324d7d 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java @@ -1,10 +1,8 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; import org.bench4q.agent.scenario.Parameter; +import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; @@ -53,7 +51,7 @@ public abstract class Behavior { public abstract boolean shouldBeCount(); - public abstract Map getBehaviorBriefResult( + public abstract BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics); public String getSpecificParamValue(String paramName) { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java index f1e5bfbf..6a493370 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java @@ -1,9 +1,7 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public class ControlBehavior extends Behavior { @@ -13,7 +11,7 @@ public class ControlBehavior extends Behavior { } @Override - public Map getBehaviorBriefResult( + public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { return null; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java index 6a1ccd21..6373e147 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java @@ -1,9 +1,7 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public class TestBehavior extends Behavior { @@ -13,7 +11,7 @@ public class TestBehavior extends Behavior { } @Override - public Map getBehaviorBriefResult( + public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { return null; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java index 57412d55..aa7ddc2f 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java @@ -1,9 +1,7 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public class TimerBehavior extends Behavior { @@ -13,7 +11,7 @@ public class TimerBehavior extends Behavior { } @Override - public Map getBehaviorBriefResult( + public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { return null; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java index ba109b3b..beed445e 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java @@ -1,9 +1,7 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public class TransactionBehavior extends Behavior { @@ -13,7 +11,7 @@ public class TransactionBehavior extends Behavior { } @Override - public Map getBehaviorBriefResult( + public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { return null; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java index 502c9c2e..231f034e 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java @@ -1,9 +1,7 @@ package org.bench4q.agent.scenario.behavior; -import java.util.Map; - import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; +import org.bench4q.share.models.agent.BehaviorBriefModel; public class UserBehavior extends Behavior { @Override @@ -12,7 +10,7 @@ public class UserBehavior extends Behavior { } @Override - public Map getBehaviorBriefResult( + public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { return dataStatistics.getBehaviorBriefStatistics(this.getId()); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java index 671bf4ea..6f0edd5b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java @@ -36,7 +36,7 @@ public class PageResultStatisticsTest { @Test public void pageResultTest() { PageResult pageResult = PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1)); + ScenarioStatisticsTest.makeBehaviorList(1, 1)); assertEquals(2, pageResult.getPageId()); assertEquals(200, pageResult.getExecuteRange()); } @@ -44,7 +44,7 @@ public class PageResultStatisticsTest { @Test public void pageResultWithTwoBehaviorTest() { PageResult pageResult = PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2)); + ScenarioStatisticsTest.makeBehaviorList(2, 1)); assertEquals(2, pageResult.getPageId()); assertEquals(220, pageResult.getExecuteRange()); assertEquals(420, pageResult.getPageEndTime()); @@ -60,7 +60,7 @@ public class PageResultStatisticsTest { public void testOnePaegWithOneBehaviorResult() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1))); + ScenarioStatisticsTest.makeBehaviorList(1, 1))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, pageBriefModel.getPageId()); @@ -77,7 +77,7 @@ public class PageResultStatisticsTest { public void testOnePageWithTwoBehaviorResult() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2))); + ScenarioStatisticsTest.makeBehaviorList(2, 1))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); System.out.println(pageBriefModel.getCountFromBegin()); @@ -95,10 +95,10 @@ public class PageResultStatisticsTest { public void testTwoPageWithStatisticsAtLast() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1))); + ScenarioStatisticsTest.makeBehaviorList(1, 1))); this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2))); + ScenarioStatisticsTest.makeBehaviorList(2, 2))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, pageBriefModel.getPageId()); @@ -116,7 +116,7 @@ public class PageResultStatisticsTest { testOnePaegWithOneBehaviorResult(); this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2))); + ScenarioStatisticsTest.makeBehaviorList(2, 1))); AgentPageBriefModel model = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, model.getPageId()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java index 8b233eb9..a28a24c2 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java @@ -4,15 +4,16 @@ 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.BehaviorStatusCodeResult; 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; @@ -46,7 +47,7 @@ public class ScenarioStatisticsTest { @Test public void addZeroBriefTest() { - for (BehaviorResult behaviorResult : makeBehaviorListWhoseBehaviorIdIsOne(0)) { + for (BehaviorResult behaviorResult : makeBehaviorList(0, 1)) { this.getDataStatistics().add(behaviorResult); } AgentBriefStatusModel model = (AgentBriefStatusModel) this @@ -58,11 +59,10 @@ public class ScenarioStatisticsTest { @Test public void addOneBriefTest() throws InterruptedException { - List generatedBehaviorResults = makeBehaviorListWhoseBehaviorIdIsOne(1); + List generatedBehaviorResults = makeBehaviorList(1, 1); for (BehaviorResult behaviorResult : generatedBehaviorResults) { this.getDataStatistics().add(behaviorResult); } - Thread.sleep(100); AgentBriefStatusModel model = (AgentBriefStatusModel) this .getDataStatistics().getScenarioBriefStatistics(); @@ -77,21 +77,22 @@ public class ScenarioStatisticsTest { List generatedBehaviorResults, AgentBriefStatusModel model) { int successCount = 0; + HashSet set = new HashSet(); for (BehaviorResult unit : generatedBehaviorResults) { - BehaviorStatusCodeResult behaviorStatusCodeResult = this - .getDataStatistics() - .getBehaviorBriefStatistics(unit.getBehaviorId()).get(200); - if (behaviorStatusCodeResult == null) { + BehaviorBriefModel briefModel = this.getDataStatistics() + .getBehaviorBriefStatistics(unit.getBehaviorId()); + if (set.contains(unit.getBehaviorId()) || briefModel == null) { continue; } - successCount += behaviorStatusCodeResult.count; + set.add(unit.getBehaviorId()); + successCount += briefModel.getSuccessfulCount(); } assertEquals(successCount, model.getSuccessCountFromBegin()); } @Test public void addTwoBriefTest() throws InterruptedException { - List behaviorResults = makeBehaviorListWhoseBehaviorIdIsOne(2); + List behaviorResults = makeBehaviorList(2, 1); for (BehaviorResult behaviorResult : behaviorResults) { this.getDataStatistics().add(behaviorResult); } @@ -152,17 +153,17 @@ public class ScenarioStatisticsTest { } public static PageResult makePageResultWithBehaviorResult(int count) { - List behaviorResults = makeBehaviorListWhoseBehaviorIdIsOne(count); + List behaviorResults = makeBehaviorList(count, 1); return PageResult.buildPageResult(2, behaviorResults); } - public static List makeBehaviorListWhoseBehaviorIdIsOne( - int count) { + public static List makeBehaviorList(int count, + int behaviorId) { List behaviorResults = new ArrayList(); for (int i = 0; i < count; i++) { int statusCode = i % 2 == 0 ? 200 : 400; behaviorResults.add(buildBehaviorResult(200 + 10 * i, i % 2 == 0, - statusCode, i, 200 + 10 * i)); + statusCode, behaviorId, 200 + 10 * i)); } return behaviorResults; } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java new file mode 100644 index 00000000..424d8e76 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java @@ -0,0 +1,117 @@ +package org.bench4q.agent.test.datastatistics; + +import static org.junit.Assert.*; + +import java.util.UUID; + +import org.bench4q.agent.datacollector.DataCollector; +import org.bench4q.agent.datacollector.impl.BehaviorResultCollector; +import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; +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.BehaviorStatusCodeResultModel; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class Test_BehaviorResultCollector { + private DataCollector detailStatistics; + + private DataCollector getDetailStatistics() { + return detailStatistics; + } + + private void setDetailStatistics(DataCollector detailStatistics) { + this.detailStatistics = detailStatistics; + } + + public Test_BehaviorResultCollector() { + init(); + } + + private 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.setDetailStatistics(agentResultDataCollector); + } + + @Test + public void addZeroTest() { + for (BehaviorResult behaviorResult : ScenarioStatisticsTest + .makeBehaviorList(0, 1)) { + this.getDetailStatistics().add(behaviorResult); + } + + Object object = this.detailStatistics.getBehaviorBriefStatistics(0); + assertEquals(null, object); + } + + @Test + public void addOneDetailTest() { + BehaviorResultCollector collector = new BehaviorResultCollector(1); + for (BehaviorResult behaviorResult : ScenarioStatisticsTest + .makeBehaviorList(1, 1)) { + collector.add(behaviorResult); + } + BehaviorBriefModel briefModel = collector.getStatistics(); + assertEquals(briefModel.getSuccessfulCount(), 1); + assertEquals(briefModel.getTotalCount(), 1); + assertEquals(1, briefModel.getDetailStatusCodeResultModels().size()); + BehaviorStatusCodeResultModel resultModel = briefModel + .getDetailStatusCodeResultModels().get(0); + assertEquals(resultModel.getContentLength(), 20); + assertEquals(resultModel.getCountThisTime(), 1); + assertEquals(resultModel.getCount(), 1); + assertEquals(resultModel.getMaxResponseTime(), 200); + assertEquals(resultModel.getMinResponseTime(), 200); + assertEquals(resultModel.getTotalResponseTimeThisTime(), 200); + } + + @Test + public void addTwoDetailTest() { + int behaviorId = 1; + BehaviorResultCollector collector = new BehaviorResultCollector( + behaviorId); + for (BehaviorResult behaviorResult : ScenarioStatisticsTest + .makeBehaviorList(2, behaviorId)) { + collector.add(behaviorResult); + } + BehaviorBriefModel briefModel = collector.getStatistics(); + assertEquals(1, briefModel.getSuccessfulCount()); + assertEquals(2, briefModel.getTotalCount()); + assertEquals(2, briefModel.getDetailStatusCodeResultModels().size()); + for (BehaviorStatusCodeResultModel statusCodeResultModel : briefModel + .getDetailStatusCodeResultModels()) { + if (statusCodeResultModel.getStatusCode() == 200) { + assertEquals(statusCodeResultModel.getContentLength(), 20); + assertEquals(statusCodeResultModel.getCount(), 1); + assertEquals(statusCodeResultModel.getCountThisTime(), 1); + assertEquals(statusCodeResultModel.getMaxResponseTime(), 200); + assertEquals(statusCodeResultModel.getMinResponseTime(), 200); + assertEquals( + statusCodeResultModel.getTotalResponseTimeThisTime(), + 200); + } else { + assertEquals(statusCodeResultModel.getContentLength(), 0); + assertEquals(statusCodeResultModel.getCount(), 1); + assertEquals(statusCodeResultModel.getCountThisTime(), 1); + assertEquals(statusCodeResultModel.getMaxResponseTime(), + Long.MIN_VALUE); + assertEquals(statusCodeResultModel.getMinResponseTime(), + Long.MAX_VALUE); + assertEquals( + statusCodeResultModel.getTotalResponseTimeThisTime(), 0); + } + } + } + + @Test + public void test_addSomeSameUrlResult() { + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java new file mode 100644 index 00000000..29ed62a7 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java @@ -0,0 +1,75 @@ +package org.bench4q.agent.test.datastatistics; + +import static org.junit.Assert.assertEquals; + +import java.util.Date; + +import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResultCollector; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.junit.Test; + +public class Test_BehaviorStatusResultStatistics { + @Test + public void addOneDetailTest() { + BehaviorStatusCodeResultCollector statusCodeResultCollector = new BehaviorStatusCodeResultCollector( + 0, 200, "text/html"); + for (BehaviorResult behaviorResult : ScenarioStatisticsTest + .makeBehaviorList(1, 0)) { + statusCodeResultCollector.add(behaviorResult); + } + BehaviorStatusCodeResultModel resultModel = statusCodeResultCollector + .getStatistics(); + assertEquals(resultModel.getContentLength(), 20); + assertEquals(resultModel.getCountThisTime(), 1); + assertEquals(resultModel.getCount(), 1); + assertEquals(resultModel.getMaxResponseTime(), 200); + assertEquals(resultModel.getMinResponseTime(), 200); + assertEquals(resultModel.getTotalResponseTimeThisTime(), 200); + } + + @Test + public void addTwoTest() throws InterruptedException { + BehaviorStatusCodeResultCollector statusCodeResultCollector = new BehaviorStatusCodeResultCollector( + 1, 200, "text/html"); + statusCodeResultCollector.add(ScenarioStatisticsTest + .buildBehaviorResult(300, true, 200, 1, new Date().getTime())); + Thread.sleep(100); + statusCodeResultCollector.add(ScenarioStatisticsTest + .buildBehaviorResult(320, true, 200, 1, new Date().getTime())); + BehaviorStatusCodeResultModel result = statusCodeResultCollector + .getStatistics(); + assertEquals(result.getContentLength(), 40); + assertEquals(result.getCountThisTime(), 2); + assertEquals(result.getCount(), 2); + assertEquals(result.getMaxResponseTime(), 320); + assertEquals(result.getMinResponseTime(), 300); + assertEquals(result.getTotalResponseTimeThisTime(), 620); + } + + @Test + public void addThreeAndBriefTwice() { + BehaviorStatusCodeResultCollector collector = new BehaviorStatusCodeResultCollector( + 1, 302, "text/html"); + collector.add(ScenarioStatisticsTest.buildBehaviorResult(210, true, + 302, 1, new Date().getTime())); + collector.add(ScenarioStatisticsTest.buildBehaviorResult(230, true, + 302, 1, new Date().getTime())); + BehaviorStatusCodeResultModel resultModel = collector.getStatistics(); + assertEquals(resultModel.getContentLength(), 40); + assertEquals(resultModel.getCountThisTime(), 2); + assertEquals(resultModel.getCount(), 2); + assertEquals(resultModel.getMaxResponseTime(), 230); + assertEquals(resultModel.getMinResponseTime(), 210); + assertEquals(resultModel.getTotalResponseTimeThisTime(), 440); + collector.add(ScenarioStatisticsTest.buildBehaviorResult(190, true, + 302, 1, new Date().getTime())); + resultModel = collector.getStatistics(); + assertEquals(resultModel.getContentLength(), 60); + assertEquals(resultModel.getCountThisTime(), 1); + assertEquals(resultModel.getCount(), 3); + assertEquals(resultModel.getMaxResponseTime(), 230); + assertEquals(resultModel.getMinResponseTime(), 190); + assertEquals(resultModel.getTotalResponseTimeThisTime(), 190); + } +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java index 49ff0efb..10a79ba2 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlanScript.java @@ -223,7 +223,7 @@ public class TestPlanScript implements RunningScriptInterface { public List sample(Date sampleTime) { try { ScriptResultModel scriptResultModel = this.getSampler() - .getResultModelFromAgent(); + .briefResultFromAgent(); List testPlanScriptResultList = this .getTestPlanFactory().createScriptResultsWithoutId( diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/RunningScriptSampler.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/RunningScriptSampler.java index 21f0b746..8cde4aca 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/RunningScriptSampler.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/RunningScriptSampler.java @@ -8,6 +8,10 @@ import java.util.List; import javax.xml.bind.JAXBException; import org.bench4q.master.domain.RunningAgentInterface; +import org.bench4q.master.domain.valueobject.datastatistics.impl.BehaviorsBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.PagesBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.ScriptBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.ScriptStatistics; import org.bench4q.share.models.agent.TestBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; @@ -52,7 +56,7 @@ public class RunningScriptSampler { this.getRunningAgents().addAll(runningAgents); } - public ScriptResultModel getResultModelFromAgent() throws JAXBException { + public ScriptResultModel briefResultFromAgent() throws JAXBException { for (RunningAgentInterface runningAgent : getRunningAgents()) { if (runningAgent.isBreakDown()) { continue; diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java new file mode 100644 index 00000000..58ca2653 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java @@ -0,0 +1,70 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import java.util.HashMap; +import java.util.Map; + +import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorBriefModel; + +public class BehaviorBriefStatistics extends ScriptStatistics { + private long countFromBegin; + private long countThisTime; + private long successCountThisTime; + private long successCountFromBegin; + private long maxResponseTimeFromBegin; + private long minResponseTimeFromBegin; + private long totalResponseTimeThisTime; + + private volatile Map behaviorStatusMap; + + public BehaviorBriefStatistics() { + this.behaviorStatusMap = new HashMap(); + resetTempraryFields(); + this.maxResponseTimeFromBegin = 0; + this.minResponseTimeFromBegin = Long.MAX_VALUE; + } + + /** + * Now, for totalCount, i just collect all result from agent, and add their + * countFromBegin together. But there will be a error there. if an agent + * doesn't give back its result, the the countFromBegin maybe less that last + * one. + */ + private void resetTempraryFields() { + this.countFromBegin = 0; + this.countThisTime = 0; + this.successCountThisTime = 0; + this.successCountFromBegin = 0; + this.totalResponseTimeThisTime = 0; + } + + @Override + public void add(DataStatisticsModel dataUnit) { + if (!(dataUnit instanceof BehaviorBriefModel)) { + return; + } + BehaviorBriefModel behaviorBriefModel = (BehaviorBriefModel) dataUnit; + this.countFromBegin += behaviorBriefModel.getTotalCount(); + this.successCountFromBegin += behaviorBriefModel.getSuccessfulCount(); + for (BehaviorStatusCodeResultModel newOne : behaviorBriefModel + .getDetailStatusCodeResultModels()) { + if (!this.behaviorStatusMap.containsKey(newOne.getStatusCode())) { + behaviorStatusMap.put(newOne.getStatusCode(), newOne); + continue; + } + BehaviorStatusCodeResultModel origin = behaviorStatusMap.get(newOne + .getStatusCode()); + origin.plus(newOne); + } + } + + @Override + public Object getStatistics() { + ScriptBehaviorBriefModel result = new ScriptBehaviorBriefModel(); + + return null; + } + +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java new file mode 100644 index 00000000..fa81071e --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java @@ -0,0 +1,134 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; + +public class BehaviorsBriefStatistics extends ScriptStatistics { + private Map> map; + private Map idUrlMap; + private Map behaviorStatisticsMap; + + private Map> getMap() { + return map; + } + + private void setMap( + Map> map) { + this.map = map; + } + + private Map getIdUrlMap() { + return idUrlMap; + } + + private void setIdUrlMap(Map idUrlMap) { + this.idUrlMap = idUrlMap; + } + + private Map getBehaviorStatisticsMap() { + return behaviorStatisticsMap; + } + + private void setBehaviorStatisticsMap( + Map behaviorStatisticsMap) { + this.behaviorStatisticsMap = behaviorStatisticsMap; + } + + public BehaviorsBriefStatistics() { + this.setMap(new ConcurrentHashMap>()); + this.setIdUrlMap(new ConcurrentHashMap()); + this.setBehaviorStatisticsMap(new ConcurrentHashMap()); + } + + public ScriptBehaviorsBriefModel getStatistics() { + ScriptBehaviorsBriefModel result = new ScriptBehaviorsBriefModel(); + AgentBehaviorsBriefModel agentBehaviorsBriefModel = new AgentBehaviorsBriefModel(); + List list = new ArrayList(); + for (int behaviorId : this.getMap().keySet()) { + BehaviorBriefModel behaviorBriefModel = new BehaviorBriefModel(); + behaviorBriefModel.setBehaviorId(behaviorId); + behaviorBriefModel.setBehaviorUrl(this.getIdUrlMap() + .get(behaviorId)); + List statusList = new ArrayList(); + for (int statusCode : this.getMap().get(behaviorId).keySet()) { + statusList.add(this.getMap().get(behaviorId).get(statusCode) + .getCopy()); + } + behaviorBriefModel.setDetailStatusCodeResultModels(statusList); + this.setBehaviorCount(behaviorBriefModel); + list.add(behaviorBriefModel); + } + agentBehaviorsBriefModel.setBehaviorBriefModels(list); + result.setBehaviorBriefModels(agentBehaviorsBriefModel + .getBehaviorBriefModels()); + makeUpResultModelWithSamplingTime(result); + return result; + } + + private void setBehaviorCount(BehaviorBriefModel behaviorBriefModel) { + long totalCount = 0, successfulCount = 0; + for (BehaviorStatusCodeResultModel behaviorStatusCodeResultModel : behaviorBriefModel + .getDetailStatusCodeResultModels()) { + totalCount += behaviorStatusCodeResultModel.getCount(); + if (behaviorStatusCodeResultModel.isSuccess()) { + successfulCount += behaviorStatusCodeResultModel.getCount(); + } + } + behaviorBriefModel.setTotalCount(totalCount); + behaviorBriefModel.setSuccessfulCount(successfulCount); + } + + public void add(DataStatisticsModel dataUnit) { + if (!(dataUnit instanceof AgentBehaviorsBriefModel)) { + return; + } + AgentBehaviorsBriefModel input = (AgentBehaviorsBriefModel) dataUnit; + for (BehaviorBriefModel behaviorBriefModel : input + .getBehaviorBriefModels()) { + guardAllMapsKeyExists(behaviorBriefModel); + // Map behaviorStatusMap = + // this + // .getMap().get(behaviorBriefModel.getBehaviorId()); + // for (BehaviorStatusCodeResultModel newOne : behaviorBriefModel + // .getDetailStatusCodeResultModels()) { + // if (!behaviorStatusMap.containsKey(newOne.getStatusCode())) { + // behaviorStatusMap.put(newOne.getStatusCode(), newOne); + // continue; + // } + // BehaviorStatusCodeResultModel origin = behaviorStatusMap + // .get(newOne.getStatusCode()); + // origin.plus(newOne); + // } + this.getBehaviorStatisticsMap() + .get(behaviorBriefModel.getBehaviorId()) + .add(behaviorBriefModel); + + } + } + + private synchronized void guardAllMapsKeyExists( + BehaviorBriefModel behaviorBriefModel) { + int behaviorId = behaviorBriefModel.getBehaviorId(); + if (!this.getMap().containsKey(behaviorId)) { + this.getMap() + .put(behaviorId, + new ConcurrentHashMap()); + } + if (!this.getIdUrlMap().containsKey(behaviorId)) { + this.getIdUrlMap().put(behaviorId, + behaviorBriefModel.getBehaviorUrl()); + } + if (!this.getBehaviorStatisticsMap().containsKey(behaviorId)) { + this.getBehaviorStatisticsMap().put(behaviorId, + new BehaviorBriefStatistics()); + } + } +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PageBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PageBriefStatistics.java new file mode 100644 index 00000000..3469d6dd --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PageBriefStatistics.java @@ -0,0 +1,79 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; + +/** + * + * @author coderfengyun :This class is only statistics for One page, should + * construct it once each page; + * @Tip countFromBegin : Because of the statistics means that just accumulate + * the "countFromBegin" field of AgentPageBriefModel, so I can just take it + * as a temperary field for this class + */ +public class PageBriefStatistics extends ScriptStatistics { + private long countFromBegin; + private long maxResponseTimeFromBegin; + private long minResponseTimeFromBegin; + private long totalResponseTimeThisTime; + private long countThisTime; + private long latestResponseTime; + + public PageBriefStatistics() { + resetTemperaryFields(); + this.maxResponseTimeFromBegin = Long.MIN_VALUE; + this.minResponseTimeFromBegin = Long.MAX_VALUE; + } + + /** + * Now, for totalCount, i just collect all result from agent, and add their + * countFromBegin together. But there will be a error there. if an agent + * doesn't give back its result, the the countFromBegin maybe less that last + * one. + */ + private void resetTemperaryFields() { + this.totalResponseTimeThisTime = 0; + this.countThisTime = 0; + this.countFromBegin = 0; + } + + public void add(DataStatisticsModel dataUnit) { + if (!(dataUnit instanceof AgentPageBriefModel)) { + return; + } + add((AgentPageBriefModel) dataUnit); + } + + private void add(AgentPageBriefModel pageBriefModel) { + this.countFromBegin += pageBriefModel.getCountFromBegin(); + this.countThisTime += pageBriefModel.getCountThisTime(); + this.totalResponseTimeThisTime += pageBriefModel + .getTotalResponseTimeThisTime(); + this.latestResponseTime = pageBriefModel.getLatestResponseTime(); + if (pageBriefModel.getMaxResponseTimeFromBegin() > this.maxResponseTimeFromBegin) { + this.maxResponseTimeFromBegin = pageBriefModel + .getMaxResponseTimeFromBegin(); + } + if (pageBriefModel.getMinResponseTimeFromBegin() < this.minResponseTimeFromBegin) { + this.minResponseTimeFromBegin = pageBriefModel + .getMinResponseTimeFromBegin(); + } + } + + public Object getStatistics() { + ScriptPageBriefModel result = new ScriptPageBriefModel(); + result.setCountFromBegin(this.countFromBegin); + result.setLatestResponseTime(this.latestResponseTime); + result.setMaxResponseTimeFromBegin(this.maxResponseTimeFromBegin); + result.setMinResponseTimeFromBegin(this.minResponseTimeFromBegin); + if (this.countThisTime > 0) { + result.setAverageResponseTimeThisTime(this.totalResponseTimeThisTime + / this.countThisTime); + } + resetTemperaryFields(); + makeUpResultModelWithSamplingTime(result); + return result; + } + +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PagesBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PagesBriefStatistics.java new file mode 100644 index 00000000..c4619ab0 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/PagesBriefStatistics.java @@ -0,0 +1,60 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; +import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; +import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; + +public class PagesBriefStatistics extends ScriptStatistics { + private Map map; + + private Map getMap() { + return map; + } + + private void setMap(Map map) { + this.map = map; + } + + public PagesBriefStatistics() { + this.setMap(new ConcurrentHashMap()); + } + + public void add(DataStatisticsModel dataUnit) { + if (dataUnit instanceof AgentPagesBriefModel) { + add((AgentPagesBriefModel) dataUnit); + } + } + + private void add(AgentPagesBriefModel agentPagesBriefModel) { + for (AgentPageBriefModel unit : agentPagesBriefModel + .getPageBriefModels()) { + int key = unit.getPageId(); + if (!this.getMap().containsKey(key)) { + guardMapKeyExists(key); + } + this.getMap().get(key).add(unit); + } + } + + private synchronized void guardMapKeyExists(int key) { + if (!this.getMap().containsKey(key)) { + this.getMap().put(key, new PageBriefStatistics()); + } + } + + public Object getStatistics() { + ScriptPagesBriefModel scriptPagesBriefModel = new ScriptPagesBriefModel(); + for (Integer key : this.getMap().keySet()) { + scriptPagesBriefModel.getScriptPageBriefModels().add( + (ScriptPageBriefModel) this.getMap().get(key) + .getStatistics()); + } + return scriptPagesBriefModel; + } + +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java new file mode 100644 index 00000000..09c318d9 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java @@ -0,0 +1,214 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import java.util.Date; + +import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; + +public class ScriptBriefStatistics extends ScriptStatistics { + private long totalFailCountThisTime; + private long totalSuccessCountThisTime; + private long totalSquareResponseTime; + private long totalFailThroughputThisTime; + private long totalSuccessThroughputThisTime; + private long minResponseTimeThisTime; + private long maxResponseTimeThisTime; + private long totalResponseTimeThisTime; + private long totalFailCountFromBegin; + private long totalSuccessCountFromBegin; + + private long totalElapsedTime; + private long activeVUserCountThisTime; + private long effectiveCount; + + private ScriptBriefResultModel lastValidBriefResultModel; + + private void setTotalFailCountThisTime(long totalFailCountThisTime) { + this.totalFailCountThisTime = totalFailCountThisTime; + } + + private void setTotalSuccessCountThisTime(long totalSuccessCountThisTime) { + this.totalSuccessCountThisTime = totalSuccessCountThisTime; + } + + private void setTotalSquareResponseTime(long totalSquareResponseTime) { + this.totalSquareResponseTime = totalSquareResponseTime; + } + + private void setTotalFailThroughputThisTime(long totalFailThroughputThisTime) { + this.totalFailThroughputThisTime = totalFailThroughputThisTime; + } + + private void setTotalSuccessThroughputThisTime( + long totalSuccessThroughputThisTime) { + this.totalSuccessThroughputThisTime = totalSuccessThroughputThisTime; + } + + private void setMinResponseTimeThisTime(long minResponseTimeThisTime) { + this.minResponseTimeThisTime = minResponseTimeThisTime; + } + + private void setMaxResponseTimeThisTime(long maxResponseTimeThisTime) { + this.maxResponseTimeThisTime = maxResponseTimeThisTime; + } + + private void setTotalResponseTimeThisTime(long totalResponseTimeThisTime) { + this.totalResponseTimeThisTime = totalResponseTimeThisTime; + } + + private void setTotalFailCountFromBegin(long totalFailCountFromBegin) { + this.totalFailCountFromBegin = totalFailCountFromBegin; + } + + private void setTotalSuccessCoutFromBegin(long totalSuccessCoutFromBegin) { + this.totalSuccessCountFromBegin = totalSuccessCoutFromBegin; + } + + private void setTotalElapsedTime(long totalElapsedTime) { + this.totalElapsedTime = totalElapsedTime; + } + + private void setActiveVUserCountThisTime(long activeVUserCountThisTime) { + this.activeVUserCountThisTime = activeVUserCountThisTime; + } + + private void setEffectiveCounte(long effetiveCount) { + this.effectiveCount = effetiveCount; + } + + private ScriptBriefResultModel getLastValidBriefResultModel() { + return lastValidBriefResultModel; + } + + private void setLastValidBriefResultModel( + ScriptBriefResultModel lastValidBriefResultModel) { + this.lastValidBriefResultModel = lastValidBriefResultModel; + } + + public ScriptBriefStatistics() { + reset(); + initLastValidBriefResultModel(); + } + + private void reset() { + this.setTotalFailCountFromBegin(0); + this.setTotalFailCountThisTime(0); + this.setTotalFailThroughputThisTime(0); + this.setTotalResponseTimeThisTime(0); + this.setTotalSquareResponseTime(0); + this.setTotalSuccessCountThisTime(0); + this.setTotalSuccessCoutFromBegin(0); + this.setTotalSuccessThroughputThisTime(0); + this.setTotalElapsedTime(0); + this.setEffectiveCounte(0); + this.setMaxResponseTimeThisTime(Long.MIN_VALUE); + this.setMinResponseTimeThisTime(Long.MAX_VALUE); + this.setActiveVUserCountThisTime(0); + } + + private void initLastValidBriefResultModel() { + ScriptBriefResultModel initBriefModel = new ScriptBriefResultModel(); + initBriefModel.setAverageElapsedTime(0); + initBriefModel.setAverageResponseTime(0); + initBriefModel.setFailRateThisTime(0); + initBriefModel.setFailThroughputThisTime(0); + initBriefModel.setFinished(false); + initBriefModel.setMaxResponseTime(0); + initBriefModel.setMinResponseTime(0); + initBriefModel.setPlanedRunningTime(0); + initBriefModel.setResponseTimeDeviationThisTime(0); + initBriefModel.setSuccessThroughputThisTime(0); + initBriefModel.setTotalFailCountFromBegin(0); + initBriefModel.setTotalSuccessCountFromBegin(0); + this.setLastValidBriefResultModel(initBriefModel); + } + + public void add(DataStatisticsModel dataUnit) { + if (!(dataUnit instanceof AgentBriefStatusModel)) { + return; + } + add((AgentBriefStatusModel) dataUnit); + } + + private void add(AgentBriefStatusModel briefModel) { + if (briefModel == null) { + return; + } + this.effectiveCount++; + if (briefModel.getMaxResponseTime() > this.maxResponseTimeThisTime) { + this.setMaxResponseTimeThisTime(briefModel.getMaxResponseTime()); + } + if (briefModel.getMinResponseTime() < this.minResponseTimeThisTime) { + this.setMinResponseTimeThisTime(briefModel.getMinResponseTime()); + } + this.totalFailCountFromBegin += briefModel.getFailCountFromBegin(); + this.totalFailCountThisTime += briefModel.getFailCountThisTime(); + this.totalFailThroughputThisTime += briefModel + .getFailThroughputThisTime(); + this.totalResponseTimeThisTime += briefModel + .getTotalResponseTimeThisTime(); + this.totalSquareResponseTime += briefModel + .getTotalSqureResponseTimeThisTime(); + this.totalSuccessCountThisTime += briefModel.getSuccessCountThisTime(); + this.totalSuccessCountFromBegin += briefModel + .getSuccessCountFromBegin(); + this.totalSuccessThroughputThisTime += briefModel + .getSuccessThroughputThisTime(); + this.activeVUserCountThisTime += briefModel.getvUserCount(); + } + + public ScriptBriefResultModel getStatistics() { + ScriptBriefResultModel result = new ScriptBriefResultModel(); + long averageResponseTime = 0; + if (this.totalSuccessCountThisTime == 0) { + result.setAverageResponseTime(0); + result.setMaxResponseTime(0); + result.setMinResponseTime(0); + result.setResponseTimeDeviationThisTime(0); + if (this.totalFailCountThisTime == 0) { + result.setFailRateThisTime(0); + } else { + result.setFailRateThisTime(100); + } + } else { + averageResponseTime = this.totalResponseTimeThisTime + / this.totalSuccessCountThisTime; + result.setAverageResponseTime(averageResponseTime); + result.setMaxResponseTime(this.maxResponseTimeThisTime); + result.setMinResponseTime(this.minResponseTimeThisTime); + result.setResponseTimeDeviationThisTime(Math.round(Math + .sqrt(this.totalSquareResponseTime + / this.totalSuccessCountThisTime + - averageResponseTime * averageResponseTime))); + result.setFailRateThisTime(this.totalFailCountThisTime + * 100 + / (this.totalFailCountThisTime + this.totalSuccessCountThisTime)); + } + result.setFailThroughputThisTime(this.totalFailThroughputThisTime); + result.setSuccessThroughputThisTime(this.totalSuccessThroughputThisTime); + result.setTotalFailCountFromBegin(this.totalFailCountFromBegin); + result.setTotalSuccessCountFromBegin(this.totalSuccessCountFromBegin); + if (this.effectiveCount == 0) { + result.setAverageElapsedTime(0); + } else { + result.setAverageElapsedTime(this.totalElapsedTime + / this.effectiveCount); + } + result = dealWithInvalidSamplePoint(result); + result.setSamplingTime(new Date()); + result.setvUserCount(this.activeVUserCountThisTime); + reset(); + return result; + } + + private ScriptBriefResultModel dealWithInvalidSamplePoint( + ScriptBriefResultModel inputModel) { + if (this.totalSuccessCountFromBegin == 0 + && this.totalFailCountFromBegin == 0) { + return this.getLastValidBriefResultModel(); + } + this.setLastValidBriefResultModel(inputModel); + return inputModel; + } +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptStatistics.java new file mode 100644 index 00000000..473b9435 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptStatistics.java @@ -0,0 +1,14 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import java.util.Date; + +import org.bench4q.master.domain.valueobject.datastatistics.DataStatistics; +import org.bench4q.share.models.master.statistics.SampleModel; + +public abstract class ScriptStatistics implements DataStatistics { + protected void makeUpResultModelWithSamplingTime( + SampleModel resultModel) { + resultModel.setSamplingTime(new Date()); + } + +} diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_RunningScriptSampler.java b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_RunningScriptSampler.java index 5271c786..a567615d 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_RunningScriptSampler.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/Test_RunningScriptSampler.java @@ -53,7 +53,7 @@ public class Test_RunningScriptSampler extends TestBase_MakeUpTestPlan { public void testGetScriptResult() throws InterruptedException, JAXBException { ScriptResultModel scriptResultModel = this.getRunningScriptSampler() - .getResultModelFromAgent(); + .briefResultFromAgent(); Thread.sleep(1000); assertNotNull(scriptResultModel); assertNotNull(scriptResultModel.getScriptBriefResultModel()); diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java index 8c86dec9..1e5b3b12 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java @@ -1,7 +1,11 @@ package org.bench4q.master.unitTest.datastatistics; -import org.bench4q.master.domain.valueobject.datastatistics.BehaviorsBriefStatistics; +import java.util.Arrays; + +import org.bench4q.master.domain.valueobject.datastatistics.impl.BehaviorsBriefStatistics; import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.junit.Test; @@ -10,6 +14,15 @@ public class BehaviorsBriefStatisticsTest { public void testAddNull() { BehaviorsBriefStatistics behaviorsBriefStatistics = new BehaviorsBriefStatistics(); AgentBehaviorsBriefModel dataUnit = new AgentBehaviorsBriefModel(); + BehaviorBriefModel e = new BehaviorBriefModel(); + e.setBehaviorId(1); + e.setBehaviorUrl("www.baidu.com"); + e.setDetailStatusCodeResultModels(Arrays + .asList(new BehaviorStatusCodeResultModel())); + e.setSuccessfulCount(2); + e.setTotalCount(3); + dataUnit.getBehaviorBriefModels().add(e); + behaviorsBriefStatistics.add(dataUnit); System.out.println(MarshalHelper.tryMarshal(behaviorsBriefStatistics .getStatistics())); diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/ScriptBriefStatisticsTest.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/ScriptBriefStatisticsTest.java index 7143101d..64f20447 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/ScriptBriefStatisticsTest.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/ScriptBriefStatisticsTest.java @@ -2,7 +2,7 @@ package org.bench4q.master.unitTest.datastatistics; import static org.junit.Assert.*; -import org.bench4q.master.domain.valueobject.datastatistics.ScriptBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.ScriptBriefStatistics; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.junit.Test; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/TestPageBrief.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/TestPageBrief.java index 6fc16fb7..26e7eb7a 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/TestPageBrief.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/TestPageBrief.java @@ -5,7 +5,7 @@ import static org.junit.Assert.*; import java.util.ArrayList; import java.util.List; -import org.bench4q.master.domain.valueobject.datastatistics.PageBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.PageBriefStatistics; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; import org.junit.Test; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_PagesBrief.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_PagesBrief.java index b8cc2897..fc2d2ab1 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_PagesBrief.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_PagesBrief.java @@ -3,7 +3,7 @@ package org.bench4q.master.unitTest.datastatistics; import java.util.ArrayList; import java.util.List; -import org.bench4q.master.domain.valueobject.datastatistics.PagesBriefStatistics; +import org.bench4q.master.domain.valueobject.datastatistics.impl.PagesBriefStatistics; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_ScriptSample.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_ScriptSample.java index 566aced1..6b65ff16 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_ScriptSample.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_ScriptSample.java @@ -45,12 +45,12 @@ public class Test_ScriptSample { .buildRunningAgentWithOutIdAndRunningScript( this.testcase2.get(1), 20, UUID.randomUUID())); RunningScriptSampler sampler = new RunningScriptSampler(runningAgents); - long totalSuccessCountBeforeStop = sampler.getResultModelFromAgent() + long totalSuccessCountBeforeStop = sampler.briefResultFromAgent() .getScriptBriefResultModel().getTotalSuccessCountFromBegin(); runningAgents.get(0).stop(); - long totalSuccessCountAfterStop = sampler.getResultModelFromAgent() + long totalSuccessCountAfterStop = sampler.briefResultFromAgent() .getScriptBriefResultModel().getTotalSuccessCountFromBegin(); assertTrue(totalSuccessCountAfterStop >= totalSuccessCountBeforeStop); } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java index e4bf9386..5ae14572 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java @@ -7,8 +7,10 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; + @XmlRootElement(name = "BehaviorBriefModel") -public class BehaviorBriefModel { +public class BehaviorBriefModel extends DataStatisticsModel { private int behaviorId; private String behaviorUrl; private long totalCount; diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java index cd35e7cb..33f4a37a 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java @@ -5,7 +5,9 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "BehaviorStatusCodeResultModel") public class BehaviorStatusCodeResultModel { + private int behaviorId; private int statusCode; + private long countThisTime; private long count; private String contentType; private long contentLength; @@ -13,6 +15,15 @@ public class BehaviorStatusCodeResultModel { private long maxResponseTime; private long totalResponseTimeThisTime; + @XmlElement + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + @XmlElement public int getStatusCode() { return statusCode; @@ -31,6 +42,15 @@ public class BehaviorStatusCodeResultModel { this.count = count; } + @XmlElement + public long getCountThisTime() { + return countThisTime; + } + + public void setCountThisTime(long countThisTime) { + this.countThisTime = countThisTime; + } + @XmlElement public String getContentType() { return contentType; @@ -111,4 +131,8 @@ public class BehaviorStatusCodeResultModel { this.setMinResponseTime(newOne.getMinResponseTime()); } } + + public BehaviorStatusCodeResultModel() { + } + } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java new file mode 100644 index 00000000..891bfe59 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java @@ -0,0 +1,70 @@ +package org.bench4q.share.models.master.statistics; + +import java.util.LinkedList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; + +@XmlRootElement +public class ScriptBehaviorBriefModel { + private int behaviorId; + private String BehaviorUrl; + private int successCount; + private int totalCount; + private List list; + + public ScriptBehaviorBriefModel() { + this.setList(new LinkedList()); + } + + @XmlElement + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + + @XmlElement + public String getBehaviorUrl() { + return BehaviorUrl; + } + + public void setBehaviorUrl(String behaviorUrl) { + BehaviorUrl = behaviorUrl; + } + + @XmlElement + public int getSuccessCount() { + return successCount; + } + + public void setSuccessCount(int successCount) { + this.successCount = successCount; + } + + @XmlElement + public int getTotalCount() { + return totalCount; + } + + public void setTotalCount(int totalCount) { + this.totalCount = totalCount; + } + + @XmlElementWrapper(name = "statusResults") + @XmlElement + public List getList() { + return list; + } + + public void setList(List list) { + this.list = list; + } + +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java new file mode 100644 index 00000000..74df8a52 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java @@ -0,0 +1,59 @@ +package org.bench4q.share.models.master.statistics; + +public class ScriptBehaviorStatusCodeModel { + private int statusCode; + private long count; + private String contentType; + private long contentLength; + private long minResponseTime; + private long maxResponseTime; + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + public long getCount() { + return count; + } + + public void setCount(long count) { + this.count = count; + } + + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public long getContentLength() { + return contentLength; + } + + public void setContentLength(long contentLength) { + this.contentLength = contentLength; + } + + public long getMinResponseTime() { + return minResponseTime; + } + + public void setMinResponseTime(long minResponseTime) { + this.minResponseTime = minResponseTime; + } + + public long getMaxResponseTime() { + return maxResponseTime; + } + + public void setMaxResponseTime(long maxResponseTime) { + this.maxResponseTime = maxResponseTime; + } + +} From a703ecc65c152ba3f76cdd967037b540111a4368 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Fri, 12 Sep 2014 15:09:54 +0800 Subject: [PATCH 3/8] refacotr refacotr --- .../impl/BehaviorStatusCodeResult.java | 52 ----- .../datastatistics/DetailStatisticsTest.java | 165 -------------- .../BehaviorsBriefStatistics.java | 113 --------- .../datastatistics/PageBriefStatistics.java | 73 ------ .../datastatistics/PagesBriefStatistics.java | 60 ----- .../datastatistics/ScriptBriefStatistics.java | 214 ------------------ .../datastatistics/ScriptStatistics.java | 13 -- 7 files changed, 690 deletions(-) delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResult.java delete mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PageBriefStatistics.java delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PagesBriefStatistics.java delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java delete mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptStatistics.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResult.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResult.java deleted file mode 100644 index 5fe233de..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResult.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.bench4q.agent.datacollector.impl; - -import java.lang.reflect.Field; - -public class BehaviorStatusCodeResult { - public long count; - public long contentLength; - public long minResponseTime; - public long maxResponseTime; - public long totalResponseTimeThisTime; - public String contentType; - - public BehaviorStatusCodeResult(String contentType) { - this.totalResponseTimeThisTime = 0; - this.contentType = contentType; - this.count = 0; - this.contentLength = 0; - this.minResponseTime = Long.MAX_VALUE; - this.maxResponseTime = Long.MIN_VALUE; - } - - public static boolean isSuccess(int statusCode) { - return statusCode == 200; - } - - 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; - } -} \ No newline at end of file diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java deleted file mode 100644 index 4a2893ab..00000000 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/DetailStatisticsTest.java +++ /dev/null @@ -1,165 +0,0 @@ -package org.bench4q.agent.test.datastatistics; - -import static org.junit.Assert.*; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResult; -import org.bench4q.agent.scenario.engine.BehaviorResult; -import org.bench4q.agent.storage.StorageHelper; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class DetailStatisticsTest { - private DataCollector detailStatistics; - - private DataCollector getDetailStatistics() { - return detailStatistics; - } - - private void setDetailStatistics(DataCollector detailStatistics) { - this.detailStatistics = detailStatistics; - } - - public DetailStatisticsTest() { - init(); - } - - private 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.setDetailStatistics(agentResultDataCollector); - } - - @Test - public void addZeroTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest - .makeBehaviorListWhoseBehaviorIdIsOne(0)) { - this.getDetailStatistics().add(behaviorResult); - } - - Object object = this.detailStatistics.getBehaviorBriefStatistics(0); - assertEquals(null, object); - } - - @Test - public void addOneDetailTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest - .makeBehaviorListWhoseBehaviorIdIsOne(1)) { - this.getDetailStatistics().add(behaviorResult); - } - Map map = this.detailStatistics - .getBehaviorBriefStatistics(1); - - BehaviorStatusCodeResult actualResult = map.get(200); - assertTrue(actualResult.equals(makeExpectedResultForOne())); - } - - private BehaviorStatusCodeResult makeExpectedResultForOne() { - BehaviorStatusCodeResult ret = new BehaviorStatusCodeResult(""); - ret.contentLength = 20; - ret.count = 1; - ret.maxResponseTime = 200; - ret.minResponseTime = 200; - ret.totalResponseTimeThisTime = 200; - return ret; - } - - @Test - public void addTwoDetailTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest - .makeBehaviorListWhoseBehaviorIdIsOne(2)) { - this.getDetailStatistics().add(behaviorResult); - } - Map map = this.detailStatistics - .getBehaviorBriefStatistics(1); - assertTrue(mapEquals(map, makeExpectedMapForTwo())); - } - - private Map makeExpectedMapForTwo() { - Map ret = new HashMap(); - ret.put(new Integer(200), buildCodeResult(20, 1, 200, 200, 200)); - ret.put(new Integer(400), - buildCodeResult(0, 1, Long.MIN_VALUE, Long.MAX_VALUE, 0)); - return ret; - } - - @Test - public void addFourDetailTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest - .makeBehaviorListWhoseBehaviorIdIsOne(4)) { - this.getDetailStatistics().add(behaviorResult); - } - Map map = this.detailStatistics - .getBehaviorBriefStatistics(1); - assertTrue(mapEquals(map, makeExpectedMapForFour())); - - } - - private Map makeExpectedMapForFour() { - Map ret = new HashMap(); - ret.put(new Integer(200), buildCodeResult(40, 2, 220, 200, 420)); - ret.put(new Integer(400), - buildCodeResult(0, 2, Long.MIN_VALUE, Long.MAX_VALUE, 0)); - return ret; - } - - private BehaviorStatusCodeResult buildCodeResult(long contentLength, - long count, long maxResponseTime, long minResponseTime, - long totalResponseTimeThisTime) { - BehaviorStatusCodeResult ret = new BehaviorStatusCodeResult(""); - ret.contentLength = contentLength; - ret.count = count; - ret.maxResponseTime = maxResponseTime; - ret.minResponseTime = minResponseTime; - ret.totalResponseTimeThisTime = totalResponseTimeThisTime; - return ret; - } - - private boolean mapEquals(Map mapActual, - Map mapExpected) { - boolean equal = true; - if (mapActual.size() != mapExpected.size()) { - return false; - } - for (int i : mapActual.keySet()) { - if (!mapActual.get(i).equals(mapExpected.get(i))) { - equal = false; - } - } - return equal; - } - - @Test - public void addThreeTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest - .makeBehaviorListWhoseBehaviorIdIsOne(3)) { - this.getDetailStatistics().add(behaviorResult); - } - Map mapActual = this - .getDetailStatistics().getBehaviorBriefStatistics(1); - assertTrue(mapEquals(mapActual, makeExpectedMapForThree())); - } - - private Map makeExpectedMapForThree() { - Map retMap = new HashMap(); - retMap.put(200, buildCodeResult(40, 2, 220, 200, 420)); - retMap.put(400, - buildCodeResult(0, 1, Long.MIN_VALUE, Long.MAX_VALUE, 0)); - return retMap; - } - - @Test - public void test_addSomeSameUrlResult() { - } -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java deleted file mode 100644 index 1b4a3928..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/BehaviorsBriefStatistics.java +++ /dev/null @@ -1,113 +0,0 @@ -package org.bench4q.master.domain.valueobject.datastatistics; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; -import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; -import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; - -public class BehaviorsBriefStatistics extends ScriptStatistics { - private Map> map; - private Map idUrlMap; - - private Map> getMap() { - return map; - } - - private void setMap( - Map> map) { - this.map = map; - } - - public Map getIdUrlMap() { - return idUrlMap; - } - - public void setIdUrlMap(Map idUrlMap) { - this.idUrlMap = idUrlMap; - } - - public BehaviorsBriefStatistics() { - this.setMap(new ConcurrentHashMap>()); - this.setIdUrlMap(new ConcurrentHashMap()); - } - - public ScriptBehaviorsBriefModel getStatistics() { - ScriptBehaviorsBriefModel result = new ScriptBehaviorsBriefModel(); - AgentBehaviorsBriefModel agentBehaviorsBriefModel = new AgentBehaviorsBriefModel(); - List list = new ArrayList(); - for (int behaviorId : this.getMap().keySet()) { - BehaviorBriefModel behaviorBriefModel = new BehaviorBriefModel(); - behaviorBriefModel.setBehaviorId(behaviorId); - behaviorBriefModel.setBehaviorUrl(this.getIdUrlMap() - .get(behaviorId)); - List statusList = new ArrayList(); - for (int statusCode : this.getMap().get(behaviorId).keySet()) { - statusList.add(this.getMap().get(behaviorId).get(statusCode) - .getCopy()); - } - behaviorBriefModel.setDetailStatusCodeResultModels(statusList); - this.setBehaviorCount(behaviorBriefModel); - list.add(behaviorBriefModel); - } - agentBehaviorsBriefModel.setBehaviorBriefModels(list); - result.setBehaviorBriefModels(agentBehaviorsBriefModel - .getBehaviorBriefModels()); - makeUpResultModelWithSamplingTime(result); - return result; - } - - private void setBehaviorCount(BehaviorBriefModel behaviorBriefModel) { - long totalCount = 0, successfulCount = 0; - for (BehaviorStatusCodeResultModel behaviorStatusCodeResultModel : behaviorBriefModel - .getDetailStatusCodeResultModels()) { - totalCount += behaviorStatusCodeResultModel.getCount(); - if (behaviorStatusCodeResultModel.isSuccess()) { - successfulCount += behaviorStatusCodeResultModel.getCount(); - } - } - behaviorBriefModel.setTotalCount(totalCount); - behaviorBriefModel.setSuccessfulCount(successfulCount); - } - - public void add(DataStatisticsModel dataUnit) { - if (!(dataUnit instanceof AgentBehaviorsBriefModel)) { - return; - } - AgentBehaviorsBriefModel input = (AgentBehaviorsBriefModel) dataUnit; - for (BehaviorBriefModel behaviorBriefModel : input - .getBehaviorBriefModels()) { - guardAllMapsKeyExists(behaviorBriefModel); - Map behaviorStatusMap = this - .getMap().get(behaviorBriefModel.getBehaviorId()); - for (BehaviorStatusCodeResultModel newOne : behaviorBriefModel - .getDetailStatusCodeResultModels()) { - if (!behaviorStatusMap.containsKey(newOne.getStatusCode())) { - behaviorStatusMap.put(newOne.getStatusCode(), newOne); - continue; - } - BehaviorStatusCodeResultModel origin = behaviorStatusMap - .get(newOne.getStatusCode()); - origin.plus(newOne); - } - } - } - - private synchronized void guardAllMapsKeyExists( - BehaviorBriefModel behaviorBriefModel) { - if (!this.getMap().containsKey(behaviorBriefModel.getBehaviorId())) { - this.getMap() - .put(behaviorBriefModel.getBehaviorId(), - new ConcurrentHashMap()); - } - if (!this.getIdUrlMap().containsKey(behaviorBriefModel.getBehaviorId())) { - this.getIdUrlMap().put(behaviorBriefModel.getBehaviorId(), - behaviorBriefModel.getBehaviorUrl()); - } - } -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PageBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PageBriefStatistics.java deleted file mode 100644 index f46fee43..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PageBriefStatistics.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.bench4q.master.domain.valueobject.datastatistics; - -import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; -import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; - -/** - * - * @author coderfengyun :This class is only statistics for One page, should - * construct it once each page; - * @Tip countFromBegin : Because of the statistics means that just accumulate - * the "countFromBegin" field of AgentPageBriefModel, so I can just take it - * as a temperary field for this class - */ -public class PageBriefStatistics extends ScriptStatistics { - private long countFromBegin; - private long maxResponseTimeFromBegin; - private long minResponseTimeFromBegin; - private long totalResponseTimeThisTime; - private long countThisTime; - private long latestResponseTime; - - public PageBriefStatistics() { - resetTemperaryFields(); - this.maxResponseTimeFromBegin = Long.MIN_VALUE; - this.minResponseTimeFromBegin = Long.MAX_VALUE; - } - - private void resetTemperaryFields() { - this.totalResponseTimeThisTime = 0; - this.countThisTime = 0; - this.countFromBegin = 0; - } - - public void add(DataStatisticsModel dataUnit) { - if (!(dataUnit instanceof AgentPageBriefModel)) { - return; - } - add((AgentPageBriefModel) dataUnit); - } - - private void add(AgentPageBriefModel pageBriefModel) { - this.countFromBegin += pageBriefModel.getCountFromBegin(); - this.countThisTime += pageBriefModel.getCountThisTime(); - this.totalResponseTimeThisTime += pageBriefModel - .getTotalResponseTimeThisTime(); - this.latestResponseTime = pageBriefModel.getLatestResponseTime(); - if (pageBriefModel.getMaxResponseTimeFromBegin() > this.maxResponseTimeFromBegin) { - this.maxResponseTimeFromBegin = pageBriefModel - .getMaxResponseTimeFromBegin(); - } - if (pageBriefModel.getMinResponseTimeFromBegin() < this.minResponseTimeFromBegin) { - this.minResponseTimeFromBegin = pageBriefModel - .getMinResponseTimeFromBegin(); - } - } - - public Object getStatistics() { - ScriptPageBriefModel result = new ScriptPageBriefModel(); - result.setCountFromBegin(this.countFromBegin); - result.setLatestResponseTime(this.latestResponseTime); - result.setMaxResponseTimeFromBegin(this.maxResponseTimeFromBegin); - result.setMinResponseTimeFromBegin(this.minResponseTimeFromBegin); - if (this.countThisTime > 0) { - result.setAverageResponseTimeThisTime(this.totalResponseTimeThisTime - / this.countThisTime); - } - resetTemperaryFields(); - makeUpResultModelWithSamplingTime(result); - return result; - } - -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PagesBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PagesBriefStatistics.java deleted file mode 100644 index 891eea2c..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/PagesBriefStatistics.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.bench4q.master.domain.valueobject.datastatistics; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; -import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; -import org.bench4q.share.models.master.statistics.ScriptPageBriefModel; -import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; - -public class PagesBriefStatistics extends ScriptStatistics { - private Map map; - - private Map getMap() { - return map; - } - - private void setMap(Map map) { - this.map = map; - } - - public PagesBriefStatistics() { - this.setMap(new ConcurrentHashMap()); - } - - public void add(DataStatisticsModel dataUnit) { - if (dataUnit instanceof AgentPagesBriefModel) { - add((AgentPagesBriefModel) dataUnit); - } - } - - private void add(AgentPagesBriefModel agentPagesBriefModel) { - for (AgentPageBriefModel unit : agentPagesBriefModel - .getPageBriefModels()) { - int key = unit.getPageId(); - if (!this.getMap().containsKey(key)) { - guardMapKeyExists(key); - } - this.getMap().get(key).add(unit); - } - } - - private synchronized void guardMapKeyExists(int key) { - if (!this.getMap().containsKey(key)) { - this.getMap().put(key, new PageBriefStatistics()); - } - } - - public Object getStatistics() { - ScriptPagesBriefModel scriptPagesBriefModel = new ScriptPagesBriefModel(); - for (Integer key : this.getMap().keySet()) { - scriptPagesBriefModel.getScriptPageBriefModels().add( - (ScriptPageBriefModel) this.getMap().get(key) - .getStatistics()); - } - return scriptPagesBriefModel; - } - -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java deleted file mode 100644 index 19f97f86..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptBriefStatistics.java +++ /dev/null @@ -1,214 +0,0 @@ -package org.bench4q.master.domain.valueobject.datastatistics; - -import java.util.Date; - -import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; -import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; - -public class ScriptBriefStatistics extends ScriptStatistics { - private long totalFailCountThisTime; - private long totalSuccessCountThisTime; - private long totalSquareResponseTime; - private long totalFailThroughputThisTime; - private long totalSuccessThroughputThisTime; - private long minResponseTimeThisTime; - private long maxResponseTimeThisTime; - private long totalResponseTimeThisTime; - private long totalFailCountFromBegin; - private long totalSuccessCountFromBegin; - - private long totalElapsedTime; - private long activeVUserCountThisTime; - private long effectiveCount; - - private ScriptBriefResultModel lastValidBriefResultModel; - - private void setTotalFailCountThisTime(long totalFailCountThisTime) { - this.totalFailCountThisTime = totalFailCountThisTime; - } - - private void setTotalSuccessCountThisTime(long totalSuccessCountThisTime) { - this.totalSuccessCountThisTime = totalSuccessCountThisTime; - } - - private void setTotalSquareResponseTime(long totalSquareResponseTime) { - this.totalSquareResponseTime = totalSquareResponseTime; - } - - private void setTotalFailThroughputThisTime(long totalFailThroughputThisTime) { - this.totalFailThroughputThisTime = totalFailThroughputThisTime; - } - - private void setTotalSuccessThroughputThisTime( - long totalSuccessThroughputThisTime) { - this.totalSuccessThroughputThisTime = totalSuccessThroughputThisTime; - } - - private void setMinResponseTimeThisTime(long minResponseTimeThisTime) { - this.minResponseTimeThisTime = minResponseTimeThisTime; - } - - private void setMaxResponseTimeThisTime(long maxResponseTimeThisTime) { - this.maxResponseTimeThisTime = maxResponseTimeThisTime; - } - - private void setTotalResponseTimeThisTime(long totalResponseTimeThisTime) { - this.totalResponseTimeThisTime = totalResponseTimeThisTime; - } - - private void setTotalFailCountFromBegin(long totalFailCountFromBegin) { - this.totalFailCountFromBegin = totalFailCountFromBegin; - } - - private void setTotalSuccessCoutFromBegin(long totalSuccessCoutFromBegin) { - this.totalSuccessCountFromBegin = totalSuccessCoutFromBegin; - } - - private void setTotalElapsedTime(long totalElapsedTime) { - this.totalElapsedTime = totalElapsedTime; - } - - private void setActiveVUserCountThisTime(long activeVUserCountThisTime) { - this.activeVUserCountThisTime = activeVUserCountThisTime; - } - - private void setEffectiveCounte(long effetiveCount) { - this.effectiveCount = effetiveCount; - } - - private ScriptBriefResultModel getLastValidBriefResultModel() { - return lastValidBriefResultModel; - } - - private void setLastValidBriefResultModel( - ScriptBriefResultModel lastValidBriefResultModel) { - this.lastValidBriefResultModel = lastValidBriefResultModel; - } - - public ScriptBriefStatistics() { - reset(); - initLastValidBriefResultModel(); - } - - private void reset() { - this.setTotalFailCountFromBegin(0); - this.setTotalFailCountThisTime(0); - this.setTotalFailThroughputThisTime(0); - this.setTotalResponseTimeThisTime(0); - this.setTotalSquareResponseTime(0); - this.setTotalSuccessCountThisTime(0); - this.setTotalSuccessCoutFromBegin(0); - this.setTotalSuccessThroughputThisTime(0); - this.setTotalElapsedTime(0); - this.setEffectiveCounte(0); - this.setMaxResponseTimeThisTime(Long.MIN_VALUE); - this.setMinResponseTimeThisTime(Long.MAX_VALUE); - this.setActiveVUserCountThisTime(0); - } - - private void initLastValidBriefResultModel() { - ScriptBriefResultModel initBriefModel = new ScriptBriefResultModel(); - initBriefModel.setAverageElapsedTime(0); - initBriefModel.setAverageResponseTime(0); - initBriefModel.setFailRateThisTime(0); - initBriefModel.setFailThroughputThisTime(0); - initBriefModel.setFinished(false); - initBriefModel.setMaxResponseTime(0); - initBriefModel.setMinResponseTime(0); - initBriefModel.setPlanedRunningTime(0); - initBriefModel.setResponseTimeDeviationThisTime(0); - initBriefModel.setSuccessThroughputThisTime(0); - initBriefModel.setTotalFailCountFromBegin(0); - initBriefModel.setTotalSuccessCountFromBegin(0); - this.setLastValidBriefResultModel(initBriefModel); - } - - public void add(DataStatisticsModel dataUnit) { - if (!(dataUnit instanceof AgentBriefStatusModel)) { - return; - } - add((AgentBriefStatusModel) dataUnit); - } - - private void add(AgentBriefStatusModel briefModel) { - if (briefModel == null) { - return; - } - this.effectiveCount++; - if (briefModel.getMaxResponseTime() > this.maxResponseTimeThisTime) { - this.setMaxResponseTimeThisTime(briefModel.getMaxResponseTime()); - } - if (briefModel.getMinResponseTime() < this.minResponseTimeThisTime) { - this.setMinResponseTimeThisTime(briefModel.getMinResponseTime()); - } - this.totalFailCountFromBegin += briefModel.getFailCountFromBegin(); - this.totalFailCountThisTime += briefModel.getFailCountThisTime(); - this.totalFailThroughputThisTime += briefModel - .getFailThroughputThisTime(); - this.totalResponseTimeThisTime += briefModel - .getTotalResponseTimeThisTime(); - this.totalSquareResponseTime += briefModel - .getTotalSqureResponseTimeThisTime(); - this.totalSuccessCountThisTime += briefModel.getSuccessCountThisTime(); - this.totalSuccessCountFromBegin += briefModel - .getSuccessCountFromBegin(); - this.totalSuccessThroughputThisTime += briefModel - .getSuccessThroughputThisTime(); - this.activeVUserCountThisTime += briefModel.getvUserCount(); - } - - public ScriptBriefResultModel getStatistics() { - ScriptBriefResultModel result = new ScriptBriefResultModel(); - long averageResponseTime = 0; - if (this.totalSuccessCountThisTime == 0) { - result.setAverageResponseTime(0); - result.setMaxResponseTime(0); - result.setMinResponseTime(0); - result.setResponseTimeDeviationThisTime(0); - if (this.totalFailCountThisTime == 0) { - result.setFailRateThisTime(0); - } else { - result.setFailRateThisTime(100); - } - } else { - averageResponseTime = this.totalResponseTimeThisTime - / this.totalSuccessCountThisTime; - result.setAverageResponseTime(averageResponseTime); - result.setMaxResponseTime(this.maxResponseTimeThisTime); - result.setMinResponseTime(this.minResponseTimeThisTime); - result.setResponseTimeDeviationThisTime(Math.round(Math - .sqrt(this.totalSquareResponseTime - / this.totalSuccessCountThisTime - - averageResponseTime * averageResponseTime))); - result.setFailRateThisTime(this.totalFailCountThisTime - * 100 - / (this.totalFailCountThisTime + this.totalSuccessCountThisTime)); - } - result.setFailThroughputThisTime(this.totalFailThroughputThisTime); - result.setSuccessThroughputThisTime(this.totalSuccessThroughputThisTime); - result.setTotalFailCountFromBegin(this.totalFailCountFromBegin); - result.setTotalSuccessCountFromBegin(this.totalSuccessCountFromBegin); - if (this.effectiveCount == 0) { - result.setAverageElapsedTime(0); - } else { - result.setAverageElapsedTime(this.totalElapsedTime - / this.effectiveCount); - } - result = dealWithInvalidSamplePoint(result); - result.setSamplingTime(new Date()); - result.setvUserCount(this.activeVUserCountThisTime); - reset(); - return result; - } - - private ScriptBriefResultModel dealWithInvalidSamplePoint( - ScriptBriefResultModel inputModel) { - if (this.totalSuccessCountFromBegin == 0 - && this.totalFailCountFromBegin == 0) { - return this.getLastValidBriefResultModel(); - } - this.setLastValidBriefResultModel(inputModel); - return inputModel; - } -} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptStatistics.java deleted file mode 100644 index 40de4471..00000000 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/ScriptStatistics.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.bench4q.master.domain.valueobject.datastatistics; - -import java.util.Date; - -import org.bench4q.share.models.master.statistics.SampleModel; - -public abstract class ScriptStatistics implements DataStatistics { - protected void makeUpResultModelWithSamplingTime( - SampleModel resultModel) { - resultModel.setSamplingTime(new Date()); - } - -} From 513cbf3a4f82d8b916c1002173e844f7a2ff8525 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Fri, 12 Sep 2014 16:59:15 +0800 Subject: [PATCH 4/8] refactor refactor --- .../org/bench4q/agent/api/TestController.java | 6 +- .../agent/datacollector/DataCollector.java | 4 +- .../agent/datacollector/DataStatistics.java | 9 + .../impl/BehaviorStatusCodeCollector.java | 109 ++++++++++++ .../impl/BehaviorsResultCollector.java | 101 ++++++++++++ .../datacollector/impl/DataCollectorImpl.java | 117 +++++++++++++ .../impl/PagesResultCollector.java | 116 +++++++++++++ .../impl/ScenarioResultCollector.java | 119 +++----------- .../agent/scenario/behavior/UserBehavior.java | 2 +- .../agent/scenario/engine/BehaviorResult.java | 2 +- .../agent/scenario/engine/PageResult.java | 2 +- .../scenario/engine/ScenarioContext.java | 15 +- .../agent/scenario/engine/ScenarioEngine.java | 13 +- .../scenario/engine/UnderStatistics.java | 5 + .../bench4q/agent/scenario/engine/VUser.java | 4 +- .../java/org/bench4q/agent/test/TestBase.java | 14 +- .../PageResultStatisticsTest.java | 25 ++- .../Test_BehaviorResultCollector.java | 58 ++++--- .../Test_BehaviorStatusResultStatistics.java | 20 +-- .../Test_DataCollectorImpl.java | 72 ++++++++ .../Test_ScenarioBriefCollector.java | 155 ++++++++++++++++++ .../test/scenario/Test_ScenarioContext.java | 7 +- .../test/scenario/Test_ScenarioEngine.java | 15 +- .../RecordScriptTestCase/baidu.html | 1 + 24 files changed, 810 insertions(+), 181 deletions(-) create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataStatistics.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PagesResultCollector.java create mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/UnderStatistics.java create mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java create mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_ScenarioBriefCollector.java create mode 100644 Bench4Q-Master/RecordScriptTestCase/baidu.html diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index dd752b6e..3500b8f1 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -136,7 +136,7 @@ public class TestController { return null; } for (int i = 0; i < context.getScenario().getPages().length; i++) { - pageBrieves.add((AgentPageBriefModel) context.getDataStatistics() + pageBrieves.add((AgentPageBriefModel) context.getDataCollector() .getPageBriefStatistics(i)); } result.setPageBriefModels(pageBrieves); @@ -156,7 +156,7 @@ public class TestController { for (Behavior behavior : scenarioContext.getScenario() .getAllBehaviors()) { BehaviorBriefModel briefModel = behavior - .getBehaviorBriefResult(scenarioContext.getDataStatistics()); + .getBehaviorBriefResult(scenarioContext.getDataCollector()); briefModel.setBehaviorUrl(behavior.getSpecificParamValue("url")); behaviorBriefModels.add(briefModel); } @@ -173,7 +173,7 @@ public class TestController { return null; } AgentBriefStatusModel agentStatusModel = (AgentBriefStatusModel) scenarioContext - .getDataStatistics().getScenarioBriefStatistics(); + .getDataCollector().getScenarioBriefStatistics(); agentStatusModel.setvUserCount(scenarioContext.getExecutor() .getActiveCount()); return agentStatusModel; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java index 7c11589d..6823c5f0 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataCollector.java @@ -2,7 +2,6 @@ package org.bench4q.agent.datacollector; import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.agent.scenario.engine.PageResult; -import org.bench4q.share.models.agent.BehaviorBriefModel; public interface DataCollector { public void add(BehaviorResult behaviorResult); @@ -11,8 +10,7 @@ public interface DataCollector { public Object getScenarioBriefStatistics(); - public BehaviorBriefModel getBehaviorBriefStatistics( - int behaviorId); + public Object getBehaviorBriefStatistics(int behaviorId); public Object getPageBriefStatistics(int pageId); } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataStatistics.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataStatistics.java new file mode 100644 index 00000000..983ab6c3 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/DataStatistics.java @@ -0,0 +1,9 @@ +package org.bench4q.agent.datacollector; + +import org.bench4q.agent.scenario.engine.UnderStatistics; + +public interface DataStatistics { + public void add(UnderStatistics unit); + + public Object getStatistics(); +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java new file mode 100644 index 00000000..ad62d163 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java @@ -0,0 +1,109 @@ +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 BehaviorStatusCodeCollector { + private int behaviorId; + private int statusCode; + private long count; + private long countThisTime; + private long contentLength; + private long minResponseTime; + private long maxResponseTime; + private long totalResponseTimeThisTime; + private String contentType; + + public BehaviorStatusCodeCollector(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; + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java new file mode 100644 index 00000000..8ca29d80 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java @@ -0,0 +1,101 @@ +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.datacollector.DataStatistics; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.UnderStatistics; +import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; + +public class BehaviorsResultCollector { + private final Map behaviorResultMap; + + public BehaviorsResultCollector() { + this.behaviorResultMap = new HashMap(); + } + + public void add(BehaviorResult behaviorResult) { + if (!this.behaviorResultMap.containsKey(behaviorResult.getBehaviorId())) { + synchronized (this) { + this.behaviorResultMap.put( + behaviorResult.getBehaviorId(), + new BehaviorResultCollector(behaviorResult + .getBehaviorId())); + } + this.behaviorResultMap.get(behaviorResult.getBehaviorId()).add( + behaviorResult); + } + } + + public Object getStatistics(int behaviorId) { + if (!this.behaviorResultMap.containsKey(behaviorId)) { + return null; + } + BehaviorResultCollector m = this.behaviorResultMap.get(behaviorId); + return m.getStatistics(); + } + + public static class BehaviorResultCollector implements DataStatistics { + private int behaviorId; + private long successCountFromBegin; + private long failCountFromBegin; + // statusCode - BehaviorStatusCodeResultCollector + private final Map detailStatusMap; + + public BehaviorResultCollector(int behaviorId) { + this.behaviorId = behaviorId; + this.detailStatusMap = new HashMap(); + } + + @Override + public void add(UnderStatistics unit) { + if (!(unit instanceof BehaviorResult)) { + return; + } + addCore((BehaviorResult) unit); + } + + public void addCore(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(); + BehaviorStatusCodeCollector 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 BehaviorStatusCodeCollector(this.behaviorId, + statusCode, contentType)); + } + } + + public Object getStatistics() { + BehaviorBriefModel result = new BehaviorBriefModel(); + List list = new LinkedList(); + for (BehaviorStatusCodeCollector 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; + } + } +} \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java new file mode 100644 index 00000000..b1dbcafd --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java @@ -0,0 +1,117 @@ +package org.bench4q.agent.datacollector.impl; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; +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.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; +import org.bench4q.agent.storage.StorageHelper; +import org.bench4q.share.models.agent.BehaviorResultModel; + +public class DataCollectorImpl implements DataCollector { + protected StorageHelper storageHelper; + private final UUID testId; + private final PagesResultCollector pagesResultCollector; + private final ScenarioResultCollector scenarioResultCollector; + private final BehaviorsResultCollector behaviorsResultCollector; + + protected StorageHelper getStorageHelper() { + return storageHelper; + } + + public void setStorageHelper(StorageHelper storageHelper) { + this.storageHelper = storageHelper; + } + + public DataCollectorImpl(UUID testId, StorageHelper storageHelper) { + // mustDoWhenIniti(); + this.storageHelper = storageHelper; + this.testId = testId; + this.pagesResultCollector = new PagesResultCollector(); + this.behaviorsResultCollector = new BehaviorsResultCollector(); + 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; + } + saveItAsync(behaviorResult); + this.scenarioResultCollector.add(behaviorResult); + this.behaviorsResultCollector.add(behaviorResult); + } + + private void saveItAsync(final BehaviorResult behaviorResult) { + 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; + } + + public void add(PageResult pageResult) { + this.pagesResultCollector.add(pageResult); + } + + public Object getPageBriefStatistics(int pageId) { + return this.pagesResultCollector.getPageBriefStatistics(pageId); + } + + protected String calculateSavePath(BehaviorResult behaviorResult) { + Date now = new Date(); + return "DetailResults" + System.getProperty("file.separator") + + new SimpleDateFormat("yyyyMMdd").format(now) + + System.getProperty("file.separator") + this.testId + "_" + + behaviorResult.getBehaviorId() + "_" + + new SimpleDateFormat("hhmm") + ".txt"; + } + + public Object getScenarioBriefStatistics() { + return this.scenarioResultCollector.getScenarioBriefStatistics(); + } + + public Object getBehaviorBriefStatistics(int id) { + return this.behaviorsResultCollector.getStatistics(id); + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PagesResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PagesResultCollector.java new file mode 100644 index 00000000..2566098d --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PagesResultCollector.java @@ -0,0 +1,116 @@ +package org.bench4q.agent.datacollector.impl; + +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import org.bench4q.agent.datacollector.DataStatistics; +import org.bench4q.agent.scenario.engine.PageResult; +import org.bench4q.agent.scenario.engine.UnderStatistics; +import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; + +public class PagesResultCollector { + Map pageBriefMap; + + private Map getPageBriefMap() { + return pageBriefMap; + } + + private void setPageBriefMap(Map pageBriefMap) { + this.pageBriefMap = pageBriefMap; + } + + public PagesResultCollector() { + this.setPageBriefMap(new HashMap()); + } + + public void add(PageResult pageResult) { + if (pageResult == null || pageResult.getPageId() < 0) { + return; + } + PageResultCollector pageBrief = this.getPageBriefMap().get( + pageResult.getPageId()); + if (!this.getPageBriefMap().containsKey(pageResult.getPageId())) { + pageBrief = guardTheValueOfThePageIdExists(pageResult.getPageId()); + } + pageBrief.addCore(pageResult); + } + + private synchronized PageResultCollector guardTheValueOfThePageIdExists( + int pageId) { + if (!this.getPageBriefMap().containsKey(pageId)) { + this.getPageBriefMap().put(pageId, new PageResultCollector(pageId)); + } + return this.getPageBriefMap().get(pageId); + } + + public Object getPageBriefStatistics(int pageId) { + PageResultCollector pageBrief = guardTheValueOfThePageIdExists(pageId); + return pageBrief.getStatistics(); + } + + public class PageResultCollector implements DataStatistics { + private long lastSampleTime; + private long countThisTime; + private long totalResponseTimeThisTime; + private long maxResponseTimeFromBegin; + private long minResponseTimeFromBegin; + private long countFromBegin; + private long latestTimeResponseTime; + private final int pageId; + + public PageResultCollector(int pageId) { + resetTemperatyField(); + this.maxResponseTimeFromBegin = Long.MIN_VALUE; + this.minResponseTimeFromBegin = Long.MAX_VALUE; + this.countFromBegin = 0; + this.latestTimeResponseTime = 0; + this.pageId = pageId; + } + + public void resetTemperatyField() { + this.lastSampleTime = new Date().getTime(); + this.countThisTime = 0; + this.totalResponseTimeThisTime = 0; + } + + public void add(UnderStatistics unit) { + if (!(unit instanceof PageResult)) { + return; + } + addCore((PageResult) unit); + } + + public void addCore(PageResult pageResult) { + if (pageResult == null || pageResult.getPageId() != this.pageId) { + return; + } + this.countThisTime++; + this.countFromBegin++; + this.totalResponseTimeThisTime += pageResult.getExecuteRange(); + this.latestTimeResponseTime = pageResult.getExecuteRange(); + if (pageResult.getExecuteRange() > this.maxResponseTimeFromBegin) { + this.maxResponseTimeFromBegin = pageResult.getExecuteRange(); + } + if (pageResult.getExecuteRange() < this.minResponseTimeFromBegin) { + this.minResponseTimeFromBegin = pageResult.getExecuteRange(); + } + } + + public Object getStatistics() { + AgentPageBriefModel result = new AgentPageBriefModel(); + result.setCountFromBegin(countFromBegin); + result.setCountThisTime(countThisTime); + result.setMaxResponseTimeFromBegin(maxResponseTimeFromBegin); + result.setMinResponseTimeFromBegin(minResponseTimeFromBegin); + result.setTotalResponseTimeThisTime(totalResponseTimeThisTime); + result.setPageId(pageId); + long nowTime = new Date().getTime(); + result.setTimeFrame(nowTime - lastSampleTime); + result.setLatestResponseTime(latestTimeResponseTime); + this.resetTemperatyField(); + return result; + } + + } +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java index 88069b4d..d22ab33c 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/ScenarioResultCollector.java @@ -1,14 +1,6 @@ package org.bench4q.agent.datacollector.impl; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - 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.AgentBriefStatusModel; /** @@ -17,7 +9,7 @@ import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; * @author coderfengyun * */ -public class ScenarioResultCollector extends AbstractDataCollector { +public class ScenarioResultCollector { private long timeOfPreviousCall; private long failCountOfThisCall; private long successCountOfThisCall; @@ -28,10 +20,6 @@ public class ScenarioResultCollector extends AbstractDataCollector { private long cumulativeSucessfulCount; private long cumulativeFailCount; private static long TIME_UNIT = 1000; - private UUID testID; - private PageResultCollector pageResultCollector; - // behaviorId -- BehaviorResultCollector - private final Map behaviorCollectors; private void setTimeOfPreviousCall(long timeOfPreviousCall) { this.timeOfPreviousCall = timeOfPreviousCall; @@ -82,26 +70,7 @@ public class ScenarioResultCollector extends AbstractDataCollector { this.cumulativeFailCount = cumulativeFailCount; } - private String getTestID() { - return testID == null ? "default" : testID.toString(); - } - - private void setTestID(UUID testID) { - this.testID = testID; - } - - private PageResultCollector getPageResultCollector() { - return pageResultCollector; - } - - private void setPageResultCollector(PageResultCollector pageResultCollector) { - this.pageResultCollector = pageResultCollector; - } - - public ScenarioResultCollector(UUID testId) { - this.behaviorCollectors = new HashMap(); - this.setTestID(testId); - this.setPageResultCollector(new PageResultCollector()); + public ScenarioResultCollector() { init(); } @@ -125,6 +94,24 @@ public class ScenarioResultCollector extends AbstractDataCollector { // DataStatistics Interface start // /////////////////////////////// + public void add(BehaviorResult behaviorResult) { + this.successCountOfThisCall += behaviorResult.getSuccessCount(); + this.failCountOfThisCall += behaviorResult.getFailCount(); + if (behaviorResult.getSuccessCount() == 0) + return; + this.totalResponseTimeOfThisCall += behaviorResult.getResponseTime(); + this.totalSqureResponseTimeOfThisCall += Math.pow( + behaviorResult.getResponseTime(), 2); + if (behaviorResult.getResponseTime() > this + .getMaxResponseTimeOfThisCall()) { + this.setMaxResponseTimeOfThisCall(behaviorResult.getResponseTime()); + } + if (behaviorResult.getResponseTime() < this + .getMinResponseTimeOfThisCall()) { + this.setMinResponseTimeOfThisCall(behaviorResult.getResponseTime()); + } + } + public AgentBriefStatusModel getScenarioBriefStatistics() { AgentBriefStatusModel result = new AgentBriefStatusModel(); result.setTimeFrame(System.currentTimeMillis() @@ -167,70 +154,4 @@ public class ScenarioResultCollector extends AbstractDataCollector { * @param behaviorResult */ - private void statisticScenarioBriefResult(BehaviorResult behaviorResult) { - this.successCountOfThisCall += behaviorResult.getSuccessCount(); - this.failCountOfThisCall += behaviorResult.getFailCount(); - if (behaviorResult.getSuccessCount() == 0) - return; - this.totalResponseTimeOfThisCall += behaviorResult.getResponseTime(); - this.totalSqureResponseTimeOfThisCall += Math.pow( - behaviorResult.getResponseTime(), 2); - if (behaviorResult.getResponseTime() > this - .getMaxResponseTimeOfThisCall()) { - this.setMaxResponseTimeOfThisCall(behaviorResult.getResponseTime()); - } - if (behaviorResult.getResponseTime() < this - .getMinResponseTimeOfThisCall()) { - this.setMinResponseTimeOfThisCall(behaviorResult.getResponseTime()); - } - } - - private void statisticBehaviorBriefResult(BehaviorResult behaviorResult) { - BehaviorResultCollector collector = this.behaviorCollectors - .get(behaviorResult.getBehaviorId()); - collector.add(behaviorResult); - } - - @Override - protected String calculateSavePath(BehaviorResult behaviorResult) { - Date now = new Date(); - - return "DetailResults" + System.getProperty("file.separator") - + new SimpleDateFormat("yyyyMMdd").format(now) - + System.getProperty("file.separator") + this.getTestID() + "_" - + behaviorResult.getBehaviorId() + "_" - + new SimpleDateFormat("hhmm") + ".txt"; - } - - public synchronized void add(PageResult pageResult) { - this.getPageResultCollector().add(pageResult); - } - - @Override - public synchronized void add(BehaviorResult behaviorResult) { - super.add(behaviorResult); - if (!this.behaviorCollectors - .containsKey(behaviorResult.getBehaviorId())) { - this.behaviorCollectors - .put(behaviorResult.getBehaviorId(), - new BehaviorResultCollector(behaviorResult - .getBehaviorId())); - } - statisticScenarioBriefResult(behaviorResult); - statisticBehaviorBriefResult(behaviorResult); - } - - @Override - public BehaviorBriefModel getBehaviorBriefStatistics(int behaviorId) { - if (!this.behaviorCollectors.containsKey(behaviorId)) { - return null; - } - BehaviorResultCollector m = this.behaviorCollectors.get(behaviorId); - return m.getStatistics(); - } - - public Object getPageBriefStatistics(int pageId) { - return this.getPageResultCollector().getPageBriefStatistics(pageId); - } - } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java index 231f034e..35c066f7 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java @@ -12,7 +12,7 @@ public class UserBehavior extends Behavior { @Override public BehaviorBriefModel getBehaviorBriefResult( DataCollector dataStatistics) { - return dataStatistics.getBehaviorBriefStatistics(this.getId()); + return (BehaviorBriefModel) dataStatistics.getBehaviorBriefStatistics(this.getId()); } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java index 8c1bf6f5..6c7c56da 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/BehaviorResult.java @@ -2,7 +2,7 @@ package org.bench4q.agent.scenario.engine; import java.util.Date; -public class BehaviorResult { +public class BehaviorResult implements UnderStatistics { private String pluginId; private String pluginName; private int behaviorId; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java index c8e1e64c..7dc4a0cf 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/PageResult.java @@ -3,7 +3,7 @@ package org.bench4q.agent.scenario.engine; import java.util.ArrayList; import java.util.List; -public class PageResult { +public class PageResult implements UnderStatistics { private int pageId; private long pageStartTime; private long pageEndTime; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java index 4ce02c61..91a09e6b 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioContext.java @@ -11,9 +11,10 @@ import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; import org.apache.log4j.Logger; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; +import org.bench4q.agent.datacollector.impl.DataCollectorImpl; import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.scenario.Scenario; +import org.bench4q.agent.storage.StorageHelper; public class ScenarioContext implements Observer { private static final long keepAliveTime = 10; @@ -73,7 +74,7 @@ public class ScenarioContext implements Observer { this.finished = finished; } - public DataCollector getDataStatistics() { + public DataCollector getDataCollector() { return dataCollector; } @@ -90,15 +91,16 @@ public class ScenarioContext implements Observer { } public static ScenarioContext buildScenarioContextWithoutScenario( - UUID testId, int poolSize, PluginManager pluginManager) { + UUID testId, int poolSize, PluginManager pluginManager, + StorageHelper storageHelper) { final ArrayBlockingQueue workQueue = new ArrayBlockingQueue( poolSize); ThreadPoolExecutor executor = new ThreadPoolExecutor(poolSize, poolSize, keepAliveTime, TimeUnit.MINUTES, workQueue, new DiscardPolicy()); ScenarioContext scenarioContext = new ScenarioContext(testId, new Date( - System.currentTimeMillis()), executor, - new ScenarioResultCollector(testId), pluginManager); + System.currentTimeMillis()), executor, new DataCollectorImpl( + testId, storageHelper), pluginManager); return scenarioContext; } @@ -108,7 +110,8 @@ public class ScenarioContext implements Observer { realStartTime), this.executor, this.dataCollector, this.pluginManager); result.setSchedule(schedule); - result.setEndDate(new Date(result.getSchedule().getScheduleRangeInMilliSecond() + result.setEndDate(new Date(result.getSchedule() + .getScheduleRangeInMilliSecond() + result.getStartDate().getTime())); result.setFinished(this.isFinished()); result.setScenario(scenario); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java index 0e1723b0..a21783e0 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/ScenarioEngine.java @@ -9,6 +9,7 @@ import java.util.UUID; import org.apache.log4j.Logger; import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.scenario.Scenario; +import org.bench4q.agent.storage.StorageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -17,6 +18,7 @@ public class ScenarioEngine implements Observer { private Map runningTests; private final Logger logger = Logger.getLogger(ScenarioEngine.class); private PluginManager pluginManager; + private StorageHelper storageHelper; public ScenarioEngine() { this.setRunningTests(new HashMap()); @@ -39,11 +41,20 @@ public class ScenarioEngine implements Observer { this.pluginManager = pluginManager; } + private StorageHelper getStorageHelper() { + return storageHelper; + } + + @Autowired + private void setStorageHelper(StorageHelper storageHelper) { + this.storageHelper = storageHelper; + } + public void addRunningTestWithoutScenario(UUID runId, int poolSize) { try { final ScenarioContext scenarioContext = ScenarioContext .buildScenarioContextWithoutScenario(runId, poolSize, - getPluginManager()); + getPluginManager(), this.getStorageHelper()); this.getRunningTests().put(runId, scenarioContext); } catch (Exception e) { e.printStackTrace(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/UnderStatistics.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/UnderStatistics.java new file mode 100644 index 00000000..95a936d0 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/UnderStatistics.java @@ -0,0 +1,5 @@ +package org.bench4q.agent.scenario.engine; + +public interface UnderStatistics { + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java index 51c709de..7c499b99 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/VUser.java @@ -74,10 +74,10 @@ public class VUser implements Runnable { Page page = context.getScenario().getPages()[i]; long startTime = System.currentTimeMillis(); for (Batch batch : page.getBatches()) { - doRunBatch(plugins, batch, context.getDataStatistics()); + doRunBatch(plugins, batch, context.getDataCollector()); } long endTime = System.currentTimeMillis(); - context.getDataStatistics().add( + context.getDataCollector().add( PageResult.buildPageResult(i, startTime, endTime)); } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java index 13468c01..ccb2d0a0 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestBase.java @@ -15,6 +15,7 @@ import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.engine.ScenarioContext; import org.bench4q.agent.scenario.engine.Schedule; import org.bench4q.agent.scenario.engine.VUser; +import org.bench4q.agent.storage.StorageHelper; import org.bench4q.share.helper.TestHelper; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.RunScenarioModel; @@ -29,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; public abstract class TestBase { private ParameterBarn basePara; private PluginManager pluginManager; + private StorageHelper storageHelper; private PluginManager getPluginManager() { return pluginManager; @@ -39,6 +41,11 @@ public abstract class TestBase { this.pluginManager = pluginManager; } + @Autowired + private void setStorageHelper(StorageHelper storageHelper) { + this.storageHelper = storageHelper; + } + protected void createFileAndWriteContent(UUID testId, String paramName, String content) throws IOException { basePara = new ParameterBarn(testId.toString()) { @@ -122,14 +129,15 @@ public abstract class TestBase { public VUser createVUser(Scenario scenario, UUID testId) { return new VUser(buildScenarioContext(testId, scenario, 10, - pluginManager), 1, this.getPluginManager()); + pluginManager, this.storageHelper), 1, this.getPluginManager()); } public static ScenarioContext buildScenarioContext(UUID testId, - final Scenario scenario, int poolSize, PluginManager pluginManager) { + final Scenario scenario, int poolSize, PluginManager pluginManager, + StorageHelper storageHelper) { ScenarioContext scenarioContext = ScenarioContext .buildScenarioContextWithoutScenario(testId, poolSize, - pluginManager); + pluginManager, storageHelper); return scenarioContext.addScenrio(scenario, Schedule.build(buildScheduleModel()), new Date().getTime()); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java index 6f0edd5b..11b4166b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/PageResultStatisticsTest.java @@ -1,7 +1,6 @@ package org.bench4q.agent.test.datastatistics; -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.PageResultCollector; +import org.bench4q.agent.datacollector.impl.PagesResultCollector; import org.bench4q.agent.scenario.engine.PageResult; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; import org.junit.Test; @@ -16,13 +15,13 @@ import static org.junit.Assert.*; @ContextConfiguration(locations = { "classpath:test-storage-context.xml", "classpath*:/org/bench4q/agent/config/application-context.xml" }) public class PageResultStatisticsTest { - private DataCollector dataCollector; + private PagesResultCollector dataCollector; - private DataCollector getDataCollector() { + private PagesResultCollector getDataCollector() { return dataCollector; } - private void setDataCollector(DataCollector dataCollector) { + private void setDataCollector(PagesResultCollector dataCollector) { this.dataCollector = dataCollector; } @@ -30,13 +29,13 @@ public class PageResultStatisticsTest { public PageResultStatisticsTest() { new ClassPathXmlApplicationContext( "classpath*:/org/bench4q/agent/config/application-context.xml"); - this.setDataCollector(new PageResultCollector()); + this.setDataCollector(new PagesResultCollector()); } @Test public void pageResultTest() { PageResult pageResult = PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(1, 1)); + Test_ScenarioBriefCollector.makeBehaviorList(1, 1)); assertEquals(2, pageResult.getPageId()); assertEquals(200, pageResult.getExecuteRange()); } @@ -44,7 +43,7 @@ public class PageResultStatisticsTest { @Test public void pageResultWithTwoBehaviorTest() { PageResult pageResult = PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(2, 1)); + Test_ScenarioBriefCollector.makeBehaviorList(2, 1)); assertEquals(2, pageResult.getPageId()); assertEquals(220, pageResult.getExecuteRange()); assertEquals(420, pageResult.getPageEndTime()); @@ -60,7 +59,7 @@ public class PageResultStatisticsTest { public void testOnePaegWithOneBehaviorResult() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(1, 1))); + Test_ScenarioBriefCollector.makeBehaviorList(1, 1))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, pageBriefModel.getPageId()); @@ -77,7 +76,7 @@ public class PageResultStatisticsTest { public void testOnePageWithTwoBehaviorResult() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(2, 1))); + Test_ScenarioBriefCollector.makeBehaviorList(2, 1))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); System.out.println(pageBriefModel.getCountFromBegin()); @@ -95,10 +94,10 @@ public class PageResultStatisticsTest { public void testTwoPageWithStatisticsAtLast() { this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(1, 1))); + Test_ScenarioBriefCollector.makeBehaviorList(1, 1))); this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(2, 2))); + Test_ScenarioBriefCollector.makeBehaviorList(2, 2))); AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, pageBriefModel.getPageId()); @@ -116,7 +115,7 @@ public class PageResultStatisticsTest { testOnePaegWithOneBehaviorResult(); this.getDataCollector().add( PageResult.buildPageResult(2, - ScenarioStatisticsTest.makeBehaviorList(2, 1))); + Test_ScenarioBriefCollector.makeBehaviorList(2, 1))); AgentPageBriefModel model = (AgentPageBriefModel) this .getDataCollector().getPageBriefStatistics(2); assertEquals(2, model.getPageId()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java index 424d8e76..690fd1e2 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java @@ -2,64 +2,61 @@ package org.bench4q.agent.test.datastatistics; import static org.junit.Assert.*; -import java.util.UUID; - -import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.agent.datacollector.impl.BehaviorResultCollector; -import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; +import org.bench4q.agent.datacollector.impl.BehaviorsResultCollector; +import org.bench4q.agent.datacollector.impl.BehaviorsResultCollector.BehaviorResultCollector; 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.BehaviorStatusCodeResultModel; import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test_BehaviorResultCollector { - private DataCollector detailStatistics; + private BehaviorsResultCollector detailStatistics = new BehaviorsResultCollector(); - private DataCollector getDetailStatistics() { + private BehaviorsResultCollector getDetailStatistics() { return detailStatistics; } - private void setDetailStatistics(DataCollector detailStatistics) { - this.detailStatistics = detailStatistics; - } + // + // private void setDetailStatistics(DataCollector detailStatistics) { + // this.detailStatistics = detailStatistics; + // } public Test_BehaviorResultCollector() { - init(); + // init(); } - private 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.setDetailStatistics(agentResultDataCollector); - } + // private 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.setDetailStatistics(agentResultDataCollector); + // } @Test public void addZeroTest() { - for (BehaviorResult behaviorResult : ScenarioStatisticsTest + for (BehaviorResult behaviorResult : Test_ScenarioBriefCollector .makeBehaviorList(0, 1)) { this.getDetailStatistics().add(behaviorResult); } - Object object = this.detailStatistics.getBehaviorBriefStatistics(0); + Object object = this.detailStatistics.getStatistics(0); assertEquals(null, object); } @Test public void addOneDetailTest() { BehaviorResultCollector collector = new BehaviorResultCollector(1); - for (BehaviorResult behaviorResult : ScenarioStatisticsTest + for (BehaviorResult behaviorResult : Test_ScenarioBriefCollector .makeBehaviorList(1, 1)) { collector.add(behaviorResult); } - BehaviorBriefModel briefModel = collector.getStatistics(); + BehaviorBriefModel briefModel = (BehaviorBriefModel) collector + .getStatistics(); assertEquals(briefModel.getSuccessfulCount(), 1); assertEquals(briefModel.getTotalCount(), 1); assertEquals(1, briefModel.getDetailStatusCodeResultModels().size()); @@ -78,11 +75,12 @@ public class Test_BehaviorResultCollector { int behaviorId = 1; BehaviorResultCollector collector = new BehaviorResultCollector( behaviorId); - for (BehaviorResult behaviorResult : ScenarioStatisticsTest + for (BehaviorResult behaviorResult : Test_ScenarioBriefCollector .makeBehaviorList(2, behaviorId)) { collector.add(behaviorResult); } - BehaviorBriefModel briefModel = collector.getStatistics(); + BehaviorBriefModel briefModel = (BehaviorBriefModel) collector + .getStatistics(); assertEquals(1, briefModel.getSuccessfulCount()); assertEquals(2, briefModel.getTotalCount()); assertEquals(2, briefModel.getDetailStatusCodeResultModels().size()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java index 29ed62a7..b2a4d878 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java @@ -4,7 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.Date; -import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeResultCollector; +import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeCollector; import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.junit.Test; @@ -12,9 +12,9 @@ import org.junit.Test; public class Test_BehaviorStatusResultStatistics { @Test public void addOneDetailTest() { - BehaviorStatusCodeResultCollector statusCodeResultCollector = new BehaviorStatusCodeResultCollector( + BehaviorStatusCodeCollector statusCodeResultCollector = new BehaviorStatusCodeCollector( 0, 200, "text/html"); - for (BehaviorResult behaviorResult : ScenarioStatisticsTest + for (BehaviorResult behaviorResult : Test_ScenarioBriefCollector .makeBehaviorList(1, 0)) { statusCodeResultCollector.add(behaviorResult); } @@ -30,12 +30,12 @@ public class Test_BehaviorStatusResultStatistics { @Test public void addTwoTest() throws InterruptedException { - BehaviorStatusCodeResultCollector statusCodeResultCollector = new BehaviorStatusCodeResultCollector( + BehaviorStatusCodeCollector statusCodeResultCollector = new BehaviorStatusCodeCollector( 1, 200, "text/html"); - statusCodeResultCollector.add(ScenarioStatisticsTest + statusCodeResultCollector.add(Test_ScenarioBriefCollector .buildBehaviorResult(300, true, 200, 1, new Date().getTime())); Thread.sleep(100); - statusCodeResultCollector.add(ScenarioStatisticsTest + statusCodeResultCollector.add(Test_ScenarioBriefCollector .buildBehaviorResult(320, true, 200, 1, new Date().getTime())); BehaviorStatusCodeResultModel result = statusCodeResultCollector .getStatistics(); @@ -49,11 +49,11 @@ public class Test_BehaviorStatusResultStatistics { @Test public void addThreeAndBriefTwice() { - BehaviorStatusCodeResultCollector collector = new BehaviorStatusCodeResultCollector( + BehaviorStatusCodeCollector collector = new BehaviorStatusCodeCollector( 1, 302, "text/html"); - collector.add(ScenarioStatisticsTest.buildBehaviorResult(210, true, + collector.add(Test_ScenarioBriefCollector.buildBehaviorResult(210, true, 302, 1, new Date().getTime())); - collector.add(ScenarioStatisticsTest.buildBehaviorResult(230, true, + collector.add(Test_ScenarioBriefCollector.buildBehaviorResult(230, true, 302, 1, new Date().getTime())); BehaviorStatusCodeResultModel resultModel = collector.getStatistics(); assertEquals(resultModel.getContentLength(), 40); @@ -62,7 +62,7 @@ public class Test_BehaviorStatusResultStatistics { assertEquals(resultModel.getMaxResponseTime(), 230); assertEquals(resultModel.getMinResponseTime(), 210); assertEquals(resultModel.getTotalResponseTimeThisTime(), 440); - collector.add(ScenarioStatisticsTest.buildBehaviorResult(190, true, + collector.add(Test_ScenarioBriefCollector.buildBehaviorResult(190, true, 302, 1, new Date().getTime())); resultModel = collector.getStatistics(); assertEquals(resultModel.getContentLength(), 60); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java new file mode 100644 index 00000000..673f16f6 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java @@ -0,0 +1,72 @@ +package org.bench4q.agent.test.datastatistics; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.UUID; + +import org.bench4q.agent.datacollector.impl.DataCollectorImpl; +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.statistics.AgentBriefStatusModel; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { "classpath:application-context.xml" }) +public class Test_DataCollectorImpl { + private DataCollectorImpl dataCollectorImpl; + + public Test_DataCollectorImpl() { + init(); + } + + private void init() { + @SuppressWarnings("resource") + ApplicationContext context = new ClassPathXmlApplicationContext( + "classpath*:/org/bench4q/agent/config/application-context.xml"); + DataCollectorImpl agentResultDataCollector = new DataCollectorImpl( + UUID.randomUUID(), + (StorageHelper) context.getBean(StorageHelper.class)); + this.dataCollectorImpl = agentResultDataCollector; + } + + @Test + public void test() { + BehaviorResult buildBehaviorResult = Test_ScenarioBriefCollector + .buildBehaviorResult(200, true, 200, 1, new Date().getTime()); + this.dataCollectorImpl.add(buildBehaviorResult); + BehaviorResult buildBehaviorResult2 = Test_ScenarioBriefCollector + .buildBehaviorResult(400, false, 200, 1, new Date().getTime()); + this.dataCollectorImpl.add(buildBehaviorResult2); + AgentBriefStatusModel model = (AgentBriefStatusModel) this.dataCollectorImpl + .getScenarioBriefStatistics(); + judgeBehaviorTotalAndScenarioTotal( + Arrays.asList(buildBehaviorResult, buildBehaviorResult2), model); + } + + private void judgeBehaviorTotalAndScenarioTotal( + List generatedBehaviorResults, + AgentBriefStatusModel model) { + int successCount = 0; + HashSet set = new HashSet(); + for (BehaviorResult unit : generatedBehaviorResults) { + BehaviorBriefModel briefModel = (BehaviorBriefModel) this.dataCollectorImpl + .getBehaviorBriefStatistics(unit.getBehaviorId()); + if (set.contains(unit.getBehaviorId()) || briefModel == null) { + continue; + } + set.add(unit.getBehaviorId()); + successCount += briefModel.getSuccessfulCount(); + } + assertEquals((long) successCount, model.getSuccessCountFromBegin()); + } +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_ScenarioBriefCollector.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_ScenarioBriefCollector.java new file mode 100644 index 00000000..363a9b56 --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_ScenarioBriefCollector.java @@ -0,0 +1,155 @@ +package org.bench4q.agent.test.datastatistics; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.bench4q.agent.datacollector.impl.ScenarioResultCollector; +import org.bench4q.agent.scenario.engine.BehaviorResult; +import org.bench4q.agent.scenario.engine.PageResult; +import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; +import org.junit.Test; + +public class Test_ScenarioBriefCollector { + private ScenarioResultCollector dataStatistics = new ScenarioResultCollector(); + + protected ScenarioResultCollector getDataStatistics() { + return dataStatistics; + } + + public Test_ScenarioBriefCollector() { + } + + @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 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)); + } + + @Test + public void addTwoBriefTest() throws InterruptedException { + List 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)); + } + + 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 behaviorResults = makeBehaviorList(count, 1); + return PageResult.buildPageResult(2, behaviorResults); + } + + public static List makeBehaviorList(int count, + int behaviorId) { + List behaviorResults = new ArrayList(); + 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; + } + +} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index e23490ac..92dcce88 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -11,6 +11,7 @@ import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.engine.ScenarioContext; import org.bench4q.agent.scenario.engine.Schedule; +import org.bench4q.agent.storage.StorageHelper; import org.bench4q.agent.test.TestBase; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; import org.junit.Test; @@ -24,11 +25,13 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; public class Test_ScenarioContext extends TestBase { @Autowired private PluginManager pluginManager; + @Autowired + private StorageHelper storageHelper; @Test public void testBuildScenarioContext() { ScenarioContext context = buildScenarioContext(UUID.randomUUID(), - new Scenario(), 20, this.pluginManager); + new Scenario(), 20, this.pluginManager, this.storageHelper); assertEquals(10, context.getExecutor().getKeepAliveTime(TimeUnit.MINUTES)); assertEquals(20, context.getExecutor().getMaximumPoolSize()); @@ -46,7 +49,7 @@ public class Test_ScenarioContext extends TestBase { private ScenarioContext getScenarioWithScenarioAndSchedule() { ScenarioContext scenarioContext = ScenarioContext .buildScenarioContextWithoutScenario(UUID.randomUUID(), 100, - pluginManager); + pluginManager, this.storageHelper); scenarioContext = scenarioContext .addScenrio( Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(new LinkedList())), diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java index 5e3fcc63..fdbf6a09 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java @@ -12,6 +12,7 @@ import org.bench4q.agent.plugin.PluginManager; import org.bench4q.agent.scenario.Scenario; import org.bench4q.agent.scenario.engine.ScenarioContext; import org.bench4q.agent.scenario.engine.ScenarioEngine; +import org.bench4q.agent.storage.StorageHelper; import org.bench4q.agent.test.TestBase; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; @@ -29,6 +30,8 @@ public class Test_ScenarioEngine extends TestBase { private UUID testId = UUID.randomUUID(); @Autowired private PluginManager pluginManager; + @Autowired + private StorageHelper storageHelper; private ScenarioEngine getScenarioEngine() { return scenarioEngine; @@ -41,13 +44,12 @@ public class Test_ScenarioEngine extends TestBase { @Test public void test_UpdatePopulation() { - ScenarioContext scenarioContext = buildScenarioContext( - testId, + ScenarioContext scenarioContext = buildScenarioContext(testId, Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith( new ArrayList(), BehaviorModel .UserBehaviorBuilder(1, "ok", "http", new ArrayList()))), - 100, this.pluginManager); + 100, this.pluginManager, this.storageHelper); this.getScenarioEngine().getRunningTests().put(testId, scenarioContext); scenarioContext.updatePopulation(20); assertEquals(20, scenarioContext.getExecutor().getMaximumPoolSize()); @@ -56,11 +58,12 @@ public class Test_ScenarioEngine extends TestBase { @Test public void test_RunWithContext() throws IOException { - ScenarioContext scenarioContext = buildScenarioContext( - testId, Scenario.scenarioBuilderWithCompile(FileUtils + ScenarioContext scenarioContext = buildScenarioContext(testId, + Scenario.scenarioBuilderWithCompile(FileUtils .readFileToString(new File("Scripts" + System.getProperty("file.separator") - + "homepage.xml"))), 100, this.pluginManager); + + "homepage.xml"))), 100, this.pluginManager, + this.storageHelper); this.getScenarioEngine().getRunningTests().put(testId, scenarioContext); this.getScenarioEngine().runWith(testId); } diff --git a/Bench4Q-Master/RecordScriptTestCase/baidu.html b/Bench4Q-Master/RecordScriptTestCase/baidu.html new file mode 100644 index 00000000..7742d644 --- /dev/null +++ b/Bench4Q-Master/RecordScriptTestCase/baidu.html @@ -0,0 +1 @@ + Date: Fri, 12 Sep 2014 17:36:51 +0800 Subject: [PATCH 5/8] remove the bug when brief remove the bug when brief --- .../org/bench4q/agent/api/TestController.java | 4 + .../impl/AbstractDataCollector.java | 82 -------- .../impl/BehaviorResultCollector.java | 62 ------ .../BehaviorStatusCodeResultCollector.java | 109 ---------- .../impl/BehaviorsResultCollector.java | 4 +- .../datacollector/impl/DataCollectorImpl.java | 12 +- .../impl/PageResultCollector.java | 110 ---------- .../ScenarioStatisticsTest.java | 197 ------------------ .../Test_DataCollectorImpl.java | 13 +- 9 files changed, 21 insertions(+), 572 deletions(-) delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java delete mode 100644 Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java delete mode 100644 Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index 3500b8f1..cfafe6e5 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -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")); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java deleted file mode 100644 index 682e17bc..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/AbstractDataCollector.java +++ /dev/null @@ -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); - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java deleted file mode 100644 index ddaf3757..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorResultCollector.java +++ /dev/null @@ -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 detailStatusMap; - - public BehaviorResultCollector(int behaviorId) { - this.behaviorId = behaviorId; - this.detailStatusMap = new HashMap(); - } - - 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 list = new LinkedList(); - 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; - } -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java deleted file mode 100644 index ea6d11b0..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeResultCollector.java +++ /dev/null @@ -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; - } -} \ No newline at end of file diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java index 8ca29d80..568ef3e9 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java @@ -26,9 +26,9 @@ public class BehaviorsResultCollector { new BehaviorResultCollector(behaviorResult .getBehaviorId())); } - this.behaviorResultMap.get(behaviorResult.getBehaviorId()).add( - behaviorResult); } + this.behaviorResultMap.get(behaviorResult.getBehaviorId()).add( + behaviorResult); } public Object getStatistics(int behaviorId) { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java index b1dbcafd..0f3268e1 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/DataCollectorImpl.java @@ -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( diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java deleted file mode 100644 index fa841d10..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/PageResultCollector.java +++ /dev/null @@ -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 pageBriefMap; - - private Map getPageBriefMap() { - return pageBriefMap; - } - - private void setPageBriefMap(Map pageBriefMap) { - this.pageBriefMap = pageBriefMap; - } - - public PageResultCollector() { - this.setPageBriefMap(new HashMap()); - } - - 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; - } - } -} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java deleted file mode 100644 index a28a24c2..00000000 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/ScenarioStatisticsTest.java +++ /dev/null @@ -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 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 generatedBehaviorResults, - AgentBriefStatusModel model) { - int successCount = 0; - HashSet set = new HashSet(); - 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 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 behaviorResults = makeBehaviorList(count, 1); - return PageResult.buildPageResult(2, behaviorResults); - } - - public static List makeBehaviorList(int count, - int behaviorId) { - List behaviorResults = new ArrayList(); - 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; - } - -} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java index 673f16f6..7dc115cc 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java @@ -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 generatedBehaviorResults, AgentBriefStatusModel model) { - int successCount = 0; + int successCount = 0, totalCount = 0; HashSet set = new HashSet(); 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()); } } From 093f2d0a841140f57c2465cc0e9c481ad16d0e73 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Mon, 15 Sep 2014 15:07:39 +0800 Subject: [PATCH 6/8] refactor and remove thtat bug of briefing behaviorsBrief refactor and remove thtat bug of briefing behaviorsBrief --- .../org/bench4q/agent/api/TestController.java | 20 ++- .../impl/BehaviorStatusCodeCollector.java | 2 +- .../impl/BehaviorsResultCollector.java | 4 +- .../org/bench4q/agent/scenario/Scenario.java | 4 +- .../agent/scenario/behavior/Behavior.java | 2 +- .../scenario/behavior/ControlBehavior.java | 2 +- .../agent/scenario/behavior/TestBehavior.java | 2 +- .../scenario/behavior/TimerBehavior.java | 2 +- .../behavior/TransactionBehavior.java | 2 +- .../agent/scenario/behavior/UserBehavior.java | 2 +- .../agent/test/ExtractScenarioTest.java | 2 +- .../agent/test/TestWithScriptFile.java | 2 +- .../Test_BehaviorResultCollector.java | 4 +- .../Test_BehaviorStatusResultStatistics.java | 2 +- .../Test_DataCollectorImpl.java | 2 +- .../agent/test/plugin/Test_ContextPlugin.java | 2 +- .../agent/test/plugin/Test_CsvProvider.java | 4 +- .../agent/test/plugin/Test_HBasePlugin.java | 2 +- .../agent/test/plugin/Test_HttpPlugin.java | 2 +- .../agent/test/plugin/Test_MongoDBPlugin.java | 2 +- .../agent/test/scenario/Test_Scenario.java | 6 +- .../test/scenario/Test_ScenarioContext.java | 2 +- .../test/scenario/Test_ScenarioEngine.java | 2 +- .../agent/test/scenario/Test_VUser.java | 2 +- .../RecordScriptTestCase/baidu.html | 8 +- .../master/domain/entity/RunningAgentDB.java | 6 +- .../impl/BehaviorBriefStatistics.java | 60 ++++--- .../impl/BehaviorStatusCodeStatistics.java | 82 +++++++++ .../impl/BehaviorsBriefStatistics.java | 75 ++------ .../impl/ScriptBriefStatistics.java | 4 +- .../communication/Test_AgentMessenger.java | 16 +- .../Test_BehaviorBriefStatistics.java | 58 +++++++ .../Test_BehaviorStatusStatistics.java | 11 ++ .../Test_BehaviorsBriefStatistics.java | 50 ++++++ .../test/java/stubs/Mock_AgentMessenger.java | 2 +- .../statistics/AgentBehaviorsBriefModel.java | 2 - .../agent/statistics/BehaviorBriefModel.java | 68 ++++++++ .../BehaviorStatusCodeResultModel.java | 164 ++++++++++++++++++ .../statistics/ScriptBehaviorBriefModel.java | 30 ++-- .../ScriptBehaviorStatusCodeModel.java | 40 +++++ .../statistics/ScriptBehaviorsBriefModel.java | 8 +- .../bench4q/web/api/TestPlanController.java | 23 +-- .../bench4q/web/service/TestPlanService.java | 18 +- 43 files changed, 624 insertions(+), 179 deletions(-) create mode 100644 Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorStatusCodeStatistics.java create mode 100644 Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java create mode 100644 Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorStatusStatistics.java create mode 100644 Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorsBriefStatistics.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorBriefModel.java create mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorStatusCodeResultModel.java diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java index cfafe6e5..bd459350 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/api/TestController.java @@ -13,7 +13,6 @@ import org.bench4q.agent.scenario.engine.ScenarioContext; import org.bench4q.agent.scenario.engine.ScenarioEngine; import org.bench4q.agent.scenario.engine.Schedule; import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.CleanTestResultModel; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; @@ -24,6 +23,7 @@ import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; @@ -93,7 +93,7 @@ public class TestController { ScheduleModel scheduleModel = (ScheduleModel) MarshalHelper .unmarshal(ScheduleModel.class, scheduleContent); this.getScenarioEngine().submitScenario(runId, - Scenario.scenarioBuilderWithCompile(runScenarioModel), + Scenario.scenarioCompiler(runScenarioModel), Schedule.build(scheduleModel), realStartTime); return MarshalHelper.tryMarshal(buildWith(runId)); } catch (Exception e) { @@ -159,10 +159,18 @@ public class TestController { if (!(behavior instanceof UserBehavior)) { continue; } - BehaviorBriefModel briefModel = behavior - .getBehaviorBriefResult(scenarioContext.getDataCollector()); - briefModel.setBehaviorUrl(behavior.getSpecificParamValue("url")); - behaviorBriefModels.add(briefModel); + try { + BehaviorBriefModel briefModel = behavior + .getBehaviorBriefResult(scenarioContext + .getDataCollector()); + logger.info(behavior.getId()); + briefModel + .setBehaviorUrl(behavior.getSpecificParamValue("url")); + behaviorBriefModels.add(briefModel); + } catch (Exception e) { + logger.info("where behaviorId is " + behavior.getId(), e); + } + } ret.setBehaviorBriefModels(behaviorBriefModels); return ret; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java index ad62d163..7a5965b9 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorStatusCodeCollector.java @@ -3,7 +3,7 @@ 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; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; public class BehaviorStatusCodeCollector { private int behaviorId; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java index 568ef3e9..83bbc861 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/datacollector/impl/BehaviorsResultCollector.java @@ -8,8 +8,8 @@ import java.util.Map; import org.bench4q.agent.datacollector.DataStatistics; import org.bench4q.agent.scenario.engine.BehaviorResult; import org.bench4q.agent.scenario.engine.UnderStatistics; -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; public class BehaviorsResultCollector { private final Map behaviorResultMap; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java index 67bf87cb..d9677a41 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Scenario.java @@ -67,11 +67,11 @@ public class Scenario { } public static Scenario scenarioBuilderWithCompile(String scenarioContent) { - return scenarioBuilderWithCompile((RunScenarioModel) MarshalHelper + return scenarioCompiler((RunScenarioModel) MarshalHelper .tryUnmarshal(RunScenarioModel.class, scenarioContent)); } - public static Scenario scenarioBuilderWithCompile( + public static Scenario scenarioCompiler( RunScenarioModel scenarioModel) { if (scenarioModel == null) { throw new NullPointerException(); diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java index b0324d7d..c483b69b 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/Behavior.java @@ -2,9 +2,9 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; import org.bench4q.agent.scenario.Parameter; -import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.ParameterModel; import org.bench4q.share.models.agent.scriptrecord.BehaviorModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public abstract class Behavior { private static final String CONTROL_BEHAVIOR = "CONTROLBEHAVIOR"; diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java index 6a493370..40633f51 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/ControlBehavior.java @@ -1,7 +1,7 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public class ControlBehavior extends Behavior { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java index 6373e147..79377d27 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TestBehavior.java @@ -1,7 +1,7 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public class TestBehavior extends Behavior { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java index aa7ddc2f..f33d5221 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TimerBehavior.java @@ -1,7 +1,7 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public class TimerBehavior extends Behavior { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java index beed445e..a96d4e48 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/TransactionBehavior.java @@ -1,7 +1,7 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public class TransactionBehavior extends Behavior { diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java index 35c066f7..5f710672 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/behavior/UserBehavior.java @@ -1,7 +1,7 @@ package org.bench4q.agent.scenario.behavior; import org.bench4q.agent.datacollector.DataCollector; -import org.bench4q.share.models.agent.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; public class UserBehavior extends Behavior { @Override diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/ExtractScenarioTest.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/ExtractScenarioTest.java index 973b90d0..e6262e0a 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/ExtractScenarioTest.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/ExtractScenarioTest.java @@ -20,7 +20,7 @@ public class ExtractScenarioTest { RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper .unmarshal(RunScenarioModel.class, FileUtils .readFileToString(new File("Scripts/goodForPage.xml"))); - Scenario scenario = Scenario.scenarioBuilderWithCompile(runScenarioModel); + Scenario scenario = Scenario.scenarioCompiler(runScenarioModel); assertTrue(scenario.getPages().length > 0); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java index c41cdb89..e88847f8 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/TestWithScriptFile.java @@ -23,12 +23,12 @@ import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.TestHelper; -import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentPageBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; import org.junit.Test; public class TestWithScriptFile extends TestBase { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java index 690fd1e2..13f931b6 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorResultCollector.java @@ -5,8 +5,8 @@ import static org.junit.Assert.*; import org.bench4q.agent.datacollector.impl.BehaviorsResultCollector; import org.bench4q.agent.datacollector.impl.BehaviorsResultCollector.BehaviorResultCollector; import org.bench4q.agent.scenario.engine.BehaviorResult; -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; import org.junit.Test; public class Test_BehaviorResultCollector { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java index b2a4d878..0d0e0f11 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_BehaviorStatusResultStatistics.java @@ -6,7 +6,7 @@ import java.util.Date; import org.bench4q.agent.datacollector.impl.BehaviorStatusCodeCollector; import org.bench4q.agent.scenario.engine.BehaviorResult; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; import org.junit.Test; public class Test_BehaviorStatusResultStatistics { diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java index 7dc115cc..5b9c2e5f 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/datastatistics/Test_DataCollectorImpl.java @@ -11,8 +11,8 @@ import java.util.UUID; import org.bench4q.agent.datacollector.impl.DataCollectorImpl; 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.statistics.AgentBriefStatusModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.ApplicationContext; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java index 498964a6..646c3f55 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_ContextPlugin.java @@ -54,7 +54,7 @@ public class Test_ContextPlugin extends TestBase { batchModel.getBehaviors().add(logIt); // System.out.println(MarshalHelper.tryMarshal(scenarioModel)); VUser vuser = createVUser( - Scenario.scenarioBuilderWithCompile(scenarioModel), + Scenario.scenarioCompiler(scenarioModel), UUID.randomUUID()); vuser.run(); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java index 2c8c55ed..a61d79f8 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_CsvProvider.java @@ -87,7 +87,7 @@ public class Test_CsvProvider extends TestBase { createFileAndWriteContent(testId, fileName, CONTENT); RunScenarioModel runScenarioModel = buildRunScenarioModel(fileName); Scenario scenario = Scenario - .scenarioBuilderWithCompile(runScenarioModel); + .scenarioCompiler(runScenarioModel); VUser vUser = createVUser(scenario, testId); try { HashMap plugins = new HashMap(); @@ -120,7 +120,7 @@ public class Test_CsvProvider extends TestBase { UUID testId = UUID.randomUUID(); createFileAndWriteContent(testId, fileName, CONTENT); Scenario scenario = Scenario - .scenarioBuilderWithCompile(buildRunScenarioModel(fileName)); + .scenarioCompiler(buildRunScenarioModel(fileName)); VUser vUser = createVUser(scenario, testId); HashMap plugins = new HashMap(); TestHelper diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HBasePlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HBasePlugin.java index ef5758a6..eec0d99b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HBasePlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HBasePlugin.java @@ -95,7 +95,7 @@ public class Test_HBasePlugin extends TestBase { TestHelper.createFileIfNotExist(file); FileUtils.writeStringToFile(file, MarshalHelper.tryMarshal(runScenarioModel)); - createVUser(Scenario.scenarioBuilderWithCompile(runScenarioModel), + createVUser(Scenario.scenarioCompiler(runScenarioModel), UUID.randomUUID()).run(); } } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java index 97ebad8b..8a33e32b 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_HttpPlugin.java @@ -154,7 +154,7 @@ public class Test_HttpPlugin extends TestBase { .readFileToString(new File( "Scripts/httpWithoutTimer.xml"))); VUser vUser = createVUser( - Scenario.scenarioBuilderWithCompile(scenarioMode), + Scenario.scenarioCompiler(scenarioMode), UUID.randomUUID()); vUser.run(); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java index 627e4f4f..1bbe4146 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/plugin/Test_MongoDBPlugin.java @@ -97,7 +97,7 @@ public class Test_MongoDBPlugin extends TestBase { @Test public void test_BuildScenario() throws IOException { RunScenarioModel result = buildUpMongoDBScenario(); - VUser vUser = createVUser(Scenario.scenarioBuilderWithCompile(result), + VUser vUser = createVUser(Scenario.scenarioCompiler(result), UUID.randomUUID()); vUser.run(); } diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java index 4236059d..15bb4cdd 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_Scenario.java @@ -78,7 +78,7 @@ public class Test_Scenario extends TestBase { TestHelper.createFileIfNotExist(file); FileUtils.writeStringToFile(file, MarshalHelper.tryMarshal(runScenarioModel)); - createVUser(Scenario.scenarioBuilderWithCompile(runScenarioModel), + createVUser(Scenario.scenarioCompiler(runScenarioModel), UUID.randomUUID()).run(); } @@ -101,7 +101,7 @@ public class Test_Scenario extends TestBase { TestHelper.createFileIfNotExist(file); FileUtils.writeStringToFile(file, MarshalHelper.tryMarshal(runScenarioModel)); - createVUser(Scenario.scenarioBuilderWithCompile(runScenarioModel), + createVUser(Scenario.scenarioCompiler(runScenarioModel), UUID.randomUUID()).run(); } @@ -117,7 +117,7 @@ public class Test_Scenario extends TestBase { "withParamPart", TEST_CASE)); } })); - Scenario scenario = Scenario.scenarioBuilderWithCompile(inputModel); + Scenario scenario = Scenario.scenarioCompiler(inputModel); for (Behavior behavior : scenario.getAllBehaviors()) { assertEquals("first", behavior.getName()); assertEquals(0, behavior.getId()); diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java index 92dcce88..4d7eb5c6 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioContext.java @@ -52,7 +52,7 @@ public class Test_ScenarioContext extends TestBase { pluginManager, this.storageHelper); scenarioContext = scenarioContext .addScenrio( - Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(new LinkedList())), + Scenario.scenarioCompiler(buildRunScenarioModelWith(new LinkedList())), Schedule.build(buildScheduleModel()), new Date() .getTime()); return scenarioContext; diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java index fdbf6a09..fb427cb3 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_ScenarioEngine.java @@ -45,7 +45,7 @@ public class Test_ScenarioEngine extends TestBase { @Test public void test_UpdatePopulation() { ScenarioContext scenarioContext = buildScenarioContext(testId, - Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith( + Scenario.scenarioCompiler(buildRunScenarioModelWith( new ArrayList(), BehaviorModel .UserBehaviorBuilder(1, "ok", "http", new ArrayList()))), diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java index b4bda65a..5dbb124a 100644 --- a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/Test_VUser.java @@ -51,7 +51,7 @@ public class Test_VUser extends TestBase { } }, behaviorModel); Scenario scenario = Scenario - .scenarioBuilderWithCompile(runScenarioModel); + .scenarioCompiler(runScenarioModel); VUser vUser = createVUser(scenario, testId); Map plugins = new HashMap(); TestHelper diff --git a/Bench4Q-Master/RecordScriptTestCase/baidu.html b/Bench4Q-Master/RecordScriptTestCase/baidu.html index 7742d644..0ed0a974 100644 --- a/Bench4Q-Master/RecordScriptTestCase/baidu.html +++ b/Bench4Q-Master/RecordScriptTestCase/baidu.html @@ -1 +1,7 @@ - + + + + + + detailStatisticsMap; - private volatile Map behaviorStatusMap; - - public BehaviorBriefStatistics() { - this.behaviorStatusMap = new HashMap(); + public BehaviorBriefStatistics(int behaviorId, String behaviorUrl) { + this.behaviorId = behaviorId; + this.behaviorUrl = behaviorUrl; + this.detailStatisticsMap = new ConcurrentHashMap(); resetTempraryFields(); - this.maxResponseTimeFromBegin = 0; - this.minResponseTimeFromBegin = Long.MAX_VALUE; } /** @@ -33,11 +30,8 @@ public class BehaviorBriefStatistics extends ScriptStatistics { * one. */ private void resetTempraryFields() { - this.countFromBegin = 0; - this.countThisTime = 0; - this.successCountThisTime = 0; this.successCountFromBegin = 0; - this.totalResponseTimeThisTime = 0; + this.totalCountFromBegin = 0; } @Override @@ -46,25 +40,35 @@ public class BehaviorBriefStatistics extends ScriptStatistics { return; } BehaviorBriefModel behaviorBriefModel = (BehaviorBriefModel) dataUnit; - this.countFromBegin += behaviorBriefModel.getTotalCount(); + this.totalCountFromBegin += behaviorBriefModel.getTotalCount(); this.successCountFromBegin += behaviorBriefModel.getSuccessfulCount(); for (BehaviorStatusCodeResultModel newOne : behaviorBriefModel .getDetailStatusCodeResultModels()) { - if (!this.behaviorStatusMap.containsKey(newOne.getStatusCode())) { - behaviorStatusMap.put(newOne.getStatusCode(), newOne); - continue; + if (!this.detailStatisticsMap.containsKey(newOne.getStatusCode())) { + detailStatisticsMap + .put(newOne.getStatusCode(), + new BehaviorStatusCodeStatistics( + this.behaviorId, + newOne.getStatusCode(), newOne + .getContentType())); } - BehaviorStatusCodeResultModel origin = behaviorStatusMap.get(newOne - .getStatusCode()); - origin.plus(newOne); + detailStatisticsMap.get(newOne.getStatusCode()).add(newOne); } } @Override public Object getStatistics() { ScriptBehaviorBriefModel result = new ScriptBehaviorBriefModel(); - - return null; + result.setBehaviorId(this.behaviorId); + result.setBehaviorUrl(this.behaviorUrl); + result.setSuccessCountFromBegin(this.successCountFromBegin); + result.setTotalCountFromBegin(this.totalCountFromBegin); + for (BehaviorStatusCodeStatistics item : this.detailStatisticsMap + .values()) { + result.getList().add( + (ScriptBehaviorStatusCodeModel) item.getStatistics()); + } + return result; } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorStatusCodeStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorStatusCodeStatistics.java new file mode 100644 index 00000000..290720a3 --- /dev/null +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorStatusCodeStatistics.java @@ -0,0 +1,82 @@ +package org.bench4q.master.domain.valueobject.datastatistics.impl; + +import org.bench4q.master.domain.valueobject.datastatistics.DataStatistics; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.DataStatisticsModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorStatusCodeModel; + +public class BehaviorStatusCodeStatistics implements DataStatistics { + private final int behaviorId; + private final int statusCode; + private final String contentType; + private long countThisTime; + private long countFromBegin; + private long contentLengthFromBegin; + private long contentLengthThisTime; + private long minResponseTime; + private long maxResponseTime; + private long totalResponseThisTime; + + public BehaviorStatusCodeStatistics(int behaviorId, int statusCode, + String contentType) { + this.behaviorId = behaviorId; + this.statusCode = statusCode; + this.contentType = contentType; + resetTemperaryField(); + } + + @Override + public void add(DataStatisticsModel dataUnit) { + if (dataUnit == null + || !(dataUnit instanceof BehaviorStatusCodeResultModel)) { + return; + } + BehaviorStatusCodeResultModel statusCodeResult = (BehaviorStatusCodeResultModel) dataUnit; + this.countThisTime++; + this.countFromBegin++; + this.contentLengthFromBegin += statusCodeResult.getContentLength(); + this.contentLengthThisTime += statusCodeResult + .getContentLengthThisTime(); + this.maxResponseTime = Math.max(this.maxResponseTime, + statusCodeResult.getMaxResponseTime()); + this.minResponseTime = Math.min(this.minResponseTime, + statusCodeResult.getMinResponseTime()); + this.totalResponseThisTime += statusCodeResult + .getTotalResponseTimeThisTime(); + } + + /** + * Now, for totalCount, i just collect all result from agent, and add their + * countFromBegin together. But there will be a error there. if an agent + * doesn't give back its result, the the countFromBegin maybe less that last + * one. + */ + private void resetTemperaryField() { + this.totalResponseThisTime = 0; + this.countFromBegin = 0; + this.countThisTime = 0; + this.maxResponseTime = Long.MIN_VALUE; + this.minResponseTime = Long.MAX_VALUE; + this.contentLengthFromBegin = 0; + this.contentLengthThisTime = 0; + } + + @Override + public Object getStatistics() { + ScriptBehaviorStatusCodeModel result = new ScriptBehaviorStatusCodeModel(); + result.setBehaviorId(this.behaviorId); + result.setContentLength(this.contentLengthFromBegin); + result.setContentType(this.contentType); + result.setCount(this.countFromBegin); + result.setMaxResponseTime(this.maxResponseTime); + result.setMinResponseTime(this.minResponseTime); + result.setStatusCode(this.statusCode); + result.setAverageResponseTime(this.countThisTime == 0 ? 0 + : this.totalResponseThisTime / this.countThisTime); + result.setContentLength(this.contentLengthFromBegin); + result.setContentLengthThisTime(this.contentLengthThisTime); + resetTemperaryField(); + return result; + } + +} diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java index fa81071e..db971e56 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorsBriefStatistics.java @@ -5,26 +5,16 @@ import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; import org.bench4q.share.models.agent.statistics.DataStatisticsModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorBriefModel; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; public class BehaviorsBriefStatistics extends ScriptStatistics { - private Map> map; private Map idUrlMap; private Map behaviorStatisticsMap; - private Map> getMap() { - return map; - } - - private void setMap( - Map> map) { - this.map = map; - } - private Map getIdUrlMap() { return idUrlMap; } @@ -43,49 +33,22 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { } public BehaviorsBriefStatistics() { - this.setMap(new ConcurrentHashMap>()); this.setIdUrlMap(new ConcurrentHashMap()); this.setBehaviorStatisticsMap(new ConcurrentHashMap()); } public ScriptBehaviorsBriefModel getStatistics() { ScriptBehaviorsBriefModel result = new ScriptBehaviorsBriefModel(); - AgentBehaviorsBriefModel agentBehaviorsBriefModel = new AgentBehaviorsBriefModel(); - List list = new ArrayList(); - for (int behaviorId : this.getMap().keySet()) { - BehaviorBriefModel behaviorBriefModel = new BehaviorBriefModel(); - behaviorBriefModel.setBehaviorId(behaviorId); - behaviorBriefModel.setBehaviorUrl(this.getIdUrlMap() - .get(behaviorId)); - List statusList = new ArrayList(); - for (int statusCode : this.getMap().get(behaviorId).keySet()) { - statusList.add(this.getMap().get(behaviorId).get(statusCode) - .getCopy()); - } - behaviorBriefModel.setDetailStatusCodeResultModels(statusList); - this.setBehaviorCount(behaviorBriefModel); - list.add(behaviorBriefModel); + List list = new ArrayList(); + for (int behaviorId : this.behaviorStatisticsMap.keySet()) { + list.add((ScriptBehaviorBriefModel) this.behaviorStatisticsMap.get( + behaviorId).getStatistics()); } - agentBehaviorsBriefModel.setBehaviorBriefModels(list); - result.setBehaviorBriefModels(agentBehaviorsBriefModel - .getBehaviorBriefModels()); + result.setBehaviorBriefModels(list); makeUpResultModelWithSamplingTime(result); return result; } - private void setBehaviorCount(BehaviorBriefModel behaviorBriefModel) { - long totalCount = 0, successfulCount = 0; - for (BehaviorStatusCodeResultModel behaviorStatusCodeResultModel : behaviorBriefModel - .getDetailStatusCodeResultModels()) { - totalCount += behaviorStatusCodeResultModel.getCount(); - if (behaviorStatusCodeResultModel.isSuccess()) { - successfulCount += behaviorStatusCodeResultModel.getCount(); - } - } - behaviorBriefModel.setTotalCount(totalCount); - behaviorBriefModel.setSuccessfulCount(successfulCount); - } - public void add(DataStatisticsModel dataUnit) { if (!(dataUnit instanceof AgentBehaviorsBriefModel)) { return; @@ -94,19 +57,6 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { for (BehaviorBriefModel behaviorBriefModel : input .getBehaviorBriefModels()) { guardAllMapsKeyExists(behaviorBriefModel); - // Map behaviorStatusMap = - // this - // .getMap().get(behaviorBriefModel.getBehaviorId()); - // for (BehaviorStatusCodeResultModel newOne : behaviorBriefModel - // .getDetailStatusCodeResultModels()) { - // if (!behaviorStatusMap.containsKey(newOne.getStatusCode())) { - // behaviorStatusMap.put(newOne.getStatusCode(), newOne); - // continue; - // } - // BehaviorStatusCodeResultModel origin = behaviorStatusMap - // .get(newOne.getStatusCode()); - // origin.plus(newOne); - // } this.getBehaviorStatisticsMap() .get(behaviorBriefModel.getBehaviorId()) .add(behaviorBriefModel); @@ -117,18 +67,15 @@ public class BehaviorsBriefStatistics extends ScriptStatistics { private synchronized void guardAllMapsKeyExists( BehaviorBriefModel behaviorBriefModel) { int behaviorId = behaviorBriefModel.getBehaviorId(); - if (!this.getMap().containsKey(behaviorId)) { - this.getMap() - .put(behaviorId, - new ConcurrentHashMap()); - } if (!this.getIdUrlMap().containsKey(behaviorId)) { this.getIdUrlMap().put(behaviorId, behaviorBriefModel.getBehaviorUrl()); } if (!this.getBehaviorStatisticsMap().containsKey(behaviorId)) { - this.getBehaviorStatisticsMap().put(behaviorId, - new BehaviorBriefStatistics()); + this.getBehaviorStatisticsMap().put( + behaviorId, + new BehaviorBriefStatistics(behaviorId, behaviorBriefModel + .getBehaviorUrl())); } } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java index 09c318d9..73b20f94 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/ScriptBriefStatistics.java @@ -128,10 +128,10 @@ public class ScriptBriefStatistics extends ScriptStatistics { if (!(dataUnit instanceof AgentBriefStatusModel)) { return; } - add((AgentBriefStatusModel) dataUnit); + addCore((AgentBriefStatusModel) dataUnit); } - private void add(AgentBriefStatusModel briefModel) { + private void addCore(AgentBriefStatusModel briefModel) { if (briefModel == null) { return; } diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/communication/Test_AgentMessenger.java b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/communication/Test_AgentMessenger.java index 332cc8ee..e198a029 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/communication/Test_AgentMessenger.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/integrated/communication/Test_AgentMessenger.java @@ -17,7 +17,9 @@ import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; +import org.bench4q.share.models.agent.TestBriefStatusModel; import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -45,7 +47,7 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { // TODO: modify this to adapt to new code @Test public void testSubmitScenarioWithParamsAndRun() throws IOException, - JAXBException { + JAXBException, InterruptedException { List paramFiles = new ArrayList(); int scriptId = getUserFirstScript(this.getUserRepository().getUser( @@ -60,7 +62,7 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { paramFiles.add(file1); paramFiles.add(file2); File scenarioFile = new File("Scripts" + FILE_SEPARATOR - + "forGoodRecord.xml"); + + "homepage.xml"); assertTrue(scenarioFile.exists()); RunScenarioModel inputModel = (RunScenarioModel) MarshalHelper .unmarshal(RunScenarioModel.class, @@ -74,14 +76,22 @@ public class Test_AgentMessenger extends TestBase_MakeUpTestPlan { assertNotNull(model); assertNotNull(model.getRunId()); System.out.println(model.getRunId()); + ScheduleModel scheduleModel = new ScheduleModel(); + scheduleModel.getPoints().add(new PointModel(0, 0)); + scheduleModel.getPoints().add(new PointModel(20, 20)); + scheduleModel.getPoints().add(new PointModel(40, 20)); RunScenarioResultModel modelAfter = this.getAgentMessenger() .submitScenrioWithParams(agent, model.getRunId(), paramFiles, - inputModel, new ScheduleModel(), new Date()); + inputModel, scheduleModel, new Date()); assertEquals(model.getRunId(), modelAfter.getRunId()); model = this.getAgentMessenger().runWithParams(agent, model.getRunId()); assertNotNull(model); assertNotNull(model.getRunId()); System.out.println(model.getRunId()); + // Thread.sleep(2000); + TestBriefStatusModel briefModel = this.getAgentMessenger() + .scriptBriefAll(agent, model.getRunId()); + assertNotNull(briefModel); } private File createParameterFile(int scriptId, String paramName) diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java new file mode 100644 index 00000000..5ecfa58f --- /dev/null +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java @@ -0,0 +1,58 @@ +package org.bench4q.master.unitTest.datastatistics; + +import static org.junit.Assert.*; + +import java.util.ArrayList; + +import org.bench4q.master.domain.valueobject.datastatistics.impl.BehaviorBriefStatistics; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorBriefModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorStatusCodeModel; +import org.junit.Test; + +public class Test_BehaviorBriefStatistics { + + @Test + public void test() { + String url = "www.baidu.com"; + int behaviorId = 1; + BehaviorBriefStatistics behaviorStatistics = new BehaviorBriefStatistics( + behaviorId, url); + behaviorStatistics.add(buildBehaviorBriefModel(behaviorId, url, 2, 3)); + behaviorStatistics.add(buildBehaviorBriefModel(behaviorId, url, 3, 5)); + ScriptBehaviorBriefModel result = (ScriptBehaviorBriefModel) behaviorStatistics + .getStatistics(); + System.out.println(MarshalHelper.tryMarshal(result)); + assertEquals(5, result.getSuccessCountFromBegin()); + assertEquals(8, result.getTotalCountFromBegin()); + int countForAllDetail = 0; + for (ScriptBehaviorStatusCodeModel behaviorStatusCodeModel : result + .getList()) { + countForAllDetail += behaviorStatusCodeModel.getCount(); + } + assertEquals(result.getTotalCountFromBegin(), countForAllDetail); + } + + private BehaviorBriefModel buildBehaviorBriefModel(int behaviorId, + String url, int successCount, int totalCount) { + BehaviorBriefModel e = new BehaviorBriefModel(); + e.setBehaviorId(behaviorId); + e.setBehaviorUrl(url); + e.setDetailStatusCodeResultModels(new ArrayList()); + for (int i = 0; i < successCount; i++) { + e.getDetailStatusCodeResultModels().add( + new BehaviorStatusCodeResultModel(1, 200, 1, i + 1, + "text/html", 320, 10, 190, 260, 450)); + } + for (int i = 0; i < totalCount - successCount; i++) { + e.getDetailStatusCodeResultModels().add( + new BehaviorStatusCodeResultModel(1, 400, 1, successCount + + i + 1, "text/html", 320, 10, 190, 260, 450)); + } + e.setSuccessfulCount(successCount); + e.setTotalCount(totalCount); + return e; + } +} diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorStatusStatistics.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorStatusStatistics.java new file mode 100644 index 00000000..fc2b61da --- /dev/null +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorStatusStatistics.java @@ -0,0 +1,11 @@ +package org.bench4q.master.unitTest.datastatistics; + +import org.junit.Test; + +public class Test_BehaviorStatusStatistics { + + @Test + public void test() { + + } +} diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorsBriefStatistics.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorsBriefStatistics.java new file mode 100644 index 00000000..db94625f --- /dev/null +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorsBriefStatistics.java @@ -0,0 +1,50 @@ +package org.bench4q.master.unitTest.datastatistics; + +import java.util.ArrayList; + +import org.bench4q.master.domain.valueobject.datastatistics.impl.BehaviorsBriefStatistics; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorStatusCodeResultModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; +import org.junit.Test; + +public class Test_BehaviorsBriefStatistics { + @Test + public void testAddNull() { + BehaviorsBriefStatistics behaviorsBriefStatistics = new BehaviorsBriefStatistics(); + AgentBehaviorsBriefModel dataUnit = new AgentBehaviorsBriefModel(); + String url = "www.baidu.com"; + dataUnit.getBehaviorBriefModels().add( + buildBehaviorBriefModel(1, url, 2, 3)); + dataUnit.getBehaviorBriefModels().add( + buildBehaviorBriefModel(1, url, 3, 5)); + behaviorsBriefStatistics.add(dataUnit); + ScriptBehaviorsBriefModel result = behaviorsBriefStatistics + .getStatistics(); + System.out.println(MarshalHelper.tryMarshal(result)); + + } + + private BehaviorBriefModel buildBehaviorBriefModel(int behaviorId, + String url, int successCount, int totalCount) { + BehaviorBriefModel e = new BehaviorBriefModel(); + e.setBehaviorId(behaviorId); + e.setBehaviorUrl(url); + e.setDetailStatusCodeResultModels(new ArrayList()); + for (int i = 0; i < successCount; i++) { + e.getDetailStatusCodeResultModels().add( + new BehaviorStatusCodeResultModel(1, 200, 2, 10, + "text/html", 320, 10, 190, 260, 450)); + } + for (int i = 0; i < totalCount - successCount; i++) { + e.getDetailStatusCodeResultModels().add( + new BehaviorStatusCodeResultModel(1, 400, 2, 10, + "text/html", 320, 10, 190, 260, 450)); + } + e.setSuccessfulCount(successCount); + e.setTotalCount(totalCount); + return e; + } +} diff --git a/Bench4Q-Master/src/test/java/stubs/Mock_AgentMessenger.java b/Bench4Q-Master/src/test/java/stubs/Mock_AgentMessenger.java index 40dc2cd6..84bcaff5 100644 --- a/Bench4Q-Master/src/test/java/stubs/Mock_AgentMessenger.java +++ b/Bench4Q-Master/src/test/java/stubs/Mock_AgentMessenger.java @@ -10,7 +10,6 @@ import java.util.concurrent.Future; import org.bench4q.master.domain.entity.Agent; import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.share.enums.master.AgentStatus; -import org.bench4q.share.models.agent.BehaviorBriefModel; import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioResultModel; import org.bench4q.share.models.agent.ServerStatusModel; @@ -20,6 +19,7 @@ import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel; import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel; +import org.bench4q.share.models.agent.statistics.BehaviorBriefModel; import org.springframework.scheduling.annotation.AsyncResult; import org.springframework.stereotype.Component; diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java index 27c55265..cf18f322 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/AgentBehaviorsBriefModel.java @@ -7,8 +7,6 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; -import org.bench4q.share.models.agent.BehaviorBriefModel; - @XmlRootElement(name = "testBehaviorBriefModel") public class AgentBehaviorsBriefModel extends DataStatisticsModel { private List behaviorBriefModels; diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorBriefModel.java new file mode 100644 index 00000000..4189ecb9 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorBriefModel.java @@ -0,0 +1,68 @@ +package org.bench4q.share.models.agent.statistics; + +import java.util.LinkedList; +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "BehaviorBriefModel") +public class BehaviorBriefModel extends DataStatisticsModel { + private int behaviorId; + private String behaviorUrl; + private long totalCount; + private long successfulCount; + private List detailStatusCodeResultModels; + + @XmlElement + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + + @XmlElement + public String getBehaviorUrl() { + return behaviorUrl; + } + + public void setBehaviorUrl(String behaviorUrl) { + this.behaviorUrl = behaviorUrl; + } + + @XmlElementWrapper(name = "detailStatusCodeResultList") + @XmlElement(name = "detailStatusCodeResult") + public List getDetailStatusCodeResultModels() { + return detailStatusCodeResultModels; + } + + public void setDetailStatusCodeResultModels( + List detailStatusCodeResultModels) { + this.detailStatusCodeResultModels = detailStatusCodeResultModels; + } + + @XmlElement + public long getTotalCount() { + return totalCount; + } + + public void setTotalCount(long totalCount) { + this.totalCount = totalCount; + } + + @XmlElement + public long getSuccessfulCount() { + return successfulCount; + } + + public void setSuccessfulCount(long successfulCount) { + this.successfulCount = successfulCount; + } + + public BehaviorBriefModel() { + this.setDetailStatusCodeResultModels(new LinkedList()); + } +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorStatusCodeResultModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorStatusCodeResultModel.java new file mode 100644 index 00000000..18fe2278 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/statistics/BehaviorStatusCodeResultModel.java @@ -0,0 +1,164 @@ +package org.bench4q.share.models.agent.statistics; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "BehaviorStatusCodeResultModel") +public class BehaviorStatusCodeResultModel extends DataStatisticsModel { + private int behaviorId; + private int statusCode; + private long countThisTime; + private long count; + private String contentType; + private long contentLengthThisTime; + private long contentLength; + private long minResponseTime; + private long maxResponseTime; + private long totalResponseTimeThisTime; + + @XmlElement + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + + @XmlElement + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } + + @XmlElement + public long getCount() { + return count; + } + + public void setCount(long count) { + this.count = count; + } + + @XmlElement + public long getCountThisTime() { + return countThisTime; + } + + public void setCountThisTime(long countThisTime) { + this.countThisTime = countThisTime; + } + + @XmlElement + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + @XmlElement + public long getContentLengthThisTime() { + return contentLengthThisTime; + } + + public void setContentLengthThisTime(long contentLengthThisTime) { + this.contentLengthThisTime = contentLengthThisTime; + } + + @XmlElement + public long getContentLength() { + return contentLength; + } + + public void setContentLength(long contentLength) { + this.contentLength = contentLength; + } + + @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 getTotalResponseTimeThisTime() { + return totalResponseTimeThisTime; + } + + public void setTotalResponseTimeThisTime(long totalResponseTimeThisTime) { + this.totalResponseTimeThisTime = totalResponseTimeThisTime; + } + + /** + * It's just the http's success judgement, if we'll turn to other protocal, + * we should build a inheritance system + * + * @return + */ + public boolean isSuccess() { + return this.getStatusCode() == 200; + } + + public BehaviorStatusCodeResultModel getCopy() { + BehaviorStatusCodeResultModel ret = new BehaviorStatusCodeResultModel(); + ret.setStatusCode(getStatusCode()); + ret.setContentLength(getContentLength()); + ret.setContentType(getContentType()); + ret.setCount(getCount()); + ret.setMaxResponseTime(getMaxResponseTime()); + ret.setMinResponseTime(getMinResponseTime()); + ret.setTotalResponseTimeThisTime(getTotalResponseTimeThisTime()); + return ret; + } + + public synchronized void plus(BehaviorStatusCodeResultModel newOne) { + this.setContentLength(this.getContentLength() + + newOne.getContentLength()); + this.setCount(this.getCount() + newOne.getCount()); + this.setTotalResponseTimeThisTime(this.getTotalResponseTimeThisTime() + + newOne.getTotalResponseTimeThisTime()); + if (newOne.getMaxResponseTime() > this.getMaxResponseTime()) { + this.setMaxResponseTime(newOne.getMaxResponseTime()); + } + if (newOne.getMinResponseTime() < this.getMinResponseTime()) { + this.setMinResponseTime(newOne.getMinResponseTime()); + } + } + + public BehaviorStatusCodeResultModel() { + } + + public BehaviorStatusCodeResultModel(int behaviorId, int statusCode, + long countThisTime, long count, String contentType, + long contentLength, long contentLengthThisTime, + long minResponseTime, long maxResponseTime, + long totalResponseTimeThisTime) { + this.behaviorId = behaviorId; + this.statusCode = statusCode; + this.countThisTime = countThisTime; + this.count = count; + this.contentType = contentType; + this.contentLength = contentLength; + this.contentLengthThisTime = contentLengthThisTime; + this.minResponseTime = minResponseTime; + this.maxResponseTime = maxResponseTime; + this.totalResponseTimeThisTime = totalResponseTimeThisTime; + } +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java index 891bfe59..d15e871e 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java @@ -7,18 +7,16 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; - @XmlRootElement public class ScriptBehaviorBriefModel { private int behaviorId; private String BehaviorUrl; - private int successCount; - private int totalCount; - private List list; + private long successCountFromBegin; + private long totalCountFromBegin; + private List list; public ScriptBehaviorBriefModel() { - this.setList(new LinkedList()); + this.setList(new LinkedList()); } @XmlElement @@ -40,30 +38,30 @@ public class ScriptBehaviorBriefModel { } @XmlElement - public int getSuccessCount() { - return successCount; + public long getSuccessCountFromBegin() { + return successCountFromBegin; } - public void setSuccessCount(int successCount) { - this.successCount = successCount; + public void setSuccessCountFromBegin(long successCountFromBegin) { + this.successCountFromBegin = successCountFromBegin; } @XmlElement - public int getTotalCount() { - return totalCount; + public long getTotalCountFromBegin() { + return totalCountFromBegin; } - public void setTotalCount(int totalCount) { - this.totalCount = totalCount; + public void setTotalCountFromBegin(long totalCountFromBegin) { + this.totalCountFromBegin = totalCountFromBegin; } @XmlElementWrapper(name = "statusResults") @XmlElement - public List getList() { + public List getList() { return list; } - public void setList(List list) { + public void setList(List list) { this.list = list; } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java index 74df8a52..4b306647 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorStatusCodeModel.java @@ -1,13 +1,30 @@ package org.bench4q.share.models.master.statistics; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement public class ScriptBehaviorStatusCodeModel { + private int behaviorId; private int statusCode; private long count; private String contentType; private long contentLength; + private long contentLengthThisTime; private long minResponseTime; private long maxResponseTime; + private long averageResponseTime; + @XmlElement + public int getBehaviorId() { + return behaviorId; + } + + public void setBehaviorId(int behaviorId) { + this.behaviorId = behaviorId; + } + + @XmlElement public int getStatusCode() { return statusCode; } @@ -16,6 +33,7 @@ public class ScriptBehaviorStatusCodeModel { this.statusCode = statusCode; } + @XmlElement public long getCount() { return count; } @@ -24,6 +42,7 @@ public class ScriptBehaviorStatusCodeModel { this.count = count; } + @XmlElement public String getContentType() { return contentType; } @@ -32,6 +51,7 @@ public class ScriptBehaviorStatusCodeModel { this.contentType = contentType; } + @XmlElement public long getContentLength() { return contentLength; } @@ -40,6 +60,16 @@ public class ScriptBehaviorStatusCodeModel { this.contentLength = contentLength; } + @XmlElement + public long getContentLengthThisTime() { + return contentLengthThisTime; + } + + public void setContentLengthThisTime(long contentLengthThisTime) { + this.contentLengthThisTime = contentLengthThisTime; + } + + @XmlElement public long getMinResponseTime() { return minResponseTime; } @@ -48,6 +78,7 @@ public class ScriptBehaviorStatusCodeModel { this.minResponseTime = minResponseTime; } + @XmlElement public long getMaxResponseTime() { return maxResponseTime; } @@ -56,4 +87,13 @@ public class ScriptBehaviorStatusCodeModel { this.maxResponseTime = maxResponseTime; } + @XmlElement + public long getAverageResponseTime() { + return averageResponseTime; + } + + public void setAverageResponseTime(long averageResponseTime) { + this.averageResponseTime = averageResponseTime; + } + } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorsBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorsBriefModel.java index a20a2e25..2c3065c1 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorsBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorsBriefModel.java @@ -5,19 +5,19 @@ import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; -import org.bench4q.share.models.agent.BehaviorBriefModel; + @XmlRootElement(name = "scriptBehaviorsBriefModel") public class ScriptBehaviorsBriefModel extends SampleModel { - private List behaviorBriefModels; + private List behaviorBriefModels; @XmlElementWrapper(name = "behaviorBriefList") @XmlElement(name = "behaviorBrief") - public List getBehaviorBriefModels() { + public List getBehaviorBriefModels() { return behaviorBriefModels; } public void setBehaviorBriefModels( - List behaviorBriefModels) { + List behaviorBriefModels) { this.behaviorBriefModels = behaviorBriefModels; } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java index 6530085a..ca5e14c9 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java @@ -15,7 +15,6 @@ import javax.xml.bind.JAXBException; import org.apache.log4j.Logger; import org.bench4q.share.helper.ExceptionLog; import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.ScriptHandleModel; import org.bench4q.share.models.master.TestPlanDBModel; @@ -23,6 +22,7 @@ import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestPlanResponseModel; import org.bench4q.share.models.master.TestPlanResultModel; import org.bench4q.share.models.master.TestPlanScriptBriefResultModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorStatusCodeModel; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel; @@ -129,7 +129,7 @@ public class TestPlanController extends BaseController { Map map = new HashMap(); TestPlanResultModel testPlanResultModel = this.getTestPlanMessager() .getRunningTestInfo(accessToken, testPlanId); - + if (testPlanResultModel == null) { return fail(map, ""); } else { @@ -369,7 +369,7 @@ public class TestPlanController extends BaseController { @PathVariable String testPlanId) { Map map = new HashMap(); - List behaviorStatusCodeResultModels = this + List behaviorStatusCodeResultModels = this .getTestPlanService().getAllBehaviorStatusCodeResultModels( accessToken, testPlanId); if (behaviorStatusCodeResultModels == null) { @@ -377,7 +377,7 @@ public class TestPlanController extends BaseController { return fail(map, ""); } Map urlContentCountMap = new HashMap(); - for (BehaviorStatusCodeResultModel behaviorStatusCodeResultModel : behaviorStatusCodeResultModels) { + for (ScriptBehaviorStatusCodeModel behaviorStatusCodeResultModel : behaviorStatusCodeResultModels) { if (urlContentCountMap.get(behaviorStatusCodeResultModel .getContentType()) == null) urlContentCountMap.put( @@ -419,17 +419,18 @@ public class TestPlanController extends BaseController { return fail(map, testPlanResponseModel.getFailCause()); } } - + @RequestMapping("loadFilterTypeList") - public @ResponseBody - Map loadFilterTypeList(HttpServletRequest request, + public @ResponseBody Map loadFilterTypeList( + HttpServletRequest request, @ModelAttribute("accessToken") String accessToken) { - TestPlanResponseModel responseModel = this.getTestPlanMessager().loadFilterTypeList(accessToken); - Map map = new HashMap(); - if(responseModel.isSuccess()){ + TestPlanResponseModel responseModel = this.getTestPlanMessager() + .loadFilterTypeList(accessToken); + Map map = new HashMap(); + if (responseModel.isSuccess()) { map.put("filterTypeList", responseModel.getFilterTypeList()); success(map); - }else{ + } else { fail(map, responseModel.getFailCause()); } return map; diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java index 4181911c..58db912b 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java @@ -9,8 +9,6 @@ import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; import org.bench4q.share.models.agent.ScriptFilterOptionsModel; import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.RunningScriptModel; @@ -18,6 +16,8 @@ import org.bench4q.share.models.master.ScriptHandleModel; import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestPlanResultModel; import org.bench4q.share.models.master.TestPlanScriptBriefResultModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorBriefModel; +import org.bench4q.share.models.master.statistics.ScriptBehaviorStatusCodeModel; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.bench4q.web.masterMessager.TestPlanMessager; @@ -121,7 +121,7 @@ public class TestPlanService { } - public List getAllBehaviorStatusCodeResultModels( + public List getAllBehaviorStatusCodeResultModels( String accessToken, String testPlanId) { TestPlanResultModel testPlanResultModel = this.getTestPlanMessager() .getRunningTestInfo(accessToken, testPlanId); @@ -134,7 +134,7 @@ public class TestPlanService { if (scriptList == null) { return null; } - List behaviorStatusCodeResultModels = new LinkedList(); + List behaviorStatusCodeResultModels = new LinkedList(); for (ScriptHandleModel scriptHandleModel : scriptList) { ScriptBehaviorsBriefModel scriptBehaviorBriefModel = this .getTestPlanMessager().getScriptBehaviorsBriefResult( @@ -147,19 +147,19 @@ public class TestPlanService { } - private List extractBehaviorStatusCodeResultModels( + private List extractBehaviorStatusCodeResultModels( ScriptBehaviorsBriefModel scriptBehaviorsBriefModel) { - List behaviorStatusCodeResultModels = new ArrayList(); + List behaviorStatusCodeResultModels = new ArrayList(); if (scriptBehaviorsBriefModel == null || scriptBehaviorsBriefModel.getBehaviorBriefModels() == null) { return behaviorStatusCodeResultModels; } - for (BehaviorBriefModel behaviorBriefModel : scriptBehaviorsBriefModel + for (ScriptBehaviorBriefModel behaviorBriefModel : scriptBehaviorsBriefModel .getBehaviorBriefModels()) { - if (behaviorBriefModel.getDetailStatusCodeResultModels() != null) + if (behaviorBriefModel.getList() != null) behaviorStatusCodeResultModels.addAll(behaviorBriefModel - .getDetailStatusCodeResultModels()); + .getList()); } return behaviorStatusCodeResultModels; } From 030794d5cad81e1b7b3a1ff3e3419256db1bd155 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Mon, 15 Sep 2014 15:08:56 +0800 Subject: [PATCH 7/8] refactor refactor --- .../BehaviorsBriefStatisticsTest.java | 30 ---- .../models/agent/BehaviorBriefModel.java | 70 --------- .../agent/BehaviorStatusCodeResultModel.java | 138 ------------------ 3 files changed, 238 deletions(-) delete mode 100644 Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java delete mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java delete mode 100644 Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java deleted file mode 100644 index 1e5b3b12..00000000 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/BehaviorsBriefStatisticsTest.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.bench4q.master.unitTest.datastatistics; - -import java.util.Arrays; - -import org.bench4q.master.domain.valueobject.datastatistics.impl.BehaviorsBriefStatistics; -import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.agent.BehaviorBriefModel; -import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel; -import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel; -import org.junit.Test; - -public class BehaviorsBriefStatisticsTest { - @Test - public void testAddNull() { - BehaviorsBriefStatistics behaviorsBriefStatistics = new BehaviorsBriefStatistics(); - AgentBehaviorsBriefModel dataUnit = new AgentBehaviorsBriefModel(); - BehaviorBriefModel e = new BehaviorBriefModel(); - e.setBehaviorId(1); - e.setBehaviorUrl("www.baidu.com"); - e.setDetailStatusCodeResultModels(Arrays - .asList(new BehaviorStatusCodeResultModel())); - e.setSuccessfulCount(2); - e.setTotalCount(3); - dataUnit.getBehaviorBriefModels().add(e); - - behaviorsBriefStatistics.add(dataUnit); - System.out.println(MarshalHelper.tryMarshal(behaviorsBriefStatistics - .getStatistics())); - } -} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java deleted file mode 100644 index 5ae14572..00000000 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorBriefModel.java +++ /dev/null @@ -1,70 +0,0 @@ -package org.bench4q.share.models.agent; - -import java.util.LinkedList; -import java.util.List; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; -import javax.xml.bind.annotation.XmlRootElement; - -import org.bench4q.share.models.agent.statistics.DataStatisticsModel; - -@XmlRootElement(name = "BehaviorBriefModel") -public class BehaviorBriefModel extends DataStatisticsModel { - private int behaviorId; - private String behaviorUrl; - private long totalCount; - private long successfulCount; - private List detailStatusCodeResultModels; - - @XmlElement - public int getBehaviorId() { - return behaviorId; - } - - public void setBehaviorId(int behaviorId) { - this.behaviorId = behaviorId; - } - - @XmlElement - public String getBehaviorUrl() { - return behaviorUrl; - } - - public void setBehaviorUrl(String behaviorUrl) { - this.behaviorUrl = behaviorUrl; - } - - @XmlElementWrapper(name = "detailStatusCodeResultList") - @XmlElement(name = "detailStatusCodeResult") - public List getDetailStatusCodeResultModels() { - return detailStatusCodeResultModels; - } - - public void setDetailStatusCodeResultModels( - List detailStatusCodeResultModels) { - this.detailStatusCodeResultModels = detailStatusCodeResultModels; - } - - @XmlElement - public long getTotalCount() { - return totalCount; - } - - public void setTotalCount(long totalCount) { - this.totalCount = totalCount; - } - - @XmlElement - public long getSuccessfulCount() { - return successfulCount; - } - - public void setSuccessfulCount(long successfulCount) { - this.successfulCount = successfulCount; - } - - public BehaviorBriefModel() { - this.setDetailStatusCodeResultModels(new LinkedList()); - } -} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java deleted file mode 100644 index 33f4a37a..00000000 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/BehaviorStatusCodeResultModel.java +++ /dev/null @@ -1,138 +0,0 @@ -package org.bench4q.share.models.agent; - -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; - -@XmlRootElement(name = "BehaviorStatusCodeResultModel") -public class BehaviorStatusCodeResultModel { - private int behaviorId; - private int statusCode; - private long countThisTime; - private long count; - private String contentType; - private long contentLength; - private long minResponseTime; - private long maxResponseTime; - private long totalResponseTimeThisTime; - - @XmlElement - public int getBehaviorId() { - return behaviorId; - } - - public void setBehaviorId(int behaviorId) { - this.behaviorId = behaviorId; - } - - @XmlElement - public int getStatusCode() { - return statusCode; - } - - public void setStatusCode(int statusCode) { - this.statusCode = statusCode; - } - - @XmlElement - public long getCount() { - return count; - } - - public void setCount(long count) { - this.count = count; - } - - @XmlElement - public long getCountThisTime() { - return countThisTime; - } - - public void setCountThisTime(long countThisTime) { - this.countThisTime = countThisTime; - } - - @XmlElement - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - @XmlElement - public long getContentLength() { - return contentLength; - } - - public void setContentLength(long contentLength) { - this.contentLength = contentLength; - } - - @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 getTotalResponseTimeThisTime() { - return totalResponseTimeThisTime; - } - - public void setTotalResponseTimeThisTime(long totalResponseTimeThisTime) { - this.totalResponseTimeThisTime = totalResponseTimeThisTime; - } - - /** - * It's just the http's success judgement, if we'll turn to other protocal, - * we should build a inheritance system - * - * @return - */ - public boolean isSuccess() { - return this.getStatusCode() == 200; - } - - public BehaviorStatusCodeResultModel getCopy() { - BehaviorStatusCodeResultModel ret = new BehaviorStatusCodeResultModel(); - ret.setStatusCode(getStatusCode()); - ret.setContentLength(getContentLength()); - ret.setContentType(getContentType()); - ret.setCount(getCount()); - ret.setMaxResponseTime(getMaxResponseTime()); - ret.setMinResponseTime(getMinResponseTime()); - ret.setTotalResponseTimeThisTime(getTotalResponseTimeThisTime()); - return ret; - } - - public synchronized void plus(BehaviorStatusCodeResultModel newOne) { - this.setContentLength(this.getContentLength() - + newOne.getContentLength()); - this.setCount(this.getCount() + newOne.getCount()); - this.setTotalResponseTimeThisTime(this.getTotalResponseTimeThisTime() - + newOne.getTotalResponseTimeThisTime()); - if (newOne.getMaxResponseTime() > this.getMaxResponseTime()) { - this.setMaxResponseTime(newOne.getMaxResponseTime()); - } - if (newOne.getMinResponseTime() < this.getMinResponseTime()) { - this.setMinResponseTime(newOne.getMinResponseTime()); - } - } - - public BehaviorStatusCodeResultModel() { - } - -} From 226e9ec52210eee22ad6e19236f7563e5fa801c0 Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Mon, 15 Sep 2014 16:25:29 +0800 Subject: [PATCH 8/8] refactor refactor --- .../master/api/TestPlanController.java | 16 ++-- .../impl/BehaviorBriefStatistics.java | 2 +- .../Test_BehaviorBriefStatistics.java | 2 +- .../statistics/ScriptBehaviorBriefModel.java | 12 +-- .../bench4q/web/api/TestPlanController.java | 1 + .../bench4q/web/service/TestPlanService.java | 5 +- .../script/testResult/behaviorResult.js | 73 +++++++++++-------- 7 files changed, 60 insertions(+), 51 deletions(-) diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/api/TestPlanController.java b/Bench4Q-Master/src/main/java/org/bench4q/master/api/TestPlanController.java index 993dfb11..4b190735 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/api/TestPlanController.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/api/TestPlanController.java @@ -174,10 +174,10 @@ public class TestPlanController extends BaseController { public ScriptBehaviorsBriefModel getBehaviorsBrief( @PathVariable UUID testPlanRunID, @PathVariable int scriptId) throws Bench4QException, NullPointerException { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - throw new Bench4QException(HAVE_NO_POWER, EXCEPTION_HAPPEND - + "when get behaviors's brief", "/getBehaviorsBrief"); - } + // if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { + // throw new Bench4QException(HAVE_NO_POWER, EXCEPTION_HAPPEND + // + "when get behaviors's brief", "/getBehaviorsBrief"); + // } ScriptBehaviorsBriefModel result = this .getTestPlanScriptResultService() .getLatestScriptBehaviorsBrief(testPlanRunID, scriptId); @@ -189,10 +189,10 @@ public class TestPlanController extends BaseController { public ScriptPagesBriefModel getPagesBrief( @PathVariable UUID testPlanRunId, @PathVariable int scriptId) throws Bench4QException { - if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { - throw new Bench4QException(HAVE_NO_POWER, EXCEPTION_HAPPEND - + "when get behaviors's brief", "/getBehaviorsBrief"); - } + // if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { + // throw new Bench4QException(HAVE_NO_POWER, EXCEPTION_HAPPEND + // + "when get behaviors's brief", "/getBehaviorsBrief"); + // } ScriptPagesBriefModel pagesBriefModel = this .getTestPlanScriptResultService().getLatestScriptPagesBrief( testPlanRunId, scriptId); diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java index 1d545578..2b79687a 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/valueobject/datastatistics/impl/BehaviorBriefStatistics.java @@ -65,7 +65,7 @@ public class BehaviorBriefStatistics extends ScriptStatistics { result.setTotalCountFromBegin(this.totalCountFromBegin); for (BehaviorStatusCodeStatistics item : this.detailStatisticsMap .values()) { - result.getList().add( + result.getDetailStatusList().add( (ScriptBehaviorStatusCodeModel) item.getStatistics()); } return result; diff --git a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java index 5ecfa58f..395d46df 100644 --- a/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java +++ b/Bench4Q-Master/src/test/java/org/bench4q/master/unitTest/datastatistics/Test_BehaviorBriefStatistics.java @@ -29,7 +29,7 @@ public class Test_BehaviorBriefStatistics { assertEquals(8, result.getTotalCountFromBegin()); int countForAllDetail = 0; for (ScriptBehaviorStatusCodeModel behaviorStatusCodeModel : result - .getList()) { + .getDetailStatusList()) { countForAllDetail += behaviorStatusCodeModel.getCount(); } assertEquals(result.getTotalCountFromBegin(), countForAllDetail); diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java index d15e871e..99e96318 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/statistics/ScriptBehaviorBriefModel.java @@ -13,10 +13,10 @@ public class ScriptBehaviorBriefModel { private String BehaviorUrl; private long successCountFromBegin; private long totalCountFromBegin; - private List list; + private List detailStatusList; public ScriptBehaviorBriefModel() { - this.setList(new LinkedList()); + this.setDetailStatusList(new LinkedList()); } @XmlElement @@ -57,12 +57,12 @@ public class ScriptBehaviorBriefModel { @XmlElementWrapper(name = "statusResults") @XmlElement - public List getList() { - return list; + public List getDetailStatusList() { + return detailStatusList; } - public void setList(List list) { - this.list = list; + public void setDetailStatusList(List list) { + this.detailStatusList = list; } } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java index ca5e14c9..8c4ebe52 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/TestPlanController.java @@ -289,6 +289,7 @@ public class TestPlanController extends BaseController { return fail(map, ""); } map = success(map); + System.out.println(MarshalHelper.tryMarshal(scriptBehaviorsBriefModel)); map.put("behaviors", scriptBehaviorsBriefModel); return map; } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java index 58db912b..e5074c74 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java @@ -156,10 +156,9 @@ public class TestPlanService { } for (ScriptBehaviorBriefModel behaviorBriefModel : scriptBehaviorsBriefModel .getBehaviorBriefModels()) { - - if (behaviorBriefModel.getList() != null) + if (behaviorBriefModel.getDetailStatusList() != null) behaviorStatusCodeResultModels.addAll(behaviorBriefModel - .getList()); + .getDetailStatusList()); } return behaviorStatusCodeResultModels; } diff --git a/Bench4Q-Web/src/main/webapp/script/testResult/behaviorResult.js b/Bench4Q-Web/src/main/webapp/script/testResult/behaviorResult.js index 612ecf7e..5c519094 100644 --- a/Bench4Q-Web/src/main/webapp/script/testResult/behaviorResult.js +++ b/Bench4Q-Web/src/main/webapp/script/testResult/behaviorResult.js @@ -1,37 +1,37 @@ function Behavior() { this.table = $("#url").DataTable(); this.table.column(5).visible(false); - this.getBehaviorResultsFirstly = function(testPlanId, map,scripts) { + this.getBehaviorResultsFirstly = function(testPlanId, map, scripts) { + + for (var i = 0; i < scripts.length; i++) { + var url = testPlanId + "/" + scripts[i].scriptId + "/behaviors"; + + this.getBehaviorResult(url, map, scripts[i].scriptName, this.table); + } - for(var i=0;i"); table.addClass("innerTable"); $(table).attr("style", "width: 100%;"); var thead = $(""); var tr = $(""); - for ( var i = 0; i < theader.length; i++) { + for (var i = 0; i < theader.length; i++) { var th = $("" + theader[i] + ""); th.attr("style", "width:10% ;"); tr.append(th); @@ -96,7 +102,7 @@ function Behavior() { table.append(thead); var tbody = $(""); table.append(tbody); - for ( var i = 0; i < detailDatas.length; i++) { + for (var i = 0; i < detailDatas.length; i++) { var tr = this.createDetailRow(detailDatas[i], url, theader) tbody.append(tr); @@ -106,9 +112,12 @@ function Behavior() { this.createDetailRow = function(detailData, url, theader) { var tr = $(""); - var dataArray = [url,detailData.statusCode,detailData.count,detailData.contentLength,detailData.minResponseTime,detailData.maxResponseTime,detailData.totalResponseTimeThisTime]; + var dataArray = [ url, detailData.statusCode, detailData.count, + detailData.contentLength, detailData.minResponseTime, + detailData.maxResponseTime, + detailData.totalResponseTimeThisTime ]; dataArray[0] = url; - for ( var i = 0; i < dataArray.length; i++) { + for (var i = 0; i < dataArray.length; i++) { var td = $(""); td.attr("style", "width: 10%;"); td.html(dataArray[i]);