little refa

This commit is contained in:
coderfengyun 2014-02-18 15:18:43 +08:00
parent 2be8b29119
commit 49cf396f7f
8 changed files with 243 additions and 274 deletions

View File

@ -86,18 +86,6 @@ public class BusinessModelMapFactory {
runningScriptModel.getScriptId(), runningScriptModel.getScriptId(),
runningScriptModel.getTestPlanID(), runningScriptModel.getTestPlanID(),
toBusiness(runningScriptModel.getRunningAgentModels())); toBusiness(runningScriptModel.getRunningAgentModels()));
// result.setConfig(runningScriptModel.getConfig());
// result.setFinished(runningScriptModel.isFinished());
// result.setRequireLoad(runningScriptModel.getRequireLoad());
// List<RunningAgent> runningAgents = new ArrayList<RunningAgent>();
// for (RunningAgentModel runningAgentModel : runningScriptModel
// .getRunningAgentModels()) {
// runningAgents.add(BusinessModelMapFactory
// .toBusiness(runningAgentModel));
// }
// result.setRunningAgents(runningAgents);
// result.setScriptId(runningScriptModel.getScriptId());
// result.setTestPlanID(runningScriptModel.getTestPlanID());
return result; return result;
} }
@ -198,7 +186,6 @@ public class BusinessModelMapFactory {
ret.setFailTimes(testPlanDB.getFailTimes()); ret.setFailTimes(testPlanDB.getFailTimes());
ret.setId(testPlanDB.getId()); ret.setId(testPlanDB.getId());
ret.setName(testPlanDB.getName()); ret.setName(testPlanDB.getName());
ret.setReportCreated(testPlanDB.isReportCreated());
ret.setTestPlanModelContent(testPlanDB.getTestPlanModelContent()); ret.setTestPlanModelContent(testPlanDB.getTestPlanModelContent());
ret.setTestPlanRunId(testPlanDB.getTestPlanRunId()); ret.setTestPlanRunId(testPlanDB.getTestPlanRunId());
ret.setUserModel(toModel(testPlanDB.getUser())); ret.setUserModel(toModel(testPlanDB.getUser()));

View File

@ -1,111 +1,101 @@
package org.bench4q.master.entity; package org.bench4q.master.entity;
import java.util.Date; import java.util.Date;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.Table; import javax.persistence.Table;
@Entity @Entity
@Table(name = "testplan") @Table(name = "testplan")
public class TestPlanDB { public class TestPlanDB {
private int id; private int id;
private String name; private String name;
private Date createDateTime; private Date createDateTime;
private User user; private User user;
private String testPlanRunId; private String testPlanRunId;
private String testPlanModelContent; private String testPlanModelContent;
private String currentStatus; private String currentStatus;
private int failTimes; private int failTimes;
private boolean reportCreated;
@Id
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
@GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", nullable = false)
@Column(name = "id", nullable = false) public int getId() {
public int getId() { return id;
return id; }
}
public void setId(int id) {
public void setId(int id) { this.id = id;
this.id = id; }
}
@Column(name = "name", nullable = false)
@Column(name = "name", nullable = false) public String getName() {
public String getName() { return name;
return name; }
}
public void setName(String name) {
public void setName(String name) { this.name = name;
this.name = name; }
}
@Column(name = "testPlanRunId", columnDefinition = "TEXT", nullable = false)
@Column(name = "testPlanRunId", columnDefinition = "TEXT", nullable = false) public String getTestPlanRunId() {
public String getTestPlanRunId() { return testPlanRunId;
return testPlanRunId; }
}
public void setTestPlanRunId(String testPlanRunId) {
public void setTestPlanRunId(String testPlanRunId) { this.testPlanRunId = testPlanRunId;
this.testPlanRunId = testPlanRunId; }
}
@Column(name = "createDateTime", nullable = false)
@Column(name = "createDateTime", nullable = false) public Date getCreateDateTime() {
public Date getCreateDateTime() { return createDateTime;
return createDateTime; }
}
public void setCreateDateTime(Date createDateTime) {
public void setCreateDateTime(Date createDateTime) { this.createDateTime = createDateTime;
this.createDateTime = createDateTime; }
}
@ManyToOne
@ManyToOne @JoinColumn(name = "userId", nullable = false)
@JoinColumn(name = "userId", nullable = false) public User getUser() {
public User getUser() { return user;
return user; }
}
public void setUser(User user) {
public void setUser(User user) { this.user = user;
this.user = user; }
}
@Column(name = "testPlanModelContent", columnDefinition = "LONGText", nullable = false)
@Column(name = "testPlanModelContent", columnDefinition = "LONGText", nullable = false) public String getTestPlanModelContent() {
public String getTestPlanModelContent() { return testPlanModelContent;
return testPlanModelContent; }
}
public void setTestPlanModelContent(String testPlanModelContent) {
public void setTestPlanModelContent(String testPlanModelContent) { this.testPlanModelContent = testPlanModelContent;
this.testPlanModelContent = testPlanModelContent; }
}
@Column(name = "currentStatus", nullable = false)
@Column(name = "currentStatus", nullable = false) public String getCurrentStatus() {
public String getCurrentStatus() { return currentStatus;
return currentStatus; }
}
public void setCurrentStatus(String currentStatus) {
public void setCurrentStatus(String currentStatus) { this.currentStatus = currentStatus;
this.currentStatus = currentStatus; }
}
@Column(name = "failTimes", nullable = false)
@Column(name = "failTimes", nullable = false) public int getFailTimes() {
public int getFailTimes() { return failTimes;
return failTimes; }
}
public void setFailTimes(int failTimes) {
public void setFailTimes(int failTimes) { this.failTimes = failTimes;
this.failTimes = failTimes; }
}
}
@Column(name = "reportCreated", nullable = false)
public boolean isReportCreated() {
return reportCreated;
}
public void setReportCreated(boolean reportCreated) {
this.reportCreated = reportCreated;
}
}

View File

@ -66,8 +66,7 @@ public class TestPlanService {
} }
public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam, public boolean saveTestPlanToDB(final TestPlanInBusiness testPlanInParam,
final User user, final UUID testPlanRunId, final User user, final UUID testPlanRunId) {
final Collection<RunningScript> collection) {
Session session = this.sessionHelper.openSession(); Session session = this.sessionHelper.openSession();
Transaction transaction = session.beginTransaction(); Transaction transaction = session.beginTransaction();
TestPlanDB testPlanInSession; TestPlanDB testPlanInSession;
@ -81,7 +80,7 @@ public class TestPlanService {
return false; return false;
} }
if (!saveToTestPlanScriptAndScriptConfig(session, if (!saveToTestPlanScriptAndScriptConfig(session,
testPlanInSession, collection)) { testPlanInSession, testPlanInParam.getRunningScripts())) {
return false; return false;
} }
transaction.commit(); transaction.commit();
@ -197,11 +196,6 @@ public class TestPlanService {
.getTestPlan(testPlanRunID).getCurrentStatus()); .getTestPlan(testPlanRunID).getCurrentStatus());
} }
public TestPlanDB pickALatestPengdingWithMinimalFailTimes() {
// TODO: fulfill this
return null;
}
public TestPlanDBModel getTestPlanDBModel(UUID runId) { public TestPlanDBModel getTestPlanDBModel(UUID runId) {
return this.getBusinessMapFactory().toModel( return this.getBusinessMapFactory().toModel(
this.getTestPlanRepository().getTestPlan(runId)); this.getTestPlanRepository().getTestPlan(runId));

View File

@ -1,136 +1,136 @@
package org.bench4q.master.testplan; package org.bench4q.master.testplan;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.bench4q.master.domain.RunningScript; import org.bench4q.master.domain.RunningScript;
import org.bench4q.master.domain.TestPlanContext; import org.bench4q.master.domain.TestPlanContext;
import org.bench4q.master.domain.TestPlanInBusiness; import org.bench4q.master.domain.TestPlanInBusiness;
import org.bench4q.master.entity.User; import org.bench4q.master.entity.User;
import org.bench4q.master.service.infrastructure.TestPlanService; import org.bench4q.master.service.infrastructure.TestPlanService;
import org.bench4q.master.testplan.highavailable.CurrentLoadObserver; import org.bench4q.master.testplan.highavailable.CurrentLoadObserver;
import org.bench4q.master.testplan.highavailable.HighAvailablePool; import org.bench4q.master.testplan.highavailable.HighAvailablePool;
import org.bench4q.master.testplan.schedulscript.TaskCompleteCallback; import org.bench4q.master.testplan.schedulscript.TaskCompleteCallback;
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 TestPlanEngine implements TaskCompleteCallback, public class TestPlanEngine implements TaskCompleteCallback,
CurrentLoadObserver { CurrentLoadObserver {
private TestPlanService testPlanService; private TestPlanService testPlanService;
private LoadDistribute loadBallancer; private LoadDistribute loadBallancer;
private TestPlanContainer testPlanContainer; private TestPlanContainer testPlanContainer;
private HighAvailablePool haPool; private HighAvailablePool haPool;
public static Logger logger = Logger.getLogger(TestPlanEngine.class); public static Logger logger = Logger.getLogger(TestPlanEngine.class);
private LoadDistribute getLoadBallancer() { private LoadDistribute getLoadBallancer() {
return loadBallancer; return loadBallancer;
} }
@Autowired @Autowired
private void setLoadBallancer(LoadDistribute loadBallancer) { private void setLoadBallancer(LoadDistribute loadBallancer) {
this.loadBallancer = loadBallancer; this.loadBallancer = loadBallancer;
} }
private TestPlanContainer getContainer() { private TestPlanContainer getContainer() {
return testPlanContainer; return testPlanContainer;
} }
@Autowired @Autowired
private void setTestPlanContainer(TestPlanContainer testPlanContainer) { private void setTestPlanContainer(TestPlanContainer testPlanContainer) {
this.testPlanContainer = testPlanContainer; this.testPlanContainer = testPlanContainer;
} }
private TestPlanService getTestPlanService() { private TestPlanService getTestPlanService() {
return testPlanService; return testPlanService;
} }
@Autowired @Autowired
private void setTestPlanService(TestPlanService testPlanService) { private void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService; this.testPlanService = testPlanService;
} }
public HighAvailablePool getHaPool() { public HighAvailablePool getHaPool() {
return haPool; return haPool;
} }
@Autowired @Autowired
private void setHaPool(HighAvailablePool haPool) { private void setHaPool(HighAvailablePool haPool) {
this.haPool = haPool; this.haPool = haPool;
this.getHaPool().setObserver(this); this.getHaPool().setObserver(this);
} }
public UUID runTestPlanWithModel(final TestPlanInBusiness testPlan, public UUID runTestPlanWithModel(final TestPlanInBusiness testPlan,
User user) { User user) {
ExecutorService executorService = Executors.newCachedThreadPool(); ExecutorService executorService = Executors.newCachedThreadPool();
final UUID testPlanId = UUID.randomUUID(); final UUID testPlanId = UUID.randomUUID();
final TestPlanContext testPlanContext = new TestPlanContext(testPlan, final TestPlanContext testPlanContext = new TestPlanContext(testPlan,
testPlanId); testPlanId);
if (!this.getTestPlanService().saveTestPlanToDB(testPlan, user, if (!this.getTestPlanService().saveTestPlanToDB(testPlan, user,
testPlanId, testPlanContext.getAllScript())) { testPlanId)) {
return null; return null;
} }
this.getContainer().addATask(testPlanId, testPlanContext); this.getContainer().addATask(testPlanId, testPlanContext);
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
public void run() { public void run() {
doRunTestPlan(testPlanContext, testPlanId); doRunTestPlan(testPlanContext, testPlanId);
} }
}; };
executorService.execute(runnable); executorService.execute(runnable);
executorService.shutdown(); executorService.shutdown();
return testPlanId; return testPlanId;
} }
private void doRunTestPlan(TestPlanContext testPlanContext, UUID testPlanId) { private void doRunTestPlan(TestPlanContext testPlanContext, UUID testPlanId) {
if (!this.getLoadBallancer().generateLoadForTestPlan(testPlanContext, if (!this.getLoadBallancer().generateLoadForTestPlan(testPlanContext,
testPlanId)) { testPlanId)) {
logger.error("there is a error happened in generateLoadForTestPlan"); logger.error("there is a error happened in generateLoadForTestPlan");
this.getTestPlanService().handleTestPlanError(testPlanId); this.getTestPlanService().handleTestPlanError(testPlanId);
return; return;
} }
schedulePlanConfig( schedulePlanConfig(
this.testPlanContainer.queryTestPlanContext(testPlanId), this.testPlanContainer.queryTestPlanContext(testPlanId),
testPlanId); testPlanId);
this.getTestPlanService().handleTestPlanRunning(testPlanId); this.getTestPlanService().handleTestPlanRunning(testPlanId);
} }
private boolean schedulePlanConfig(TestPlanContext testPlanContext, private boolean schedulePlanConfig(TestPlanContext testPlanContext,
UUID testPlanId) { UUID testPlanId) {
for (RunningScript runningScript : testPlanContext.getAllScript()) { for (RunningScript runningScript : testPlanContext.getAllScript()) {
runningScript.scheduleAsConfig(this, testPlanId); runningScript.scheduleAsConfig(this, testPlanId);
} }
return true; return true;
} }
public void doTaskComplete(UUID testPlanID) { public void doTaskComplete(UUID testPlanID) {
if (!this.getContainer().checkTestPlanFinish(testPlanID)) { if (!this.getContainer().checkTestPlanFinish(testPlanID)) {
return; return;
} }
TestPlanContext context = this.getContainer().queryTestPlanContext( TestPlanContext context = this.getContainer().queryTestPlanContext(
testPlanID); testPlanID);
if (context == null) { if (context == null) {
logger.error("testPlanContext with testPlanID " logger.error("testPlanContext with testPlanID "
+ testPlanID.toString() + " is null!"); + testPlanID.toString() + " is null!");
return; return;
} }
this.getHaPool().doForTestPlanComplete(context); this.getHaPool().doForTestPlanComplete(context);
this.getContainer().doForTestPlanComplete(testPlanID); this.getContainer().doForTestPlanComplete(testPlanID);
this.getTestPlanService().handleTestPlanComplete(testPlanID); this.getTestPlanService().handleTestPlanComplete(testPlanID);
logger.info("Test plan with id " + testPlanID.toString() + " finishes!"); logger.info("Test plan with id " + testPlanID.toString() + " finishes!");
} }
public void executePendingTestPlan() { public void executePendingTestPlan() {
// TODO: when taskComplete do something, pick a test plan which is not // TODO: when taskComplete do something, pick a test plan which is not
// Pending or Error to execute. // Pending or Error to execute.
// TestPlan testPlan = this.getTestPlanService() // TestPlan testPlan = this.getTestPlanService()
// .pickALatestPengdingWithMinimalFailTimes(); // .pickALatestPengdingWithMinimalFailTimes();
// if (testPlan == null) { // if (testPlan == null) {
// return; // return;
// } // }
} }
} }

View File

@ -133,7 +133,7 @@ public class TestBase_MakeUpTestPlan extends TestBase {
.getScriptId()); .getScriptId());
TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model); TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().saveTestPlanToDB(testPlan, user, this.getTestPlanService().saveTestPlanToDB(testPlan, user,
testPlanRunId, testPlan.getRunningScripts()); testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId); this.setTestPlanRunIdUuid(testPlanRunId);
return testPlanRunId; return testPlanRunId;
} }

View File

@ -52,7 +52,7 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan
.getScriptId()); .getScriptId());
TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model); TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().saveTestPlanToDB(testPlan, user, this.getTestPlanService().saveTestPlanToDB(testPlan, user,
testPlanRunId, testPlan.getRunningScripts()); 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().saveScriptBriefResult(

View File

@ -28,7 +28,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" }) @ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan { public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
@Test @Test
public void testGetSpecficFieldValue() throws Exception { public void testGetSpecficFieldValue() throws Exception {
long averageResponseTime = (Long) TestHelper.invokePrivateMethod( long averageResponseTime = (Long) TestHelper.invokePrivateMethod(
@ -123,7 +122,7 @@ public class Test_TestPlanScriptResultService extends TestBase_MakeUpTestPlan {
.getScriptId()); .getScriptId());
TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model); TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().saveTestPlanToDB(testPlan, user, this.getTestPlanService().saveTestPlanToDB(testPlan, user,
testPlanRunId, testPlan.getRunningScripts()); 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().saveScriptBriefResult(

View File

@ -40,8 +40,7 @@ public class TestPlanTester extends TestBase_MakeUpTestPlan {
private TestPlanBusinessModel testPlanBusinessModel = new TestPlanBusinessModel(); private TestPlanBusinessModel testPlanBusinessModel = new TestPlanBusinessModel();
private String _url = TestBase.BASE_URL + "/testPlan"; private String _url = TestBase.BASE_URL + "/testPlan";
private int scriptSumNum; private int scriptSumNum;
public static int SCRIPTID1 = 61; public static int SCRIPTID1 = 78;
// public static int SCRIPTID1 = 1;
private static int SCRIPTID2 = 2; private static int SCRIPTID2 = 2;
private static String Monitor_Host_Name = "133.133.12.3"; private static String Monitor_Host_Name = "133.133.12.3";
private static String monitor_port = "5556"; private static String monitor_port = "5556";