This commit is contained in:
coderfengyun 2014-09-19 10:59:23 +08:00
commit 423ae1c21d
10 changed files with 156 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.CascadeType;
import javax.persistence.Column;
@ -21,6 +22,7 @@ import javax.persistence.Transient;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.domain.valueobject.datastatistics.TestMonitorSampler;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
@ -121,6 +123,8 @@ public class Monitor {
MonitorMain monitorMain = getTestMonitorSampler().getMonitorResult(
hostName, port);
System.out.println(MarshalHelper.tryMarshal(monitorMain));
//tell test plan has to stop itself
testPlan.setHasToStop(monitorMain.isFinished());
List<MonitorResult> monitorResults = this.getTestPlanFactory()
.createMonitorResultListWithOutId(monitorMain, testPlan,
this, sampeTime);

View File

@ -4,6 +4,7 @@ import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -24,6 +25,7 @@ import javax.persistence.Transient;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.IAggregate;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.domain.valueobject.transaction.impl.TestPlanLoadApplication;
import org.bench4q.share.enums.master.TestPlanStatus;
@ -45,6 +47,8 @@ public class TestPlan implements IAggregate {
private Set<Monitor> monitors;
private TestResultSave testResultSave;
private TestPlanRepository repository;
private TestPlanEngine testPlanEngine;
private boolean hasToStop;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@ -272,6 +276,9 @@ public class TestPlan implements IAggregate {
}
getTestResultSave().update(TestPlan.this,
collectResult(new Date()));
if(isHasToStop()){
testPlanEngine.stop(UUID.fromString(testPlanRunId));
}
}
}, 0, this.getSampleCycleInSeconds(), TimeUnit.SECONDS);
} catch (Exception e) {
@ -321,4 +328,22 @@ public class TestPlan implements IAggregate {
return getLastRunningTime();
}
@Transient
public boolean isHasToStop() {
return hasToStop;
}
public void setHasToStop(boolean hasToStop) {
this.hasToStop = hasToStop;
}
@Transient
public TestPlanEngine getTestPlanEngine() {
return testPlanEngine;
}
public void setTestPlanEngine(TestPlanEngine testPlanEngine) {
this.testPlanEngine = testPlanEngine;
}
}

View File

@ -25,6 +25,7 @@ import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.ScriptService;
import org.bench4q.master.domain.service.TestPlanEngine;
import org.bench4q.master.domain.service.TestResultSave;
import org.bench4q.master.domain.valueobject.transaction.TransactionFactory;
import org.bench4q.master.exception.ExceptionLog;
@ -102,7 +103,7 @@ public class TestPlanFactory {
}
public TestPlan createATestPlanWithoutId(TestPlanModel testPlanModel,
User user, UUID runId) throws IllegalParameterException {
User user, UUID runId, TestPlanEngine testPlanEngine) throws IllegalParameterException {
this.logger.info("testPlanName:" + testPlanModel.getName());
TestPlan result = new TestPlan();
result.setCreateDateTime(new Date());
@ -142,6 +143,7 @@ public class TestPlanFactory {
monitors.add(monitorInBusiness);
}
result.setMonitors(monitors);
result.setTestPlanEngine(testPlanEngine);
return result;
}

View File

@ -168,4 +168,27 @@ public class PortPoolService {
}
}
@SuppressWarnings("unchecked")
public void resetAllPortState(){
Session session = this.getSessionHelper().openSession();
Transaction transaction = session.beginTransaction();
try {
List<Port> portList = (List<Port>) session
.createCriteria(Port.class)
.add(Restrictions.eq("inUse", true)).list();
for(Port port: portList){
port.setInUse(false);
}
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
if (session != null) {
session.close();
}
}
}
}

View File

@ -126,7 +126,7 @@ public class TestPlanEngine implements TaskCompleteCallback,
final User user, final UUID testPlanRunId)
throws IllegalParameterException {
TestPlan testPlan = this.getTestPlanFactory().createATestPlanWithoutId(
testPlanBusinessModel, user, testPlanRunId);
testPlanBusinessModel, user, testPlanRunId, this);
Logger.getLogger(TestPlanService.class).info(
"test plan name:" + testPlan.getName());
return this.getTestPlanRepository().attach(testPlan);

View File

@ -1,11 +1,16 @@
package org.bench4q.monitor.api;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import org.bench4q.monitor.model.LimitModel;
import org.bench4q.monitor.model.MonitorMain;
import org.hyperic.sigar.SigarException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ -13,9 +18,23 @@ import org.springframework.web.bind.annotation.ResponseBody;
public class MainController {
@RequestMapping("/all")
@ResponseBody
MonitorMain getMainModel() throws SigarException, InterruptedException, ExecutionException {
MonitorMain getMainModel() throws SigarException, InterruptedException,
ExecutionException {
MonitorMain retModel = new MonitorMain();
return retModel;
}
@RequestMapping("/startMonitor/{testPlanId}")
@ResponseBody
public String start(@PathVariable UUID testPlanId,
@RequestBody LimitModel limits) {
return new String("startted");
}
@RequestMapping(value = "/{testPlanId}", method = RequestMethod.GET)
@ResponseBody
MonitorMain brief(@PathVariable UUID testPlanId) throws SigarException,
InterruptedException, ExecutionException {
return new MonitorMain();
}
}

View File

@ -0,0 +1,47 @@
package org.bench4q.monitor.model;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class LimitModel {
@XmlElementWrapper
@XmlElement
private List<ParameterModel> params;
public List<ParameterModel> getParams() {
return params;
}
public void setParams(List<ParameterModel> params) {
this.params = params;
}
@XmlRootElement
public static class ParameterModel {
private String name;
private double limit;
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public double getLimit() {
return limit;
}
public void setLimit(double limit) {
this.limit = limit;
}
}
}

View File

@ -1,11 +1,11 @@
package org.bench4q.monitor.model;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.ExecutionException;
import java.text.SimpleDateFormat;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@ -15,7 +15,7 @@ import org.hyperic.sigar.SigarException;
import com.google.gson.annotations.Expose;
@XmlRootElement(name = "history")
public class MonitorMain {
public class MonitorMain extends SampleModel{
@XmlElement
private String date;
@XmlElement(name = "processor_info")

View File

@ -0,0 +1,31 @@
package org.bench4q.monitor.model;
import java.util.Date;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public abstract class SampleModel {
protected Date samplingTime;
protected boolean finished;
@XmlElement
public Date getSamplingTime() {
return samplingTime;
}
public void setSamplingTime(Date samplingTime) {
this.samplingTime = samplingTime;
}
@XmlElement
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
}

View File

@ -63,5 +63,4 @@ public class MonitorMain extends SampleModel{
public void setProcessModel(ProcessModel processModel) {
this.processModel = processModel;
}
}