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 {
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 MIN_EXECUTE_INTERVAL_IN_SECONDS = 600;
public static int PICK_CYCLE_IN_SECONDS = 60;
@ -42,11 +43,14 @@ public class Main {
.getProperty("minExcuteIntervalInSeconds"));
PICK_CYCLE_IN_SECONDS = Integer.parseInt(prop
.getProperty("pickTestPlanCycleInSeconds"));
Min_Sample_Cycle_InSecond = Integer.parseInt(prop
.getProperty("minSampleCycleInSeconds"));
} catch (Exception e) {
portToUse = 8080;
MAX_FAIL_TIMES = 10;
MIN_EXECUTE_INTERVAL_IN_SECONDS = 600;
PICK_CYCLE_IN_SECONDS = 60;
Min_Sample_Cycle_InSecond = 10;
logger.error("There is no config file for port to serve! where path is "
+ configFile);
}

View File

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

View File

@ -7,11 +7,16 @@ import java.util.List;
import java.util.Set;
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.PlanedConfig;
import org.bench4q.master.domain.entity.RunningAgentDB;
import org.bench4q.master.domain.entity.TestPlan;
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.interfaces.RunningAgentInterface;
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.infrastructure.communication.AgentMessenger;
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.RunningScriptModel;
import org.bench4q.share.models.master.TestPlanModel;
import org.bench4q.share.models.master.TestScriptConfig;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.exception.ExceptionUtils.IllegalParameterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestPlanFactory {
private static final int Min_Sample_Cycle_InSecond = 10;
private Logger logger = Logger.getLogger(TestPlanFactory.class);
private ScriptService scriptService;
private AgentRepository agentRepository;
@ -96,7 +103,7 @@ public class TestPlanFactory {
private int getLargerOneBetweenUserOptionAndMinInSystem(
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();
}
@ -118,6 +125,22 @@ public class TestPlanFactory {
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) {
if (testPlanInRepo == null) {
return null;

View File

@ -102,6 +102,13 @@ public class TestPlanRepository extends AbstractRepositoty {
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) {
Session session = this.getSessionHelper().openSession();
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) {
return this.getRunningTestPlans().get(testPlanRunId);
}

View File

@ -7,25 +7,18 @@ import java.util.Observable;
import java.util.Observer;
import java.util.UUID;
import javax.xml.bind.JAXBException;
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.TestPlanScript;
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.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog;
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.ScriptPageBriefModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.bench4q.share.models.master.statistics.SampleModel;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -35,6 +28,7 @@ public class TestPlanScriptService implements Observer {
private static Logger logger = Logger.getLogger(TestPlanService.class);
private SessionHelper sessionHelper;
private TestPlanRepository testPlanRepository;
private TestPlanFactory testPlanFactory;
private SessionHelper getSessionHelper() {
return sessionHelper;
@ -54,6 +48,15 @@ public class TestPlanScriptService implements Observer {
this.testPlanRepository = testPlanRepository;
}
private TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
}
@Autowired
private void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
public TestPlanScript getTestPlanScript(int scriptId, UUID testPlanRunId) {
TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy(
testPlanRunId);
@ -61,65 +64,23 @@ public class TestPlanScriptService implements Observer {
.extracSpecifiedScript(scriptId);
}
public boolean saveScriptBriefResult(final UUID testPlanRunId,
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,
public void doSaveResult(UUID testPlanRunId, int scriptId,
Object resultModel, Date now) {
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
TestPlan testPlanDB = (TestPlan) session
.createCriteria(TestPlan.class)
.add(Restrictions.eq("testPlanRunId",
testPlanRunId.toString())).uniqueResult();
if (testPlanDB == null) {
TestPlan testPlan = this.getTestPlanRepository().doGetTestPlanBy(
session, testPlanRunId);
if (testPlan == null) {
return;
}
Script script = (Script) session.createCriteria(Script.class)
.add(Restrictions.eq("id", scriptId)).uniqueResult();
if (script == null) {
return;
}
TestPlanScript testPlanScript = testPlanDB
.extracSpecifiedScript(script.getId());
if (testPlanScript == null) {
return;
}
session.merge(buildScriptResultWithModel(resultModel,
testPlanScript, now));
transaction.commit();
return;
TestPlanScript specifiedScript = testPlan
.extracSpecifiedScript(scriptId);
specifiedScript.getTestPlanScriptResults().add(
this.getTestPlanFactory().createScriptResultWithOutId(
resultModel, specifiedScript, now));
this.getTestPlanRepository().doUpdateEntity(session, testPlan);
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return;
logger.error(ExceptionLog.getStackTrace(e));
} finally {
if (session != null) {
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) {
return this.getTestPlanRepository().getTestPlanBy(testPlanRunId)
.getTestPlanScripts();

View File

@ -1,4 +1,5 @@
portToServe=7979
pickTestPlanCycleInSeconds=60
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 java.io.IOException;
import java.util.Date;
import java.util.UUID;
import javax.xml.bind.JAXBException;
@ -44,21 +45,20 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan
UUID testPlanRunId = UUID.randomUUID();
User user = this.getUserRepository().getUser("admin");
this.setScriptId(this.getUserFirstScript(user));
TestPlanModel model = createATestPlanWithOneScript(this
.getScriptId());
TestPlanModel model = createATestPlanWithOneScript(this.getScriptId());
// TestPlanInBusiness testPlan =
// BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().submitTestPlan(model, user, testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId);
for (int i = 0; i < 3; i++) {
this.getTestPlanScriptService().saveScriptBriefResult(
testPlanRunId, this.getScriptId(),
buildScriptBriefResultModel(i));
this.getTestPlanScriptService().saveBehaviorsBriefResult(
testPlanRunId, this.getScriptId(),
new ScriptBehaviorsBriefModel());
this.getTestPlanScriptService().savePageBriefResult(testPlanRunId,
this.getScriptId(), new ScriptPageBriefModel());
this.getTestPlanScriptService().doSaveResult(testPlanRunId,
this.getScriptId(), buildScriptBriefResultModel(i),
new Date());
this.getTestPlanScriptService().doSaveResult(testPlanRunId,
this.getScriptId(), new ScriptBehaviorsBriefModel(),
new Date());
this.getTestPlanScriptService().doSaveResult(testPlanRunId,
this.getScriptId(), new ScriptPageBriefModel(), new Date());
}
}

View File

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

View File

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