add test schedule to agent

add tests and pass them
This commit is contained in:
coderfengyun 2014-09-01 16:06:47 +08:00
parent 37622fc0ee
commit 167d8f4934
7 changed files with 224 additions and 102 deletions

View File

@ -16,7 +16,7 @@ public class Scenario {
private UsePlugin[] usePlugins;
private Page[] pages;
private List<Behavior> 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;
}

View File

@ -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<Segment> segments;
private final long beginTime;
public List<Segment> getSegments() {
return segments;
}
public Schedule(List<Segment> 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<Segment> extractSegments(List<PointModel> pointModels) {
List<Point> points = new LinkedList<Schedule.Point>();
for (PointModel model : pointModels) {
points.add(new Point(model.getTime(), model.getLoad()));
}
Collections.sort(points, new Comparator<Point>() {
@Override
public int compare(Point o1, Point o2) {
return (int) (o1.getTime() - o2.getTime());
}
});
List<Segment> result = new LinkedList<Schedule.Segment>();
for (int i = 0; i < points.size() - 1; i++) {
result.add(new Segment(points.get(i), points.get(i + 1)));
}
return result;
}
}

View File

@ -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<Segment> points;
public List<Segment> getPoints() {
return points;
}
public void setPoints(List<Segment> 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<Point> extractPoints(List<PointModel> points) {
List<Point> result = new LinkedList<TestSchedule.Point>();
for (PointModel model : points) {
result.add(new Point(model.getTime(), model.getLoad()));
}
return result;
}
}

View File

@ -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 {
}
}
}
}

View File

@ -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);
}
}

View File

@ -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<UsePluginModel> usePlugins;
private List<PageModel> 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;
}

View File

@ -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<PointModel> points;
@XmlElementWrapper(name = "points")
@ -21,21 +21,21 @@ public class TestScheduleModel {
this.points = points;
}
public TestScheduleModel() {
public ScheduleModel() {
this.points = new LinkedList<PointModel>();
}
@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;
}
}
}