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

View File

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

View File

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

View File

@ -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() {

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.statistics.ScriptResultModel;
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.stereotype.Component;
@ -127,7 +126,8 @@ public class TestPlanFactory {
testPlanScripts.add(createATestPlanScriptWithoutId(
runningScriptModel.getRequireLoad(),
runningScriptModel.getScriptId(),
runningScriptModel.getConfig(), result, runningScriptModel.getScriptFilterOptionsModel()));
runningScriptModel.getConfig(), result,
runningScriptModel.getScriptFilterOptionsModel()));
}
result.setRequiredLoad(requiredLoad);
result.setTestPlanScripts(testPlanScripts);
@ -150,33 +150,43 @@ public class TestPlanFactory {
}
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.setRequireLoad(requireLoad);
//filter the script
testPlanScript.setScript(createAScriptWithFilter(scriptFilterOptionsModel, this.getScriptService().getScript(scriptId)));
// filter the script
testPlanScript.setScript(createAScriptWithFilter(
scriptFilterOptionsModel,
this.getScriptService().getScript(scriptId)));
testPlanScript.setPlanedConfig(createAPlanedConfigWithoutId(config));
testPlanScript.setTestPlan(testPlanDB);
return testPlanScript;
}
private Script createAScriptWithFilter(ScriptFilterOptionsModel scriptFilterOptionsModel, Script script){
private Script createAScriptWithFilter(
ScriptFilterOptionsModel scriptFilterOptionsModel, Script script) {
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.tryUnmarshal(RunScenarioModel.class,
script.getFilteredScriptCnt());
List<String> filterPluginId = new ArrayList<String>();
if(scriptFilterOptionsModel.isFilterTimer()){
for(UsePluginModel pluginModel: runScenarioModel.getUsePlugins()){
if(pluginModel.getName().equals("ConstantTimer")){
if (scriptFilterOptionsModel.isFilterTimer()) {
for (UsePluginModel pluginModel : runScenarioModel.getUsePlugins()) {
if (pluginModel.getName().equals("ConstantTimer")) {
filterPluginId.add(pluginModel.getId());
}
}
}
if(scriptFilterOptionsModel.getFilterTypeMatches() != null && !scriptFilterOptionsModel.getFilterTypeMatches().equals("")){
for(PageModel pageModel: runScenarioModel.getPages()){
for(BatchModel batchModel : pageModel.getBatches()){
for(BehaviorModel behaviorModel: batchModel.getBehaviors()){
if(filterPluginId.contains(behaviorModel.getUse()) || isFilterType(scriptFilterOptionsModel.getFilterTypeMatches(), behaviorModel)){
if (scriptFilterOptionsModel.getFilterTypeMatches() != null
&& !scriptFilterOptionsModel.getFilterTypeMatches().equals("")) {
for (PageModel pageModel : runScenarioModel.getPages()) {
for (BatchModel batchModel : pageModel.getBatches()) {
for (BehaviorModel behaviorModel : batchModel
.getBehaviors()) {
if (filterPluginId.contains(behaviorModel.getUse())
|| isFilterType(
scriptFilterOptionsModel
.getFilterTypeMatches(),
behaviorModel)) {
batchModel.getBehaviors().remove(behaviorModel);
}
}
@ -188,13 +198,14 @@ public class TestPlanFactory {
return script;
}
private boolean isFilterType(String filterTypeMatch, BehaviorModel behaviorModel){
for(ParameterModel parameterModel:behaviorModel.getParameters()){
if(parameterModel.getKey().equals("expResCnttype")){
private boolean isFilterType(String filterTypeMatch,
BehaviorModel behaviorModel) {
for (ParameterModel parameterModel : behaviorModel.getParameters()) {
if (parameterModel.getKey().equals("expResCnttype")) {
if(parameterModel.getValue().matches(filterTypeMatch)){
if (parameterModel.getValue().matches(filterTypeMatch)) {
return true;
}else{
} else {
return false;
}
}

View File

@ -1,11 +1,7 @@
package org.bench4q.master.domain.service;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
@ -21,7 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component
public class ScriptService {
private ScriptRepositoty scriptRepositoty;
@ -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);
if (script.getUser().getId() != ownerId) {
return false;

View File

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