fix the bug in load monitor reuslt in monitor result service

This commit is contained in:
fanfuxiaoran 2014-03-24 10:17:08 +08:00
parent 3f51106f0a
commit 268f751220
5 changed files with 97 additions and 111 deletions

View File

@ -20,7 +20,6 @@ import org.apache.log4j.Logger;
import org.bench4q.master.domain.testplan.TestMonitorSampler;
import org.bench4q.master.exception.ExceptionLog;
import org.bench4q.master.helper.ApplicationContextHelper;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.monitor.MonitorMain;
@Entity
@ -93,10 +92,8 @@ public class Monitor extends Observable {
public void doAfterRun() {
try {
logger.info("enter monitor do after run");
MonitorMain monitorResult = getTestMonitorSampler()
.getMonitorResult(hostName, port);
logger.info(MarshalHelper.marshal(MonitorMain.class, monitorResult));
collectResult(monitorResult);
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
@ -105,10 +102,9 @@ public class Monitor extends Observable {
private void collectResult(MonitorMain monitorMain) {
try {
logger.info("set change");
this.setChanged();
this.notifyObservers(monitorMain);
logger.info("entry notify:" + monitorMain);
logger.info("enter update");
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
}

View File

@ -33,7 +33,6 @@ public class MonitorResultService {
private SessionHelper sessionHelper;
private static Logger logger = Logger.getLogger(MonitorResult.class);
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
@ -53,7 +52,8 @@ public class MonitorResultService {
}
@SuppressWarnings("unchecked")
public List<MonitorResult> queryMonitorResults(UUID testPlanRunId) throws Bench4QException {
public List<MonitorResult> queryMonitorResults(UUID testPlanRunId)
throws Bench4QException {
Session session = this.getSessionHelper().openSession();
try {
return session
@ -66,7 +66,8 @@ public class MonitorResultService {
} catch (Exception e) {
logger.info(e, e.fillInStackTrace());
throw new Bench4QException("", "get MonitorResult from db failed", "monitor service");
throw new Bench4QException("", "get MonitorResult from db failed",
"monitor service");
} finally {
if (session != null) {
session.close();
@ -77,12 +78,15 @@ public class MonitorResultService {
public MonitorPhysicalDiskResponseModel physicalDiskDiskResults(
UUID testPlanRunId, String hostName, int port, long beginTime)
throws Bench4QException {
try{
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
PhysicalDiskModel.class, MonitorPhysicalDiskResponseModel.class);
}catch(Exception e){
try {
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
PhysicalDiskModel.class,
MonitorPhysicalDiskResponseModel.class);
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
throw new Bench4QException("", "laod monitor physicalDisk result failed", "monitor service");
throw new Bench4QException("",
"laod monitor physicalDisk result failed",
"monitor service");
}
}
@ -90,6 +94,7 @@ public class MonitorResultService {
private List<MonitorResult> doGetMonitorResultsByeTypeAndBeginDate(
UUID testPlanRunId, String hostName, long beginTime,
Session session, Class<?> resultClass) {
logger.info(resultClass.getName());
return session
.createCriteria(MonitorResult.class)
.add(Restrictions.eq("testPlanDB", this.getTestPlanRepository()
@ -102,23 +107,26 @@ public class MonitorResultService {
public MonitorMemoryResponseModel loadMemoryModels(UUID testPlanRunId,
String hostName, int port, long beginTime) throws Bench4QException {
try{
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
MemoryModel.class, MonitorMemoryResponseModel.class);
}catch(Exception e){
try {
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
MemoryModel.class, MonitorMemoryResponseModel.class);
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
throw new Bench4QException("", "laod monitor memory result failed", "monitor service");
throw new Bench4QException("", "laod monitor memory result failed",
"monitor service");
}
}
public MonitorProcessorResponseModel loadProcessorModels(
UUID testPlanRunId, String hostName, int port, long beginTime) throws Bench4QException {
try{
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
ProcessorModel.class, MonitorProcessorResponseModel.class);
}catch(Exception e){
UUID testPlanRunId, String hostName, int port, long beginTime)
throws Bench4QException {
try {
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
ProcessorModel.class, MonitorProcessorResponseModel.class);
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
throw new Bench4QException("", "laod monitor processor result failed", "monitor service");
throw new Bench4QException("",
"laod monitor processor result failed", "monitor service");
}
}
@ -132,19 +140,21 @@ public class MonitorResultService {
List<MonitorResult> monitorResults = doGetMonitorResultsByeTypeAndBeginDate(
testPlanRunId, hostName, beginTime, session,
fromMonitorClass);
logger.info("minitor result size:" + monitorResults.size());
for (MonitorResult monitorResult : monitorResults) {
assert (fromMonitorClass.getName().equals(monitorResult
.getType()));
T1 result = (T1) MarshalHelper.unmarshal(fromMonitorClass,
monitorResult.getContent());
ret.add(result);
}
return ret;
} catch (Exception e) {
throw e;
}
finally{
} finally {
if (session != null) {
session.close();
}
@ -152,14 +162,16 @@ public class MonitorResultService {
}
public MonitorNetworkReponseModel loadNetworkInterfaceModels(
UUID testPlanRunId, String hostName, int port, long beginTime) throws Bench4QException {
try{
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
NetworkInterfaceModel.class, MonitorNetworkReponseModel.class);
}
catch(Exception e){
UUID testPlanRunId, String hostName, int port, long beginTime)
throws Bench4QException {
try {
return loadMonitorResultModel(testPlanRunId, hostName, beginTime,
NetworkInterfaceModel.class,
MonitorNetworkReponseModel.class);
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
throw new Bench4QException("", "laod monitor network result failed", "monitor service");
throw new Bench4QException("",
"laod monitor network result failed", "monitor service");
}
}

View File

@ -48,29 +48,6 @@ public class TestPlanScriptService {
.extracSpecifiedScript(scriptId);
}
// public void doSaveResult(UUID testPlanRunId, int scriptId,
// Object resultModel, Date now) {
// Session session = this.getSessionHelper().openSession();
// try {
// TestPlan testPlan = this.getTestPlanRepository().doGetTestPlanBy(
// session, testPlanRunId);
// if (testPlan == null) {
// return;
// }
// TestPlanScript specifiedScript = testPlan
// .extracSpecifiedScript(scriptId);
// specifiedScript.getTestPlanScriptResults().add(
// this.getTestPlanFactory().createScriptResultWithOutId(
// resultModel, specifiedScript, now));
// this.getTestPlanRepository().doUpdateEntity(session, testPlan);
// } catch (Exception e) {
// logger.error(ExceptionLog.getStackTrace(e));
// } finally {
// if (session != null) {
// session.close();
// }
// }
// }
public Collection<TestPlanScript> queryTestPlanScripts(UUID testPlanRunId) {
return this.getTestPlanRepository().getTestPlanBy(testPlanRunId)
@ -109,11 +86,4 @@ public class TestPlanScriptService {
}
}
// public void update(Observable o, Object arg) {
// System.out.println("enter update");
// RunningScriptInterface runningScript = (RunningScriptInterface) o;
// this.doSaveResult(runningScript.getTestPlanID(),
// runningScript.getScriptId(), arg,
// ((SampleModel) arg).getSamplingTime());
// }
}

View File

@ -50,7 +50,6 @@ public class TestMoniorResultSave implements Observer {
public void update(Observable o, Object arg) {
// TODO Auto-generated method stub
Monitor monitor = (Monitor) o;
logger.info("enter monitor update:" + monitor.getHostName());
this.saveMonitorResult(
@ -115,7 +114,8 @@ public class TestMoniorResultSave implements Observer {
monitorResult.setCreateDatetime(createDatetime);
monitorResult.setHostNameUnderMonitor(monitor.getHostName());
fields[i].setAccessible(true);
monitorResult.setType(fields[i].getName());
logger.info(fields[i].getType().getName());
monitorResult.setType(fields[i].getType().getName());
monitorResult.setContent(MarshalHelper.marshal(fields[i].getType(),
fields[i].get(monitorMain)));
monitorResults.add(monitorResult);

View File

@ -2,13 +2,22 @@ package org.bench4q.master.test.service;
import static org.junit.Assert.*;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.test.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.monitor.MemoryModel;
import org.bench4q.share.models.monitor.NetworkInterfaceModel;
import org.bench4q.share.models.monitor.PhysicalDiskModel;
import org.bench4q.share.models.monitor.ProcessorModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -20,8 +29,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
// private String monitor = "133.133.12.3";
// private int port = 5556;
private String monitor = "133.133.12.3";
private int port = 5556;
private MonitorResultService monitorResultService;
private TestPlanRepository testPlanRepository;
private TestPlanFactory testPlanFactory;
@ -63,7 +72,6 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult()
throws JAXBException {
prepareForTestPlanRunning();
submitATestPlanWithOneScript();
}
@After
@ -73,47 +81,47 @@ public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
@Test
public void testLoadMemoryResults() throws InterruptedException,
Bench4QException {/*
* TestPlan testPlanInDomain =
* this.getTestPlanFactory().convertToDomain(
* this.getTestPlanRepository().getTestPlanBy(
* getTestPlanRunIdUuid()));
* this.getTestPlanEngine()
* .runWith(testPlanInDomain, user)
* assertEquals(TestPlanStatus.InRunning,
* testPlanInDomain.run()); Thread.sleep(1000);
* TestPlan testPlan =
* this.getTestPlanRepository().getTestPlanBy(
* getTestPlanRunIdUuid());
* assertEquals(TestPlanStatus.InRunning,
* TestPlanStatus
* .valueOf(testPlan.getCurrentStatus()));
* Thread.sleep(4000); List<PhysicalDiskModel>
* logicalDiskModels = this
* .getMonitorResultService()
* .physicalDiskDiskResults
* (this.getTestPlanRunIdUuid(), monitor, port,
* 0).getPhysicalDiskModels(); List<MemoryModel>
* memoryModels = this .getMonitorResultService()
* .loadMemoryModels(this.getTestPlanRunIdUuid(),
* monitor, port, 0) .getMemoryModels();
* List<ProcessorModel> processorModels = this
* .getMonitorResultService()
* .loadProcessorModels(this.getTestPlanRunIdUuid(),
* monitor, port, 0).getProcessorModels();
* List<NetworkInterfaceModel>
* networkInterfaceModels = this
* .getMonitorResultService()
* .loadNetworkInterfaceModels
* (this.getTestPlanRunIdUuid(), monitor, port,
* 0).getModels(); assertEquals(1,
* processorModels.size());
* assertTrue(processorModels.size() >= 1);
* assertTrue(logicalDiskModels.size() > 0);
* assertTrue(memoryModels.size() > 0);
*
* assertTrue(networkInterfaceModels.size() >= 1);
*/
}
Bench4QException {
assertNotNull(this.getAgentMessenger().askLiving(Test_AGENT_HOSTNAME,
TEST_PORT));
User user = this.getUserRepository().getUser("admin");
int scriptId = getUserFirstScript(user);
this.getHaPool().timerTask();
this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
createATestPlanWithOneScript(scriptId), user));
Thread.sleep(500);
TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain(
this.getTestPlanRepository().getTestPlanBy(
getTestPlanRunIdUuid()));
assertEquals(TestPlanStatus.InRunning.name(),
testPlanInDomain.getCurrentStatus());
Thread.sleep(4000);
TestPlan testPlan = this.getTestPlanRepository().getTestPlanBy(
getTestPlanRunIdUuid());
assertEquals(TestPlanStatus.InRunning,
TestPlanStatus.valueOf(testPlan.getCurrentStatus()));
List<PhysicalDiskModel> logicalDiskModels = this
.getMonitorResultService()
.physicalDiskDiskResults(this.getTestPlanRunIdUuid(), monitor,
port, 0).getPhysicalDiskModels();
List<MemoryModel> memoryModels = this
.getMonitorResultService()
.loadMemoryModels(this.getTestPlanRunIdUuid(), monitor, port, 0)
.getMemoryModels();
List<ProcessorModel> processorModels = this
.getMonitorResultService()
.loadProcessorModels(this.getTestPlanRunIdUuid(), monitor,
port, 0).getProcessorModels();
List<NetworkInterfaceModel> networkInterfaceModels = this
.getMonitorResultService()
.loadNetworkInterfaceModels(this.getTestPlanRunIdUuid(),
monitor, port, 0).getModels();
assertEquals(1, processorModels.size());
assertTrue(processorModels.size() >= 1);
assertTrue(logicalDiskModels.size() > 0);
assertTrue(memoryModels.size() > 0);
assertTrue(networkInterfaceModels.size() >= 1);
}
}