refactor, let the schedule be the stand-alone one

refactor, let the schedule be the stand-alone one
This commit is contained in:
coderfengyun 2014-09-02 11:38:47 +08:00
parent 1bfbbde54d
commit fccd5d0a27
13 changed files with 74 additions and 68 deletions

View File

@ -12,6 +12,7 @@ import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.ScenarioEngine;
import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.BehaviorBriefModel;
import org.bench4q.share.models.agent.BehaviorStatusCodeResultModel;
@ -20,6 +21,7 @@ import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.RunScenarioResultModel;
import org.bench4q.share.models.agent.StopTestModel;
import org.bench4q.share.models.agent.TestBriefStatusModel;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.agent.statistics.AgentBehaviorsBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
@ -88,8 +90,11 @@ public class TestController {
System.out.println(scenarioModel);
RunScenarioModel runScenarioModel = (RunScenarioModel) MarshalHelper
.unmarshal(RunScenarioModel.class, scenarioModel);
ScheduleModel scheduleModel = (ScheduleModel) MarshalHelper
.unmarshal(ScheduleModel.class, scheduleContent);
this.getScenarioEngine().submitScenario(runId,
Scenario.scenarioBuilderWithCompile(runScenarioModel), realStartTime);
Scenario.scenarioBuilderWithCompile(runScenarioModel),
Schedule.build(scheduleModel), realStartTime);
return MarshalHelper.tryMarshal(buildWith(runId));
} catch (Exception e) {
logger.error("/submitScenarioWithParams", e);

View File

@ -5,7 +5,6 @@ import java.util.Collections;
import java.util.List;
import org.bench4q.agent.scenario.behavior.Behavior;
import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.ParameterModel;
import org.bench4q.share.models.agent.RunScenarioModel;
@ -17,7 +16,6 @@ public class Scenario {
private UsePlugin[] usePlugins;
private Page[] pages;
private List<Behavior> behaviors;
private Schedule schedule;
public UsePlugin[] getUsePlugins() {
return usePlugins;
@ -43,14 +41,6 @@ public class Scenario {
this.behaviors = behaviors;
}
public Schedule getSchedule() {
return schedule;
}
private void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
public Scenario() {
this.setBehaviors(new ArrayList<Behavior>());
}
@ -98,7 +88,6 @@ public class Scenario {
scenario.setPages(new Page[runScenarioModel.getPages().size()]);
extractUsePlugins(runScenarioModel, scenario);
extractPages(runScenarioModel, scenario);
scenario.setSchedule(Schedule.build(runScenarioModel.getScheduleModel()));
return scenario;
}

View File

@ -25,6 +25,7 @@ public class ScenarioContext implements Observer {
private boolean finished;
private final DataCollector dataCollector;
private final PluginManager pluginManager;
private Schedule schedule;
public ScenarioContext(UUID testId, Date startDate,
ThreadPoolExecutor executor, DataCollector dataCollector, PluginManager pluginManager) {
@ -79,15 +80,16 @@ public class ScenarioContext implements Observer {
return pluginManager;
}
public static ScenarioContext buildScenarioContext(UUID testId,
final Scenario scenario, int poolSize, PluginManager pluginManager) {
ScenarioContext scenarioContext = buildScenarioContextWithoutScenario(
testId, poolSize, pluginManager);
scenarioContext.setScenario(scenario);
scenario.getSchedule().addObserver(scenarioContext);
return scenarioContext;
Schedule getSchedule() {
return schedule;
}
private void setSchedule(Schedule schedule) {
this.schedule = schedule;
}
public static ScenarioContext buildScenarioContextWithoutScenario(
UUID testId, int poolSize, PluginManager pluginManager) {
final ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(
@ -101,11 +103,13 @@ public class ScenarioContext implements Observer {
return scenarioContext;
}
public ScenarioContext addScenrio(final Scenario scenario, final long realStartTime) {
public ScenarioContext addScenrio(final Scenario scenario, Schedule schedule, final long realStartTime) {
ScenarioContext result = new ScenarioContext(this.testId, new Date(realStartTime), executor, this.dataCollector, pluginManager);
result.setEndDate(new Date(scenario.getSchedule().getScheduleRange() + this.getStartDate().getTime()));
result.setSchedule(schedule);
result.setEndDate(new Date(result.getSchedule().getScheduleRange() + this.getStartDate().getTime()));
result.setFinished(this.isFinished());
result.setScenario(scenario);
this.getSchedule().addObserver(result);
return result;
}

View File

@ -50,10 +50,10 @@ public class ScenarioEngine implements Observer {
}
}
public void submitScenario(final UUID runId, final Scenario scenario, final long realStartTime) {
public void submitScenario(final UUID runId, final Scenario scenario, final Schedule schedule, final long realStartTime) {
try {
ScenarioContext old = this.getRunningTests().get(runId);
this.getRunningTests().put(runId, old.addScenrio(scenario, realStartTime));
this.getRunningTests().put(runId, old.addScenrio(scenario, schedule, realStartTime));
} catch (Exception e) {
e.printStackTrace();
}
@ -74,7 +74,6 @@ public class ScenarioEngine implements Observer {
}
try {
int currentLoad = scenarioContext
.getScenario()
.getSchedule()
.loadFor(
System.currentTimeMillis()

View File

@ -15,18 +15,18 @@ public class Supervisor extends Observable {
}
void start(){
long time = context.getScenario().getSchedule().getScheduleRange()
long time = context.getSchedule().getScheduleRange()
+ context.getStartDate().getTime();
this.timer.schedule(new TimerTask() {
@Override
public void run() {
context.getScenario().getSchedule().stop();
context.getSchedule().stop();
}
}, new Date(time));
}
void stop(){
this.context.getScenario().getSchedule().stop();
this.context.getSchedule().stop();
this.context.stop();
this.timer.cancel();
}

View File

@ -3,6 +3,7 @@ package org.bench4q.agent.test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
@ -12,6 +13,7 @@ import org.bench4q.agent.plugin.ParameterBarn;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.agent.scenario.engine.VUser;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.ParameterModel;
@ -119,7 +121,14 @@ public abstract class TestBase {
}
public VUser createVUser(Scenario scenario, UUID testId) {
return new VUser(ScenarioContext.buildScenarioContext(testId, scenario,
return new VUser(buildScenarioContext(testId, scenario,
10, pluginManager), 1, this.getPluginManager());
}
public static ScenarioContext buildScenarioContext(UUID testId,
final Scenario scenario, int poolSize, PluginManager pluginManager) {
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContextWithoutScenario(
testId, poolSize, pluginManager);
return scenarioContext.addScenrio(scenario, Schedule.build(new ScheduleModel()), new Date().getTime());
}
}

View File

@ -5,8 +5,9 @@ import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.junit.Assert.*;
@ -200,16 +201,14 @@ public class TestWithScriptFile {
assertNotNull(bookResponse);
assertNotNull(bookResponse.getRunId().toString());
System.out.println(bookResponse.getRunId().toString());
Map<String, String> stringPart = new LinkedHashMap<String, String>();
stringPart.put("scenarioModel", MarshalHelper.tryMarshal(getScenarioModel()));
//TODO:
HttpResponse httpResponse = this.getHttpRequester().postFiles(
null,
url + "/submitScenarioWithParams/"
+ bookResponse.getRunId().toString(), "files[]", files,
"scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L;
{
add(MarshalHelper.tryMarshal(getScenarioModel()));
}
});
stringPart);
assertNotNull(httpResponse);
assertNotNull(httpResponse.getContent());
assertEquals(200, httpResponse.getCode());

View File

@ -8,6 +8,7 @@ import java.util.concurrent.TimeUnit;
import org.bench4q.agent.plugin.PluginManager;
import org.bench4q.agent.scenario.Scenario;
import org.bench4q.agent.scenario.engine.ScenarioContext;
import org.bench4q.agent.test.TestBase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -16,13 +17,13 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:application-context.xml" })
public class Test_ScenarioContext {
public class Test_ScenarioContext extends TestBase {
@Autowired
private PluginManager pluginManager;
@Test
public void testBuildScenarioContext() {
ScenarioContext context = ScenarioContext.buildScenarioContext(
ScenarioContext context = buildScenarioContext(
UUID.randomUUID(), new Scenario(), 20, this.pluginManager);
assertEquals(10,
context.getExecutor().getKeepAliveTime(TimeUnit.MINUTES));

View File

@ -41,7 +41,7 @@ public class Test_ScenarioEngine extends TestBase {
@Test
public void test_UpdatePopulation() {
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
ScenarioContext scenarioContext = buildScenarioContext(
testId,
Scenario.scenarioBuilderWithCompile(buildRunScenarioModelWith(
new ArrayList<UsePluginModel>(), BehaviorModel
@ -56,7 +56,7 @@ public class Test_ScenarioEngine extends TestBase {
@Test
public void test_RunWithContext() throws IOException {
ScenarioContext scenarioContext = ScenarioContext.buildScenarioContext(
ScenarioContext scenarioContext = buildScenarioContext(
testId, Scenario.scenarioBuilderWithCompile(FileUtils
.readFileToString(new File("Scripts"
+ System.getProperty("file.separator")

View File

@ -84,4 +84,6 @@ public class Test_Shedule {
Segment segment = schedule.getSegment(500 * 1000);
assertNotNull(segment);
}
//TODO : add more test about getSegment, you will got some error
}

View File

@ -2,8 +2,9 @@ package org.bench4q.master.infrastructure.communication.impl;
import java.io.File;
import java.util.Date;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
@ -79,15 +80,14 @@ public class AgentMessengerImpl implements AgentMessenger {
try {
final String modelContent = MarshalHelper
.tryMarshal(runScenarioModel);
final Map<String, String> stringParts = new LinkedHashMap<String, String>();
stringParts.put("scenarioModel", modelContent);
stringParts.put("realStartTime",
String.valueOf(realStartDate.getTime()));
httpResponse = this.httpRequester.postFiles(null,
buildBaseUrl(agent) + "/test/submitScenarioWithParams/"
+ agentRunId+ "/" + realStartDate.getTime(), "files[]", paramFiles,
"scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L;
{
add(modelContent);
}
});
+ agentRunId + "/" + realStartDate.getTime(),
"files[]", paramFiles, stringParts);
return (RunScenarioResultModel) MarshalHelper.unmarshal(
RunScenarioResultModel.class, httpResponse.getContent());
} catch (Exception e) {
@ -98,7 +98,8 @@ public class AgentMessengerImpl implements AgentMessenger {
public Future<RunScenarioResultModel> submitScenarioWithParamsAsync(
final Agent agent, final UUID agentRunId,
final List<File> paramFiles, final RunScenarioModel runScenarioModel, final Date realStartDate) {
final List<File> paramFiles,
final RunScenarioModel runScenarioModel, final Date realStartDate) {
return this.executorService
.submit(new Callable<RunScenarioResultModel>() {
@Override

View File

@ -116,8 +116,7 @@ public class HttpRequester {
}
public HttpResponse postFiles(Map<String, String> headers, String url,
String filePartName, List<File> files, String stringPartName,
List<String> strings) {
String filePartName, List<File> files, Map<String, String> stringParts) {
if (!url.startsWith("http"))
url = "http://" + url;
PostMethod postMethod = new PostMethod(url);
@ -131,15 +130,17 @@ public class HttpRequester {
}
}
Part[] parts = new Part[files.size() + strings.size()];
for (int i = 0; i < files.size(); i++) {
Part[] parts = new Part[files.size() + stringParts.size()];
int i = 0;
for (; i < files.size(); i++) {
FilePart filePart = new FilePart(filePartName, files.get(i)
.getName(), files.get(i));
parts[i] = filePart;
}
for (int i = 0; i < strings.size(); i++) {
parts[i + files.size()] = new StringPart(stringPartName,
strings.get(i));
for (Entry<String, String> entry : stringParts.entrySet()) {
parts[i + files.size()] = new StringPart(entry.getKey(),
entry.getValue());
i++;
}
postMethod.setRequestEntity(new MultipartRequestEntity(parts,
postMethod.getParams()));
@ -154,7 +155,7 @@ public class HttpRequester {
public HttpResponse postFilesMulti(Map<String, String> headers, String url,
String filePartName, MultipartFile[] multipartFiles,
String stringPartName, List<String> strings) throws IOException {
Map<String, String> stringParts) throws IOException {
List<File> files = new LinkedList<File>();
if (multipartFiles != null) {
for (MultipartFile multipartFile : multipartFiles) {
@ -165,8 +166,7 @@ public class HttpRequester {
}
}
return postFiles(headers, url, filePartName, files, stringPartName,
strings);
return postFiles(headers, url, filePartName, files, stringParts);
}

View File

@ -1,8 +1,7 @@
package org.bench4q.web.masterMessager;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.xml.bind.JAXBException;
@ -64,15 +63,13 @@ public class ScriptMessager extends MasterMessager {
public OperateScriptServerResponseModel uploadScriptFile(
String accessToken, String scriptName, String scenarioModel) {
String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName;
Map<String, String> params = new HashMap<String, String>();
params.put("scenarioModel", scenarioModel);
Map<String, String> stringPart = new HashMap<String, String>();
stringPart.put("scenarioModel", scenarioModel);
HttpResponse httpResponse = null;
List<String> stringPartsList = new LinkedList<String>();
stringPartsList.add(scenarioModel);
try {
httpResponse = this.getHttpRequester().postFilesMulti(
makeAccessTockenMap(accessToken), url, "paramFiles[]",
null, "scenarioModel", stringPartsList);
null, stringPart);
System.out.println(httpResponse.getContent());
if (!validateHttpResponse(httpResponse))
return null;
@ -90,14 +87,14 @@ public class ScriptMessager extends MasterMessager {
public OperateScriptServerResponseModel uploadScript(String accessToken,
String scriptName, String scenarioModel, MultipartFile[] paramFiles) {
String url = this.getBaseUrl() + "/uploadScript" + "/" + scriptName;
List<String> stringPart = new LinkedList<String>();
stringPart.add(scenarioModel);
HttpResponse httpResponse = null;
try {
Map<String, String> stringParts = new LinkedHashMap<String, String>();
stringParts.put("scenarioModel", scenarioModel);
httpResponse = this.getHttpRequester().postFilesMulti(
makeAccessTockenMap(accessToken), url, "paramFiles[]",
paramFiles, "scenarioModel", stringPart);
paramFiles, stringParts);
if (!validateHttpResponse(httpResponse))
return null;
return (OperateScriptServerResponseModel) MarshalHelper.unmarshal(