finish save test result refactor

This commit is contained in:
fanfuxiaoran 2014-04-09 10:52:25 +08:00
parent 93e7ea5ccc
commit 28554f125e
8 changed files with 214 additions and 76 deletions

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.share.models.agent.RunScenarioModel;
public interface RunningScriptInterface {
@ -22,6 +23,6 @@ public interface RunningScriptInterface {
public RunningScriptInterface doForComplete();
public void doAfterRun();
public List<TestPlanScriptResult> doAfterRun();
}

View File

@ -23,7 +23,6 @@ import org.bench4q.master.domain.testplan.TestMonitorSampler;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.share.models.monitor.MonitorMain;
import org.springframework.beans.factory.annotation.Autowired;
@Entity
@Table(name = "monitor")
@ -99,7 +98,6 @@ public class Monitor {
return testPlanFactory;
}
@Autowired
public void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
@ -108,23 +106,19 @@ public class Monitor {
}
public void doAfterRun() {
System.out.println("enter do run after");
public List<MonitorResult> doAfterRun() {
try {
MonitorMain monitorMain = getTestMonitorSampler().getMonitorResult(
hostName, port);
List<MonitorResult> monitorResults = this.getTestPlanFactory()
.createMonitorResultListWithOutId(monitorMain, testPlan,
this, monitorMain.getSamplingTime());
System.out.println(monitorResults.size());
this.getResults().addAll(monitorResults);
return monitorResults;
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
public void clearResult() {
this.getResults().clear();
}
}

View File

@ -1,7 +1,8 @@
package org.bench4q.master.domain.entity;
import java.util.Date;
import java.util.Observable;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -23,11 +24,12 @@ import javax.persistence.Transient;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.IAggregate;
import org.bench4q.master.domain.testplan.LoadDistribute;
import org.bench4q.master.domain.testplan.TestResultSave;
import org.bench4q.share.enums.master.TestPlanStatus;
@Entity
@Table(name = "testplan")
public class TestPlan extends Observable implements IAggregate {
public class TestPlan implements IAggregate {
private int id;
private String name;
private int sampleCycleInSeconds;
@ -42,6 +44,8 @@ public class TestPlan extends Observable implements IAggregate {
private Set<Monitor> monitors;
private LoadDistribute loadDistribute;
private TestResultSave testResultSave;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
@ -162,6 +166,15 @@ public class TestPlan extends Observable implements IAggregate {
this.loadDistribute = loadDistribute;
}
@Transient
public TestResultSave getTestResultSave() {
return testResultSave;
}
public void setTestResultSave(TestResultSave testResultSave) {
this.testResultSave = testResultSave;
}
public TestPlanScript extracSpecifiedScript(int scriptId) {
for (TestPlanScript testPlanScript : testPlanScripts) {
if (testPlanScript.getScript().getId() == scriptId) {
@ -194,9 +207,8 @@ public class TestPlan extends Observable implements IAggregate {
if (isFinish()) {
scheScheduledExecutorService.shutdown();
}
collectResult();
flushResultToRepo();
clearResult();
doSaveResult(collectResult());
}
}, 0, this.getSampleCycleInSeconds(), TimeUnit.SECONDS);
} catch (Exception e) {
@ -206,34 +218,27 @@ public class TestPlan extends Observable implements IAggregate {
}
public void flushResultToRepo() {
this.setChanged();
this.notifyObservers(this);
}
public void collectResult() {
public List<Object> collectResult() {
List<Object> resultList = new LinkedList<>();
for (TestPlanScript testPlanScript : getTestPlanScripts()) {
testPlanScript.doAfterRun();
List<TestPlanScriptResult> testPlanScriptResults = testPlanScript
.doAfterRun();
if (testPlanScriptResults != null)
resultList.addAll(testPlanScriptResults);
}
if (getMonitors() != null) {
for (Monitor monitor : getMonitors()) {
monitor.doAfterRun();
List<MonitorResult> monitorResults = monitor.doAfterRun();
if (monitorResults != null)
resultList.addAll(monitorResults);
}
}
return resultList;
}
public void clearResult() {
for (TestPlanScript testPlanScript : getTestPlanScripts()) {
testPlanScript.clearResult();
}
if (getMonitors() != null) {
for (Monitor monitor : getMonitors()) {
monitor.clearResult();
}
}
public void doSaveResult(List<Object> results) {
this.getTestResultSave().update(this, results);
}
@Transient

View File

@ -1,11 +1,13 @@
package org.bench4q.master.domain.entity;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -19,6 +21,7 @@ import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.RunningScriptInterface;
@ -235,18 +238,18 @@ public class TestPlanScript implements RunningScriptInterface {
this.scheduleAsConfig();
}
public void doAfterRun() {
public List<TestPlanScriptResult> doAfterRun() {
try {
ScriptResultModel scriptResultModel = isFinish() ? ScriptResultModel
.buildFinishedResult() : this.getSampler()
.getResultModelFromAgent();
List<TestPlanScriptResult> testPlanScriptResultList = this
.getTestPlanFactory().createScriptResultsWithoutId(
scriptResultModel, testPlan, this,
scriptResultModel.getSamplingTime());
this.getTestPlanScriptResults().addAll(testPlanScriptResultList);
scriptResultModel, testPlan, this, new Date());
return testPlanScriptResultList;
} catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e));
return null;
}
}
@ -293,7 +296,4 @@ public class TestPlanScript implements RunningScriptInterface {
return null;
}
public void clearResult() {
this.getTestPlanScriptResults().clear();
}
}

View File

@ -196,14 +196,16 @@ public class TestPlanFactory {
List<TestPlanScriptResult> testPlanScriptResults = new LinkedList<TestPlanScriptResult>();
Field[] fields = scriptResultModel.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
TestPlanScriptResult testPlanScriptResult = new TestPlanScriptResult();
testPlanScriptResult.setCreateDatetime(createDatetime);
testPlanScriptResult.setTestPlanScript(testPlanScript);
testPlanScriptResult.setResultType(fields[i].getType().getName());
try {
fields[i].setAccessible(true);
testPlanScriptResult.setResultContent(MarshalHelper.marshal(
fields[i].getType(),
fields[i].get(testPlanScriptResult)));
fields[i].getType(), fields[i].get(scriptResultModel)));
} catch (IllegalArgumentException | IllegalAccessException
| JAXBException e) {
Logger.getLogger(MonitorResult.class).info(
@ -258,7 +260,6 @@ public class TestPlanFactory {
convertToDomain(monitor);
}
}
testPlanInRepo.addObserver(this.getTestResultSave());
logger.info("convertToDomain testPlan run id:"
+ testPlanInRepo.getTestPlanRunId());
return testPlanInRepo;
@ -270,9 +271,6 @@ public class TestPlanFactory {
testPlanScript.setAgentService(this.getAgentService());
}
private void convertToDomain(Monitor monitor) {
}
public List<RunningAgentDB> createRunningAgentDBsWithoutId(
List<? extends RunningAgentInterface> runningAgents,
TestPlanScript testPlanScript) {
@ -292,4 +290,8 @@ public class TestPlanFactory {
}
return runningAgentDBs;
}
private void convertToDomain(Monitor monitor) {
monitor.setTestPlanFactory(this);
}
}

View File

@ -232,7 +232,7 @@ public class TestPlanRepository extends AbstractRepositoty {
}
@SuppressWarnings("unchecked")
public List<TestPlanScriptResult> getMonitorResultsByMonitor(
public List<TestPlanScriptResult> getScriptResultsByTestPlanScript(
TestPlanScript testPlanScript) {
Session session = this.getSessionHelper().openSession();
try {

View File

@ -1,13 +1,14 @@
package org.bench4q.master.domain.testplan;
import java.util.Observable;
import java.util.Observer;
import java.util.List;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.MonitorResult;
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.repository.TestPlanRepository;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.SessionHelper;
@ -16,7 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class TestResultSave implements Observer {
public class TestResultSave{
private SessionHelper sessionHelper;
private TestPlanRepository testPlanRepository;
private Logger logger = Logger.getLogger(TestResultSave.class);
@ -42,54 +43,52 @@ public class TestResultSave implements Observer {
this.sessionHelper = sessionHelper;
}
@Override
public void update(Observable o, Object arg) {
public void update(TestPlan testPlan, List<Object> messages) {
Session session = this.getSessionHelper().openSession();
TestPlan testPlan = (TestPlan) o;
TestPlan testPlanFromRepo = this.getTestPlanRepository()
.doGetTestPlanBy(session,
UUID.fromString(testPlan.getTestPlanRunId()));
updateTestPlanResult(session, testPlan, testPlanFromRepo);
updateTestPlanResult(messages, testPlanFromRepo);
saveUpdatedResult(testPlanFromRepo, session);
session.close();
}
private void updateTestPlanResult(Session session,TestPlan testPlan,
private void updateTestPlanResult(List<Object> messages,
TestPlan testPlanFromRepo) {
for (TestPlanScript testPlanScript : testPlan.getTestPlanScripts()) {
updateTestPlanScriptResult(testPlanScript, testPlanFromRepo);
}
if (testPlan.getMonitors() != null)
for (Monitor monitor : testPlan.getMonitors()) {
updateTestplanMonitorResult(session,monitor, testPlanFromRepo);
for (Object message : messages) {
if(message==null)
return;
if (message instanceof MonitorResult) {
updateTestplanMonitorResult((MonitorResult) message,
testPlanFromRepo);
}
else if(message instanceof TestPlanScriptResult){
updateTestPlanScriptResult((TestPlanScriptResult)message, testPlanFromRepo);
}
}
}
private void updateTestPlanScriptResult(TestPlanScript testPlanScript,
private void updateTestPlanScriptResult(TestPlanScriptResult message,
TestPlan testPlanFromRepo) {
try {
TestPlanScript testPlanScriptFromRepo = testPlanFromRepo
.extracSpecifiedScript(testPlanScript.getScriptId());
if (testPlanScript.getTestPlanScriptResults().size() == 0)
return;
testPlanScriptFromRepo.getTestPlanScriptResults().addAll(
testPlanScript.getTestPlanScriptResults());
.extracSpecifiedScript(message.getTestPlanScript().getScriptId());
testPlanScriptFromRepo.getTestPlanScriptResults().add(message);
} catch (Exception e) {
logger.info(ExceptionLog.getStackTrace(e));
}
}
private void updateTestplanMonitorResult(Session session, Monitor monitor,
private void updateTestplanMonitorResult(MonitorResult monitorResult,
TestPlan testPlanFromRepo) {
try {
logger.info("session:"+session.isOpen());
logger.info("currentSession:"+this.getSessionHelper().getCurrentSession().isOpen());
Monitor monitorFromRepo = testPlanFromRepo
.extractSpecifiedMonitor(monitor.getHostName());
if (monitor.getResults().size() == 0)
return;
monitorFromRepo.getResults().addAll(monitor.getResults());
.extractSpecifiedMonitor(monitorResult.getMonitor()
.getHostName());
monitorFromRepo.getResults().add(monitorResult);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -0,0 +1,137 @@
package org.bench4q.master.test.domain.testPlan;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.bench4q.master.domain.entity.Monitor;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.testplan.TestMonitorSampler;
import org.bench4q.master.domain.testplan.TestResultSave;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.junit.After;
import org.junit.Before;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_TestResultSave extends TestBase_MakeUpTestPlan {
private TestResultSave testResultSave;
private TestPlanFactory testPlanFactory;
private TestMonitorSampler testMonitorSampler;
public TestResultSave getTestResultSave() {
return testResultSave;
}
@Autowired
public void setTestResultSave(TestResultSave testResultSave) {
this.testResultSave = testResultSave;
}
public TestPlanFactory getTestPlanFactory() {
return testPlanFactory;
}
@Autowired
public void setTestPlanFactory(TestPlanFactory testPlanFactory) {
this.testPlanFactory = testPlanFactory;
}
public TestMonitorSampler getTestMonitorSampler() {
return testMonitorSampler;
}
@Autowired
public void setTestMonitorSampler(TestMonitorSampler testMonitorSampler) {
this.testMonitorSampler = testMonitorSampler;
}
@Before
public void setUp() {
this.prepareForTestPlanRunning();
this.submitATestPlanWithOneScript();
}
@After
public void cleanUp() {
cleanUpForTestPlanRunning();
}
@Test
public void testDoSaveMonitorResult() {
TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanBy(
this.getTestPlanRunIdUuid());
int beforeInsertCount = getMonitorResultSize(getOneMonitor(
testPlanFromRepo, Monitor_Host_Name));
assertTrue(beforeInsertCount == 0);
TestPlan testPlan = this.getTestPlanFactory().convertToDomain(
testPlanFromRepo);
List<Object> resultList = new ArrayList<>();
resultList.addAll(getOneMonitor(testPlan, Monitor_Host_Name)
.doAfterRun());
this.getTestResultSave().update(testPlan, resultList);
testPlanFromRepo = this.getTestPlanRepository().getTestPlanBy(
this.getTestPlanRunIdUuid());
int afterInsertCount = getMonitorResultSize(getOneMonitor(
testPlanFromRepo, Monitor_Host_Name));
assertEquals(beforeInsertCount + 5, afterInsertCount);
}
@Test
public void testDoSaveTestPlanScriptResult() {
TestPlan testPlanFromRepo = this.getTestPlanRepository().getTestPlanBy(
this.getTestPlanRunIdUuid());
TestPlanScript testPlanScript = getOneTestPlanScript(testPlanFromRepo);
int beforeInsertCount = getTestPlanScriptResultSize(testPlanScript);
assertTrue(beforeInsertCount == 0);
TestPlan testPlan = this.getTestPlanFactory().convertToDomain(
testPlanFromRepo);
assertEquals(TestPlanStatus.InRunning, testPlan.run());
testPlanScript = getOneTestPlanScript(testPlan);
List<Object> resultList = new ArrayList<>();
resultList.addAll(testPlanScript.doAfterRun());
this.getTestResultSave().update(testPlan, resultList);
testPlanFromRepo = this.getTestPlanRepository().getTestPlanBy(
this.getTestPlanRunIdUuid());
int afterInsertCount = getTestPlanScriptResultSize(testPlanScript);
assertEquals(beforeInsertCount + 3, afterInsertCount);
}
private Monitor getOneMonitor(TestPlan testPlan, String hostName) {
for (Monitor monitor : testPlan.getMonitors()) {
if (monitor.getHostName().equals(hostName))
return testPlan.getMonitors().iterator().next();
}
return null;
}
private int getMonitorResultSize(Monitor monitor) {
return this.getTestPlanRepository().getMonitorResultsByMonitor(monitor)
.size();
}
private int getTestPlanScriptResultSize(TestPlanScript testPlanScript) {
return this.getTestPlanRepository()
.getScriptResultsByTestPlanScript(testPlanScript).size();
}
private TestPlanScript getOneTestPlanScript(TestPlan testPlan) {
return testPlan.getTestPlanScripts().iterator().next();
}
}