From 8e11cdc79bee913f9d50e377f5ff0e1068238cb3 Mon Sep 17 00:00:00 2001 From: Tienan Chen Date: Tue, 24 Sep 2013 15:17:44 +0800 Subject: [PATCH] refactor --- .../master/report/MonitorReportService.java | 58 +++++++++---------- .../bench4q/master/report/ReportService.java | 48 +++++++++++++++ .../bench4q/master/test/TestPlanTester.java | 27 +++++++-- 3 files changed, 97 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/bench4q/master/report/MonitorReportService.java b/src/main/java/org/bench4q/master/report/MonitorReportService.java index ae2b86b3..3792b50a 100644 --- a/src/main/java/org/bench4q/master/report/MonitorReportService.java +++ b/src/main/java/org/bench4q/master/report/MonitorReportService.java @@ -30,13 +30,13 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.lowagie.text.Document; -import com.lowagie.text.Element; -import com.lowagie.text.Paragraph; @Component public class MonitorReportService { public static String Processor = "ProcessorTimePercent"; public static String Memory = "Memory"; + public static String logical_Disk = "logicalDisk"; + public static String network_Interface = "networkInterface"; private TestPlanScriptService testPlanScriptService; private TestPlanService testPlanService; private MonitorResultService monitorResultService; @@ -146,11 +146,21 @@ public class MonitorReportService { private void createSUTImage(String hostName, MonitorResultContainerModel container, Document document) { - createLogicalDiskImage(container.getLogicalDiskResponseModels(), - document); - createMemoryImage(container.getMemoryResponseModels(), document); - createNetworkImage(container.getNetWorkReponseModels(), document); - createProcessorImage(container.getProcessorResponseModels(), document); + try { + ReportService.addParagraph(logical_Disk, document); + createLogicalDiskImage(container.getLogicalDiskResponseModels(), + document); + ReportService.addParagraph(Memory, document); + createMemoryImage(container.getMemoryResponseModels(), document); + ReportService.addParagraph(network_Interface, document); + createNetworkImage(container.getNetWorkReponseModels(), document); + ReportService.addParagraph(Processor, document); + createProcessorImage(container.getProcessorResponseModels(), + document); + } catch (Exception e) { + e.printStackTrace(); + } + } private void createProcessorImage(List list, @@ -158,37 +168,26 @@ public class MonitorReportService { if (list == null || list.size() == 0) { return; } - int processorCount = list.get(0).getProcessorModel() + int seriesCount = list.get(0).getProcessorModel() .getProcessorModelList().size(); - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - TimeSeriesCollection lineDataset = new TimeSeriesCollection(); - TimeSeries[] timeSeriesArray = new TimeSeries[processorCount]; - for (int i = 0; i < timeSeriesArray.length; i++) { - timeSeriesArray[i] = new TimeSeries("Processor-" + i); - } + + TimeSeries[] timeSeriesArray = ReportService.buildSeries(seriesCount, + Processor); 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()), model.getProcessorModel().getProcessorModelList() .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 { - ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300); - Paragraph paragraph = new Paragraph(Processor); - paragraph.setAlignment(Element.ALIGN_CENTER); - document.add(paragraph); - ReportService.writeImageIntoPdf(outputStream.toByteArray(), - document); + ReportService + .writeImageIntoPdf( + ReportService.buildChartStream(document, + seriesCount, timeSeriesArray, Processor) + .toByteArray(), document); } catch (Exception e) { e.printStackTrace(); } @@ -213,9 +212,6 @@ public class MonitorReportService { chart.setBackgroundPaint(java.awt.Color.white); try { ChartUtilities.writeChartAsPNG(outputStream, chart, 400, 300); - Paragraph paragraph = new Paragraph(Memory); - paragraph.setAlignment(Element.ALIGN_CENTER); - document.add(paragraph); ReportService.writeImageIntoPdf(outputStream.toByteArray(), document); } catch (Exception e) { diff --git a/src/main/java/org/bench4q/master/report/ReportService.java b/src/main/java/org/bench4q/master/report/ReportService.java index 33b5968e..837dfd28 100644 --- a/src/main/java/org/bench4q/master/report/ReportService.java +++ b/src/main/java/org/bench4q/master/report/ReportService.java @@ -1,14 +1,23 @@ package org.bench4q.master.report; +import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; +import java.io.IOException; 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.stereotype.Component; import com.lowagie.text.Document; +import com.lowagie.text.DocumentException; import com.lowagie.text.Element; import com.lowagie.text.Image; +import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.PdfWriter; @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) throws Exception { Image image = Image.getInstance(buffer); diff --git a/src/test/java/org/bench4q/master/test/TestPlanTester.java b/src/test/java/org/bench4q/master/test/TestPlanTester.java index 008d8d47..963dd4b8 100644 --- a/src/test/java/org/bench4q/master/test/TestPlanTester.java +++ b/src/test/java/org/bench4q/master/test/TestPlanTester.java @@ -26,6 +26,8 @@ public class TestPlanTester extends TestBase { private TestPlanModel testPlan = new TestPlanModel(); private String _url = TestBase.BASE_URL + "/testPlan"; 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 { String content; @@ -129,17 +131,29 @@ public class TestPlanTester extends TestBase { 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 { + getMonitorResult(testPlanId, hostName, port, + "/monitorController/processorSUTInfo"); + } + + private void getMonitorResult(UUID testPlanId, String hostName, + String port, String relativePath) throws IOException { Map params = new HashMap(); params.put("testPlanRunId", testPlanId.toString()); params.put("hostName", hostName); params.put("port", port); HttpResponse httpResponse = this.httpRequester.sendGet( - TestBase.BASE_URL + "/monitorController/memorySUTInfo", params, + TestBase.BASE_URL + relativePath, params, this.createAccessTokenMap()); System.out.println(httpResponse.getContent()); - } public static void main(String[] args) throws JAXBException, IOException, @@ -185,10 +199,13 @@ public class TestPlanTester extends TestBase { Thread.sleep(10000); this.getScriptBrief(testPlanID, SCRIPTID); - this.getMonitorResult(testPlanID, "127.0.0.1", "5556"); + this.getMonitorMemoryResult(testPlanID, Monitor_Host_Name, monitor_port); Thread.sleep(6000); - this.getMonitorResult(testPlanID, "127.0.0.1", "5556"); + this.getMonitorMemoryResult(testPlanID, Monitor_Host_Name, monitor_port); 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); } }