Skip to content

Commit ac41f0b

Browse files
Version 2.4.0
1 parent a6feef7 commit ac41f0b

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

src/main/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/ElasticSearchMetric.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.*;
1616

1717
public class ElasticSearchMetric {
18-
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchBackendClient.class);
18+
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchMetric.class);
1919

2020
private SampleResult sampleResult;
2121
private String esTestMode;
@@ -25,8 +25,8 @@ public class ElasticSearchMetric {
2525

2626
public ElasticSearchMetric(SampleResult sr, String testMode, String timeStamp, int buildNumber) {
2727
this.sampleResult = sr;
28-
this.esTestMode = testMode;
29-
this.esTimestamp = timeStamp;
28+
this.esTestMode = testMode.trim();
29+
this.esTimestamp = timeStamp.trim();
3030
this.ciBuildNumber = buildNumber;
3131
this.json = new HashMap<>();
3232
}
@@ -63,17 +63,17 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
6363
this.json.put("InjectorHostname", InetAddress.getLocalHost().getHostName());
6464

6565
// Add the details according to the mode that is set
66-
if(this.esTestMode == "debug" || this.esTestMode == "error") {
67-
addDetails();
68-
} else if (this.esTestMode == "info") {
69-
if(!this.sampleResult.isSuccessful()) {
66+
switch(this.esTestMode) {
67+
case "debug":
7068
addDetails();
71-
}
72-
} else if (this.esTestMode != "quiet") {
73-
logger.warn("The parameter \"es.test.mode\" isn't set properly. Three modes are allowed: debug ,info, and quiet.");
74-
logger.warn(" -- \"debug\": sends request and response details to ElasticSearch. Info only sends the details if the response has an error.");
75-
logger.warn(" -- \"info\": should be used in production");
76-
logger.warn(" -- \"quiet\": should be used if you don't care to have the details.");
69+
break;
70+
case "error":
71+
addDetails();
72+
break;
73+
case "info":
74+
if(!this.sampleResult.isSuccessful())
75+
addDetails();
76+
break;
7777
}
7878

7979
addAssertions();

src/main/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/ElasticSearchMetricSender.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.apache.http.HttpStatus;
55
import org.apache.http.entity.ContentType;
66
import org.apache.http.nio.entity.NStringEntity;
7-
import org.apache.jmeter.visualizers.backend.BackendListenerContext;
87
import org.elasticsearch.client.Response;
98
import org.elasticsearch.client.RestClient;
109
import org.slf4j.Logger;
@@ -15,7 +14,7 @@
1514
import java.util.*;
1615

1716
public class ElasticSearchMetricSender {
18-
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchBackendClient.class);
17+
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchMetricSender.class);
1918

2019
private RestClient client;
2120
private String esIndex;
@@ -34,12 +33,14 @@ public ElasticSearchMetricSender(RestClient cli, String index) {
3433
public int getListSize() { return this.metricList.size(); }
3534

3635
/**
37-
* This method closes the REST client and clears the ElasticSearch documents list
36+
* This method clears the ElasticSearch documents list
3837
*/
39-
public void closeAndClear() throws IOException {
40-
this.client.close();
41-
this.metricList.clear();
42-
}
38+
public void closeConnection() throws IOException { this.client.close(); }
39+
40+
/**
41+
* This method closes the REST client
42+
*/
43+
public void clearList() { this.metricList.clear(); }
4344

4445
/**
4546
* This method adds a metric to the list (metricList).
@@ -62,9 +63,8 @@ public void createIndex() {
6263
/**
6364
* This method sends the ElasticSearch documents for each document present in the list (metricList).
6465
* All is being sent through the low-level ElasticSearch REST Client.
65-
* @throws IOException
6666
*/
67-
public void sendRequest() throws IOException {
67+
public void sendRequest() {
6868
String actionMetaData = String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\" } }%n", this.esIndex, "SampleResult");
6969

7070
StringBuilder bulkRequestBody = new StringBuilder();

src/main/java/io/github/delirius325/jmeter/backendlistener/elasticsearch/ElasticsearchBackendClient.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import org.slf4j.Logger;
1212
import org.slf4j.LoggerFactory;
1313

14-
import java.util.LinkedList;
15-
import java.util.List;
14+
import java.util.*;
1615

1716
public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
1817
private static final String BUILD_NUMBER = "BuildNumber";
@@ -30,6 +29,7 @@ public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
3029
private static final Logger logger = LoggerFactory.getLogger(ElasticsearchBackendClient.class);
3130

3231
private ElasticSearchMetricSender sender;
32+
private Set<String> modes;
3333
private List<String> filters;
3434
private RestClient client;
3535
private int buildNumber;
@@ -56,6 +56,7 @@ public Arguments getDefaultParameters() {
5656
public void setupTest(BackendListenerContext context) throws Exception {
5757
try {
5858
this.filters = new LinkedList<String>();
59+
this.modes = new HashSet<>(Arrays.asList("info","debug","error","quiet"));
5960
this.bulkSize = Integer.parseInt(context.getParameter(ES_BULK_SIZE));
6061
this.timeoutMs = JMeterUtils.getPropDefault(ES_TIMEOUT_MS, DEFAULT_TIMEOUT_MS);
6162
this.buildNumber = (JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER) != null && JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER).trim() != "") ? Integer.parseInt(JMeterUtils.getProperty(ElasticsearchBackendClient.BUILD_NUMBER)) : 0;
@@ -72,6 +73,8 @@ public void onFailure(HttpHost host) {
7273
.build();
7374
this.sender = new ElasticSearchMetricSender(this.client, context.getParameter(ES_INDEX).toLowerCase());
7475
this.sender.createIndex();
76+
77+
checkTestMode(context.getParameter(ES_TEST_MODE));
7578

7679
String[] filterArray = (context.getParameter(ES_SAMPLE_FILTER).contains(";")) ? context.getParameter(ES_SAMPLE_FILTER).split(";") : new String[] {context.getParameter(ES_SAMPLE_FILTER)};
7780
if(filterArray.length >= 1 && filterArray[0].trim() != "") {
@@ -131,7 +134,20 @@ public void teardownTest(BackendListenerContext context) throws Exception {
131134
if(this.sender.getListSize() > 0) {
132135
this.sender.sendRequest();
133136
}
134-
client.close();
137+
this.sender.closeConnection();
135138
super.teardownTest(context);
136139
}
140+
141+
/**
142+
* This method checks if the test mode is valid
143+
* @param mode The test mode as String
144+
*/
145+
private void checkTestMode(String mode) {
146+
if(!this.modes.contains(mode)) {
147+
logger.warn("The parameter \"es.test.mode\" isn't set properly. Three modes are allowed: debug ,info, and quiet.");
148+
logger.warn(" -- \"debug\": sends request and response details to ElasticSearch. Info only sends the details if the response has an error.");
149+
logger.warn(" -- \"info\": should be used in production");
150+
logger.warn(" -- \"quiet\": should be used if you don't care to have the details.");
151+
}
152+
}
137153
}

0 commit comments

Comments
 (0)