From bbdb987b642d1dd224f8ab08103be584ebe0e1f3 Mon Sep 17 00:00:00 2001 From: "Captain.B" Date: Mon, 23 Mar 2020 15:00:27 +0800 Subject: [PATCH] =?UTF-8?q?csv=20=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/pom.xml | 22 +-- .../io/metersphere/GenerateGraphTest.java | 66 ++++--- .../src/test/java/io/metersphere/Metric.java | 180 ++++++++++++++++++ 3 files changed, 220 insertions(+), 48 deletions(-) create mode 100644 backend/src/test/java/io/metersphere/Metric.java diff --git a/backend/pom.xml b/backend/pom.xml index a15f39a7a1..561fe81516 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -152,24 +152,12 @@ jmeter-plugins-casutg 2.9 - - - kg.apc - jmeter-plugins-cmd - 2.2 - - - kg.apc - jmeter-plugins-synthesis - 2.2 - - - - kg.apc - jmeter-plugins-standard - 1.4.0 - + + com.opencsv + opencsv + 5.1 + org.apache.httpcomponents diff --git a/backend/src/test/java/io/metersphere/GenerateGraphTest.java b/backend/src/test/java/io/metersphere/GenerateGraphTest.java index a3a544e9ac..07d47476b0 100644 --- a/backend/src/test/java/io/metersphere/GenerateGraphTest.java +++ b/backend/src/test/java/io/metersphere/GenerateGraphTest.java @@ -1,41 +1,45 @@ package io.metersphere; -import io.metersphere.commons.constants.JmeterReportType; -import kg.apc.jmeter.PluginsCMDWorker; -import org.apache.jmeter.util.JMeterUtils; +import com.opencsv.bean.CsvToBean; +import com.opencsv.bean.CsvToBeanBuilder; +import com.opencsv.bean.HeaderColumnNameMappingStrategy; +import com.opencsv.bean.MappingStrategy; import org.junit.Test; +import java.io.File; +import java.io.Reader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + public class GenerateGraphTest { - /* - AggregateReport = JMeter's native Aggregate Report, can be saved only as CSV - SynthesisReport = mix between JMeter's native Summary Report and Aggregate Report, can be saved only as CSV - ThreadsStateOverTime = Active Threads Over Time - BytesThroughputOverTime - HitsPerSecond - LatenciesOverTime - PerfMon = PerfMon Metrics Collector - DbMon = DbMon Metrics Collector, DataBase, get performance counters via sql - JMXMon = JMXMon Metrics Collector, Java Management Extensions counters - ResponseCodesPerSecond - ResponseTimesDistribution - ResponseTimesOverTime - ResponseTimesPercentiles - ThroughputVsThreads - TimesVsThreads = Response Times VS Threads - TransactionsPerSecond - PageDataExtractorOverTime - MergeResults = MergeResults Command Line Merge Tool to simplify the comparison of two or more load tests, need properties file (like merge-results.properties) - */ + @Test public void test1() { - JMeterUtils.setJMeterHome("/opt/fit2cloud/apache-jmeter-5.2.1"); - JMeterUtils.loadJMeterProperties("/opt/fit2cloud/apache-jmeter-5.2.1/bin/jmeter.properties"); - PluginsCMDWorker worker = new PluginsCMDWorker(); - worker.setPluginType(JmeterReportType.AggregateReport.name()); - worker.addExportMode(2); - worker.setOutputCSVFile("/tmp/test0320.csv"); - worker.setInputFile("/Users/liuruibin/Desktop/0316.jtl"); - worker.doJob(); + File csvFile = new File("/Users/liuruibin/Desktop/0316.jtl"); + HeaderColumnNameMappingStrategy ms = new HeaderColumnNameMappingStrategy<>(); + ms.setType(Metric.class); + List metrics = beanBuilderExample(csvFile.toPath(), ms); + metrics.forEach(c -> { + System.out.println(c.getTimestamp()); + }); + } + + public static List beanBuilderExample(Path path, MappingStrategy ms) { + try (Reader reader = Files.newBufferedReader(path)) { + + CsvToBean cb = new CsvToBeanBuilder(reader) + .withType(Metric.class) + .withSkipLines(0) + .withMappingStrategy(ms) + .withIgnoreLeadingWhiteSpace(true) + .build(); + + return cb.parse(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; } } diff --git a/backend/src/test/java/io/metersphere/Metric.java b/backend/src/test/java/io/metersphere/Metric.java new file mode 100644 index 0000000000..e1c6bb6370 --- /dev/null +++ b/backend/src/test/java/io/metersphere/Metric.java @@ -0,0 +1,180 @@ +package io.metersphere; + +import com.opencsv.bean.CsvBindByName; + +public class Metric { + // timestamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect + + @CsvBindByName(column = "timestamp") + private String timestamp; + @CsvBindByName(column = "elapsed") + private String elapsed; + @CsvBindByName(column = "label") + private String label; + @CsvBindByName(column = "responseCode") + private String responseCode; + @CsvBindByName(column = "responseMessage") + private String responseMessage; + @CsvBindByName(column = "threadName") + private String threadName; + @CsvBindByName(column = "dataType") + private String dataType; + @CsvBindByName(column = "success") + private String success; + @CsvBindByName(column = "failureMessage") + private String failureMessage; + @CsvBindByName(column = "bytes") + private String bytes; + @CsvBindByName(column = "sentBytes") + private String sentBytes; + @CsvBindByName(column = "grpThreads") + private String grpThreads; + @CsvBindByName(column = "allThreads") + private String allThreads; + @CsvBindByName(column = "URL") + private String url; + @CsvBindByName(column = "Latency") + private String latency; + @CsvBindByName(column = "IdleTime") + private String idleTime; + @CsvBindByName(column = "Connect") + private String connect; + + + public String getTimestamp() { + return timestamp; + } + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + + public String getElapsed() { + return elapsed; + } + + public void setElapsed(String elapsed) { + this.elapsed = elapsed; + } + + + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public String getResponseCode() { + return responseCode; + } + + public void setResponseCode(String responseCode) { + this.responseCode = responseCode; + } + + public String getResponseMessage() { + return responseMessage; + } + + public void setResponseMessage(String responseMessage) { + this.responseMessage = responseMessage; + } + + public String getThreadName() { + return threadName; + } + + public void setThreadName(String threadName) { + this.threadName = threadName; + } + + public String getDataType() { + return dataType; + } + + public void setDataType(String dataType) { + this.dataType = dataType; + } + + public String getSuccess() { + return success; + } + + public void setSuccess(String success) { + this.success = success; + } + + public String getFailureMessage() { + return failureMessage; + } + + public void setFailureMessage(String failureMessage) { + this.failureMessage = failureMessage; + } + + public String getBytes() { + return bytes; + } + + public void setBytes(String bytes) { + this.bytes = bytes; + } + + public String getSentBytes() { + return sentBytes; + } + + public void setSentBytes(String sentBytes) { + this.sentBytes = sentBytes; + } + + public String getGrpThreads() { + return grpThreads; + } + + public void setGrpThreads(String grpThreads) { + this.grpThreads = grpThreads; + } + + public String getAllThreads() { + return allThreads; + } + + public void setAllThreads(String allThreads) { + this.allThreads = allThreads; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getLatency() { + return latency; + } + + public void setLatency(String latency) { + this.latency = latency; + } + + public String getIdleTime() { + return idleTime; + } + + public void setIdleTime(String idleTime) { + this.idleTime = idleTime; + } + + public String getConnect() { + return connect; + } + + public void setConnect(String connect) { + this.connect = connect; + } +}