This commit is contained in:
daisyonly 2014-09-04 17:37:50 +08:00
commit 8bf1705460
9 changed files with 81 additions and 67 deletions

View File

@ -108,7 +108,7 @@ public class ScenarioContext implements Observer {
realStartTime), this.executor, this.dataCollector, realStartTime), this.executor, this.dataCollector,
this.pluginManager); this.pluginManager);
result.setSchedule(schedule); result.setSchedule(schedule);
result.setEndDate(new Date(result.getSchedule().getScheduleRange() result.setEndDate(new Date(result.getSchedule().getScheduleRangeInMilliSecond()
+ result.getStartDate().getTime())); + result.getStartDate().getTime()));
result.setFinished(this.isFinished()); result.setFinished(this.isFinished());
result.setScenario(scenario); result.setScenario(scenario);

View File

@ -53,9 +53,10 @@ public class Schedule extends Observable {
this.timer = new Timer(); this.timer = new Timer();
} }
public long getScheduleRange() { public long getScheduleRangeInMilliSecond() {
return this.getSegments().get(this.getSegments().size() - 1).end return this.getSegments().get(this.getSegments().size() - 1).end
.getTime() - this.getSegments().get(0).start.getTime(); .getTimeInMilliSecond()
- this.getSegments().get(0).start.getTimeInMilliSecond();
} }
public void begin() { public void begin() {
@ -85,27 +86,29 @@ public class Schedule extends Observable {
/** /**
* *
* @param time * @param timeInMilliSecond
* , is the relative time from begin * , is the relative time from begin
* @return get the segment by binary search * @return get the segment by binary search
*/ */
public Segment getSegment(long time) { public Segment getSegment(long timeInMilliSecond) {
if (this.getSegments() == null || this.getSegments().size() < 1 if (this.getSegments() == null
|| time < this.getSegments().get(0).start.getTime()) { || this.getSegments().size() < 1
|| timeInMilliSecond < this.getSegments().get(0).start
.getTimeInMilliSecond()) {
throw new Bench4QRunTimeException( throw new Bench4QRunTimeException(
"can't getSegment when segments' size is LT 2"); "can't getSegment when segments' size is LT 2");
} }
if (time >= this.getSegments().get(this.getSegments().size() - 1).end if (timeInMilliSecond >= this.getSegments().get(
.getTime()) { this.getSegments().size() - 1).end.getTimeInMilliSecond()) {
this.reachEnd(); this.reachEnd();
return null; return null;
} }
int begin = 0, end = this.getSegments().size(), mid = (begin + end) / 2; int begin = 0, end = this.getSegments().size(), mid = (begin + end) / 2;
while (begin <= end) { while (begin <= end) {
Segment midSegment = this.getSegments().get(mid); Segment midSegment = this.getSegments().get(mid);
if (midSegment.end.getTime() < time) { if (midSegment.end.getTimeInMilliSecond() < timeInMilliSecond) {
begin = mid + 1; begin = mid + 1;
} else if (midSegment.start.getTime() > time) { } else if (midSegment.start.getTimeInMilliSecond() > timeInMilliSecond) {
end = mid - 1; end = mid - 1;
} else { } else {
return midSegment; return midSegment;
@ -132,16 +135,17 @@ public class Schedule extends Observable {
public static class Segment { public static class Segment {
private final Point start; private final Point start;
private final Point end; private final Point end;
// growthUnit is the amount that'll increase per second // growthUnit is the amount that'll increase per second, its unit is
// /second
private final float growthUnit; private final float growthUnit;
public Segment(Point startPoint, Point endPoint) { public Segment(Point startPoint, Point endPoint) {
this.start = startPoint.copy(); this.start = startPoint.copy();
this.end = endPoint.copy(); this.end = endPoint.copy();
long timeDifference = this.end.getTime() / 1000 long timeDifference = this.end.getTimeInSecond()
- this.start.getTime() / 1000; - this.start.getTimeInSecond();
if (timeDifference < 0 || this.start.getTime() < 0l if (timeDifference < 0 || this.start.getTimeInMilliSecond() < 0l
|| this.end.getTime() < 0l) { || this.end.getTimeInMilliSecond() < 0l) {
throw new Bench4QRunTimeException( throw new Bench4QRunTimeException(
"The end time in TestScehdul cannot be less than start time"); "The end time in TestScehdul cannot be less than start time");
} }
@ -150,23 +154,30 @@ public class Schedule extends Observable {
/ (double) (timeDifference)); / (double) (timeDifference));
} }
public int loadFor(long timeFromBegin) { public int loadFor(long timeFromBeginInMilliSecond) {
if (timeFromBegin < this.start.getTime() if (timeFromBeginInMilliSecond < this.start.getTimeInMilliSecond()
|| timeFromBegin > this.end.getTime()) { || timeFromBeginInMilliSecond > this.end
.getTimeInMilliSecond()) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
long diffFromStart = timeFromBegin - this.start.getTime(); long diffFromStart = timeFromBeginInMilliSecond
- this.start.getTimeInMilliSecond();
return (int) (this.start.getLoad() + (diffFromStart * growthUnit / 1000)); return (int) (this.start.getLoad() + (diffFromStart * growthUnit / 1000));
} }
} }
public static class Point { public static class Point {
// This time is the relative value from begin // This time is the relative value from begin
private final long time; public static final int SECOND_TO_MS_CONVERTOR = 1000;
private final long timeInSecond;
private final int load; private final int load;
public long getTime() { public long getTimeInMilliSecond() {
return time; return timeInSecond * SECOND_TO_MS_CONVERTOR;
}
public long getTimeInSecond() {
return timeInSecond;
} }
public int getLoad() { public int getLoad() {
@ -178,12 +189,12 @@ public class Schedule extends Observable {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Load can't be negtive number!"); "Load can't be negtive number!");
} }
this.time = time; this.timeInSecond = time;
this.load = load; this.load = load;
} }
public Point copy() { public Point copy() {
return new Point(this.getTime(), this.getLoad()); return new Point(this.getTimeInSecond(), this.getLoad());
} }
} }
@ -201,12 +212,13 @@ public class Schedule extends Observable {
List<Point> points = new LinkedList<Schedule.Point>(); List<Point> points = new LinkedList<Schedule.Point>();
for (PointModel model : pointModels) { for (PointModel model : pointModels) {
maxLoad = Math.max(maxLoad, model.getLoad()); maxLoad = Math.max(maxLoad, model.getLoad());
points.add(new Point(model.getTime(), model.getLoad())); points.add(new Point(model.getTimeInSecond(), model.getLoad()));
} }
Collections.sort(points, new Comparator<Point>() { Collections.sort(points, new Comparator<Point>() {
@Override @Override
public int compare(Point o1, Point o2) { public int compare(Point o1, Point o2) {
return (int) (o1.getTime() - o2.getTime()); return (int) (o1.getTimeInMilliSecond() - o2
.getTimeInMilliSecond());
} }
}); });
for (int i = 0; i < points.size() - 1; i++) { for (int i = 0; i < points.size() - 1; i++) {

View File

@ -15,7 +15,7 @@ public class Supervisor extends Observable {
void start() { void start() {
this.context.getSchedule().begin(); this.context.getSchedule().begin();
long time = context.getSchedule().getScheduleRange() long time = context.getSchedule().getScheduleRangeInMilliSecond()
+ context.getStartDate().getTime(); + context.getStartDate().getTime();
this.timer.schedule(new TimerTask() { this.timer.schedule(new TimerTask() {
@Override @Override

View File

@ -121,23 +121,25 @@ public abstract class TestBase {
} }
public VUser createVUser(Scenario scenario, UUID testId) { public VUser createVUser(Scenario scenario, UUID testId) {
return new VUser(buildScenarioContext(testId, scenario, return new VUser(buildScenarioContext(testId, scenario, 10,
10, pluginManager), 1, this.getPluginManager()); pluginManager), 1, this.getPluginManager());
} }
public static ScenarioContext buildScenarioContext(UUID testId, public static ScenarioContext buildScenarioContext(UUID testId,
final Scenario scenario, int poolSize, PluginManager pluginManager) { final Scenario scenario, int poolSize, PluginManager pluginManager) {
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContextWithoutScenario( ScenarioContext scenarioContext = ScenarioContext
testId, poolSize, pluginManager); .buildScenarioContextWithoutScenario(testId, poolSize,
return scenarioContext.addScenrio(scenario, Schedule.build(buildScheduleModel()), new Date().getTime()); pluginManager);
return scenarioContext.addScenrio(scenario,
Schedule.build(buildScheduleModel()), new Date().getTime());
} }
public static ScheduleModel buildScheduleModel(){ public static ScheduleModel buildScheduleModel() {
ScheduleModel scheduleModel = new ScheduleModel(); ScheduleModel scheduleModel = new ScheduleModel();
List<PointModel> points = new LinkedList<ScheduleModel.PointModel>(); List<PointModel> points = new LinkedList<ScheduleModel.PointModel>();
points.add(new PointModel(0, 0)); points.add(new PointModel(0, 0));
points.add(new PointModel(20 * 1000, 20)); points.add(new PointModel(20, 20));
points.add(new PointModel(60 * 1000, 20)); points.add(new PointModel(60, 20));
scheduleModel.setPoints(points); scheduleModel.setPoints(points);
return scheduleModel; return scheduleModel;
} }

View File

@ -53,16 +53,16 @@ public class Test_Shedule {
public void test_InitWith_MinusTime() { public void test_InitWith_MinusTime() {
long firstTime = -1000000000; long firstTime = -1000000000;
ScheduleModel model = new ScheduleModel(); ScheduleModel model = new ScheduleModel();
model.getPoints().add(new PointModel(firstTime, 100)); model.getPoints().add(new PointModel(firstTime / 1000, 100));
Schedule.build(model); Schedule.build(model);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void test_LoadFor_WithIlegalArgument() { public void test_LoadFor_WithIlegalArgument() {
ScheduleModel model = new ScheduleModel(); ScheduleModel model = new ScheduleModel();
long firstTime = new Date().getTime(); long firstTime = new Date().getTime() / 1000;
model.getPoints().add(new PointModel(firstTime, 100)); model.getPoints().add(new PointModel(firstTime, 100));
model.getPoints().add(new PointModel(firstTime - 1000000, 50)); model.getPoints().add(new PointModel(firstTime - 1000, 50));
Schedule schedule = Schedule.build(model); Schedule schedule = Schedule.build(model);
schedule.getSegments().get(0).loadFor(-1000); schedule.getSegments().get(0).loadFor(-1000);
} }
@ -70,7 +70,7 @@ public class Test_Shedule {
@Test @Test
public void test_loadFor() { public void test_loadFor() {
ScheduleModel model = new ScheduleModel(); ScheduleModel model = new ScheduleModel();
model.getPoints().add(new PointModel(1000000, 100)); model.getPoints().add(new PointModel(1000, 100));
model.getPoints().add(new PointModel(0, 50)); model.getPoints().add(new PointModel(0, 50));
Schedule schedule = Schedule.build(model); Schedule schedule = Schedule.build(model);
int load = schedule.getSegments().get(0).loadFor(500 * 1000); int load = schedule.getSegments().get(0).loadFor(500 * 1000);
@ -80,10 +80,10 @@ public class Test_Shedule {
@Test @Test
public void test_getSegment() { public void test_getSegment() {
ScheduleModel model = new ScheduleModel(); ScheduleModel model = new ScheduleModel();
model.getPoints().add(new PointModel(1000000, 100)); model.getPoints().add(new PointModel(100, 100));
model.getPoints().add(new PointModel(0, 50)); model.getPoints().add(new PointModel(0, 50));
Schedule schedule = Schedule.build(model); Schedule schedule = Schedule.build(model);
Segment segment = schedule.getSegment(500 * 1000); Segment segment = schedule.getSegment(50 * 1000);
assertNotNull(segment); assertNotNull(segment);
} }
@ -92,7 +92,7 @@ public class Test_Shedule {
@Test @Test
public void test_getScheduleRange() { public void test_getScheduleRange() {
Schedule schedule = Schedule.build(TestBase.buildScheduleModel()); Schedule schedule = Schedule.build(TestBase.buildScheduleModel());
assertEquals(60 * 1000, schedule.getScheduleRange()); assertEquals(60 * 1000, schedule.getScheduleRangeInMilliSecond());
} }
@Test @Test

View File

@ -183,15 +183,13 @@ public class RunningAgentDB implements RunningAgentInterface {
.tryUnmarshal(RunScenarioModel.class, this.getTestPlanScript() .tryUnmarshal(RunScenarioModel.class, this.getTestPlanScript()
.getFilteredScriptCnt()); .getFilteredScriptCnt());
runScenarioModel.setPoolSize(getLoadInUse()); runScenarioModel.setPoolSize(getLoadInUse());
ScheduleModel scheduleModel = (ScheduleModel) MarshalHelper
.tryUnmarshal(ScheduleModel.class, this.getTestPlanScript()
.getScheduleContent());
RunScenarioResultModel runScenarioResultModel = this RunScenarioResultModel runScenarioResultModel = this
.getAgentMessenger().submitScenrioWithParams( .getAgentMessenger().submitScenrioWithParams(this.getAgent(),
this.getAgent(), this.getAgentRunId(), script2.loadParamFiles(),
this.getAgentRunId(), runScenarioModel, scheduleModel,
script2.loadParamFiles(),
runScenarioModel,
(ScheduleModel) MarshalHelper.tryUnmarshal(
ScheduleModel.class, this.getTestPlanScript()
.getScheduleContent()),
this.getRunningScript().getStartTime()); this.getRunningScript().getStartTime());
if (runScenarioResultModel == null) { if (runScenarioResultModel == null) {
return false; return false;

View File

@ -205,8 +205,9 @@ public class TestPlanScript implements RunningScriptInterface {
return false; return false;
} }
Timer timer = new Timer(); Timer timer = new Timer();
timer.schedule(new ExecutionOverTask(this), timer.schedule(
(testScriptConfig.getExecuteRange())); new ExecutionOverTask(this),
(testScriptConfig.getExecuteRange() * SECOND_MILISECOND_UNIT_CONVERSION));
return true; return true;
} }

View File

@ -399,8 +399,8 @@ public class TestBase_MakeUpTestPlan extends TestBase {
model.setRequireLoad(EACH_SCRIPT_LOAD_SMALLSCALE); model.setRequireLoad(EACH_SCRIPT_LOAD_SMALLSCALE);
ScheduleModel schedule = new ScheduleModel(); ScheduleModel schedule = new ScheduleModel();
schedule.getPoints().add(new PointModel(0, 0)); schedule.getPoints().add(new PointModel(0, 0));
schedule.getPoints().add(new PointModel(20 * 1000, 10)); schedule.getPoints().add(new PointModel(20, 10));
schedule.getPoints().add(new PointModel(60 * 1000, 10)); schedule.getPoints().add(new PointModel(60, 10));
model.setScheduleModel(schedule); model.setScheduleModel(schedule);
return model; return model;
} }

View File

@ -29,10 +29,10 @@ public class ScheduleModel {
if (this.points.size() == 0) { if (this.points.size() == 0) {
return 0; return 0;
} }
long startTime = this.points.get(0).getTime(), endTime = startTime; long startTime = this.points.get(0).getTimeInSecond(), endTime = startTime;
for (PointModel point : this.getPoints()) { for (PointModel point : this.getPoints()) {
startTime = Math.min(point.getTime(), startTime); startTime = Math.min(point.getTimeInSecond(), startTime);
endTime = Math.max(point.getTime(), endTime); endTime = Math.max(point.getTimeInSecond(), endTime);
} }
return endTime - startTime; return endTime - startTime;
} }
@ -47,16 +47,17 @@ public class ScheduleModel {
@XmlRootElement @XmlRootElement
public static class PointModel { public static class PointModel {
private long time; // Time Unit is second
private long timeInSecond;
private int load; private int load;
@XmlElement @XmlElement
public long getTime() { public long getTimeInSecond() {
return time; return timeInSecond;
} }
public void setTime(long time) { public void setTimeInSecond(long time) {
this.time = time; this.timeInSecond = time;
} }
@XmlElement @XmlElement
@ -71,8 +72,8 @@ public class ScheduleModel {
public PointModel() { public PointModel() {
} }
public PointModel(long time, int load) { public PointModel(long timeInSecond, int load) {
this.time = time; this.timeInSecond = timeInSecond;
this.load = load; this.load = load;
} }
} }