diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/api/MonitorController.java b/Bench4Q-Master/src/main/java/org/bench4q/master/api/MonitorController.java index 49e9c9bc..6079fb3c 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/api/MonitorController.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/api/MonitorController.java @@ -5,12 +5,14 @@ import java.util.UUID; import org.bench4q.master.domain.service.MonitorResultService; import org.bench4q.master.domain.service.UserService; import org.bench4q.master.exception.Bench4QException; -import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorMemoryResponseModel; import org.bench4q.share.models.master.MonitorNetworkReponseModel; +import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorProcessorResponseModel; +import org.bench4q.share.models.monitor.LimitableFieldsModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -95,4 +97,14 @@ public class MonitorController extends BaseController { duationBegin); return ret == null ? new MonitorNetworkReponseModel() : ret; } + + @RequestMapping(value = "/getLimitableFields") + @ResponseBody + public LimitableFieldsModel getLimitableFields(@ModelAttribute("accessToken") String accessToken) throws Bench4QException{ + if (!this.checkScope(UserService.NORAML_AUTHENTICATION)) { + throw new Bench4QException(400 + "", "not permitted", + "/networkInfo"); + } + return this.getMonitorResultService().getLimitableFields(); + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java index d4166d3c..b9be9e78 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/Monitor.java @@ -22,10 +22,10 @@ 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; +import org.bench4q.master.infrastructure.communication.MonitorMessenger; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.monitor.MemoryModel; import org.bench4q.share.models.monitor.MonitorMain; @@ -35,6 +35,7 @@ import org.bench4q.share.models.monitor.ProcessModel; import org.bench4q.share.models.monitor.ProcessModelChild; import org.bench4q.share.models.monitor.ProcessorModel; import org.bench4q.share.models.monitor.ProcessorModelChild; +import org.springframework.beans.factory.annotation.Autowired; @Entity @Table(name = "monitor") @@ -46,7 +47,13 @@ public class Monitor { private Set results; private Logger logger = Logger.getLogger(Monitor.class); private TestPlanFactory testPlanFactory; + private MonitorMessenger monitorMessenger; + @Autowired + private void setMonitorMessenger(MonitorMessenger monitorMessenger) { + this.monitorMessenger = monitorMessenger; + } + public Monitor() { this.results = new HashSet(); } @@ -135,6 +142,10 @@ public class Monitor { return null; } } + + public void start(UUID testPlanId, String limitModel){ + monitorMessenger.startMonitor(hostName, port, testPlanId, limitModel); + } public List createFinishedResult() { try { diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java index 9b549084..8a021b2c 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/entity/TestPlan.java @@ -29,6 +29,8 @@ 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; +import org.bench4q.share.helper.MarshalHelper; +import org.bench4q.share.models.monitor.LimitModel; @Entity @Table(name = "testplan") @@ -49,6 +51,7 @@ public class TestPlan implements IAggregate { private TestPlanRepository repository; private TestPlanEngine testPlanEngine; private boolean hasToStop; + private LimitModel limitModel; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -259,6 +262,14 @@ public class TestPlan implements IAggregate { for (TestPlanScript testPlanScript : this.getTestPlanScripts()) { testPlanScript.run(); } + //start monitor to check the limit + UUID testPlanIdUuid = UUID.fromString(this.testPlanRunId); + String limitModelString = MarshalHelper.tryMarshal(this.limitModel); + if (getMonitors() != null) { + for (Monitor monitor : getMonitors()) { + monitor.start(testPlanIdUuid, limitModelString); + } + } } /** @@ -346,4 +357,13 @@ public class TestPlan implements IAggregate { this.testPlanEngine = testPlanEngine; } + @Transient + public LimitModel getLimitModel() { + return limitModel; + } + + public void setLimitModel(LimitModel limitModel) { + limitModel = limitModel; + } + } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java index 45aa5ac5..8f6978bf 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/factory/TestPlanFactory.java @@ -144,6 +144,7 @@ public class TestPlanFactory { } result.setMonitors(monitors); result.setTestPlanEngine(testPlanEngine); + result.setLimitModel(testPlanModel.getLimitModel()); return result; } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/MonitorResultService.java b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/MonitorResultService.java index 64ec752e..a631f9e7 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/MonitorResultService.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/domain/service/MonitorResultService.java @@ -1,5 +1,7 @@ package org.bench4q.master.domain.service; +import java.io.IOException; +import java.io.InputStream; import java.util.Date; import java.util.List; import java.util.UUID; @@ -10,13 +12,15 @@ import org.bench4q.master.domain.repository.TestPlanRepository; import org.bench4q.master.exception.Bench4QException; import org.bench4q.master.exception.ExceptionLog; import org.bench4q.master.helper.SessionHelper; +import org.bench4q.recorder.httpcapture.generator.HtmlDocumentParser; import org.bench4q.share.helper.MarshalHelper; -import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorMemoryResponseModel; import org.bench4q.share.models.master.MonitorNetworkReponseModel; +import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorProcessorResponseModel; import org.bench4q.share.models.master.MonitorResultBase; import org.bench4q.share.models.master.statistics.SampleModel; +import org.bench4q.share.models.monitor.LimitableFieldsModel; import org.bench4q.share.models.monitor.MemoryModel; import org.bench4q.share.models.monitor.NetworkInterfaceModel; import org.bench4q.share.models.monitor.PhysicalDiskModel; @@ -31,6 +35,7 @@ import org.springframework.stereotype.Component; public class MonitorResultService { private TestPlanRepository testPlanRepository; private SessionHelper sessionHelper; + private LimitableFieldsModel limitableField = null; private static Logger logger = Logger.getLogger(MonitorResult.class); private TestPlanRepository getTestPlanRepository() { @@ -172,4 +177,32 @@ public class MonitorResultService { } } + private void initLimitableFields(){ + String pathSep = System.getProperty("file.separator"); + String limitableFieldsPath = "org" + pathSep + "bench4q" + pathSep + + "master" + pathSep + "config" + pathSep + "MonitorLimitableFields.xml"; + + InputStream is = HtmlDocumentParser.class.getClassLoader() + .getResourceAsStream(limitableFieldsPath); + if (is == null) + return; + StringBuffer out = new StringBuffer(); + byte[] b = new byte[4096]; + int n; + try { + while ((n = is.read(b)) != -1) { + out.append(new String(b, 0, n)); + } + limitableField = MarshalHelper.tryUnmarshal(LimitableFieldsModel.class, out.toString()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public LimitableFieldsModel getLimitableFields(){ + if(limitableField == null){ + initLimitableFields(); + } + return limitableField; + } } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/MonitorMessenger.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/MonitorMessenger.java index 99b816bb..ac7702af 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/MonitorMessenger.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/MonitorMessenger.java @@ -1,5 +1,7 @@ package org.bench4q.master.infrastructure.communication; +import java.util.UUID; + import org.bench4q.share.models.monitor.MemoryModel; import org.bench4q.share.models.monitor.MonitorMain; import org.bench4q.share.models.monitor.NetworkInterfaceModel; @@ -20,4 +22,6 @@ public interface MonitorMessenger { public ProcessModel process(String hostName, int port); + public boolean startMonitor(String hostName, int port, UUID testPlanId, String limitModel) ; + } diff --git a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/impl/MonitorMessengerImpl.java b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/impl/MonitorMessengerImpl.java index ea598b9f..054ee246 100644 --- a/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/impl/MonitorMessengerImpl.java +++ b/Bench4Q-Master/src/main/java/org/bench4q/master/infrastructure/communication/impl/MonitorMessengerImpl.java @@ -1,6 +1,8 @@ package org.bench4q.master.infrastructure.communication.impl; +import java.io.IOException; import java.util.Date; +import java.util.UUID; import org.apache.log4j.Logger; import org.bench4q.master.exception.ExceptionLog; @@ -9,8 +11,8 @@ import org.bench4q.share.communication.HttpRequester; import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.statistics.SampleModel; -import org.bench4q.share.models.monitor.MonitorMain; import org.bench4q.share.models.monitor.MemoryModel; +import org.bench4q.share.models.monitor.MonitorMain; import org.bench4q.share.models.monitor.NetworkInterfaceModel; import org.bench4q.share.models.monitor.PhysicalDiskModel; import org.bench4q.share.models.monitor.ProcessModel; @@ -98,4 +100,24 @@ public class MonitorMessengerImpl implements MonitorMessenger { return monitorMain; } + + public boolean startMonitor(String hostName, int port, UUID testPlanId, String limitModel) { + HttpResponse httpResponse; + try { + httpResponse = this.getHttpRequester().sendPutXml( + buildUrl(hostName, port, "/monitor/submitLimit/" + testPlanId) + , limitModel, null); + if (HttpRequester.isInvalidResponse(httpResponse)) { + logger.info("The httpResponse is invalid "); + return false; + } + return testPlanId.toString().equals(httpResponse.getContent()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + + } + } diff --git a/Bench4Q-Master/src/main/resources/org/bench4q/master/config/MonitorLimitableFields.xml b/Bench4Q-Master/src/main/resources/org/bench4q/master/config/MonitorLimitableFields.xml new file mode 100644 index 00000000..d83cfe42 --- /dev/null +++ b/Bench4Q-Master/src/main/resources/org/bench4q/master/config/MonitorLimitableFields.xml @@ -0,0 +1,6 @@ + + + + > + > +> \ No newline at end of file diff --git a/Bench4Q-Master/src/test/java/stubs/Mock_MonitorMessenger.java b/Bench4Q-Master/src/test/java/stubs/Mock_MonitorMessenger.java index c1642ebc..e50de992 100644 --- a/Bench4Q-Master/src/test/java/stubs/Mock_MonitorMessenger.java +++ b/Bench4Q-Master/src/test/java/stubs/Mock_MonitorMessenger.java @@ -1,6 +1,7 @@ package stubs; import java.util.Date; +import java.util.UUID; import org.bench4q.master.infrastructure.communication.MonitorMessenger; import org.bench4q.share.models.monitor.MemoryModel; @@ -46,4 +47,11 @@ public class Mock_MonitorMessenger implements MonitorMessenger { return monitorModel(hostName, port).getProcessModel(); } + public boolean startMonitor(String hostName, int port, UUID testPlanId, + String limitModel) { + // TODO Auto-generated method stub + return false; + } + + } diff --git a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/api/MainController.java b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/api/MainController.java index 8e06a8e7..4a13391f 100644 --- a/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/api/MainController.java +++ b/Bench4Q-Monitor-Sigar/src/main/java/org/bench4q/monitor/api/MainController.java @@ -34,7 +34,7 @@ public class MainController { return new LimitableFieldsModel(); } - @RequestMapping(value = "/{testPlanId}", method = RequestMethod.PUT) + @RequestMapping(value = "/submitLimit/{testPlanId}", method = RequestMethod.PUT) @ResponseBody public String submitLimit(@PathVariable UUID testPlanId, @RequestBody LimitModel limits) { @@ -42,7 +42,7 @@ public class MainController { return testPlanId.toString(); } - @RequestMapping(value = "/{testPlanId}", method = RequestMethod.GET) + @RequestMapping(value = "/brief/{testPlanId}", method = RequestMethod.GET) @ResponseBody MonitorMain brief(@PathVariable UUID testPlanId) throws SigarException, InterruptedException, ExecutionException { diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/TestPlanModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/TestPlanModel.java index 07007312..01959f75 100644 --- a/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/TestPlanModel.java +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/master/TestPlanModel.java @@ -1,16 +1,20 @@ package org.bench4q.share.models.master; import java.util.List; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; +import org.bench4q.share.models.monitor.LimitModel; + @XmlRootElement(name = "testPlanModel") public class TestPlanModel { private String name = ""; private int sampleCycleInSeconds; private List runningScriptModels; private List monitorModels; + private LimitModel limitModel; @XmlElement public String getName() { @@ -51,4 +55,13 @@ public class TestPlanModel { this.sampleCycleInSeconds = sampleCycleInSeconds; } + @XmlElement(name = "limitModel") + public LimitModel getLimitModel() { + return limitModel; + } + + public void setLimitModel(LimitModel limitModel) { + this.limitModel = limitModel; + } + } diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitModel.java new file mode 100644 index 00000000..49e2e604 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitModel.java @@ -0,0 +1,47 @@ +package org.bench4q.share.models.monitor; + +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 params; + + public List getParams() { + return params; + } + + public void setParams(List 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; + } + + } +} diff --git a/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitableFieldsModel.java b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitableFieldsModel.java new file mode 100644 index 00000000..f25ec267 --- /dev/null +++ b/Bench4Q-Share/src/main/java/org/bench4q/share/models/monitor/LimitableFieldsModel.java @@ -0,0 +1,23 @@ +package org.bench4q.share.models.monitor; + +import java.util.List; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "limitableFields") +public class LimitableFieldsModel { + private List fields; + + @XmlElementWrapper(name = "fieldList") + @XmlElement(name = "field") + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } + +} diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/api/MonitorController.java b/Bench4Q-Web/src/main/java/org/bench4q/web/api/MonitorController.java index 4bbb54d0..cf73cd4d 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/api/MonitorController.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/api/MonitorController.java @@ -3,10 +3,12 @@ package org.bench4q.web.api; import java.util.HashMap; import java.util.List; import java.util.Map; + import org.bench4q.share.models.master.MonitorMemoryResponseModel; import org.bench4q.share.models.master.MonitorNetworkReponseModel; import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorProcessorResponseModel; +import org.bench4q.share.models.monitor.LimitableFieldsModel; import org.bench4q.web.masterMessager.MonitorMessager; import org.bench4q.web.model.ResultModel; import org.bench4q.web.service.MonitorService; @@ -187,4 +189,18 @@ public class MonitorController extends BaseController { } } + + @RequestMapping(value = "/getLimitableFields") + @ResponseBody + public Map getLimitableFields(@ModelAttribute("accessToken") String accessToken){ + Map map = new HashMap(); + LimitableFieldsModel limitableFieldsModel = this.getMonitorMessager().getLimitableFields(accessToken); + if(limitableFieldsModel == null){ + return fail(map, SERVER_ERROR); + } + success(map); + map.put("limitableFields", limitableFieldsModel); + return map; + + } } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java index 9b0da12c..b6d7fe52 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/masterMessager/MonitorMessager.java @@ -2,12 +2,14 @@ package org.bench4q.web.masterMessager; import java.util.HashMap; import java.util.Map; + import org.bench4q.share.communication.HttpRequester.HttpResponse; import org.bench4q.share.helper.MarshalHelper; import org.bench4q.share.models.master.MonitorMemoryResponseModel; import org.bench4q.share.models.master.MonitorNetworkReponseModel; import org.bench4q.share.models.master.MonitorPhysicalDiskResponseModel; import org.bench4q.share.models.master.MonitorProcessorResponseModel; +import org.bench4q.share.models.monitor.LimitableFieldsModel; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -120,6 +122,25 @@ public class MonitorMessager extends MasterMessager { } } + public LimitableFieldsModel getLimitableFields(String accessToken){ + String url = this.baseUrl + "/getLimitableFields"; + HttpResponse httpResponse = null; + try { + httpResponse = this.getHttpRequester().sendGet(url, null, + makeAccessTockenMap(accessToken)); + if (!validateHttpResponse(httpResponse)) { + this.handleInvalidatedResponse(url); + return null; + } + LimitableFieldsModel limitableFieldsModel = MarshalHelper + .unmarshal(LimitableFieldsModel.class, + httpResponse.getContent()); + return limitableFieldsModel; + }catch (Exception e) { + this.handleException(httpResponse, e); + return null; + } + } private Map createParmsMap(String testPlanRunId, String hostName, String port, String duationBegin) { Map params = new HashMap(); diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/model/TestPlanRequestModel.java b/Bench4Q-Web/src/main/java/org/bench4q/web/model/TestPlanRequestModel.java index 03b6c9d3..6bdea209 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/model/TestPlanRequestModel.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/model/TestPlanRequestModel.java @@ -2,12 +2,14 @@ package org.bench4q.web.model; import java.util.List; +import org.bench4q.share.models.monitor.LimitModel; + public class TestPlanRequestModel { private List ipList; private List scriptList; private String testPlanName; - + private LimitModel limitModel; public TestPlanRequestModel() { @@ -47,4 +49,12 @@ public class TestPlanRequestModel { public void setScriptList(List scriptList) { this.scriptList = scriptList; } + + public LimitModel getLimitModel() { + return limitModel; + } + + public void setLimitModel(LimitModel limitModel) { + this.limitModel = limitModel; + } } diff --git a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java index e5074c74..5a728652 100644 --- a/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java +++ b/Bench4Q-Web/src/main/java/org/bench4q/web/service/TestPlanService.java @@ -56,6 +56,7 @@ public class TestPlanService { } else testPlanModel.setMonitorModels(new ArrayList()); + testPlanModel.setLimitModel(testPlan.getLimitModel()); return testPlanModel; }