update and refa

This commit is contained in:
coderfengyun 2014-03-03 17:34:20 +08:00
parent 29ae40c72d
commit 5bd30b0dce
9 changed files with 97 additions and 104 deletions

View File

@ -12,6 +12,7 @@ import org.apache.log4j.Logger;
*/ */
public class Main { public class Main {
private static Logger logger = Logger.getLogger(Main.class); private static Logger logger = Logger.getLogger(Main.class);
public static int Min_Sample_Cycle_InSecond = 10;
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;
@ -42,11 +43,14 @@ public class Main {
.getProperty("minExcuteIntervalInSeconds")); .getProperty("minExcuteIntervalInSeconds"));
PICK_CYCLE_IN_SECONDS = Integer.parseInt(prop PICK_CYCLE_IN_SECONDS = Integer.parseInt(prop
.getProperty("pickTestPlanCycleInSeconds")); .getProperty("pickTestPlanCycleInSeconds"));
Min_Sample_Cycle_InSecond = Integer.parseInt(prop
.getProperty("minSampleCycleInSeconds"));
} 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;
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

@ -261,7 +261,7 @@ public class TestPlanScript extends Observable implements
notifyObserver(getSampler().getPagesBrief()); notifyObserver(getSampler().getPagesBrief());
notifyObserver(getSampler().getBehaviorsBrief()); notifyObserver(getSampler().getBehaviorsBrief());
} }
}, 0, 10000); }, 0, this.getTestPlan().getSampleCycleInSeconds() * 1000);
} }
private void notifyObserver(SampleModel sample) { private void notifyObserver(SampleModel sample) {

View File

@ -7,11 +7,16 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger;
import org.bench4q.master.Main;
import org.bench4q.master.domain.entity.Monitor; import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.PlanedConfig; import org.bench4q.master.domain.entity.PlanedConfig;
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.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.entity.User; import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.interfaces.RunningAgentInterface; import org.bench4q.master.domain.interfaces.RunningAgentInterface;
import org.bench4q.master.domain.repository.AgentRepository; import org.bench4q.master.domain.repository.AgentRepository;
@ -22,17 +27,19 @@ import org.bench4q.master.domain.testplan.LoadDistribute;
import org.bench4q.master.helper.ApplicationContextHelper; import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.master.infrastructure.communication.AgentMessenger; import org.bench4q.master.infrastructure.communication.AgentMessenger;
import org.bench4q.share.enums.master.TestPlanStatus; import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.MonitorModel; import org.bench4q.share.models.master.MonitorModel;
import org.bench4q.share.models.master.RunningScriptModel; import org.bench4q.share.models.master.RunningScriptModel;
import org.bench4q.share.models.master.TestPlanModel; import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.TestScriptConfig; import org.bench4q.share.models.master.TestScriptConfig;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException; import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@Component @Component
public class TestPlanFactory { public class TestPlanFactory {
private static final int Min_Sample_Cycle_InSecond = 10; private Logger logger = Logger.getLogger(TestPlanFactory.class);
private ScriptService scriptService; private ScriptService scriptService;
private AgentRepository agentRepository; private AgentRepository agentRepository;
@ -96,7 +103,7 @@ public class TestPlanFactory {
private int getLargerOneBetweenUserOptionAndMinInSystem( private int getLargerOneBetweenUserOptionAndMinInSystem(
TestPlanModel testPlanModel) { TestPlanModel testPlanModel) {
return testPlanModel.getSampleCycleInSeconds() <= Min_Sample_Cycle_InSecond ? Min_Sample_Cycle_InSecond return testPlanModel.getSampleCycleInSeconds() <= Main.Min_Sample_Cycle_InSecond ? Main.Min_Sample_Cycle_InSecond
: testPlanModel.getSampleCycleInSeconds(); : testPlanModel.getSampleCycleInSeconds();
} }
@ -118,6 +125,22 @@ public class TestPlanFactory {
return planedConfig; return planedConfig;
} }
public TestPlanScriptResult createScriptResultWithOutId(Object model,
TestPlanScript testPlanScript, Date createDatetime) {
TestPlanScriptResult result = new TestPlanScriptResult();
result.setTestPlanScript(testPlanScript);
try {
result.setResultContent(MarshalHelper.marshal(model.getClass(),
model));
result.setResultType(model.getClass().getName());
} catch (JAXBException e) {
logger.error(ExceptionLog.getStackTrace(e));
result.setResultContent("");
}
result.setCreateDatetime(createDatetime);
return result;
}
public TestPlan convertToDomain(TestPlan testPlanInRepo) { public TestPlan convertToDomain(TestPlan testPlanInRepo) {
if (testPlanInRepo == null) { if (testPlanInRepo == null) {
return null; return null;

View File

@ -102,6 +102,13 @@ public class TestPlanRepository extends AbstractRepositoty {
return this.getTestPlanFactory().convertToDomain(result); return this.getTestPlanFactory().convertToDomain(result);
} }
public TestPlan doGetTestPlanBy(Session session, UUID testPlanRunId) {
return (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId", testPlanRunId.toString()))
.uniqueResult();
}
public List<TestPlan> loadEntities(User user) { public List<TestPlan> loadEntities(User user) {
Session session = this.getSessionHelper().openSession(); Session session = this.getSessionHelper().openSession();
try { try {
@ -154,6 +161,17 @@ public class TestPlanRepository extends AbstractRepositoty {
} }
} }
public void doUpdateEntity(Session session, TestPlan testPlan) {
Transaction transaction = session.beginTransaction();
try {
session.update(testPlan);
transaction.commit();
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
}
}
public TestPlan getRunningTestPlanBy(UUID testPlanRunId) { public TestPlan getRunningTestPlanBy(UUID testPlanRunId) {
return this.getRunningTestPlans().get(testPlanRunId); return this.getRunningTestPlans().get(testPlanRunId);
} }

View File

@ -7,25 +7,18 @@ import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Script;
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.TestPlanScriptResult; import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.interfaces.RunningScriptInterface; import org.bench4q.master.domain.interfaces.RunningScriptInterface;
import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper; import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.statistics.ScriptBehaviorsBriefModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel; import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.bench4q.share.models.master.statistics.ScriptPageBriefModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.bench4q.share.models.master.statistics.SampleModel; import org.bench4q.share.models.master.statistics.SampleModel;
import org.hibernate.Session; import org.hibernate.Session;
import org.hibernate.Transaction;
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;
@ -35,6 +28,7 @@ public class TestPlanScriptService implements Observer {
private static Logger logger = Logger.getLogger(TestPlanService.class); private static Logger logger = Logger.getLogger(TestPlanService.class);
private SessionHelper sessionHelper; private SessionHelper sessionHelper;
private TestPlanRepository testPlanRepository; private TestPlanRepository testPlanRepository;
private TestPlanFactory testPlanFactory;
private SessionHelper getSessionHelper() { private SessionHelper getSessionHelper() {
return sessionHelper; return sessionHelper;
@ -54,6 +48,15 @@ public class TestPlanScriptService implements Observer {
this.testPlanRepository = testPlanRepository; this.testPlanRepository = testPlanRepository;
} }
private TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
}
@Autowired
private void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) { public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) {
TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy( TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy(
testPlanRunId); testPlanRunId);
@ -61,65 +64,23 @@ public class TestPlanScriptService implements Observer {
.extracSpecifiedScript(scriptId); .extracSpecifiedScript(scriptId);
} }
public boolean saveScriptBriefResult(final UUID testPlanRunId, public void doSaveResult(UUID testPlanRunId, int scriptId,
final int scriptId, final ScriptBriefResultModel resultModel) {
return saveResultPrivate(testPlanRunId, scriptId, resultModel);
}
public boolean saveBehaviorsBriefResult(final UUID testPlanRunId,
final int scriptId,
final ScriptBehaviorsBriefModel scriptBehaviorsBriefModel) {
return saveResultPrivate(testPlanRunId, scriptId,
scriptBehaviorsBriefModel);
}
public boolean savePageBriefResult(final UUID testPlanRunId,
final int scriptId, final ScriptPageBriefModel pageBriefModel) {
return saveResultPrivate(testPlanRunId, scriptId, pageBriefModel);
}
public boolean savePagesBriefResult(final UUID testPlanRunId,
final int scriptId, final ScriptPagesBriefModel pagesBriefModel) {
return saveResultPrivate(testPlanRunId, scriptId, pagesBriefModel);
}
private boolean saveResultPrivate(final UUID testPlanRunId,
final int scriptId, final Object resultModel) {
doSaveResult(testPlanRunId, scriptId, resultModel, new Date());
return true;
}
private void doSaveResult(UUID testPlanRunId, int scriptId,
Object resultModel, Date now) { Object resultModel, Date now) {
Session session = this.getSessionHelper().openSession(); Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try { try {
TestPlan testPlanDB = (TestPlan) session TestPlan testPlan = this.getTestPlanRepository().doGetTestPlanBy(
.createCriteria(TestPlan.class) session, testPlanRunId);
.add(Restrictions.eq("testPlanRunId", if (testPlan == null) {
testPlanRunId.toString())).uniqueResult();
if (testPlanDB == null) {
return; return;
} }
Script script = (Script) session.createCriteria(Script.class) TestPlanScript specifiedScript = testPlan
.add(Restrictions.eq("id", scriptId)).uniqueResult(); .extracSpecifiedScript(scriptId);
if (script == null) { specifiedScript.getTestPlanScriptResults().add(
return; this.getTestPlanFactory().createScriptResultWithOutId(
} resultModel, specifiedScript, now));
this.getTestPlanRepository().doUpdateEntity(session, testPlan);
TestPlanScript testPlanScript = testPlanDB
.extracSpecifiedScript(script.getId());
if (testPlanScript == null) {
return;
}
session.merge(buildScriptResultWithModel(resultModel,
testPlanScript, now));
transaction.commit();
return;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); logger.error(ExceptionLog.getStackTrace(e));
transaction.rollback();
return;
} finally { } finally {
if (session != null) { if (session != null) {
session.close(); session.close();
@ -127,22 +88,6 @@ public class TestPlanScriptService implements Observer {
} }
} }
private TestPlanScriptResult buildScriptResultWithModel(Object model,
TestPlanScript testPlanScript, Date createDatetime) {
TestPlanScriptResult result = new TestPlanScriptResult();
result.setTestPlanScript(testPlanScript);
try {
result.setResultContent(MarshalHelper.marshal(model.getClass(),
model));
result.setResultType(model.getClass().getName());
} catch (JAXBException e) {
logger.error(ExceptionLog.getStackTrace(e));
result.setResultContent("");
}
result.setCreateDatetime(createDatetime);
return result;
}
public Collection<TestPlanScript> queryTestPlanScripts(UUID testPlanRunId) { public Collection<TestPlanScript> queryTestPlanScripts(UUID testPlanRunId) {
return this.getTestPlanRepository().getTestPlanBy(testPlanRunId) return this.getTestPlanRepository().getTestPlanBy(testPlanRunId)
.getTestPlanScripts(); .getTestPlanScripts();

View File

@ -1,4 +1,5 @@
portToServe=7979 portToServe=7979
pickTestPlanCycleInSeconds=60 pickTestPlanCycleInSeconds=60
maxFailTime=10 maxFailTime=10
minExcuteIntervalInSeconds=600 minExcuteIntervalInSeconds=600
minSampleCycleInSeconds=10

View File

@ -3,6 +3,7 @@ package org.bench4q.master.test.controller;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.util.Date;
import java.util.UUID; import java.util.UUID;
import javax.xml.bind.JAXBException; import javax.xml.bind.JAXBException;
@ -44,21 +45,20 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan
UUID testPlanRunId = UUID.randomUUID(); UUID testPlanRunId = UUID.randomUUID();
User user = this.getUserRepository().getUser("admin"); User user = this.getUserRepository().getUser("admin");
this.setScriptId(this.getUserFirstScript(user)); this.setScriptId(this.getUserFirstScript(user));
TestPlanModel model = createATestPlanWithOneScript(this TestPlanModel model = createATestPlanWithOneScript(this.getScriptId());
.getScriptId());
// TestPlanInBusiness testPlan = // TestPlanInBusiness testPlan =
// BusinessModelMapFactory.toBusiness(model); // BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().submitTestPlan(model, user, testPlanRunId); this.getTestPlanService().submitTestPlan(model, user, testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
this.getTestPlanScriptService().saveScriptBriefResult( this.getTestPlanScriptService().doSaveResult(testPlanRunId,
testPlanRunId, this.getScriptId(), this.getScriptId(), buildScriptBriefResultModel(i),
buildScriptBriefResultModel(i)); new Date());
this.getTestPlanScriptService().saveBehaviorsBriefResult( this.getTestPlanScriptService().doSaveResult(testPlanRunId,
testPlanRunId, this.getScriptId(), this.getScriptId(), new ScriptBehaviorsBriefModel(),
new ScriptBehaviorsBriefModel()); new Date());
this.getTestPlanScriptService().savePageBriefResult(testPlanRunId, this.getTestPlanScriptService().doSaveResult(testPlanRunId,
this.getScriptId(), new ScriptPageBriefModel()); this.getScriptId(), new ScriptPageBriefModel(), new Date());
} }
} }

View File

@ -2,6 +2,7 @@ package org.bench4q.master.test.service;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -118,14 +119,15 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
this.getTestPlanService().submitTestPlan(model, user, testPlanRunId); this.getTestPlanService().submitTestPlan(model, user, testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++) {
this.getTestPlanScriptService().saveScriptBriefResult( this.getTestPlanScriptService().doSaveResult(testPlanRunId,
testPlanRunId, this.getScriptId(), this.getScriptId(), buildScriptBriefResultModel(i),
buildScriptBriefResultModel(i)); new Date());
this.getTestPlanScriptService().saveBehaviorsBriefResult( this.getTestPlanScriptService().doSaveResult(testPlanRunId,
testPlanRunId, this.getScriptId(), this.getScriptId(), new ScriptBehaviorsBriefModel(),
new ScriptBehaviorsBriefModel()); new Date());
this.getTestPlanScriptService().savePagesBriefResult(testPlanRunId, this.getTestPlanScriptService().doSaveResult(testPlanRunId,
this.getScriptId(), makeUpScriptPagesBriefModel(i)); this.getScriptId(), makeUpScriptPagesBriefModel(i),
new Date());
} }
} }

View File

@ -2,6 +2,7 @@ package org.bench4q.master.test.service;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -26,9 +27,8 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
public void prepare() { public void prepare() {
submitATestPlanWithOneScript(); submitATestPlanWithOneScript();
System.out.println(getScriptId()); System.out.println(getScriptId());
this.getTestPlanScriptService().saveScriptBriefResult( this.getTestPlanScriptService().doSaveResult(getTestPlanRunIdUuid(),
getTestPlanRunIdUuid(), getScriptId(), getScriptId(), new ScriptBriefResultModel(), new Date());
new ScriptBriefResultModel());
} }
@After @After
@ -66,9 +66,9 @@ public class Test_TestPlanScriptService extends TestBase_MakeUpTestPlan {
briefResultModel.setFailRateThisTime(0); briefResultModel.setFailRateThisTime(0);
briefResultModel.setFailThroughputThisTime(0); briefResultModel.setFailThroughputThisTime(0);
briefResultModel.setFinished(true); briefResultModel.setFinished(true);
this.getTestPlanScriptService().saveScriptBriefResult( this.getTestPlanScriptService().doSaveResult(
UUID.fromString(WRONG_TEST_PLAN_RUN_ID), WRONG_SCRIPT_ID, UUID.fromString(WRONG_TEST_PLAN_RUN_ID), WRONG_SCRIPT_ID,
briefResultModel); briefResultModel, new Date());
TestPlanScript testPlanScript = this.getTestPlanScriptService() TestPlanScript testPlanScript = this.getTestPlanScriptService()
.getTestPlanScript(WRONG_SCRIPT_ID, .getTestPlanScript(WRONG_SCRIPT_ID,
UUID.fromString(WRONG_TEST_PLAN_RUN_ID)); UUID.fromString(WRONG_TEST_PLAN_RUN_ID));