remove all bugs

remove all bugs
This commit is contained in:
coderfengyun 2014-09-02 17:16:11 +08:00
parent 26d689e005
commit bed2979980
5 changed files with 49 additions and 20 deletions

View File

@ -81,7 +81,7 @@ public class ScenarioContext implements Observer {
return pluginManager; return pluginManager;
} }
Schedule getSchedule() { public Schedule getSchedule() {
return schedule; return schedule;
} }
@ -105,7 +105,8 @@ public class ScenarioContext implements Observer {
public ScenarioContext addScenrio(final Scenario scenario, public ScenarioContext addScenrio(final Scenario scenario,
Schedule schedule, final long realStartTime) { Schedule schedule, final long realStartTime) {
ScenarioContext result = new ScenarioContext(this.testId, new Date( ScenarioContext result = new ScenarioContext(this.testId, new Date(
realStartTime), executor, this.dataCollector, pluginManager); realStartTime), this.executor, this.dataCollector,
this.pluginManager);
result.setSchedule(schedule); result.setSchedule(schedule);
result.setEndDate(new Date(result.getSchedule().getScheduleRange() result.setEndDate(new Date(result.getSchedule().getScheduleRange()
+ result.getStartDate().getTime())); + result.getStartDate().getTime()));
@ -131,7 +132,7 @@ public class ScenarioContext implements Observer {
this.getExecutor().setMaximumPoolSize(requiredLoad); this.getExecutor().setMaximumPoolSize(requiredLoad);
} }
public void addTask() { public void addATask() {
if (this.isFinished()) { if (this.isFinished()) {
return; return;
} }
@ -142,9 +143,10 @@ public class ScenarioContext implements Observer {
public void initTasks(int currentLoad) { public void initTasks(int currentLoad) {
this.updatePopulation(currentLoad); this.updatePopulation(currentLoad);
for (int i = 0; i < currentLoad; i++) { for (int i = 0; i < this.getSchedule().getMaxLoad() + 2; i++) {
addTask(); addATask();
} }
addATask();
} }
@Override @Override
@ -154,17 +156,31 @@ public class ScenarioContext implements Observer {
if (schedule.hasReachEnd()) { if (schedule.hasReachEnd()) {
stop(); stop();
} else { } else {
fillTaskIfRequired();
this.updatePopulation((Integer) arg); this.updatePopulation((Integer) arg);
Logger.getLogger(this.getClass()).info(
"Active thread : "
+ this.getExecutor().getActiveCount());
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); 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() { public void stop() {
this.getSchedule().stop(); this.getSchedule().stop();
this.setFinished(true); this.setFinished(true);
this.setEndDate(new Date());
this.getExecutor().shutdownNow(); this.getExecutor().shutdownNow();
} }
} }

View File

@ -8,6 +8,7 @@ import java.util.Observable;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import org.apache.log4j.Logger;
import org.bench4q.share.exception.Bench4QRunTimeException; import org.bench4q.share.exception.Bench4QRunTimeException;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel; import org.bench4q.share.models.agent.scriptrecord.ScheduleModel;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel; 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 static final int SCHEDULE_CYCLE = 3000;
private final List<Segment> segments; private final List<Segment> segments;
private final long beginTime; private final long beginTime;
private final Timer timer = new Timer(); private final Timer timer;
private final int maxLoad;
private volatile boolean reachEnd; private volatile boolean reachEnd;
private final Logger logger = Logger.getLogger(Schedule.class);
public List<Segment> getSegments() { public List<Segment> getSegments() {
return segments; return segments;
} }
public int getMaxLoad() {
return maxLoad;
}
public boolean hasReachEnd() { public boolean hasReachEnd() {
return this.reachEnd; return this.reachEnd;
} }
@ -34,7 +41,7 @@ public class Schedule extends Observable {
this.reachEnd = true; this.reachEnd = true;
} }
public Schedule(List<Segment> segments) { public Schedule(List<Segment> segments, int maxLoad) {
if (segments == null || segments.size() == 0) { if (segments == null || segments.size() == 0) {
throw new Bench4QRunTimeException( throw new Bench4QRunTimeException(
"Can't init a schedul with zero segment"); "Can't init a schedul with zero segment");
@ -42,6 +49,8 @@ public class Schedule extends Observable {
this.segments = segments; this.segments = segments;
this.beginTime = System.currentTimeMillis(); this.beginTime = System.currentTimeMillis();
this.reachEnd = false; this.reachEnd = false;
this.maxLoad = maxLoad;
this.timer = new Timer();
} }
public long getScheduleRange() { public long getScheduleRange() {
@ -58,11 +67,14 @@ public class Schedule extends Observable {
if (segment == null) { if (segment == null) {
// exceed the range of execute, should let the context stop // exceed the range of execute, should let the context stop
// the test // the test
System.out.println("Execution Over!"); logger.info("Execution Over!");
notifyObservers(0); notifyObservers(0);
return; 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); }, 0, SCHEDULE_CYCLE);
} }
@ -176,29 +188,30 @@ public class Schedule extends Observable {
} }
public static Schedule build(ScheduleModel scheduleModel) { public static Schedule build(ScheduleModel scheduleModel) {
Schedule schedule = new Schedule( List<Segment> segements = new LinkedList<Schedule.Segment>();
extractSegments(scheduleModel.getPoints())); int maxLoad = extractSegmentsAndGetMaxLoad(scheduleModel.getPoints(),
segements);
Schedule schedule = new Schedule(segements, maxLoad);
return schedule; 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>(); List<Point> points = new LinkedList<Schedule.Point>();
for (PointModel model : pointModels) { for (PointModel model : pointModels) {
maxLoad = Math.max(maxLoad, model.getLoad());
points.add(new Point(model.getTime(), model.getLoad())); points.add(new Point(model.getTime(), 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.getTime() - o2.getTime());
} }
}); });
List<Segment> result = new LinkedList<Schedule.Segment>();
for (int i = 0; i < points.size() - 1; i++) { for (int i = 0; i < points.size() - 1; i++) {
result.add(new Segment(points.get(i), points.get(i + 1))); result.add(new Segment(points.get(i), points.get(i + 1)));
} }
return result; return maxLoad;
} }
} }

View File

@ -63,7 +63,7 @@ public class VUser implements Runnable {
} }
private void doCleanUp() { private void doCleanUp() {
this.getScenarioContext().addTask(); this.getScenarioContext().addATask();
this.setScenarioContext(null); this.setScenarioContext(null);
} }

View File

@ -40,6 +40,7 @@ public class Test_ScenarioContext extends TestBase {
assertNotNull(scenarioContext.getScenario()); assertNotNull(scenarioContext.getScenario());
assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000, assertEquals(scenarioContext.getStartDate().getTime() + 60 * 1000,
scenarioContext.getEndDate().getTime()); scenarioContext.getEndDate().getTime());
assertTrue(scenarioContext.getSchedule().countObservers() > 0);
} }
private ScenarioContext getScenarioWithScenarioAndSchedule() { private ScenarioContext getScenarioWithScenarioAndSchedule() {

View File

@ -9,7 +9,6 @@ import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;