|
| 1 | +package net.delirius.jmeter.backendlistener.elasticsearch; |
| 2 | + |
| 3 | +import org.apache.commons.lang.StringUtils; |
| 4 | +import org.apache.jmeter.assertions.AssertionResult; |
| 5 | +import org.apache.jmeter.config.Arguments; |
| 6 | +import org.apache.jmeter.samplers.SampleResult; |
| 7 | +import org.apache.jmeter.threads.JMeterContextService; |
| 8 | +import org.apache.jmeter.util.JMeterUtils; |
| 9 | +import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; |
| 10 | +import org.apache.jmeter.visualizers.backend.BackendListenerContext; |
| 11 | +import org.elasticsearch.client.transport.TransportClient; |
| 12 | +import org.elasticsearch.common.transport.InetSocketTransportAddress; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.xcontent.XContentType; |
| 15 | +import org.elasticsearch.transport.client.PreBuiltTransportClient; |
| 16 | +import java.net.InetAddress; |
| 17 | +import java.text.SimpleDateFormat; |
| 18 | +import java.util.*; |
| 19 | + |
| 20 | +/** |
| 21 | + * |
| 22 | + * @author: Delirius325 |
| 23 | + * @inspired_by: korteke & zumo64 |
| 24 | + * @source_1: https://github.com/korteke/JMeter_ElasticsearchBackendListener |
| 25 | + * @source_2: https://github.com/zumo64/ELK_POC |
| 26 | + */ |
| 27 | +public class ElasticsearchBackend extends AbstractBackendListenerClient { |
| 28 | + private static final String ES_PROTOCOL = "es.protocol"; |
| 29 | + private static final String ES_HOST = "es.host"; |
| 30 | + private static final String ES_PORT = "es.port"; |
| 31 | + private static final String ES_INDEX = "es.index"; |
| 32 | + private static final String ES_INDEX_TYPE = "es.indexType"; |
| 33 | + private static final String ES_TIMESTAMP = "es.timestamp"; |
| 34 | + private static final String ES_STATUSCODE = "es.statuscode"; |
| 35 | + //private static final String ES_TRUSTALL_SSL = "es.trustAllSslCertificates"; |
| 36 | + |
| 37 | + private TransportClient client; |
| 38 | + private String index; |
| 39 | + private String indexType; |
| 40 | + private String host; |
| 41 | + private Integer port; |
| 42 | + private Integer buildNumber; |
| 43 | + |
| 44 | + @Override |
| 45 | + public Arguments getDefaultParameters() { |
| 46 | + Arguments parameters = new Arguments(); |
| 47 | + parameters.addArgument(ES_PROTOCOL, "https"); |
| 48 | + parameters.addArgument(ES_HOST, null); |
| 49 | + parameters.addArgument(ES_PORT, "9200"); |
| 50 | + parameters.addArgument(ES_INDEX, null); |
| 51 | + parameters.addArgument(ES_INDEX_TYPE, "SampleResult"); |
| 52 | + parameters.addArgument(ES_TIMESTAMP, "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"); |
| 53 | + parameters.addArgument(ES_STATUSCODE, "531"); |
| 54 | + //parameters.addArgument(ES_TRUSTALL_SSL, "false"); |
| 55 | + return parameters; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void setupTest(BackendListenerContext context) throws Exception { |
| 60 | + try { |
| 61 | + this.index = context.getParameter(ES_INDEX); |
| 62 | + this.indexType = context.getParameter(ES_INDEX_TYPE); |
| 63 | + this.host = context.getParameter(ES_HOST); |
| 64 | + this.port = Integer.parseInt(context.getParameter(ES_PORT)); |
| 65 | + this.buildNumber = (JMeterUtils.getProperty("BuildNumber") != null && JMeterUtils.getProperty("BuildNumber").trim() != "") ? Integer.parseInt(JMeterUtils.getProperty("BuildNumber")) : 0; |
| 66 | + |
| 67 | + client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(this.host), this.port)); |
| 68 | + |
| 69 | + super.setupTest(context); |
| 70 | + } catch (Exception e) { |
| 71 | + e.printStackTrace(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void handleSampleResults(List<SampleResult> results, BackendListenerContext context) { |
| 77 | + for(SampleResult sr : results) { |
| 78 | + Map<String, Object> jsonObject = getElasticData(sr, context); |
| 79 | + client.prepareIndex(this.index, this.indexType).setSource(jsonObject, XContentType.JSON).get(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void teardownTest(BackendListenerContext context) throws Exception { |
| 85 | + this.client.close(); |
| 86 | + super.teardownTest(context); |
| 87 | + } |
| 88 | + |
| 89 | + public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContext context) { |
| 90 | + Map<String, Object> jsonObject = new HashMap<String, Object>(); |
| 91 | + SimpleDateFormat sdf = new SimpleDateFormat(context.getParameter(ES_TIMESTAMP)); |
| 92 | + |
| 93 | + //add all the default SampleResult parameters |
| 94 | + jsonObject.put("AllThreads", sr.getAllThreads()); |
| 95 | + jsonObject.put("BodySize", sr.getBodySize()); |
| 96 | + jsonObject.put("Bytes", sr.getBytes()); |
| 97 | + jsonObject.put("ConnectTime", sr.getConnectTime()); |
| 98 | + jsonObject.put("ContentType", sr.getContentType()); |
| 99 | + jsonObject.put("DataType", sr.getDataType()); |
| 100 | + jsonObject.put("EndTime", Long.toString(sr.getEndTime())); |
| 101 | + jsonObject.put("ErrorCount", sr.getErrorCount()); |
| 102 | + jsonObject.put("GrpThreads", sr.getGroupThreads()); |
| 103 | + jsonObject.put("IdleTime", sr.getIdleTime()); |
| 104 | + jsonObject.put("Latency", sr.getLatency()); |
| 105 | + jsonObject.put("ResponseTime", (sr.getEndTime() - sr.getStartTime())); |
| 106 | + jsonObject.put("SampleCount", sr.getSampleCount()); |
| 107 | + jsonObject.put("SampleLabel", sr.getSampleLabel()); |
| 108 | + jsonObject.put("StartTime", sdf.format(new Date(sr.getStartTime()))); |
| 109 | + jsonObject.put("ThreadName", sr.getThreadName()); |
| 110 | + jsonObject.put("URL", sr.getURL()); |
| 111 | + jsonObject.put("Timestamp", sdf.format(new Date(sr.getTimeStamp()))); |
| 112 | + jsonObject.put("BuildNumber", this.buildNumber); |
| 113 | + jsonObject.put("ElapsedTime", getElapsedDate()); |
| 114 | + jsonObject.put("ResponseCode", (sr.isResponseCodeOK() && StringUtils.isNumeric(sr.getResponseCode())) ? sr.getResponseCode() : context.getParameter(ES_STATUSCODE)); |
| 115 | + |
| 116 | + //all assertions |
| 117 | + AssertionResult[] assertionResults = sr.getAssertionResults(); |
| 118 | + if(assertionResults != null) { |
| 119 | + Map<String, Object> [] assertionArray = new HashMap[assertionResults.length]; |
| 120 | + Integer i = 0; |
| 121 | + for(AssertionResult assertionResult : assertionResults) { |
| 122 | + Map<String, Object> assertionMap = new HashMap<String, Object>(); |
| 123 | + boolean failure = assertionResult.isFailure() || assertionResult.isError(); |
| 124 | + assertionMap.put("failure", failure); |
| 125 | + assertionMap.put("failureMessage", assertionResult.getFailureMessage()); |
| 126 | + assertionMap.put("name", assertionResult.getName()); |
| 127 | + assertionArray[i] = assertionMap; |
| 128 | + i++; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + //for each custom property -- the elasticsearch index will automatically add new fields (unless specified otherwise) |
| 133 | + Properties props = JMeterUtils.getJMeterProperties(); |
| 134 | + for(String key : props.stringPropertyNames()) { |
| 135 | + jsonObject.put(key, props.getProperty(key)); |
| 136 | + } |
| 137 | + |
| 138 | + return jsonObject; |
| 139 | + } |
| 140 | + |
| 141 | + public Date getElapsedDate() { |
| 142 | + //Calculate the elapsed time (Starting from midnight on a random day - enables us to compare of two loads over their duration) |
| 143 | + try { |
| 144 | + SimpleDateFormat formatter = new SimpleDateFormat("YYYY-mm-dd HH:mm:ss"); |
| 145 | + long start = JMeterContextService.getTestStartTime(); |
| 146 | + long end = System.currentTimeMillis(); |
| 147 | + long elapsed = (end - start); |
| 148 | + long minutes = (elapsed / 1000) / 60; |
| 149 | + long seconds = (elapsed / 1000) % 60; |
| 150 | + |
| 151 | + Calendar cal = Calendar.getInstance(); |
| 152 | + cal.set(Calendar.HOUR_OF_DAY, 0); //If there is more than an hour of data, the number of minutes/seconds will increment this |
| 153 | + cal.set(Calendar.MINUTE, (int) minutes); |
| 154 | + cal.set(Calendar.SECOND, (int) seconds); |
| 155 | + String sElapsed = String.format("2017-01-01 %02d:%02d:%02d", cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND)); |
| 156 | + Date elapsedDate = formatter.parse(sElapsed); |
| 157 | + return elapsedDate; |
| 158 | + } catch (Exception e) { |
| 159 | + e.printStackTrace(); |
| 160 | + return null; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments