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 98256f93..aedbd97e 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 @@ -16,7 +16,7 @@ public class Scenario { private UsePlugin[] usePlugins; private Page[] pages; private List behaviors; - private TestSchedule schedule; + private Schedule schedule; public UsePlugin[] getUsePlugins() { return usePlugins; @@ -42,11 +42,11 @@ public class Scenario { this.behaviors = behaviors; } - public TestSchedule getSchedule() { + public Schedule getSchedule() { return schedule; } - private void setSchedule(TestSchedule schedule) { + private void setSchedule(Schedule schedule) { this.schedule = schedule; } @@ -97,7 +97,7 @@ public class Scenario { scenario.setPages(new Page[runScenarioModel.getPages().size()]); extractUsePlugins(runScenarioModel, scenario); extractPages(runScenarioModel, scenario); - scenario.setSchedule(TestSchedule.build(runScenarioModel.getScheduleModel())); + scenario.setSchedule(Schedule.build(runScenarioModel.getScheduleModel())); return scenario; } diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Schedule.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Schedule.java new file mode 100644 index 00000000..fdc8a4f6 --- /dev/null +++ b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/Schedule.java @@ -0,0 +1,131 @@ +package org.bench4q.agent.scenario; + +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import java.util.Observable; +import java.util.Timer; +import java.util.TimerTask; + +import org.bench4q.share.exception.Bench4QRunTimeException; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel; + +public class Schedule extends Observable { + private static final int SCHEDULE_CYCLE = 3000; + private final List segments; + private final long beginTime; + + public List getSegments() { + return segments; + } + + public Schedule(List segments){ + if (segments == null || segments.size() == 0) { + throw new Bench4QRunTimeException("Can't init a schedul with zero segment"); + } + this.segments = segments; + this.beginTime = System.currentTimeMillis(); + beginSchedul(); + + } + + private void beginSchedul() { + Timer timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + @Override + public void run() { + long time = System.currentTimeMillis(); + Segment segment = getSegment(time); + if (segment == null) { + //exceed the range of execute, should let the context stop the test + return; + } + segment.loadFor(time - beginTime); + } + }, 0, SCHEDULE_CYCLE); + } + + //get the segment by binary search + protected Segment getSegment(long time) { + + return null; + } + + public static class Segment { + private final Point start; + private final Point end; + // growthUnit is the amount that'll increase per second + private final float growthUnit; + + public Segment(Point startPoint, Point endPoint) { + this.start = startPoint.copy(); + this.end = endPoint.copy(); + long timeDifference = this.end.getTime() / 1000 - this.start.getTime() / 1000; + if (timeDifference < 0 || this.start.getTime() < 0l || this.end.getTime() < 0l) { + throw new Bench4QRunTimeException("The end time in TestScehdul cannot be less than start time"); + } + this.growthUnit = (float) (timeDifference == 0 ? 0 : (double)(this.end.getLoad() - this.start.getLoad()) + / (double)(timeDifference)); + } + + public int loadFor(long timeFromBegin){ + if (timeFromBegin < this.start.getTime() || timeFromBegin > this.end.getTime()) { + throw new IllegalArgumentException(); + } + long diffFromStart = timeFromBegin - this.start.getTime(); + return (int) (this.start.getLoad() + (diffFromStart * growthUnit / 1000)); + } + } + + public static class Point { + //This time is the relative value from begin + private final long time; + private final int load; + + public long getTime() { + return time; + } + + public int getLoad() { + return load; + } + + public Point(long time, int load) { + this.time = time; + this.load = load; + } + + public Point copy() { + return new Point(this.getTime(), this.getLoad()); + } + } + + public static Schedule build(ScheduleModel scheduleModel) { + Schedule schedule = new Schedule(extractSegments(scheduleModel.getPoints())); + + return schedule; + } + + private static List extractSegments(List pointModels) { + List points = new LinkedList(); + for (PointModel model : pointModels) { + 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; + } + +} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/TestSchedule.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/TestSchedule.java deleted file mode 100644 index d85d10b9..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/TestSchedule.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.bench4q.agent.scenario; - -import java.util.LinkedList; -import java.util.List; - -import org.bench4q.share.models.agent.scriptrecord.TestScheduleModel; -import org.bench4q.share.models.agent.scriptrecord.TestScheduleModel.PointModel; - -public class TestSchedule { - private List points; - - public List getPoints() { - return points; - } - - public void setPoints(List points) { - this.points = points; - } - - public static class Segment { - private final Point start; - private final Point end; - private final int growthUnit; - - public Segment(Point startPoint, Point endPoint) { - this.start = new Point(startPoint.time, startPoint.load); - } - } - - public static class Point { - private final int time; - private final int load; - - public int getTime() { - return time; - } - - public int getLoad() { - return load; - } - - public Point(int time, int load) { - // TODO Auto-generated constructor stub - this.time = time; - this.load = load; - } - } - - public static TestSchedule build(TestScheduleModel scheduleModel) { - TestSchedule schedule = new TestSchedule(); -// schedule.setPoints(extractPoints(scheduleModel.getPoints())); - return null; - } - - private static List extractPoints(List points) { - List result = new LinkedList(); - for (PointModel model : points) { - result.add(new Point(model.getTime(), model.getLoad())); - } - return result; - } - -} diff --git a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java b/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java deleted file mode 100644 index 3f4adc0a..00000000 --- a/Bench4Q-Agent/src/main/java/org/bench4q/agent/scenario/engine/TaskMonitor.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.bench4q.agent.scenario.engine; - -public class TaskMonitor { - - public static class TaskProducer implements Runnable { - private ScenarioContext context; - - public TaskProducer(ScenarioContext context) { - this.context = context; - } - - @Override - public void run() { - int poolSize = context.getExecutor().getCorePoolSize(); - try { - if (context.getExecutor().getTaskCount() > poolSize * 2) { - this.wait(); - } - } catch (Exception e) { - - } finally { - - } - } - } -} diff --git a/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/engine/Test_Shedule.java b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/engine/Test_Shedule.java new file mode 100644 index 00000000..4254dedf --- /dev/null +++ b/Bench4Q-Agent/src/test/java/org/bench4q/agent/test/scenario/engine/Test_Shedule.java @@ -0,0 +1,75 @@ +package org.bench4q.agent.test.scenario.engine; + +import static org.junit.Assert.*; + +import java.util.Date; +import org.bench4q.agent.scenario.Schedule; +import org.bench4q.share.exception.Bench4QRunTimeException; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel; +import org.junit.Test; + +public class Test_Shedule { + + @Test(expected = Bench4QRunTimeException.class) + public void test_InitWithZeroPointModels(){ + Schedule.build(new ScheduleModel()); + } + + @Test(expected = Bench4QRunTimeException.class) + public void test_InitWithOnePointModels(){ + ScheduleModel model = new ScheduleModel(); + model.getPoints().add(new PointModel(new Date().getTime(), 100)); + Schedule.build(model); + } + + @Test + public void test_InitWithTwoPoint_WhereSecondTime_LT_FirstTime(){ + ScheduleModel model = new ScheduleModel(); + long firstTime = new Date().getTime(); + model.getPoints().add(new PointModel(firstTime, 100)); + model.getPoints().add(new PointModel(firstTime - 1000000, 50)); + Schedule schedule = Schedule.build(model); + assertEquals(1, schedule.getSegments().size()); + } + + @Test + public void test_InitWith5Point(){ + long firstTime = new Date().getTime(); + ScheduleModel model = new ScheduleModel(); + for (int i = 0; i < 5; i++) { + model.getPoints().add(new PointModel(firstTime - 10000 * i, 190 + 10 * i)); + } + model.getPoints().add(new PointModel(new Date().getTime(), 600)); + Schedule schedule = Schedule.build(model); + assertEquals(5, schedule.getSegments().size()); + } + + @Test(expected = Bench4QRunTimeException.class) + public void test_InitWith_MinusTime(){ + long firstTime = -1000000000; + ScheduleModel model = new ScheduleModel(); + model.getPoints().add(new PointModel(firstTime, 100)); + Schedule.build(model); + } + + @Test(expected = IllegalArgumentException.class) + public void test_LoadFor_WithIlegalArgument(){ + ScheduleModel model = new ScheduleModel(); + long firstTime = new Date().getTime(); + model.getPoints().add(new PointModel(firstTime, 100)); + model.getPoints().add(new PointModel(firstTime - 1000000, 50)); + Schedule schedule = Schedule.build(model); + schedule.getSegments().get(0).loadFor(-1000); + } + + @Test + public void test_loadFor(){ + ScheduleModel model = new ScheduleModel(); + model.getPoints().add(new PointModel(1000000, 100)); + model.getPoints().add(new PointModel(0, 50)); + Schedule schedule = Schedule.build(model); + int load = schedule.getSegments().get(0).loadFor(500 * 1000); + assertEquals(75, load); + } +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/RunScenarioModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/RunScenarioModel.java index 6fd072d1..4087c576 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/RunScenarioModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/RunScenarioModel.java @@ -8,7 +8,7 @@ import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import org.bench4q.share.models.agent.scriptrecord.PageModel; -import org.bench4q.share.models.agent.scriptrecord.TestScheduleModel; +import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; import org.bench4q.share.models.agent.scriptrecord.UsePluginModel; @XmlRootElement(name = "runScenario") @@ -16,7 +16,7 @@ public class RunScenarioModel { private int poolSize; private List usePlugins; private List pages; - private TestScheduleModel scheduleModel; + private ScheduleModel scheduleModel; @XmlElement public int getPoolSize() { @@ -48,11 +48,11 @@ public class RunScenarioModel { } @XmlElement - public TestScheduleModel getScheduleModel() { + public ScheduleModel getScheduleModel() { return scheduleModel; } - public void setScheduleModel(TestScheduleModel scheduleModel) { + public void setScheduleModel(ScheduleModel scheduleModel) { this.scheduleModel = scheduleModel; } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/TestScheduleModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ScheduleModel.java similarity index 76% rename from Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/TestScheduleModel.java rename to Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ScheduleModel.java index b33ab412..f8f0a3b3 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/TestScheduleModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/agent/scriptrecord/ScheduleModel.java @@ -8,7 +8,7 @@ import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement -public class TestScheduleModel { +public class ScheduleModel { private List points; @XmlElementWrapper(name = "points") @@ -21,21 +21,21 @@ public class TestScheduleModel { this.points = points; } - public TestScheduleModel() { + public ScheduleModel() { this.points = new LinkedList(); } @XmlRootElement public static class PointModel { - private int time; + private long time; private int load; @XmlElement - public int getTime() { + public long getTime() { return time; } - public void setTime(int time) { + public void setTime(long time) { this.time = time; } @@ -48,5 +48,10 @@ public class TestScheduleModel { this.load = load; } + public PointModel(){} + public PointModel(long time, int load){ + this.time = time; + this.load = load; + } } }