refactor and try to log to front end

refactor and try to log to front end
This commit is contained in:
luqiong 2014-09-09 15:36:43 +08:00
parent a04a075ffe
commit 0ec2f364f7
11 changed files with 93 additions and 62 deletions

View File

@ -98,8 +98,8 @@ public class TestPlanController extends BaseController {
this.testPlanScriptResultService = testPlanScriptResultService; this.testPlanScriptResultService = testPlanScriptResultService;
} }
@RequestMapping(value = "/run", method = { @RequestMapping(value = "/run", method = { RequestMethod.POST,
RequestMethod.POST, RequestMethod.GET }) RequestMethod.GET })
@ResponseStatus(value = HttpStatus.OK) @ResponseStatus(value = HttpStatus.OK)
@ResponseBody @ResponseBody
public TestPlanResultModel runTestPlanWithTestPlanModel( public TestPlanResultModel runTestPlanWithTestPlanModel(
@ -107,15 +107,14 @@ public class TestPlanController extends BaseController {
throws Bench4QException { throws Bench4QException {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
throw new Bench4QException(HAVE_NO_POWER, throw new Bench4QException(HAVE_NO_POWER,
"You don't have enough power to run a test plan!", "You don't have enough power to run a test plan!", "/run");
"/run");
} }
UUID testPlanRunID = this.getTestPlanRunner().runWith( UUID testPlanRunID = null;
try {
testPlanRunID = this.getTestPlanRunner().runWith(
testPlanBusinessModel, this.getPrincipal()); testPlanBusinessModel, this.getPrincipal());
if (testPlanRunID == null) { } catch (Exception e) {
throw new Bench4QException("TestPlan_Commit_Error", throw new Bench4QException(e, "/run");
"There is an exception when commit the test plan",
"/run");
} }
return buildResponseModel(this.getTestPlanService() return buildResponseModel(this.getTestPlanService()
.queryTestPlanStatus(testPlanRunID), testPlanRunID, null, .queryTestPlanStatus(testPlanRunID), testPlanRunID, null,
@ -206,13 +205,13 @@ public class TestPlanController extends BaseController {
@ResponseBody @ResponseBody
public TestPlanResponseModel loadTestPlans() { public TestPlanResponseModel loadTestPlans() {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
return buildTestPlanResponseModel(false, "no scope", null,null); return buildTestPlanResponseModel(false, "no scope", null, null);
} }
List<TestPlan> testPlanDBs = this.testPlanService.loadTestPlans(this List<TestPlan> testPlanDBs = this.testPlanService.loadTestPlans(this
.getPrincipal()); .getPrincipal());
return testPlanDBs == null ? buildTestPlanResponseModel(false, return testPlanDBs == null ? buildTestPlanResponseModel(false,
"exception", null,null) : buildTestPlanResponseModel(true, null, "exception", null, null) : buildTestPlanResponseModel(true,
testPlanDBs,null); null, testPlanDBs, null);
} }
@RequestMapping(value = "/queryTestPlan/{runId}", method = RequestMethod.GET) @RequestMapping(value = "/queryTestPlan/{runId}", method = RequestMethod.GET)
@ -233,14 +232,16 @@ public class TestPlanController extends BaseController {
@ResponseBody @ResponseBody
public TestPlanResponseModel removeTestPlanFromPool(int testPlanId) { public TestPlanResponseModel removeTestPlanFromPool(int testPlanId) {
if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) {
return buildTestPlanResponseModel(false, "no scope", null,null); return buildTestPlanResponseModel(false, "no scope", null, null);
} }
return buildTestPlanResponseModel( return buildTestPlanResponseModel(
this.testPlanService.removeTestPlanInDB(testPlanId), null, null,null); this.testPlanService.removeTestPlanInDB(testPlanId), null,
null, null);
} }
private TestPlanResponseModel buildTestPlanResponseModel(boolean success, private TestPlanResponseModel buildTestPlanResponseModel(boolean success,
String failCause, List<TestPlan> testPlanDBs,String[] filterTypeList) { String failCause, List<TestPlan> testPlanDBs,
String[] filterTypeList) {
TestPlanResponseModel result = new TestPlanResponseModel(); TestPlanResponseModel result = new TestPlanResponseModel();
result.setSuccess(success); result.setSuccess(success);
result.setFailCause(failCause); result.setFailCause(failCause);
@ -313,7 +314,7 @@ public class TestPlanController extends BaseController {
guardIsTheOwner(testPlanId); guardIsTheOwner(testPlanId);
return buildTestPlanResponseModel( return buildTestPlanResponseModel(
this.getTestPlanRunner().stop(testPlanId), "", this.getTestPlanRunner().stop(testPlanId), "",
Collections.<TestPlan> emptyList(),null); Collections.<TestPlan> emptyList(), null);
} }
private void guardIsTheOwner(UUID testPlanId) { private void guardIsTheOwner(UUID testPlanId) {
@ -324,8 +325,8 @@ public class TestPlanController extends BaseController {
} }
} }
@RequestMapping(value = "/loadFilterTypeList", method = { RequestMethod.POST, @RequestMapping(value = "/loadFilterTypeList", method = {
RequestMethod.GET }) RequestMethod.POST, RequestMethod.GET })
@ResponseStatus(value = HttpStatus.OK) @ResponseStatus(value = HttpStatus.OK)
@ResponseBody @ResponseBody
public TestPlanResponseModel loadFilterTypeList() throws Bench4QException { public TestPlanResponseModel loadFilterTypeList() throws Bench4QException {
@ -334,7 +335,9 @@ public class TestPlanController extends BaseController {
"You don't have enough power to get filter type list!", "You don't have enough power to get filter type list!",
"/loadFilterTypeList"); "/loadFilterTypeList");
} }
String[] filterTypeList = this.getTestPlanService().loadFilterTypeList(); String[] filterTypeList = this.getTestPlanService()
return this.buildTestPlanResponseModel(true, null, null,filterTypeList); .loadFilterTypeList();
return this
.buildTestPlanResponseModel(true, null, null, filterTypeList);
} }
} }

View File

@ -103,8 +103,7 @@ public class TestPlanFactory {
public TestPlan createATestPlanWithoutId(TestPlanModel testPlanModel, public TestPlan createATestPlanWithoutId(TestPlanModel testPlanModel,
User user, UUID runId) throws IllegalParameterException { User user, UUID runId) throws IllegalParameterException {
Logger.getLogger(TestPlanFactory.class).info( this.logger.info("testPlanName:" + testPlanModel.getName());
"testPlanName:" + testPlanModel.getName());
TestPlan result = new TestPlan(); TestPlan result = new TestPlan();
result.setCreateDateTime(new Date()); result.setCreateDateTime(new Date());
result.setCurrentStatus(TestPlanStatus.NotStart.name()); result.setCurrentStatus(TestPlanStatus.NotStart.name());
@ -115,22 +114,24 @@ public class TestPlanFactory {
result.setTestPlanRunId(runId.toString()); result.setTestPlanRunId(runId.toString());
result.setUser(user); result.setUser(user);
Set<TestPlanScript> testPlanScripts = new HashSet<TestPlanScript>(); Set<TestPlanScript> testPlanScripts = new HashSet<TestPlanScript>();
int requiredLoad = 0; int totalRequiredLoad = 0;
for (RunningScriptModel runningScriptModel : testPlanModel for (RunningScriptModel runningScriptModel : testPlanModel
.getRunningScriptModels()) { .getRunningScriptModels()) {
if (runningScriptModel.getRequireLoad() <= 0) { this.logger.info(MarshalHelper.tryMarshal(runningScriptModel
.getScheduleModel()));
int scriptLoad = runningScriptModel.getScheduleModel().getMaxLoad();
if (scriptLoad <= 0) {
throw new IllegalParameterException( throw new IllegalParameterException(
"runningScriptModel's requireLoad is L.T. zero where scriptId is " "runningScriptModel's requireLoad is L.T. zero where scriptId is "
+ runningScriptModel.getScriptId()); + runningScriptModel.getScriptId());
} }
requiredLoad += runningScriptModel.getRequireLoad(); totalRequiredLoad += scriptLoad;
testPlanScripts.add(createATestPlanScriptWithoutId( testPlanScripts.add(createATestPlanScriptWithoutId(scriptLoad,
runningScriptModel.getRequireLoad(),
runningScriptModel.getScriptId(), runningScriptModel.getScriptId(),
runningScriptModel.getScheduleModel(), result, runningScriptModel.getScheduleModel(), result,
runningScriptModel.getScriptFilterOptionsModel())); runningScriptModel.getScriptFilterOptionsModel()));
} }
result.setRequiredLoad(requiredLoad); result.setRequiredLoad(totalRequiredLoad);
result.setTestPlanScripts(testPlanScripts); result.setTestPlanScripts(testPlanScripts);
Set<Monitor> monitors = new HashSet<Monitor>(); Set<Monitor> monitors = new HashSet<Monitor>();

View File

@ -17,7 +17,6 @@ import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.valueobject.schedulscript.TaskCompleteCallback; import org.bench4q.master.domain.valueobject.schedulscript.TaskCompleteCallback;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver; import org.bench4q.master.infrastructure.highavailable.CurrentLoadObserver;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
@ -94,14 +93,13 @@ public class TestPlanEngine implements TaskCompleteCallback,
}, 0, Main.PICK_CYCLE_IN_SECONDS, TimeUnit.SECONDS); }, 0, Main.PICK_CYCLE_IN_SECONDS, TimeUnit.SECONDS);
} }
public UUID runWith(final TestPlanModel testPlanModel, User user) { public UUID runWith(final TestPlanModel testPlanModel, User user)
throws IllegalParameterException {
ExecutorService executorService = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newCachedThreadPool();
final UUID testPlanId = UUID.randomUUID(); final UUID testPlanId = UUID.randomUUID();
logger.info("name:" + testPlanModel.getName() + " start to run"); logger.info("name:" + testPlanModel.getName() + " start to run");
logger.info(testPlanId + " start to run"); logger.info(testPlanId + " start to run");
if (!submitTestPlan(testPlanModel, user, testPlanId)) { submitTestPlan(testPlanModel, user, testPlanId);
return null;
}
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
public void run() { public void run() {
doRunTestPlan(testPlanId); doRunTestPlan(testPlanId);
@ -125,20 +123,13 @@ public class TestPlanEngine implements TaskCompleteCallback,
} }
public boolean submitTestPlan(final TestPlanModel testPlanBusinessModel, public boolean submitTestPlan(final TestPlanModel testPlanBusinessModel,
final User user, final UUID testPlanRunId) { final User user, final UUID testPlanRunId)
try { throws IllegalParameterException {
TestPlan testPlan = this.getTestPlanFactory() TestPlan testPlan = this.getTestPlanFactory().createATestPlanWithoutId(
.createATestPlanWithoutId(testPlanBusinessModel, user, testPlanBusinessModel, user, testPlanRunId);
testPlanRunId);
Logger.getLogger(TestPlanService.class).info( Logger.getLogger(TestPlanService.class).info(
"test plan name:" + testPlan.getName()); "test plan name:" + testPlan.getName());
return this.getTestPlanRepository().attach(testPlan); return this.getTestPlanRepository().attach(testPlan);
} catch (IllegalParameterException e) {
// TODO: give back the message
Logger.getLogger(TestPlanService.class).error(
ExceptionLog.getStackTrace(e) + e.getMessage());
return false;
}
} }
public void doRunTestPlan(final UUID testPlanId) { public void doRunTestPlan(final UUID testPlanId) {

View File

@ -44,4 +44,8 @@ public class Bench4QException extends Exception {
this.setMessage(message); this.setMessage(message);
this.setResource(resource); this.setResource(resource);
} }
public Bench4QException(Exception e, String resource) {
this("404", e.getMessage(), resource);
}
} }

View File

@ -23,6 +23,7 @@ import org.bench4q.master.domain.service.TestPlanScriptResultService;
import org.bench4q.master.domain.service.TestPlanScriptService; import org.bench4q.master.domain.service.TestPlanScriptService;
import org.bench4q.master.domain.service.TestPlanService; import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.domain.service.UserService; import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.SessionHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.master.infrastructure.highavailable.HighAvailablePool; import org.bench4q.master.infrastructure.highavailable.HighAvailablePool;
@ -317,7 +318,11 @@ public class TestBase_MakeUpTestPlan extends TestBase {
TestPlanModel model = null; TestPlanModel model = null;
model = createAtestPlanWithOneScriptAndName(load); model = createAtestPlanWithOneScriptAndName(load);
try {
this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId); this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
} catch (IllegalParameterException e) {
e.printStackTrace();
}
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
return testPlanRunId; return testPlanRunId;
} }

View File

@ -8,6 +8,7 @@ import java.util.UUID;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.entity.User;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.communication.HttpRequester.HttpResponse;
import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.helper.MarshalHelper;
@ -51,7 +52,12 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan
EACH_SCRIPT_LOAD_SMALLSCALE); EACH_SCRIPT_LOAD_SMALLSCALE);
// TestPlanInBusiness testPlan = // TestPlanInBusiness testPlan =
// BusinessModelMapFactory.toBusiness(model); // BusinessModelMapFactory.toBusiness(model);
try {
this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId); this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
} catch (IllegalParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
/* /*

View File

@ -14,6 +14,7 @@ import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.MonitorResultService; import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.exception.Bench4QException; import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.monitor.MemoryModel; import org.bench4q.share.models.monitor.MemoryModel;
@ -96,9 +97,13 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
User user = this.getUserRepository().getUser("admin"); User user = this.getUserRepository().getUser("admin");
int scriptId = getUserFirstScript(user); int scriptId = getUserFirstScript(user);
this.getHaPool().checkAllHeartBeat(); this.getHaPool().checkAllHeartBeat();
try {
this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith( this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
createATestPlanWithOneScript(scriptId, createATestPlanWithOneScript(scriptId,
EACH_SCRIPT_LOAD_SMALLSCALE), user)); EACH_SCRIPT_LOAD_SMALLSCALE), user));
} catch (IllegalParameterException e) {
e.printStackTrace();
}
Thread.sleep(500); Thread.sleep(500);
TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain( TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain(
this.getTestPlanRepository().getTestPlanInDomainBy( this.getTestPlanRepository().getTestPlanInDomainBy(

View File

@ -15,6 +15,7 @@ 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.ScriptRepositoty; import org.bench4q.master.domain.repository.ScriptRepositoty;
import org.bench4q.master.domain.service.AgentService; import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.TestHelper; import org.bench4q.share.helper.TestHelper;
@ -90,9 +91,13 @@ public class Test_TestPlanEngine extends TestBase_MakeUpTestPlan {
this.getHaPool().checkAllHeartBeat(); this.getHaPool().checkAllHeartBeat();
Date dateBeforeRun = new Date(); Date dateBeforeRun = new Date();
Thread.sleep(1000); Thread.sleep(1000);
try {
this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith( this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
createATestPlanWithOneScript(scriptId, createATestPlanWithOneScript(scriptId,
EACH_SCRIPT_LOAD_SMALLSCALE), user)); EACH_SCRIPT_LOAD_SMALLSCALE), user));
} catch (IllegalParameterException e) {
e.printStackTrace();
}
assertNotNull(getTestPlanRunIdUuid()); assertNotNull(getTestPlanRunIdUuid());
Thread.sleep(10000); Thread.sleep(10000);
TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy( TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(

View File

@ -9,6 +9,7 @@ import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory; import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.TestResultSave; import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel; import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
@ -65,7 +66,11 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
this.setScriptId(this.getUserFirstScript(user)); this.setScriptId(this.getUserFirstScript(user));
TestPlanModel model = createATestPlanWithOneScript(this.getScriptId(), TestPlanModel model = createATestPlanWithOneScript(this.getScriptId(),
EACH_SCRIPT_LOAD_SMALLSCALE); EACH_SCRIPT_LOAD_SMALLSCALE);
try {
this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId); this.getTestPlanEngine().submitTestPlan(model, user, testPlanRunId);
} catch (IllegalParameterException e) {
e.printStackTrace();
}
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
this.testPlanRepository.getTestPlanInDomainBy(getTestPlanRunIdUuid()); this.testPlanRepository.getTestPlanInDomainBy(getTestPlanRunIdUuid());

View File

@ -8,6 +8,7 @@ import java.util.UUID;
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.entity.User; import org.bench4q.master.domain.entity.User;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan; import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.master.TestPlanResultModel; import org.bench4q.share.models.master.TestPlanResultModel;
@ -52,9 +53,14 @@ public class Test_TestPlanService extends TestBase_MakeUpTestPlan {
.loadEntities(user).size(); .loadEntities(user).size();
System.out.println(planCountBeforeSubmit); System.out.println(planCountBeforeSubmit);
UUID randomUUID = UUID.randomUUID(); UUID randomUUID = UUID.randomUUID();
try {
assertTrue(this.getTestPlanEngine().submitTestPlan( assertTrue(this.getTestPlanEngine().submitTestPlan(
createATestPlanWithOneScript(getUserFirstScript(user), createATestPlanWithOneScript(getUserFirstScript(user),
EACH_SCRIPT_LOAD_SMALLSCALE), user, randomUUID)); EACH_SCRIPT_LOAD_SMALLSCALE), user, randomUUID));
} catch (IllegalParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int planCountAfterSubmit = this.getTestPlanRepository() int planCountAfterSubmit = this.getTestPlanRepository()
.loadEntities(user).size(); .loadEntities(user).size();
assertEquals(planCountBeforeSubmit + 1, planCountAfterSubmit); assertEquals(planCountBeforeSubmit + 1, planCountAfterSubmit);

View File

@ -1 +1 @@
masterAddress=133.133.2.100:8901 masterAddress=localhost:8901