parent
26d689e005
commit
bed2979980
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Segment> 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<Segment> 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<Segment> segments) {
|
||||
public Schedule(List<Segment> 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<Segment> segements = new LinkedList<Schedule.Segment>();
|
||||
int maxLoad = extractSegmentsAndGetMaxLoad(scheduleModel.getPoints(),
|
||||
segements);
|
||||
Schedule schedule = new Schedule(segements, maxLoad);
|
||||
return schedule;
|
||||
}
|
||||
|
||||
private static List<Segment> extractSegments(List<PointModel> pointModels) {
|
||||
private static int extractSegmentsAndGetMaxLoad(
|
||||
List<PointModel> pointModels, List<Segment> result) {
|
||||
int maxLoad = 0;
|
||||
List<Point> points = new LinkedList<Schedule.Point>();
|
||||
for (PointModel model : pointModels) {
|
||||
maxLoad = Math.max(maxLoad, model.getLoad());
|
||||
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;
|
||||
return maxLoad;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ public class VUser implements Runnable {
|
|||
}
|
||||
|
||||
private void doCleanUp() {
|
||||
this.getScenarioContext().addTask();
|
||||
this.getScenarioContext().addATask();
|
||||
this.setScenarioContext(null);
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue