From bed297998040f28d8432b271e0cbcb80b6095d6e Mon Sep 17 00:00:00 2001 From: coderfengyun Date: Tue, 2 Sep 2014 17:16:11 +0800 Subject: [PATCH] remove all bugs remove all bugs --- .../scenario/engine/ScenarioContext.java | 28 +++++++++++--- .../agent/scenario/engine/Schedule.java | 37 +++++++++++++------ .../bench4q/agent/scenario/engine/VUser.java | 2 +- .../test/scenario/Test_ScenarioContext.java | 1 + .../share/communication/HttpRequester.java | 1 - 5 files changed, 49 insertions(+), 20 deletions(-) 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 a847f3fb..d7156598 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 @@ -81,7 +81,7 @@ public class ScenarioContext implements Observer { return pluginManager; } - Schedule getSchedule() { + public Schedule getSchedule() { return schedule; } @@ -105,7 +105,8 @@ public class ScenarioContext implements Observer { public ScenarioContext addScenrio(final Scenario scenario, Schedule schedule, final long realStartTime) { ScenarioContext result = new ScenarioContext(this.testId, new Date( - realStartTime), executor, this.dataCollector, pluginManager); + realStartTime), this.executor, this.dataCollector, + this.pluginManager); result.setSchedule(schedule); result.setEndDate(new Date(result.getSchedule().getScheduleRange() + result.getStartDate().getTime())); @@ -131,7 +132,7 @@ public class ScenarioContext implements Observer { this.getExecutor().setMaximumPoolSize(requiredLoad); } - public void addTask() { + public void addATask() { if (this.isFinished()) { return; } @@ -142,9 +143,10 @@ public class ScenarioContext implements Observer { public void initTasks(int currentLoad) { this.updatePopulation(currentLoad); - for (int i = 0; i < currentLoad; i++) { - addTask(); + for (int i = 0; i < this.getSchedule().getMaxLoad() + 2; i++) { + addATask(); } + addATask(); } @Override @@ -154,17 +156,31 @@ public class ScenarioContext implements Observer { if (schedule.hasReachEnd()) { stop(); } else { + fillTaskIfRequired(); this.updatePopulation((Integer) arg); + Logger.getLogger(this.getClass()).info( + "Active thread : " + + this.getExecutor().getActiveCount()); } } catch (Exception e) { e.printStackTrace(); } } + private void fillTaskIfRequired() { + int taskInPool = this.getExecutor().getPoolSize() + - (this.getExecutor().getActiveCount() + this.getExecutor() + .getQueue().size()); + if (taskInPool > 0) { + for (int i = 0; i < taskInPool; i++) { + addATask(); + } + } + } + public void stop() { this.getSchedule().stop(); this.setFinished(true); - this.setEndDate(new Date()); this.getExecutor().shutdownNow(); } } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java index 3b3c195b..0d6edadb 100644 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/Schedule.java @@ -8,6 +8,7 @@ import java.util.Observable; import java.util.Timer; import java.util.TimerTask; +import org.apache.log4j.Logger; import org.bench4q.share.exception.Bench4QRunTimeException; import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel; @@ -19,13 +20,19 @@ public class Schedule extends Observable { private static final int SCHEDULE_CYCLE = 3000; private final List segments; private final long beginTime; - private final Timer timer = new Timer(); + private final Timer timer; + private final int maxLoad; private volatile boolean reachEnd; + private final Logger logger = Logger.getLogger(Schedule.class); public List getSegments() { return segments; } + public int getMaxLoad() { + return maxLoad; + } + public boolean hasReachEnd() { return this.reachEnd; } @@ -34,7 +41,7 @@ public class Schedule extends Observable { this.reachEnd = true; } - public Schedule(List segments) { + public Schedule(List segments, int maxLoad) { if (segments == null || segments.size() == 0) { throw new Bench4QRunTimeException( "Can't init a schedul with zero segment"); @@ -42,6 +49,8 @@ public class Schedule extends Observable { this.segments = segments; this.beginTime = System.currentTimeMillis(); this.reachEnd = false; + this.maxLoad = maxLoad; + this.timer = new Timer(); } public long getScheduleRange() { @@ -58,11 +67,14 @@ public class Schedule extends Observable { if (segment == null) { // exceed the range of execute, should let the context stop // the test - System.out.println("Execution Over!"); + logger.info("Execution Over!"); notifyObservers(0); return; } - notifyObservers(segment.loadFor(time - beginTime)); + int load = segment.loadFor(time - beginTime); + logger.info("Time: " + time + " ; Load: " + load); + Schedule.this.setChanged(); + notifyObservers(load); } }, 0, SCHEDULE_CYCLE); } @@ -176,29 +188,30 @@ public class Schedule extends Observable { } public static Schedule build(ScheduleModel scheduleModel) { - Schedule schedule = new Schedule( - extractSegments(scheduleModel.getPoints())); + List segements = new LinkedList(); + int maxLoad = extractSegmentsAndGetMaxLoad(scheduleModel.getPoints(), + segements); + Schedule schedule = new Schedule(segements, maxLoad); return schedule; } - private static List extractSegments(List pointModels) { + private static int extractSegmentsAndGetMaxLoad( + List pointModels, List result) { + int maxLoad = 0; List points = new LinkedList(); for (PointModel model : pointModels) { + maxLoad = Math.max(maxLoad, model.getLoad()); points.add(new Point(model.getTime(), model.getLoad())); } Collections.sort(points, new Comparator() { - @Override public int compare(Point o1, Point o2) { return (int) (o1.getTime() - o2.getTime()); } - }); - List result = new LinkedList(); for (int i = 0; i < points.size() - 1; i++) { result.add(new Segment(points.get(i), points.get(i + 1))); } - return result; + return maxLoad; } - } 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 4d158553..51c709de 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 @@ -63,7 +63,7 @@ public class VUser implements Runnable { } private void doCleanUp() { - this.getScenarioContext().addTask(); + this.getScenarioContext().addATask(); this.setScenarioContext(null); } 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 64f4cbff..e23490ac 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 @@ -40,6 +40,7 @@ public class Test_ScenarioContext extends TestBase { assertNotNull(scenarioContext.getScenario()); assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000, scenarioContext.getEndDate().getTime()); + assertTrue(scenarioContext.getSchedule().countObservers() > 0); } private ScenarioContext getScenarioWithScenarioAndSchedule() { diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/communication/HttpRequester.java b/Bench4Q-Share/src/main/java/org/bench4q/share/communication/HttpRequester.java index d331ffcf..e93c3d14 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/communication/HttpRequester.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/communication/HttpRequester.java @@ -9,7 +9,6 @@ import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; -import java.nio.charset.Charset; import java.util.HashMap; import java.util.LinkedList; import java.util.List;