Skip to content

Commit 6e8ba65

Browse files
Cache host name to avoid performance penalty of solving it for each sample result
1 parent d837c09 commit 6e8ba65

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.delirius325.jmeter.backendlistener.elasticsearch;
22

33
import java.net.InetAddress;
4+
import java.net.UnknownHostException;
45
import java.text.ParseException;
56
import java.text.SimpleDateFormat;
67
import java.time.LocalDateTime;
@@ -24,6 +25,8 @@
2425

2526
public class ElasticSearchMetric {
2627
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchMetric.class);
28+
private static final String HOSTNAME = solveHostName();
29+
2730
private SampleResult sampleResult;
2831
private String esTestMode;
2932
private String esTimestamp;
@@ -46,6 +49,15 @@ public ElasticSearchMetric(
4649
this.fields = fields;
4750
}
4851

52+
private static String solveHostName() {
53+
try {
54+
return InetAddress.getLocalHost().getHostName();
55+
} catch (UnknownHostException e) {
56+
logger.warn("Could not resolve host name, falling back to localhost", e);
57+
return "localhost";
58+
}
59+
}
60+
4961
/**
5062
* This method returns the current metric as a Map(String, Object) for the provided sampleResult
5163
*
@@ -77,7 +89,7 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
7789
addFilteredJSON("SampleStartTime", sdf.format(new Date(this.sampleResult.getStartTime())));
7890
addFilteredJSON("SampleEndTime", sdf.format(new Date(this.sampleResult.getEndTime())));
7991
addFilteredJSON("Timestamp", this.sampleResult.getTimeStamp());
80-
addFilteredJSON("InjectorHostname", InetAddress.getLocalHost().getHostName());
92+
addFilteredJSON("InjectorHostname", HOSTNAME);
8193

8294
// Add the details according to the mode that is set
8395
switch (this.esTestMode) {

0 commit comments

Comments
 (0)