This commit is contained in:
Tienan Chen 2013-09-24 15:17:44 +08:00
parent d922460310
commit 8e11cdc79b
3 changed files with 97 additions and 36 deletions

View File

@ -30,13 +30,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.lowagie.text.Document; import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
@Component @Component
public class MonitorReportService { public class MonitorReportService {
public static String Processor = "ProcessorTimePercent"; public static String Processor = "ProcessorTimePercent";
public static String Memory = "Memory"; public static String Memory = "Memory";
public static String logical_Disk = "logicalDisk";
public static String network_Interface = "networkInterface";
private TestPlanScriptService testPlanScriptService; private TestPlanScriptService testPlanScriptService;
private TestPlanService testPlanService; private TestPlanService testPlanService;
private MonitorResultService monitorResultService; private MonitorResultService monitorResultService;
@ -146,11 +146,21 @@ public class MonitorReportService {
private void createSUTImage(String hostName, private void createSUTImage(String hostName,
MonitorResultContainerModel container, Document document) { MonitorResultContainerModel container, Document document) {
try {
ReportService.addParagraph(logical_Disk, document);
createLogicalDiskImage(container.getLogicalDiskResponseModels(), createLogicalDiskImage(container.getLogicalDiskResponseModels(),
document); document);
ReportService.addParagraph(Memory, document);
createMemoryImage(container.getMemoryResponseModels(), document); createMemoryImage(container.getMemoryResponseModels(), document);
ReportService.addParagraph(network_Interface, document);
createNetworkImage(container.getNetWorkReponseModels(), document); createNetworkImage(container.getNetWorkReponseModels(), document);
createProcessorImage(container.getProcessorResponseModels(), document); ReportService.addParagraph(Processor, document);
createProcessorImage(container.getProcessorResponseModels(),
document);
} catch (Exception e) {
e.printStackTrace();
}
} }
private void createProcessorImage(List<MonitorProcessorResponseModel> list, private void createProcessorImage(List<MonitorProcessorResponseModel> list,
@ -158,37 +168,26 @@ public class MonitorReportService {
if (list == null || list.size() == 0) { if (list == null || list.size() == 0) {
return; return;
} }
int processorCount = list.get(0).getProcessorModel() int seriesCount = list.get(0).getProcessorModel()
.getProcessorModelList().size(); .getProcessorModelList().size();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
TimeSeriesCollection lineDataset = new TimeSeriesCollection(); TimeSeries[] timeSeriesArray = ReportService.buildSeries(seriesCount,
TimeSeries[] timeSeriesArray = new TimeSeries[processorCount]; Processor);
for (int i = 0; i < timeSeriesArray.length; i++) {
timeSeriesArray[i] = new TimeSeries("Processor-" + i);
}
for (MonitorProcessorResponseModel model : list) { for (MonitorProcessorResponseModel model : list) {
for (int i = 0; i < processorCount; i++) { for (int i = 0; i < seriesCount; i++) {
timeSeriesArray[i].add(new Second(model.getCreateDatetime()), timeSeriesArray[i].add(new Second(model.getCreateDatetime()),
model.getProcessorModel().getProcessorModelList() model.getProcessorModel().getProcessorModelList()
.get(i).getProcessorTimePercent()); .get(i).getProcessorTimePercent());
} }
} }
for (int i = 0; i < timeSeriesArray.length; i++) {
lineDataset.addSeries(timeSeriesArray[i]);
}
JFreeChart chart = ChartFactory.createTimeSeriesChart(Processor,
ReportService.Time, Processor, lineDataset, true, true, true);
chart.setBackgroundPaint(java.awt.Color.white);
try { try {
ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300); ReportService
Paragraph paragraph = new Paragraph(Processor); .writeImageIntoPdf(
paragraph.setAlignment(Element.ALIGN_CENTER); ReportService.buildChartStream(document,
document.add(paragraph); seriesCount, timeSeriesArray, Processor)
ReportService.writeImageIntoPdf(outputStream.toByteArray(), .toByteArray(), document);
document);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -213,9 +212,6 @@ public class MonitorReportService {
chart.setBackgroundPaint(java.awt.Color.white); chart.setBackgroundPaint(java.awt.Color.white);
try { try {
ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300); ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300);
Paragraph paragraph = new Paragraph(Memory);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
ReportService.writeImageIntoPdf(outputStream.toByteArray(), ReportService.writeImageIntoPdf(outputStream.toByteArray(),
document); document);
} catch (Exception e) { } catch (Exception e) {

View File

@ -1,14 +1,23 @@
package org.bench4q.master.report; package org.bench4q.master.report;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.lowagie.text.Document; import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element; import com.lowagie.text.Element;
import com.lowagie.text.Image; import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.PdfWriter;
@Component @Component
@ -53,6 +62,45 @@ public class ReportService {
} }
} }
static void addParagraph(String content, Document document)
throws DocumentException {
Paragraph paragraph = new Paragraph(content);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
static ByteArrayOutputStream buildChartStream(Document document,
int seriesCount, TimeSeries[] timeSeriesArray, String value)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JFreeChart chart = ChartFactory.createTimeSeriesChart(value,
ReportService.Time, value, buildDataSet(timeSeriesArray), true,
true, true);
chart.setBackgroundPaint(java.awt.Color.white);
ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300);
return outputStream;
}
static TimeSeries[] buildSeries(int seriesCount, String mainTitle) {
TimeSeries[] timeSeriesArray = new TimeSeries[seriesCount];
for (int i = 0; i < seriesCount; i++) {
timeSeriesArray[i] = new TimeSeries(mainTitle + "-" + i);
}
return timeSeriesArray;
}
static TimeSeriesCollection buildDataSet(final TimeSeries[] timeSeriesArray) {
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
if (timeSeriesArray == null) {
return null;
}
for (int i = 0; i < timeSeriesArray.length; i++) {
lineDataset.addSeries(timeSeriesArray[i]);
}
return lineDataset;
}
static void writeImageIntoPdf(byte[] buffer, Document document) static void writeImageIntoPdf(byte[] buffer, Document document)
throws Exception { throws Exception {
Image image = Image.getInstance(buffer); Image image = Image.getInstance(buffer);

View File

@ -26,6 +26,8 @@ public class TestPlanTester extends TestBase {
private TestPlanModel testPlan = new TestPlanModel(); private TestPlanModel testPlan = new TestPlanModel();
private String _url = TestBase.BASE_URL + "/testPlan"; private String _url = TestBase.BASE_URL + "/testPlan";
private static int SCRIPTID = 3; private static int SCRIPTID = 3;
private static String Monitor_Host_Name = "127.0.0.1";
private static String monitor_port = "5556";
public UUID testWithTestPlanModel() throws JAXBException, IOException { public UUID testWithTestPlanModel() throws JAXBException, IOException {
String content; String content;
@ -129,17 +131,29 @@ public class TestPlanTester extends TestBase {
return properties; return properties;
} }
public void getMonitorResult(UUID testPlanId, String hostName, String port) public void getMonitorMemoryResult(UUID testPlanId, String hostName,
String port) throws IOException {
getMonitorResult(testPlanId, hostName, port,
"/monitorController/memorySUTInfo");
}
public void getProcessorResult(UUID testPlanId, String hostName, String port)
throws IOException { throws IOException {
getMonitorResult(testPlanId, hostName, port,
"/monitorController/processorSUTInfo");
}
private void getMonitorResult(UUID testPlanId, String hostName,
String port, String relativePath) throws IOException {
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("testPlanRunId", testPlanId.toString()); params.put("testPlanRunId", testPlanId.toString());
params.put("hostName", hostName); params.put("hostName", hostName);
params.put("port", port); params.put("port", port);
HttpResponse httpResponse = this.httpRequester.sendGet( HttpResponse httpResponse = this.httpRequester.sendGet(
TestBase.BASE_URL + "/monitorController/memorySUTInfo", params, TestBase.BASE_URL + relativePath, params,
this.createAccessTokenMap()); this.createAccessTokenMap());
System.out.println(httpResponse.getContent()); System.out.println(httpResponse.getContent());
} }
public static void main(String[] args) throws JAXBException, IOException, public static void main(String[] args) throws JAXBException, IOException,
@ -185,10 +199,13 @@ public class TestPlanTester extends TestBase {
Thread.sleep(10000); Thread.sleep(10000);
this.getScriptBrief(testPlanID, SCRIPTID); this.getScriptBrief(testPlanID, SCRIPTID);
this.getMonitorResult(testPlanID, "127.0.0.1", "5556"); this.getMonitorMemoryResult(testPlanID, Monitor_Host_Name, monitor_port);
Thread.sleep(6000); Thread.sleep(6000);
this.getMonitorResult(testPlanID, "127.0.0.1", "5556"); this.getMonitorMemoryResult(testPlanID, Monitor_Host_Name, monitor_port);
Thread.sleep(1000); Thread.sleep(1000);
this.getProcessorResult(testPlanID, Monitor_Host_Name, monitor_port);
Thread.sleep(1000);
this.getProcessorResult(testPlanID, Monitor_Host_Name, monitor_port);
this.getTestPlanReport(testPlanID); this.getTestPlanReport(testPlanID);
} }
} }