This commit is contained in:
hmm 2014-09-02 17:26:19 +08:00
commit 4e01cf19db
7 changed files with 87 additions and 51 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

@ -43,7 +43,6 @@ import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.TestScriptConfig; import org.bench4q.share.models.master.TestScriptConfig;
import org.bench4q.share.models.master.statistics.ScriptResultModel; import org.bench4q.share.models.master.statistics.ScriptResultModel;
import org.bench4q.share.models.monitor.MonitorMain; import org.bench4q.share.models.monitor.MonitorMain;
import org.python.antlr.PythonParser.return_stmt_return;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -127,7 +126,8 @@ public class TestPlanFactory {
testPlanScripts.add(createATestPlanScriptWithoutId( testPlanScripts.add(createATestPlanScriptWithoutId(
runningScriptModel.getRequireLoad(), runningScriptModel.getRequireLoad(),
runningScriptModel.getScriptId(), runningScriptModel.getScriptId(),
runningScriptModel.getConfig(), result, runningScriptModel.getScriptFilterOptionsModel())); runningScriptModel.getConfig(), result,
runningScriptModel.getScriptFilterOptionsModel()));
} }
result.setRequiredLoad(requiredLoad); result.setRequiredLoad(requiredLoad);
result.setTestPlanScripts(testPlanScripts); result.setTestPlanScripts(testPlanScripts);
@ -150,33 +150,43 @@ public class TestPlanFactory {
} }
public TestPlanScript createATestPlanScriptWithoutId(int requireLoad, public TestPlanScript createATestPlanScriptWithoutId(int requireLoad,
int scriptId, TestScriptConfig config, TestPlan testPlanDB, ScriptFilterOptionsModel scriptFilterOptionsModel) { int scriptId, TestScriptConfig config, TestPlan testPlanDB,
ScriptFilterOptionsModel scriptFilterOptionsModel) {
TestPlanScript testPlanScript = new TestPlanScript(); TestPlanScript testPlanScript = new TestPlanScript();
testPlanScript.setRequireLoad(requireLoad); testPlanScript.setRequireLoad(requireLoad);
//filter the script // filter the script
testPlanScript.setScript(createAScriptWithFilter(scriptFilterOptionsModel, this.getScriptService().getScript(scriptId))); testPlanScript.setScript(createAScriptWithFilter(
scriptFilterOptionsModel,
this.getScriptService().getScript(scriptId)));
testPlanScript.setPlanedConfig(createAPlanedConfigWithoutId(config)); testPlanScript.setPlanedConfig(createAPlanedConfigWithoutId(config));
testPlanScript.setTestPlan(testPlanDB); testPlanScript.setTestPlan(testPlanDB);
return testPlanScript; return testPlanScript;
} }
private Script createAScriptWithFilter(ScriptFilterOptionsModel scriptFilterOptionsModel, Script script){ private Script createAScriptWithFilter(
ScriptFilterOptionsModel scriptFilterOptionsModel, Script script) {
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.tryUnmarshal(RunScenarioModel.class, .tryUnmarshal(RunScenarioModel.class,
script.getFilteredScriptCnt()); script.getFilteredScriptCnt());
List<String> filterPluginId = new ArrayList<String>(); List<String> filterPluginId = new ArrayList<String>();
if(scriptFilterOptionsModel.isFilterTimer()){ if (scriptFilterOptionsModel.isFilterTimer()) {
for(UsePluginModel pluginModel: runScenarioModel.getUsePlugins()){ for (UsePluginModel pluginModel : runScenarioModel.getUsePlugins()) {
if(pluginModel.getName().equals("ConstantTimer")){ if (pluginModel.getName().equals("ConstantTimer")) {
filterPluginId.add(pluginModel.getId()); filterPluginId.add(pluginModel.getId());
} }
} }
} }
if(scriptFilterOptionsModel.getFilterTypeMatches() != null && !scriptFilterOptionsModel.getFilterTypeMatches().equals("")){ if (scriptFilterOptionsModel.getFilterTypeMatches() != null
for(PageModel pageModel: runScenarioModel.getPages()){ && !scriptFilterOptionsModel.getFilterTypeMatches().equals("")) {
for(BatchModel batchModel : pageModel.getBatches()){ for (PageModel pageModel : runScenarioModel.getPages()) {
for(BehaviorModel behaviorModel: batchModel.getBehaviors()){ for (BatchModel batchModel : pageModel.getBatches()) {
if(filterPluginId.contains(behaviorModel.getUse()) || isFilterType(scriptFilterOptionsModel.getFilterTypeMatches(), behaviorModel)){ for (BehaviorModel behaviorModel : batchModel
.getBehaviors()) {
if (filterPluginId.contains(behaviorModel.getUse())
|| isFilterType(
scriptFilterOptionsModel
.getFilterTypeMatches(),
behaviorModel)) {
batchModel.getBehaviors().remove(behaviorModel); batchModel.getBehaviors().remove(behaviorModel);
} }
} }
@ -187,20 +197,21 @@ public class TestPlanFactory {
script.setFilteredScriptCnt(contentString); script.setFilteredScriptCnt(contentString);
return script; return script;
} }
private boolean isFilterType(String filterTypeMatch, BehaviorModel behaviorModel){ private boolean isFilterType(String filterTypeMatch,
for(ParameterModel parameterModel:behaviorModel.getParameters()){ BehaviorModel behaviorModel) {
if(parameterModel.getKey().equals("expResCnttype")){ for (ParameterModel parameterModel : behaviorModel.getParameters()) {
if (parameterModel.getKey().equals("expResCnttype")) {
if(parameterModel.getValue().matches(filterTypeMatch)){
if (parameterModel.getValue().matches(filterTypeMatch)) {
return true; return true;
}else{ } else {
return false; return false;
} }
} }
} }
return false; return false;
} }
private PlanedConfig createAPlanedConfigWithoutId(TestScriptConfig config) { private PlanedConfig createAPlanedConfigWithoutId(TestScriptConfig config) {

View File

@ -1,11 +1,7 @@
package org.bench4q.master.domain.service; package org.bench4q.master.domain.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -21,13 +17,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@Component @Component
public class ScriptService { public class ScriptService {
private ScriptRepositoty scriptRepositoty; private ScriptRepositoty scriptRepositoty;
private UserRepository userRepository; private UserRepository userRepository;
private Logger logger = Logger.getLogger(ScriptService.class); private Logger logger = Logger.getLogger(ScriptService.class);
private ScriptRepositoty getScriptRepositoty() { private ScriptRepositoty getScriptRepositoty() {
return scriptRepositoty; return scriptRepositoty;
} }
@ -79,7 +74,7 @@ public class ScriptService {
public List<Script> loadScripts(User user) { public List<Script> loadScripts(User user) {
return this.getScriptRepositoty().loadEntities(user); return this.getScriptRepositoty().loadEntities(user);
} }
public List<Script> queryScriptsByCreateTime(Date startDate, Date endDate, public List<Script> queryScriptsByCreateTime(Date startDate, Date endDate,
User user) { User user) {
return this.getScriptRepositoty().getWith( return this.getScriptRepositoty().getWith(
@ -114,7 +109,8 @@ public class ScriptService {
} }
} }
public boolean alterScriptContent(int id, int ownerId, String content, String scriptName) { public boolean alterScriptContent(int id, int ownerId, String content,
String scriptName) {
Script script = this.getScriptRepositoty().getEntity(id); Script script = this.getScriptRepositoty().getEntity(id);
if (script.getUser().getId() != ownerId) { if (script.getUser().getId() != ownerId) {
return false; return false;
@ -123,5 +119,5 @@ public class ScriptService {
script.setScriptContent(content); script.setScriptContent(content);
return this.getScriptRepositoty().update(script); return this.getScriptRepositoty().update(script);
} }
} }

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;