Merge Bench4Q-Monitor

This commit is contained in:
coderfengyun 2014-03-20 11:27:12 +08:00
parent 07faeeb7c4
commit 02bd1f5b89
161 changed files with 5377 additions and 5101 deletions

View File

@ -0,0 +1,246 @@
package org.bench4q.master.report;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bench4q.master.domain.entity.MonitorResult;
import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.domain.service.TestPlanScriptService;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.MonitorResultContainerModel;
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.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lowagie.text.Document;
@Component
public class MonitorReportService {
public static String Processor = "ProcessorTimePercent (unit: %)";
public static String Memory = "Memory Available Memory(unit: KiloByte)";
public static String logical_Disk = "logicalDisk IO (unit: Byte)";
public static String network_Interface = "networkInterface IO (unit: Byte/second)";
private TestPlanScriptService testPlanScriptService;
private TestPlanService testPlanService;
private MonitorResultService monitorResultService;
public TestPlanScriptService getTestPlanScriptService() {
return testPlanScriptService;
}
@Autowired
public void setTestPlanScriptService(
TestPlanScriptService testPlanScriptService) {
this.testPlanScriptService = testPlanScriptService;
}
public TestPlanService getTestPlanService() {
return testPlanService;
}
@Autowired
public void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
public MonitorResultService getMonitorResultService() {
return monitorResultService;
}
@Autowired
public void setMonitorResultService(
MonitorResultService monitorResultService) {
this.monitorResultService = monitorResultService;
}
void createMonitorResultImages(UUID testPlanRunID, Document document) throws Bench4QException {
Map<String, MonitorResultContainerModel> SUTResultMap = new HashMap<String, MonitorResultContainerModel>();
List<MonitorResult> results = this.getMonitorResultService()
.queryMonitorResults(testPlanRunID);
if (results == null) {
return;
}
for (MonitorResult result : results) {
if (result == null) {
continue;
}
buildSUTResultMap(SUTResultMap, result);
}
for (String hostName : SUTResultMap.keySet()) {
createSUTImage(hostName, SUTResultMap.get(hostName), document);
}
}
private void buildSUTResultMap(
Map<String, MonitorResultContainerModel> sUTResultMap,
MonitorResult result) {
String hostName = result.getHostNameUnderMonitor();
if (!sUTResultMap.containsKey(hostName)) {
sUTResultMap.put(result.getHostNameUnderMonitor(),
new MonitorResultContainerModel());
}
sortResultToList(result, sUTResultMap.get(hostName));
}
private void sortResultToList(final MonitorResult result,
MonitorResultContainerModel container) {
try {
if (Class.forName(result.getType()).equals(PhysicalDiskModel.class)) {
container.getLogicalDiskModels().add(
(PhysicalDiskModel) MarshalHelper.unmarshal(
PhysicalDiskModel.class, result.getContent()));
} else if (Class.forName(result.getType())
.equals(MemoryModel.class)) {
container.getMemoryModels().add(
(MemoryModel) MarshalHelper.unmarshal(
MemoryModel.class, result.getContent()));
} else if (Class.forName(result.getType()).equals(
NetworkInterfaceModel.class)) {
container.getNetWorkModels().add(
(NetworkInterfaceModel) MarshalHelper.unmarshal(
NetworkInterfaceModel.class,
result.getContent()));
} else if (Class.forName(result.getType()).equals(
ProcessorModel.class)) {
container.getProcessorModels().add(
(ProcessorModel) MarshalHelper.unmarshal(
ProcessorModel.class, result.getContent()));
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private void createSUTImage(String hostName,
MonitorResultContainerModel container, Document document) {
try {
ReportService.addParagraph(logical_Disk, document);
createLogicalDiskImage(container.getLogicalDiskModels(), document);
ReportService.addParagraph(Memory, document);
createMemoryImage(container.getMemoryModels(), document);
ReportService.addParagraph(network_Interface, document);
// createNetworkImage(container.getNetWorkModels(), document);
ReportService.addParagraph(Processor, document);
createProcessorImage(container.getProcessorModels(), document);
} catch (Exception e) {
e.printStackTrace();
}
}
private void createProcessorImage(List<ProcessorModel> list,
Document document) throws Exception {
if (list == null || list.size() == 0) {
return;
}
int seriesCount = list.get(0).getProcessorModelList().size();
List<TimeSeries> timeSeriesArray = ReportService.buildSeries(
seriesCount, Processor);
for (ProcessorModel model : list) {
if (model == null)
continue;
for (int i = 0; i < timeSeriesArray.size(); i++) {
timeSeriesArray.get(i).addOrUpdate(
new Second(model.getSamplingTime()),
model.getProcessorModelList().get(i)
.getProcessorTimePercent());
}
}
ReportService.writeImageIntoPdf(
ReportService.buildChartStream(document, seriesCount,
timeSeriesArray, Processor).toByteArray(), document);
}
private void createMemoryImage(List<MemoryModel> list, Document document)
throws Exception {
if (list == null || list.size() == 0) {
return;
}
int seriesCount = 1;
List<TimeSeries> timeSeriesArray = ReportService.buildSeries(
seriesCount, Memory);
for (MemoryModel model : list) {
if (model == null)
continue;
for (int i = 0; i < timeSeriesArray.size(); i++) {
timeSeriesArray.get(i).addOrUpdate(
new Second(model.getSamplingTime()),
model.getAvailableKiloBytes());
}
}
ReportService.writeImageIntoPdf(
ReportService.buildChartStream(document, seriesCount,
timeSeriesArray, Memory).toByteArray(), document);
}
private void createLogicalDiskImage(List<PhysicalDiskModel> list,
Document document) throws Exception {
if (list == null || list.size() == 0) {
return;
}
int seriesCount = list.get(0).getFieFileSystemModels().size();
List<TimeSeries> timeSeriesArray = ReportService.buildSeries(
seriesCount, logical_Disk);
for (PhysicalDiskModel model : list) {
if (model == null)
continue;
for (int i = 0; i < timeSeriesArray.size(); i++) {
timeSeriesArray.get(i).addOrUpdate(
new Second(model.getSamplingTime()),
model.getFieFileSystemModels().get(i)
.getDiskTotalKBytesRate());
}
}
ReportService.writeImageIntoPdf(
ReportService.buildChartStream(document, seriesCount,
timeSeriesArray, logical_Disk).toByteArray(), document);
}
/**
* refactor this kind of use
*
* @param list
* @param document
* @throws Exception
*/
// private void createNetworkImage(List<NetworkInterfaceModel> list,
// Document document) throws Exception {
// if (list == null || list.size() == 0) {
// return;
// }
// int seriesCount = list.get(0).getNetworkList().size();
// List<TimeSeries> timeSeriesArray = ReportService.buildSeries(
// seriesCount, network_Interface);
// for (NetworkInterfaceModel model : list) {
// for (int i = 0; i < timeSeriesArray.size(); i++) {
// timeSeriesArray.get(i).addOrUpdate(
// new Second(model.getSamplingTime()),
// model.getNetworkList().get(i).getBytesTotalPerSecond());
// }
// }
// ReportService.writeImageIntoPdf(
// ReportService.buildChartStream(document, seriesCount,
// timeSeriesArray, network_Interface).toByteArray(),
// document);
// }
//}
}

View File

@ -0,0 +1,175 @@
package org.bench4q.master.report;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.bench4q.master.exception.ExceptionLog;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.title.TextTitle;
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
public class ReportService {
public static String Time = "time";
public static String PDF_FOLDER = "report";
private ScriptReportService scriptReportService;
private MonitorReportService monitorReportService;
private Logger logger = Logger.getLogger(ReportService.class);
private static int PICTURE_WIDTH = 500;
private static int PICTURE_HIGHT = 450;
public ScriptReportService getScriptReportService() {
return scriptReportService;
}
@Autowired
public void setScriptReportService(ScriptReportService scriptReportService) {
this.scriptReportService = scriptReportService;
}
public MonitorReportService getMonitorReportService() {
return monitorReportService;
}
@Autowired
public void setMonitorReportService(
MonitorReportService monitorReportService) {
this.monitorReportService = monitorReportService;
}
public byte[] createReport(UUID testPlanRunID) {
try {
if (isReportCreated(testPlanRunID)) {
return FileUtils.readFileToByteArray(new File(ReportService
.buildFilePath(testPlanRunID)));
}
isFolderExist();
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(
buildFilePath(testPlanRunID)));
document.open();
this.getScriptReportService().createScriptsResultsImage(
testPlanRunID, document);
this.getMonitorReportService().createMonitorResultImages(
testPlanRunID, document);
document.close();
return FileUtils.readFileToByteArray(new File(
buildFilePath(testPlanRunID)));
} catch (Exception e) {
logger.error(ExceptionLog.getStackTrace(e));
return null;
}
}
public boolean isReportCreated(UUID testPlanRunID) {
return new File(buildFilePath(testPlanRunID)).exists();
}
public static String buildFilePath(UUID testPlanRunID) {
return PDF_FOLDER + System.getProperty("file.separator")
+ testPlanRunID + ".pdf";
}
private void isFolderExist() {
try {
if (!new File(PDF_FOLDER).isDirectory()) {
new File(PDF_FOLDER).mkdir();
}
} catch (SecurityException e) {
e.printStackTrace();
}
}
static void addParagraph(String content, Document document)
throws DocumentException {
Paragraph paragraph = new Paragraph(content);
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
}
public static ByteArrayOutputStream buildChartStream(Document document,
int seriesCount, List<TimeSeries> timeSeriesArray, String value)
throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JFreeChart chart = ChartFactory.createTimeSeriesChart(value,
ReportService.Time, value, buildDataSet(timeSeriesArray), true,
true, true);
adjustChartFont(value, chart);
ChartUtilities.writeChartAsPNG(outputStream, chart, PICTURE_WIDTH,
PICTURE_HIGHT);
return outputStream;
}
private static void adjustChartFont(String value, JFreeChart chart) {
Font xfont = new Font("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", Font.CENTER_BASELINE, 14);// X
Font yfont = new Font("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", Font.CENTER_BASELINE, 14);// Y
Font kfont = new Font("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>", Font.CENTER_BASELINE, 12);//
Font titleFont = new Font("΢<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>", Font.CENTER_BASELINE, 16); // title
chart.setBorderStroke(new BasicStroke(1));
chart.setBorderVisible(true);
chart.setBorderPaint(Color.cyan);
chart.setTitle(new TextTitle(value, titleFont));
XYPlot plot = chart.getXYPlot();
plot.getRangeAxis().setLabelFont(yfont);
plot.getRangeAxis().setTickLabelFont(kfont);
plot.getDomainAxis().setLabelFont(xfont);
plot.getDomainAxis().setTickLabelFont(kfont);
chart.setBackgroundPaint(java.awt.Color.white);
}
public static List<TimeSeries> buildSeries(int seriesCount, String mainTitle) {
List<TimeSeries> timeSeriesArray = new ArrayList<TimeSeries>(
seriesCount);
for (int i = 0; i < seriesCount; i++) {
timeSeriesArray.add(new TimeSeries(mainTitle + "-" + i));
}
return timeSeriesArray;
}
static TimeSeriesCollection buildDataSet(
final List<TimeSeries> timeSeriesArray) {
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
if (timeSeriesArray == null) {
return null;
}
for (TimeSeries timeSeries : timeSeriesArray) {
lineDataset.addSeries(timeSeries);
}
return lineDataset;
}
public static void writeImageIntoPdf(byte[] buffer, Document document)
throws Exception {
Image image = Image.getInstance(buffer);
image.setAlignment(Element.ALIGN_CENTER);
document.add(image);
}
}

View File

@ -0,0 +1,140 @@
package org.bench4q.master.report;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.log4j.Logger;
import org.bench4q.master.domain.entity.TestPlanScript;
import org.bench4q.master.domain.entity.TestPlanScriptResult;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.domain.service.TestPlanScriptService;
import org.bench4q.master.domain.service.TestPlanService;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.lowagie.text.Document;
@Component
public class ScriptReportService {
public static String SCRIPT_TITLE = "Script result";
public static String Average_Response_Time = "averageResponseTime(unit: ms)";
public static String MAX_RESPONSE_TIME = "maxResponseTime(unit : time)";
private TestPlanScriptService testPlanScriptService;
private TestPlanService testPlanService;
private TestPlanRepository testPlanRepository;
private MonitorResultService monitorResultService;
private Logger logger = Logger.getLogger(ScriptReportService.class);
public TestPlanScriptService getTestPlanScriptService() {
return testPlanScriptService;
}
@Autowired
public void setTestPlanScriptService(
TestPlanScriptService testPlanScriptService) {
this.testPlanScriptService = testPlanScriptService;
}
public TestPlanService getTestPlanService() {
return testPlanService;
}
@Autowired
public void setTestPlanService(TestPlanService testPlanService) {
this.testPlanService = testPlanService;
}
public MonitorResultService getMonitorResultService() {
return monitorResultService;
}
@Autowired
public void setMonitorResultService(
MonitorResultService monitorResultService) {
this.monitorResultService = monitorResultService;
}
private TestPlanRepository getTestPlanRepository() {
return testPlanRepository;
}
@Autowired
private void setTestPlanRepository(TestPlanRepository testPlanRepository) {
this.testPlanRepository = testPlanRepository;
}
void createScriptsResultsImage(UUID testPlanRunId, Document document) {
Set<TestPlanScript> scripts = this.getTestPlanRepository()
.getTestPlanBy(testPlanRunId).getTestPlanScripts();
if (scripts == null) {
return;
}
for (TestPlanScript testPlanScript : scripts) {
createScriptImage(this.getTestPlanScriptService()
.queryScriptBriefResults(testPlanScript), document);
}
}
private void createScriptImage(List<TestPlanScriptResult> results,
Document document) {
if (results == null) {
return;
}
createAverageReponseTimeImage(results, document);
}
private void createAverageReponseTimeImage(
List<TestPlanScriptResult> results, Document document) {
List<TimeSeries> timeSeriesList = new ArrayList<TimeSeries>();
if (results == null) {
return;
}
addScriptSeries(results, timeSeriesList, "averageResponseTime");
addScriptSeries(results, timeSeriesList, "maxResponseTime");
try {
ReportService.writeImageIntoPdf(
ReportService.buildChartStream(document,
timeSeriesList.size(), timeSeriesList,
Average_Response_Time + "&" + MAX_RESPONSE_TIME)
.toByteArray(), document);
} catch (Exception e) {
e.printStackTrace();
}
}
// TODO: refactor this to a kind of command mode
private void addScriptSeries(List<TestPlanScriptResult> results,
List<TimeSeries> timeSeriesList, String fieldName) {
try {
TimeSeries timeSeries = new TimeSeries(fieldName);
for (TestPlanScriptResult result : results) {
if (!result.getResultType().equals(
ScriptBriefResultModel.class.getName())) {
continue;
}
ScriptBriefResultModel scriptBriefResultModel = result
.extractScriptBriefResultModel();
Field field = scriptBriefResultModel.getClass()
.getDeclaredField(fieldName);
field.setAccessible(true);
timeSeries.addOrUpdate(new Second(result.getCreateDatetime()),
field.getLong(scriptBriefResultModel));
}
timeSeriesList.add(timeSeries);
} catch (Exception e) {
logger.error(e.toString()
+ " in addScriptSeries where fieldName is : " + fieldName);
}
}
}

View File

@ -0,0 +1,54 @@
package org.bench4q.master.test.report;
import static org.junit.Assert.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.bench4q.master.report.ReportService;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.junit.Test;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfWriter;
public class ReportFontTest {
@Test
public void test() {
fail("Not yet implemented");
}
public void createReportTest() throws FileNotFoundException,
DocumentException {
Document document = new Document();
PdfWriter.getInstance(
document,
new FileOutputStream(ReportService.buildFilePath(UUID
.randomUUID())));
document.open();
}
public void createTestImage(UUID id, Document document) throws IOException,
Exception {
List<TimeSeries> timeSeriesList = new ArrayList<TimeSeries>();
TimeSeries timeSeries = new TimeSeries("test");
long now = System.currentTimeMillis();
timeSeries.add(new Second(new Date(now)), 3000);
timeSeries.add(new Second(new Date(now + 100)), 4000);
timeSeriesList.add(timeSeries);
ReportService.buildSeries(1, "test");
ReportService.writeImageIntoPdf(
ReportService.buildChartStream(document, 1, timeSeriesList,
"test").toByteArray(), document);
}
}

Some files were not shown because too many files have changed in this diff Show More