fix the bug when save monitor result

This commit is contained in:
fanfuxiaoran 2014-03-12 17:22:32 +08:00
parent 4e6ad98792
commit 43cb4fb4cf
7 changed files with 108 additions and 58 deletions

View File

@ -98,7 +98,7 @@ public class Monitor extends Observable {
}
private void collectResult(MonitorMain monitorMain) {
private void collectResult( MonitorMain monitorMain) {
this.setChanged();
this.notifyObservers(monitorMain);
}

View File

@ -118,7 +118,7 @@ public class TestPlanRepository extends AbstractRepositoty {
return ret;
} catch (Exception e) {
logger.error("loadEntities");
logger.error(e,e.fillInStackTrace());
logger.error(e, e.fillInStackTrace());
return null;
} finally {
releaseSession(session);
@ -162,14 +162,18 @@ public class TestPlanRepository extends AbstractRepositoty {
}
}
public void doUpdateEntity(Session session, TestPlan testPlan) {
public boolean doUpdateEntity(Session session, TestPlan testPlan) {
Transaction transaction = session.beginTransaction();
try {
session.update(testPlan);
transaction.commit();
return true;
} catch (Exception e) {
transaction.rollback();
logger.error(ExceptionLog.getStackTrace(e));
return false;
} finally {
releaseSession(session);
}
}

View File

@ -72,18 +72,24 @@ public class TestMoniorResultSave implements Observer {
monitor, createDatetime);
if (monitorResults == null)
return false;
if (monitorResults.size() == 0) {
System.out.println("size is 0 ");
return false;
}
for (MonitorResult monitorResult : monitorResults) {
if (session == null)
;
return false;
monitorResultToAdd.add(monitorResult);
}
this.getTestPlanRepository().doUpdateEntity(session, testPlan);
return true;
System.out.println("monitor ressult size:" + monitorResultToAdd.size());
return this.getTestPlanRepository().doUpdateEntity(session,
testPlan);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
if (session != null) {
if (session.isConnected()) {
session.close();
}
}
@ -96,13 +102,15 @@ public class TestMoniorResultSave implements Observer {
IllegalAccessException, JAXBException {
List<MonitorResult> monitorResults = new LinkedList<MonitorResult>();
MonitorResult monitorResult = new MonitorResult();
monitorResult.setTestPlanDB(testPlan);
monitorResult.setMonitor(monitor);
monitorResult.setCreateDatetime(createDatetime);
Field[] fields = monitorMain.getClass().getDeclaredFields();
monitorResult.setHostNameUnderMonitor(monitor.getHostName());
for (int i = 0; i < fields.length; i++) {
MonitorResult monitorResult = new MonitorResult();
monitorResult.setTestPlanDB(testPlan);
monitorResult.setMonitor(monitor);
monitorResult.setCreateDatetime(createDatetime);
monitorResult.setHostNameUnderMonitor(monitor.getHostName());
fields[i].setAccessible(true);
monitorResult.setType(fields[i].getName());
monitorResult.setContent(MarshalHelper.marshal(fields[i].getType(),

View File

@ -18,7 +18,7 @@ public class TestMonitorSampler {
public MonitorMain getMonitorResult( String hostName,
int port) {
return this.monitorMessenger
.monitorModel( hostName, port);
.monitorModel(hostName, port);
}
}

View File

@ -2,7 +2,6 @@ package org.bench4q.master.infrastructure.communication;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import javax.xml.bind.JAXBException;
@ -17,6 +16,7 @@ import org.bench4q.share.models.monitor.MonitorMain;
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.ProcessModel;
import org.bench4q.share.models.monitor.ProcessorModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -50,35 +50,11 @@ public class MonitorMessenger {
return hostName + ":" + port + relativePath;
}
public PhysicalDiskModel physicalDiskModel(UUID testPlanRunId,
String hostName, int port) {
return getMonitorModel( hostName, port,
PhysicalDiskModel.class, "physicalDisk");
public PhysicalDiskModel physicalDisk(String hostName, int port) {
return getMonitorModel(hostName, port, PhysicalDiskModel.class,
"physicalDisk");
}
public <T extends SampleModel> T getMonitorMainModel(String hostName,
int port) {
try {
Date sampleDate = new Date();
HttpResponse httpResponse = this.getHttpRequester().sendGet(
buildUrl(hostName, port, "/monitor/" + "all"), null, null);
if (HttpRequester.isInvalidResponse(httpResponse)) {
logger.info("The httpResponses is invalid ");
return null;
}
@SuppressWarnings("unchecked")
T model = (T) MarshalHelper.unmarshal(MonitorMain.class,
httpResponse.getContent());
model.setSamplingTime(sampleDate);
return model;
} catch (IOException e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
} catch (JAXBException e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
private <T extends SampleModel> T getMonitorModel(String hostName,
int port, Class<T> resultClass, String monitorTarget) {
@ -106,32 +82,26 @@ public class MonitorMessenger {
}
public MemoryModel memory(String hostName, int port) {
return getMonitorModel( hostName, port,
MemoryModel.class, "memory");
return getMonitorModel(hostName, port, MemoryModel.class, "memory");
}
public ProcessorModel processor( String hostName,
int port) {
return this.getMonitorModel( hostName, port,
ProcessorModel.class, "processor");
public ProcessorModel processor(String hostName, int port) {
return this.getMonitorModel(hostName, port, ProcessorModel.class,
"processor");
}
public NetworkInterfaceModel networkInterface(
String hostName, int port) {
public NetworkInterfaceModel networkInterface(String hostName, int port) {
return this.getMonitorModel(hostName, port,
NetworkInterfaceModel.class, "network");
}
public ProcessorModel processorModel( String hostName,
int port) {
return this.getMonitorModel(hostName, port,
ProcessorModel.class, "process");
public ProcessModel process(String hostName, int port) {
return this.getMonitorModel(hostName, port, ProcessModel.class,
"process");
}
public MonitorMain monitorModel( String hostName,
int port) {
return this.getMonitorModel( hostName, port,
MonitorMain.class, "all");
public MonitorMain monitorModel(String hostName, int port) {
return this.getMonitorModel(hostName, port, MonitorMain.class, "all");
}
}

View File

@ -0,0 +1,68 @@
package MonitorModelTest;
import static org.junit.Assert.*;
import org.bench4q.master.infrastructure.communication.MonitorMessenger;
import org.bench4q.share.models.master.MonitorModel;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class MonitorModelTest {
private MonitorModel monitor;
private MonitorMessenger monitorMessenger;
public MonitorMessenger getMonitorMessenger() {
return monitorMessenger;
}
@Autowired
public void setMonitorMessenger(MonitorMessenger monitorMessenger) {
this.monitorMessenger = monitorMessenger;
}
@Before
public void setUp() {
this.monitor = new MonitorModel();
monitor.setHostName("127.0.0.1");
monitor.setPort(5556);
}
@Test
public void testMemeoryModel() {
assertTrue(this.monitor.getHostName().equals("127.0.0.1"));
assertTrue(this.monitor.getPort() == 5556);
Assert.assertNotNull(this.getMonitorMessenger().memory(
monitor.getHostName(), monitor.getPort()));
}
@Test
public void testProcessorModel() {
Assert.assertNotNull(this.getMonitorMessenger().processor(
monitor.getHostName(), monitor.getPort()));
}
@Test
public void testProcessModel() {
Assert.assertNotNull(this.getMonitorMessenger().process(
monitor.getHostName(), monitor.getPort()));
}
@Test
public void testPhysicalDiskModel() {
Assert.assertNotNull(this.getMonitorMessenger().physicalDisk(
monitor.getHostName(), monitor.getPort()));
}
@Test
public void testMonitorMain() {
Assert.assertNotNull(this.getMonitorMessenger().monitorModel(
monitor.getHostName(), monitor.getPort()));
}
}

View File

@ -34,7 +34,7 @@ public class Test_MonitorMessenger {
@Test
public void test_getMonitorModel() {
Assert.assertNotNull(this.getMonitorMessenger().getMonitorMainModel(
Assert.assertNotNull(this.getMonitorMessenger().monitorModel(
monitor.getHostName(), monitor.getPort()));
}
}