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.getTestPlanID(),
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;
}
@ -198,7 +186,6 @@ public class BusinessModelMapFactory {
ret.setFailTimes(testPlanDB.getFailTimes());
ret.setId(testPlanDB.getId());
ret.setName(testPlanDB.getName());
ret.setReportCreated(testPlanDB.isReportCreated());
ret.setTestPlanModelContent(testPlanDB.getTestPlanModelContent());
ret.setTestPlanRunId(testPlanDB.getTestPlanRunId());
ret.setUserModel(toModel(testPlanDB.getUser()));

View File

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

View File

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

View File

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

View File

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

View File

@ -52,7 +52,7 @@ public class TestPlanScriptResultControllerTest extends TestBase_MakeUpTestPlan
.getScriptId());
TestPlanInBusiness testPlan = BusinessModelMapFactory.toBusiness(model);
this.getTestPlanService().saveTestPlanToDB(testPlan, user,
testPlanRunId, testPlan.getRunningScripts());
testPlanRunId);
this.setTestPlanRunIdUuid(testPlanRunId);
for (int i = 0; i < 3; i++) {
this.getTestPlanScriptService().saveScriptBriefResult(

View File

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

View File

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