csv 解析

This commit is contained in:
Captain.B 2020-03-23 15:00:27 +08:00
parent 34fe47c1bc
commit bbdb987b64
3 changed files with 220 additions and 48 deletions

View File

@ -152,24 +152,12 @@
<artifactId>jmeter-plugins-casutg</artifactId> <artifactId>jmeter-plugins-casutg</artifactId>
<version>2.9</version> <version>2.9</version>
</dependency> </dependency>
<!-- jmeter graph -->
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-cmd</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-synthesis</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/kg.apc/jmeter-plugins-standard -->
<dependency>
<groupId>kg.apc</groupId>
<artifactId>jmeter-plugins-standard</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>

View File

@ -1,41 +1,45 @@
package io.metersphere; package io.metersphere;
import io.metersphere.commons.constants.JmeterReportType; import com.opencsv.bean.CsvToBean;
import kg.apc.jmeter.PluginsCMDWorker; import com.opencsv.bean.CsvToBeanBuilder;
import org.apache.jmeter.util.JMeterUtils; import com.opencsv.bean.HeaderColumnNameMappingStrategy;
import com.opencsv.bean.MappingStrategy;
import org.junit.Test; 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 { 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 @Test
public void test1() { public void test1() {
JMeterUtils.setJMeterHome("/opt/fit2cloud/apache-jmeter-5.2.1"); File csvFile = new File("/Users/liuruibin/Desktop/0316.jtl");
JMeterUtils.loadJMeterProperties("/opt/fit2cloud/apache-jmeter-5.2.1/bin/jmeter.properties"); HeaderColumnNameMappingStrategy<Metric> ms = new HeaderColumnNameMappingStrategy<>();
PluginsCMDWorker worker = new PluginsCMDWorker(); ms.setType(Metric.class);
worker.setPluginType(JmeterReportType.AggregateReport.name()); List<Metric> metrics = beanBuilderExample(csvFile.toPath(), ms);
worker.addExportMode(2); metrics.forEach(c -> {
worker.setOutputCSVFile("/tmp/test0320.csv"); System.out.println(c.getTimestamp());
worker.setInputFile("/Users/liuruibin/Desktop/0316.jtl"); });
worker.doJob(); }
public static List<Metric> beanBuilderExample(Path path, MappingStrategy<Metric> ms) {
try (Reader reader = Files.newBufferedReader(path)) {
CsvToBean<Metric> cb = new CsvToBeanBuilder<Metric>(reader)
.withType(Metric.class)
.withSkipLines(0)
.withMappingStrategy(ms)
.withIgnoreLeadingWhiteSpace(true)
.build();
return cb.parse();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
} }
} }

View File

@ -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;
}
}