Skip to content

Commit

Permalink
Cache host name to avoid performance penalty of solving it for each s…
Browse files Browse the repository at this point in the history
…ample result
  • Loading branch information
rabelenda-abstracta authored and anthonygauthier committed Mar 6, 2024
1 parent 07886da commit 78f446b
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.delirius325.jmeter.backendlistener.elasticsearch;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
Expand All @@ -24,6 +25,8 @@

public class ElasticSearchMetric {
private static final Logger logger = LoggerFactory.getLogger(ElasticSearchMetric.class);
private static final String HOSTNAME = solveHostName();

private SampleResult sampleResult;
private String esTestMode;
private String esTimestamp;
Expand All @@ -46,6 +49,15 @@ public ElasticSearchMetric(
this.fields = fields;
}

private static String solveHostName() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
logger.warn("Could not resolve host name, falling back to localhost", e);
return "localhost";
}
}

/**
* This method returns the current metric as a Map(String, Object) for the provided sampleResult
*
Expand Down Expand Up @@ -77,7 +89,7 @@ public Map<String, Object> getMetric(BackendListenerContext context) throws Exce
addFilteredJSON("SampleStartTime", sdf.format(new Date(this.sampleResult.getStartTime())));
addFilteredJSON("SampleEndTime", sdf.format(new Date(this.sampleResult.getEndTime())));
addFilteredJSON("Timestamp", this.sampleResult.getTimeStamp());
addFilteredJSON("InjectorHostname", InetAddress.getLocalHost().getHostName());
addFilteredJSON("InjectorHostname", HOSTNAME);

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

0 comments on commit 78f446b

Please sign in to comment.