edit UploadScriptWithParams

edit UploadScriptWithParams, and need to be tested
This commit is contained in:
coderfengyun 2014-04-22 17:38:06 +08:00
parent dc45262df6
commit 8331391b35
26 changed files with 215 additions and 163 deletions

View File

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,7 @@ import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import org.bench4q.share.helper.FileHelper;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@ -24,10 +25,7 @@ public class ParameterFileCollector {
private String guardDirExists(UUID runId) { private String guardDirExists(UUID runId) {
String dirPath = "ScenarioParameters" + FILE_SEPARATOR String dirPath = "ScenarioParameters" + FILE_SEPARATOR
+ runId.toString() + FILE_SEPARATOR; + runId.toString() + FILE_SEPARATOR;
File dirFile = new File(dirPath); FileHelper.guardFolderExist(dirPath);
if (!dirFile.exists()) {
dirFile.mkdirs();
}
return dirPath; return dirPath;
} }
} }

View File

@ -192,7 +192,7 @@ public class TestWithScriptFile {
+ "forGoodRecord.xml")); + "forGoodRecord.xml"));
files.add(new File("Scripts" + System.getProperty("file.separator") files.add(new File("Scripts" + System.getProperty("file.separator")
+ "testJD.xml")); + "testJD.xml"));
HttpResponse httpResponse = this.getHttpRequester().postFiles( HttpResponse httpResponse = this.getHttpRequester().postFiles(null,
url + "/submitScenarioWithParams", "files[]", files, url + "/submitScenarioWithParams", "files[]", files,
"scenarioModel", new LinkedList<String>() { "scenarioModel", new LinkedList<String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -136,6 +136,13 @@
<artifactId>httpcore-nio</artifactId> <artifactId>httpcore-nio</artifactId>
<version>4.3</version> <version>4.3</version>
</dependency> </dependency>
<!--Start: for upload file -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>
<!--End : for upload file -->
<dependency> <dependency>
<groupId>org.bench4q</groupId> <groupId>org.bench4q</groupId>
<artifactId>bench4q-recorder</artifactId> <artifactId>bench4q-recorder</artifactId>

View File

@ -15,6 +15,8 @@ public class Main {
public static int MAX_FAIL_TIMES = 10; public static int MAX_FAIL_TIMES = 10;
public static int MIN_EXECUTE_INTERVAL_IN_SECONDS = 600; public static int MIN_EXECUTE_INTERVAL_IN_SECONDS = 600;
public static int PICK_CYCLE_IN_SECONDS = 60; public static int PICK_CYCLE_IN_SECONDS = 60;
public static String SCRIPT_PARAM_ROOT_FOLDER = "ScriptParameterization";
public static String FILE_SEPARATOR = System.getProperty("file.separator");
public static void main(String[] args) { public static void main(String[] args) {
try { try {
@ -43,12 +45,15 @@ public class Main {
.getProperty("pickTestPlanCycleInSeconds")); .getProperty("pickTestPlanCycleInSeconds"));
Min_Sample_Cycle_InSecond = Integer.parseInt(prop Min_Sample_Cycle_InSecond = Integer.parseInt(prop
.getProperty("minSampleCycleInSeconds")); .getProperty("minSampleCycleInSeconds"));
SCRIPT_PARAM_ROOT_FOLDER = prop
.getProperty("scriptParamRootFolder");
} catch (Exception e) { } catch (Exception e) {
portToUse = 8080; portToUse = 8080;
MAX_FAIL_TIMES = 10; MAX_FAIL_TIMES = 10;
MIN_EXECUTE_INTERVAL_IN_SECONDS = 600; MIN_EXECUTE_INTERVAL_IN_SECONDS = 600;
PICK_CYCLE_IN_SECONDS = 60; PICK_CYCLE_IN_SECONDS = 60;
Min_Sample_Cycle_InSecond = 10; Min_Sample_Cycle_InSecond = 10;
SCRIPT_PARAM_ROOT_FOLDER = "ScriptParameterization";
logger.error("There is no config file for port to serve! where path is " logger.error("There is no config file for port to serve! where path is "
+ configFile); + configFile);
} }

View File

@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller @Controller
@RequestMapping("/RecordScript") @RequestMapping("/RecordScript")
@ -152,7 +153,7 @@ public class ScriptController extends BaseController {
String content = FileUtils.readFileToString(file); String content = FileUtils.readFileToString(file);
logger.info("when saveToDB, scriptContent is " + content); logger.info("when saveToDB, scriptContent is " + content);
boolean success = this.getScriptService().saveScript(scriptName, boolean success = this.getScriptService().saveScript(scriptName,
this.getPrincipal().getId(), content); this.getPrincipal().getId(), content, null);
// this.getUserService().add // this.getUserService().add
return buildReponseModel(success, "Save to DataBase!!", "", port, return buildReponseModel(success, "Save to DataBase!!", "", port,
null, null, null); null, null, null);
@ -166,7 +167,8 @@ public class ScriptController extends BaseController {
@ResponseBody @ResponseBody
public OperateScriptServerResponseModel uploadScriptToDB( public OperateScriptServerResponseModel uploadScriptToDB(
@PathVariable String scriptName, @PathVariable String scriptName,
@RequestBody RunScenarioModel scenarioModel) @RequestParam RunScenarioModel scenarioModel,
@RequestParam(value = "paramFiles[]", required = false) List<MultipartFile> paramFiles)
throws Bench4QException { throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER throw new Bench4QException(HAVE_NO_POWER, HAVE_NO_POWER
@ -177,7 +179,8 @@ public class ScriptController extends BaseController {
scriptName, scriptName,
this.getPrincipal().getId(), this.getPrincipal().getId(),
MarshalHelper MarshalHelper
.marshal(RunScenarioModel.class, scenarioModel)); .marshal(RunScenarioModel.class, scenarioModel),
paramFiles);
logger.info("upload script:" logger.info("upload script:"
+ MarshalHelper.marshal(RunScenarioModel.class, + MarshalHelper.marshal(RunScenarioModel.class,
scenarioModel)); scenarioModel));

View File

@ -1,6 +1,9 @@
package org.bench4q.master.domain.entity; package org.bench4q.master.domain.entity;
import java.io.File;
import java.io.IOException;
import java.util.Date; import java.util.Date;
import java.util.List;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
@ -11,6 +14,10 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
import org.bench4q.master.Main;
import org.bench4q.share.helper.FileHelper;
import org.springframework.web.multipart.MultipartFile;
@Entity @Entity
@Table(name = "Script") @Table(name = "Script")
public class Script { public class Script {
@ -79,13 +86,29 @@ public class Script {
} }
public static Script createScriptWithoutId(String name, public static Script createScriptWithoutId(String name,
String scriptContent, User user) { String scriptContent, User user, List<MultipartFile> paramFiles) {
Script script = new Script(); Script script = new Script();
script.setBehaviorCount(0); script.setBehaviorCount(0);
script.setCreateDateTime(new Date()); script.setCreateDateTime(new Date());
script.setName(name); script.setName(name);
script.setScriptContent(scriptContent); script.setScriptContent(scriptContent);
script.setUser(user); script.setUser(user);
script.saveScriptParamFiles(paramFiles);
return script; return script;
} }
private void saveScriptParamFiles(List<MultipartFile> paramFiles) {
String folderPath = Main.SCRIPT_PARAM_ROOT_FOLDER + Main.FILE_SEPARATOR
+ this.getId() + Main.FILE_SEPARATOR;
FileHelper.guardFolderExist(folderPath);
for (MultipartFile multipartFile : paramFiles) {
try {
multipartFile.transferTo(new File(folderPath
+ Main.FILE_SEPARATOR
+ multipartFile.getOriginalFilename()));
} catch (IllegalStateException | IOException e) {
throw new RuntimeException(e);
}
}
}
} }

View File

@ -31,10 +31,10 @@ import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.domain.valueobject.datastatistics.RunningScriptSampler; import org.bench4q.master.domain.valueobject.datastatistics.RunningScriptSampler;
import org.bench4q.master.domain.valueobject.schedulscript.ExecutionOverTask; import org.bench4q.master.domain.valueobject.schedulscript.ExecutionOverTask;
import org.bench4q.master.domain.valueobject.schedulscript.WarmUpOverTask; import org.bench4q.master.domain.valueobject.schedulscript.WarmUpOverTask;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.TransactionFactory;
import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.highavailable.AgentRunBlotter;
import org.bench4q.master.infrastructure.highavailable.faultolerence.StopAgentFault;
import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.agent.RunScenarioModel; import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.share.models.agent.StopTestModel; import org.bench4q.share.models.agent.StopTestModel;
@ -235,7 +235,6 @@ public class TestPlanScript implements RunningScriptInterface {
agentsAfterDistribute, this)); agentsAfterDistribute, this));
this.setSampler(new RunningScriptSampler(Collections this.setSampler(new RunningScriptSampler(Collections
.unmodifiableCollection(this.getRunningAgentsDB()))); .unmodifiableCollection(this.getRunningAgentsDB())));
this.scheduleAsConfig();
} }
public List<TestPlanScriptResult> doAfterRun() { public List<TestPlanScriptResult> doAfterRun() {
@ -253,6 +252,29 @@ public class TestPlanScript implements RunningScriptInterface {
} }
} }
public boolean distributeLoad() {
return this.distributeLoad(this.getRequireLoad());
}
@SuppressWarnings("unchecked")
public boolean distributeLoad(int requiredLoad) {
Transaction scriptLoadCommand = TransactionFactory
.buildScriptTransaction(this, this.getRequireLoad());
List<? extends RunningAgentInterface> runningAgents = null;
try {
runningAgents = (List<? extends RunningAgentInterface>) scriptLoadCommand
.execute();
} catch (Exception e) {
logger.error("distribute load error, the runningAgents is null", e);
scriptLoadCommand.rollBack();
return false;
}
this.doAfterDistributeLoad(runningAgents);
this.scheduleAsConfig();
// this.getTestPlanRepository().updateEntity(this.getTestPlan());
return true;
}
public TestPlanScript doForComplete() { public TestPlanScript doForComplete() {
for (RunningAgentInterface runningAgent : this.getRunningAgentsDB()) { for (RunningAgentInterface runningAgent : this.getRunningAgentsDB()) {
if (runningAgent == null) { if (runningAgent == null) {
@ -263,7 +285,6 @@ public class TestPlanScript implements RunningScriptInterface {
if (resultModel == null || !resultModel.isSuccess()) { if (resultModel == null || !resultModel.isSuccess()) {
logger.error("can't stop the agent with hostName" logger.error("can't stop the agent with hostName"
+ runningAgent.getAgent().getHostName()); + runningAgent.getAgent().getHostName());
doFaultTolerance(runningAgent);
continue; continue;
} }
backLoadAndCleanUp(runningAgent); backLoadAndCleanUp(runningAgent);
@ -272,28 +293,10 @@ public class TestPlanScript implements RunningScriptInterface {
return this; return this;
} }
private void doFaultTolerance(RunningAgentInterface runningAgent) {
logger.error("when stop agent with hostName "
+ runningAgent.getAgent().getHostName() + " returns null!");
StopAgentFault stopAgentFault = new StopAgentFault(
AgentRunBlotter.buildAgentRunBlotter(this.getTestPlanID(),
runningAgent));
stopAgentFault.doTolerance();
}
private void backLoadAndCleanUp(RunningAgentInterface runningAgent) { private void backLoadAndCleanUp(RunningAgentInterface runningAgent) {
String hostName = runningAgent.getAgent().getHostName(); String hostName = runningAgent.getAgent().getHostName();
this.getAgentService().backLoadToAgent(hostName, this.getAgentService().backLoadToAgent(hostName,
runningAgent.getLoadInUse()); runningAgent.getLoadInUse());
} }
public RunningAgentDB extractSpecificRunningAgentDB(String hostName) {
for (RunningAgentDB runningAgentDB : this.getRunningAgentsDB()) {
if (runningAgentDB.getAgent().getHostName().equals(hostName)) {
return runningAgentDB;
}
}
return null;
}
} }

View File

@ -16,6 +16,7 @@ import org.bench4q.share.models.agent.RunScenarioModel;
import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
@Component @Component
public class ScriptService { public class ScriptService {
@ -41,11 +42,20 @@ public class ScriptService {
this.userRepository = userRepository; this.userRepository = userRepository;
} }
/**
*
* @param scriptName
* @param userId
* @param scriptContent
* @param paramFiles
* , this param may be null, and is permitted so.
* @return
*/
public boolean saveScript(String scriptName, int userId, public boolean saveScript(String scriptName, int userId,
String scriptContent) { String scriptContent, List<MultipartFile> paramFiles) {
return this.getScriptRepositoty().attach( return this.getScriptRepositoty().attach(
Script.createScriptWithoutId(scriptName, scriptContent, this Script.createScriptWithoutId(scriptName, scriptContent, this
.getUserRepository().getEntity(userId))); .getUserRepository().getEntity(userId), paramFiles));
} }
public Script getScript(int scriptId) { public Script getScript(int scriptId) {

View File

@ -118,27 +118,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
return; return;
} }
TestPlanStatus returnStatus = testPlan.run(); TestPlanStatus returnStatus = testPlan.run();
commitTestPlanStaus(returnStatus, testPlan); testPlan.setCurrentStatus(returnStatus.name());
}
private void commitTestPlanStaus(TestPlanStatus returnStatus,
TestPlan testPlan) {
if (returnStatus == TestPlanStatus.InRunning) {
commitInRunning(testPlan);
} else {
commitError(testPlan, returnStatus);
}
}
private void commitError(TestPlan testPlan, TestPlanStatus errorStatus) {
testPlan.setCurrentStatus(errorStatus.name());
getTestPlanRepository().updateEntity(testPlan);
}
private void commitInRunning(TestPlan testPlan) {
testPlan.setCurrentStatus(TestPlanStatus.InRunning.name());
testPlan.setLastRunningTime(new Date());
getTestPlanRepository().attachRunningTestPlan(testPlan);
getTestPlanRepository().updateEntity(testPlan); getTestPlanRepository().updateEntity(testPlan);
} }
@ -159,11 +139,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
this.getTestPlanRepository().updateEntity(testPlan); this.getTestPlanRepository().updateEntity(testPlan);
} }
public void executePendingTestPlan() { public synchronized TestPlan pickATestPlan() {
}
public TestPlan pickATestPlan() {
logger.info("enter pick"); logger.info("enter pick");
try { try {
List<Criterion> criterions = new ArrayList<Criterion>(); List<Criterion> criterions = new ArrayList<Criterion>();
@ -191,4 +167,9 @@ public class TestPlanEngine implements TaskCompleteCallback,
return null; return null;
} }
} }
@Override
public void executePendingTestPlan() {
pickATestPlan();
}
} }

View File

@ -1,15 +1,8 @@
package org.bench4q.master.domain.valueobject; package org.bench4q.master.domain.valueobject;
import java.util.List;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.valueobject.transaction.Transaction;
import org.bench4q.master.domain.valueobject.transaction.TransactionFactory;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,7 +12,6 @@ import org.springframework.stereotype.Component;
public class LoadDistribute { public class LoadDistribute {
private HighAvailablePool highAvailableAgentPool; private HighAvailablePool highAvailableAgentPool;
private TestPlanRepository testPlanRepository;
private static Logger logger = Logger.getLogger(LoadDistribute.class); private static Logger logger = Logger.getLogger(LoadDistribute.class);
public HighAvailablePool getHighAvailableAgentPool() { public HighAvailablePool getHighAvailableAgentPool() {
@ -32,15 +24,6 @@ public class LoadDistribute {
this.highAvailableAgentPool = highAvailableAgentPool; this.highAvailableAgentPool = highAvailableAgentPool;
} }
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
@Autowired
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
}
private boolean hasEnoughCurrentLoad(int totalRequireLoad) { private boolean hasEnoughCurrentLoad(int totalRequireLoad) {
return this.highAvailableAgentPool.getCurrentAvailableLoad() >= totalRequireLoad; return this.highAvailableAgentPool.getCurrentAvailableLoad() >= totalRequireLoad;
} }
@ -59,9 +42,9 @@ public class LoadDistribute {
logger.info("There is no enough current load in the pool!"); logger.info("There is no enough current load in the pool!");
return TestPlanStatus.PendingNoEnoughCurrentLoad; return TestPlanStatus.PendingNoEnoughCurrentLoad;
} }
for (RunningScriptInterface testPlanScript : testPlanInDomain for (TestPlanScript testPlanScript : testPlanInDomain
.getTestPlanScripts()) { .getTestPlanScripts()) {
if (!distributeLoadForScript(testPlanScript)) { if (!testPlanScript.distributeLoad()) {
return TestPlanStatus.ErrorInDistributeLoadForScript; return TestPlanStatus.ErrorInDistributeLoadForScript;
} }
} }
@ -75,43 +58,4 @@ public class LoadDistribute {
testPlanInDomain.doAfterRun(); testPlanInDomain.doAfterRun();
} }
private boolean distributeLoadForScript(RunningScriptInterface runningScript) {
return distributeLoadForScript(runningScript,
runningScript.getRequireLoad());
}
@SuppressWarnings("unchecked")
public boolean distributeLoadForScript(
RunningScriptInterface runningScript, int requireLoad) {
Transaction scriptLoadCommand = TransactionFactory
.buildScriptTransaction(runningScript, requireLoad);
List<? extends RunningAgentInterface> runningAgents = null;
try {
runningAgents = (List<? extends RunningAgentInterface>) scriptLoadCommand
.execute();
} catch (Exception e) {
logger.error("distribute load error, the runningAgents is null");
scriptLoadCommand.rollBack();
return false;
}
if (runningAgents == null) {
logger.error("distribute load error, the runningAgents is null");
scriptLoadCommand.rollBack();
return false;
}
doAfterDistributeLoadSuccessForScript(runningScript, runningAgents);
return true;
}
private void doAfterDistributeLoadSuccessForScript(
RunningScriptInterface scriptInput,
List<? extends RunningAgentInterface> runningAgents) {
scriptInput.doAfterDistributeLoad(runningAgents);
if (!(scriptInput instanceof TestPlanScript)) {
return;
}
this.getTestPlanRepository().updateEntity(
((TestPlanScript) scriptInput).getTestPlan());
}
} }

View File

@ -105,19 +105,14 @@ public abstract class ScriptLoadBase implements Transaction {
throw new IllegalArgumentException("runScenarioModel is null!"); throw new IllegalArgumentException("runScenarioModel is null!");
} }
try { try {
this.runAgentsWithScenario(runScenarioModel, getRequireLoad(), this
.getRunningScript().getScriptId(), this.getTestPlanRunID());
if (this.getAgentListThisTime() == null) {
logger.error("runAgentsWithScenario fails where scriptId = "
+ this.getRunningScript().getScriptId());
return null;
}
if (isNotValidScript(this.getRunningScript())) { if (isNotValidScript(this.getRunningScript())) {
logger.error("The running agents of runningScriptModel is null, whose scriptId is" logger.error("The running agents of runningScriptModel is null, whose scriptId is"
+ this.getRunningScript().getScriptId()); + this.getRunningScript().getScriptId());
return null; throw new RuntimeException("Running Script not valid!");
} }
this.runAgentsWithScenario(runScenarioModel, getRequireLoad(), this
.getRunningScript().getScriptId(), this.getTestPlanRunID());
return this.getAgentListThisTime(); return this.getAgentListThisTime();
} catch (Exception e) { } catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e).toString() logger.error(ExceptionLog.getStackTrace(e).toString()
@ -125,7 +120,7 @@ public abstract class ScriptLoadBase implements Transaction {
+ this.getRunningScript().getScriptId() + this.getRunningScript().getScriptId()
+ " and TestPlanID is " + " and TestPlanID is "
+ this.getTestPlanRunID().toString()); + this.getTestPlanRunID().toString());
return null; throw new RuntimeException(e);
} }
} }

View File

@ -73,9 +73,10 @@ public class AgentMessenger {
try { try {
final String modelCOntent = MarshalHelper final String modelCOntent = MarshalHelper
.tryMarshal(runScenarioModel); .tryMarshal(runScenarioModel);
httpResponse = this.httpRequester.postFiles(buildBaseUrl(agent) httpResponse = this.httpRequester.postFiles(null,
+ "/test/submitScenarioWithParams", "files[]", paramFiles, buildBaseUrl(agent) + "/test/submitScenarioWithParams",
"scenarioModel", new LinkedList<String>() { "files[]", paramFiles, "scenarioModel",
new LinkedList<String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
{ {
add(modelCOntent); add(modelCOntent);
@ -112,7 +113,8 @@ public class AgentMessenger {
return null; return null;
} }
} }
//there is bug in here
// there is bug in here
public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId) { public TestBriefStatusModel scriptBriefAll(Agent agent, UUID runId) {
HttpResponse httpResponse = null; HttpResponse httpResponse = null;
try { try {
@ -129,7 +131,8 @@ public class AgentMessenger {
TestBriefStatusModel.class, httpResponse.getContent()); TestBriefStatusModel.class, httpResponse.getContent());
} catch (Exception e) { } catch (Exception e) {
logIt(httpResponse, e); logIt(httpResponse, e);
logger.error(e.toString() + " When get script all brief the agent with hostName " logger.error(e.toString()
+ " When get script all brief the agent with hostName "
+ agent.getHostName()); + agent.getHostName());
FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance(); FaultTolerenceFactory.getBriefFaultTolerance(agent).doTolerance();
return null; return null;

View File

@ -7,7 +7,6 @@ import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript; import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.valueobject.LoadDistribute;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
public class AgentRunBlotter { public class AgentRunBlotter {
@ -15,7 +14,6 @@ public class AgentRunBlotter {
private UUID testPlanId; private UUID testPlanId;
private boolean hasSubstitute; private boolean hasSubstitute;
private TestPlanRepository testPlanRepository; private TestPlanRepository testPlanRepository;
private LoadDistribute loadDistribute;
public RunningAgentInterface getRunningAgent() { public RunningAgentInterface getRunningAgent() {
return runningAgent; return runningAgent;
@ -49,20 +47,10 @@ public class AgentRunBlotter {
this.testPlanRepository = testPlanRepository; this.testPlanRepository = testPlanRepository;
} }
private LoadDistribute getLoadDistribute() {
return loadDistribute;
}
private void setLoadDistribute(LoadDistribute loadDistribute) {
this.loadDistribute = loadDistribute;
}
private AgentRunBlotter() { private AgentRunBlotter() {
this.setHasSubstitute(false); this.setHasSubstitute(false);
this.setTestPlanRepository(ApplicationContextHelper.getContext() this.setTestPlanRepository(ApplicationContextHelper.getContext()
.getBean(TestPlanRepository.class)); .getBean(TestPlanRepository.class));
this.setLoadDistribute(ApplicationContextHelper.getContext().getBean(
LoadDistribute.class));
} }
private AgentRunBlotter(UUID testPlanID, RunningAgentInterface runningAgent) { private AgentRunBlotter(UUID testPlanID, RunningAgentInterface runningAgent) {
@ -92,8 +80,8 @@ public class AgentRunBlotter {
} }
final TestPlanScript testPlanScript = testPlan final TestPlanScript testPlanScript = testPlan
.extracSpecifiedScript(getRunningAgent().getScriptId()); .extracSpecifiedScript(getRunningAgent().getScriptId());
this.getLoadDistribute().distributeLoadForScript(testPlanScript, testPlanScript.distributeLoad(this.getRunningAgent().getLoadInUse());
this.getRunningAgent().getLoadInUse());
handleAfterSubstituteOn(); handleAfterSubstituteOn();
} }
} }

View File

@ -3,3 +3,4 @@ pickTestPlanCycleInSeconds=60
maxFailTime=10 maxFailTime=10
minExcuteIntervalInSeconds=600 minExcuteIntervalInSeconds=600
minSampleCycleInSeconds=10 minSampleCycleInSeconds=10
scriptParamRootFolder=ScriptParameterization

View File

@ -10,4 +10,10 @@
<context:component-scan base-package="org.bench4q" /> <context:component-scan base-package="org.bench4q" />
<mvc:annotation-driven /> <mvc:annotation-driven />
<task:annotation-driven /> <task:annotation-driven />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="5000000" />
<property name="maxInMemorySize" value="5000000" />
</bean>
</beans> </beans>

View File

@ -6,6 +6,7 @@ import java.util.UUID;
import org.bench4q.master.domain.entity.RunningAgentDB; import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlan; import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.TestPlanRepository;
@ -165,8 +166,6 @@ public class TestBase_MakeUpTestPlan extends TestBase {
this.testPlanEngine = testPlanEngine; this.testPlanEngine = testPlanEngine;
} }
public UUID getTestPlanRunIdUuid() { public UUID getTestPlanRunIdUuid() {
return testPlanRunIdUuid; return testPlanRunIdUuid;
} }
@ -213,8 +212,8 @@ public class TestBase_MakeUpTestPlan extends TestBase {
TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy( TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy(
getTestPlanRunIdUuid()); getTestPlanRunIdUuid());
testPlan.setCurrentStatus(TestPlanStatus.Complete.name()); testPlan.setCurrentStatus(TestPlanStatus.Complete.name());
RunningAgentDB runningAgent = testPlan.extracSpecifiedScript( RunningAgentDB runningAgent = extractRunningAgent(
getScriptId()).extractSpecificRunningAgentDB( testPlan.extracSpecifiedScript(getScriptId()),
Test_AGENT_HOSTNAME); Test_AGENT_HOSTNAME);
if (runningAgent == null) if (runningAgent == null)
return; return;
@ -226,6 +225,17 @@ public class TestBase_MakeUpTestPlan extends TestBase {
this.getTestPlanRepository().detach(testPlan.getId()); this.getTestPlanRepository().detach(testPlan.getId());
} }
protected RunningAgentDB extractRunningAgent(TestPlanScript testPlanScript,
String hostName) {
for (RunningAgentDB runningAgentDB : testPlanScript
.getRunningAgentsDB()) {
if (runningAgentDB.getAgent().getHostName().equals(hostName)) {
return runningAgentDB;
}
}
return null;
}
protected UUID submitATestPlanWithOneScript() { protected UUID submitATestPlanWithOneScript() {
return submitATestPlan(1); return submitATestPlan(1);
} }

View File

@ -60,7 +60,6 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan {
TestPlanStatus returnStatus = testPlanInDomain.run(); TestPlanStatus returnStatus = testPlanInDomain.run();
assertEquals(TestPlanStatus.InRunning, returnStatus); assertEquals(TestPlanStatus.InRunning, returnStatus);
assertTrue(this.getTestPlanRepository().updateEntity(testPlanInDomain)); assertTrue(this.getTestPlanRepository().updateEntity(testPlanInDomain));
TestPlan testPlanAfterRun = this.getTestPlanRepository().getTestPlanBy( TestPlan testPlanAfterRun = this.getTestPlanRepository().getTestPlanBy(
getTestPlanRunIdUuid()); getTestPlanRunIdUuid());
@ -77,8 +76,8 @@ public class Test_TestPlan extends TestBase_MakeUpTestPlan {
testPlanScript.getRequireLoad()); testPlanScript.getRequireLoad());
assertNotNull(testPlanScript.getRunningAgentsDB()); assertNotNull(testPlanScript.getRunningAgentsDB());
assertEquals(1, testPlanScript.getRunningAgentsDB().size()); assertEquals(1, testPlanScript.getRunningAgentsDB().size());
RunningAgentDB runningAgentDB = testPlanScript RunningAgentDB runningAgentDB = extractRunningAgent(testPlanScript,
.extractSpecificRunningAgentDB(Test_AGENT_HOSTNAME); Test_AGENT_HOSTNAME);
assertNotNull(runningAgentDB); assertNotNull(runningAgentDB);
assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgentDB.getLoadInUse()); assertEquals(EACH_SCRIPT_LOAD_SMALLSCALE, runningAgentDB.getLoadInUse());
Thread.sleep(10000); Thread.sleep(10000);

View File

@ -85,7 +85,7 @@ public class Test_ScriptService {
user.getId(), user.getId(),
FileUtils.readFileToString(new File("Scripts" FileUtils.readFileToString(new File("Scripts"
+ System.getProperty("file.separator") + System.getProperty("file.separator")
+ "HBaseScript.xml"))); + "HBaseScript.xml")), null);
} }
private boolean doSaveAScript(User user) throws JAXBException { private boolean doSaveAScript(User user) throws JAXBException {
@ -93,7 +93,7 @@ public class Test_ScriptService {
"ad", "ad",
user.getId(), user.getId(),
MarshalHelper.marshal(RunScenarioModel.class, MarshalHelper.marshal(RunScenarioModel.class,
new RunScenarioModel())); new RunScenarioModel()), null);
} }
@Before @Before
@ -110,7 +110,7 @@ public class Test_ScriptService {
"adef", "adef",
user.getId(), user.getId(),
MarshalHelper.marshal(RunScenarioModel.class, MarshalHelper.marshal(RunScenarioModel.class,
new RunScenarioModel()))); new RunScenarioModel()), null));
assertEquals(1, this.getScriptService().loadScripts(user).size()); assertEquals(1, this.getScriptService().loadScripts(user).size());
} }

View File

@ -13,6 +13,7 @@ import java.nio.charset.Charset;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PostMethod;
@ -110,10 +111,16 @@ public class HttpRequester {
return httpResponse; return httpResponse;
} }
public HttpResponse postFiles(String url, String filePartName, public HttpResponse postFiles(Map<String, String> headers, String url,
List<File> files, String stringPartName, List<String> strings) { String filePartName, List<File> files, String stringPartName,
List<String> strings) {
PostMethod postMethod = new PostMethod(url); PostMethod postMethod = new PostMethod(url);
try { try {
if (headers != null) {
for (Entry<String, String> entry : headers.entrySet()) {
postMethod.addParameter(entry.getKey(), entry.getValue());
}
}
Part[] parts = new Part[files.size() + strings.size()]; Part[] parts = new Part[files.size() + strings.size()];
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
parts[i] = new FilePart(filePartName, files.get(i)); parts[i] = new FilePart(filePartName, files.get(i));

View File

@ -0,0 +1,15 @@
package org.bench4q.share.helper;
import java.io.File;
public class FileHelper {
public static void guardFolderExist(String folderFullPath) {
guardFolderExist(new File(folderFullPath));
}
public static void guardFolderExist(File folder) {
if (!folder.exists()) {
folder.mkdirs();
}
}
}

View File

@ -0,0 +1,2 @@
123,234,566
167,567,789

View File

@ -4,6 +4,8 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
@ -13,7 +15,9 @@ import org.apache.log4j.Logger;
import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester;
import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.ExceptionLog; import org.bench4q.share.helper.ExceptionLog;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.ErrorResponseModel; import org.bench4q.share.models.ErrorResponseModel;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.web.api.BaseControllerService; import org.bench4q.web.api.BaseControllerService;
import org.bench4q.web.exception.CustomGenericException; import org.bench4q.web.exception.CustomGenericException;
import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange; import org.bench4q.web.extractObjectFromXml.ObjectXmlExchange;
@ -70,6 +74,18 @@ public class CommunicateWithMaster {
} }
public HttpResponse uploadScriptWithParamFiles(String accessToken,
String scriptName, RunScenarioModel runScenarioModel,
List<File> paramFiles) {
String url = masterAddress + "/uploadScript/" + scriptName;
List<String> stringPart = new LinkedList<String>();
stringPart.add(MarshalHelper.tryMarshal(runScenarioModel));
return this.getHttpRequester().postFiles(
makeAccessTockenMap(accessToken), url, "paramFiles[]",
paramFiles, "", stringPart);
}
public Object getRespnseObjectByPostXml(String accessToken, String url, public Object getRespnseObjectByPostXml(String accessToken, String url,
Class<?> objectClass, String content, String caller) Class<?> objectClass, String content, String caller)
throws CustomGenericException { throws CustomGenericException {

View File

@ -1 +1 @@
masterAddress=133.133.12.1:7979/ masterAddress=127.0.0.1:7979/

View File

@ -0,0 +1,35 @@
package org.bench4q.web.tool.test;
import static org.junit.Assert.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.TestHelper;
import org.bench4q.share.models.agent.RunScenarioModel;
import org.bench4q.web.service.CommunicateWithMaster;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class Test_CommunicateWithMaster {
private CommunicateWithMaster communicateWithMaster = new CommunicateWithMaster();
private LoginHelper loginHelper = new LoginHelper();
@Test
public void test_uploadScriptWithParamFiles() {
String accessToken = this.loginHelper.login();
List<File> paramFiles = new ArrayList<File>();
String filePath = "ScriptParameters"
+ System.getProperty("file.separator") + "param1.txt";
TestHelper.createFileIfNotExist(new File(filePath));
paramFiles.add(new File(filePath));
HttpResponse httpResponse = this.communicateWithMaster
.uploadScriptWithParamFiles(accessToken, "test_chen",
new RunScenarioModel(), paramFiles);
assertEquals(200, httpResponse.getCode());
}
}