1010import org .apache .jmeter .visualizers .backend .BackendListenerContext ;
1111import org .elasticsearch .action .bulk .BulkRequestBuilder ;
1212import org .elasticsearch .client .Client ;
13- //import org.elasticsearch.client.transport.TransportClient;
1413import org .elasticsearch .common .transport .InetSocketTransportAddress ;
1514import org .elasticsearch .common .settings .Settings ;
1615import org .elasticsearch .common .xcontent .XContentType ;
@@ -33,9 +32,9 @@ public class ElasticsearchBackend extends AbstractBackendListenerClient {
3332 private static final String ES_INDEX = "es.index" ;
3433 private static final String ES_INDEX_TYPE = "es.indexType" ;
3534 private static final String ES_TIMESTAMP = "es.timestamp" ;
36- private static final String ES_STATUSCODE = "es.statuscode " ;
35+ private static final String ES_STATUS_CODE = "es.status.code " ;
3736 private static final String ES_CLUSTER = "es.cluster" ;
38- // private static final String ES_TRUSTALL_SSL = "es.trustAllSslCertificates ";
37+ private static final String ES_BULK_SIZE = "es.bulk.size " ;
3938
4039 private Client client ;
4140 private Settings settings ;
@@ -44,6 +43,8 @@ public class ElasticsearchBackend extends AbstractBackendListenerClient {
4443 private String host ;
4544 private Integer port ;
4645 private Integer buildNumber ;
46+ private Integer bulkSize ;
47+ private BulkRequestBuilder bulkRequest ;
4748
4849 @ Override
4950 public Arguments getDefaultParameters () {
@@ -54,9 +55,9 @@ public Arguments getDefaultParameters() {
5455 parameters .addArgument (ES_INDEX , null );
5556 parameters .addArgument (ES_INDEX_TYPE , "SampleResult" );
5657 parameters .addArgument (ES_TIMESTAMP , "yyyy-MM-dd'T'HH:mm:ss.SSSZZ" );
57- parameters .addArgument (ES_STATUSCODE , "531" );
58+ parameters .addArgument (ES_STATUS_CODE , "531" );
5859 parameters .addArgument (ES_CLUSTER , "elasticsearch" );
59- // parameters.addArgument(ES_TRUSTALL_SSL , "false ");
60+ parameters .addArgument (ES_BULK_SIZE , "100 " );
6061 return parameters ;
6162 }
6263
@@ -66,11 +67,12 @@ public void setupTest(BackendListenerContext context) throws Exception {
6667 this .index = context .getParameter (ES_INDEX );
6768 this .indexType = context .getParameter (ES_INDEX_TYPE );
6869 this .host = context .getParameter (ES_HOST );
70+ this .bulkSize = Integer .parseInt (context .getParameter (ES_BULK_SIZE ));
6971 this .port = Integer .parseInt (context .getParameter (ES_PORT ));
7072 this .buildNumber = (JMeterUtils .getProperty ("BuildNumber" ) != null && JMeterUtils .getProperty ("BuildNumber" ).trim () != "" ) ? Integer .parseInt (JMeterUtils .getProperty ("BuildNumber" )) : 0 ;
7173 this .settings = Settings .builder ().put ("cluster.name" , context .getParameter (ES_CLUSTER )).build ();
7274 this .client = new PreBuiltTransportClient (this .settings ).addTransportAddress (new InetSocketTransportAddress (InetAddress .getByName (this .host ), this .port ));
73-
75+ this . bulkRequest = this . client . prepareBulk ();
7476 super .setupTest (context );
7577 } catch (Exception e ) {
7678 e .printStackTrace ();
@@ -79,25 +81,27 @@ public void setupTest(BackendListenerContext context) throws Exception {
7981
8082 @ Override
8183 public void handleSampleResults (List <SampleResult > results , BackendListenerContext context ) {
82- BulkRequestBuilder bulkRequest = client .prepareBulk ();
83-
8484 for (SampleResult sr : results ) {
85- Map <String , Object > jsonObject = getElasticData (sr , context );
86- bulkRequest .add (this .client .prepareIndex (this .index , this .indexType ).setSource (jsonObject , XContentType .JSON ));
85+ this .bulkRequest .add (this .client .prepareIndex (this .index , this .indexType ).setSource (this .getElasticData (sr , context ), XContentType .JSON ));
8786 }
8887
89- if (bulkRequest .numberOfActions () >= 50 )
90- bulkRequest .get ();
88+ if (this .bulkRequest .numberOfActions () >= this .bulkSize ) {
89+ this .bulkRequest .get ();
90+ this .bulkRequest = this .client .prepareBulk ();
91+ }
9192 }
9293
9394 @ Override
9495 public void teardownTest (BackendListenerContext context ) throws Exception {
96+ if (this .bulkRequest .numberOfActions () > 0 )
97+ this .bulkRequest .get ();
98+
9599 this .client .close ();
96100 super .teardownTest (context );
97101 }
98102
99- public Map <String , Object > getElasticData (SampleResult sr , BackendListenerContext context ) {
100- Map <String , Object > jsonObject = new HashMap <String , Object >();
103+ public HashMap <String , Object > getElasticData (SampleResult sr , BackendListenerContext context ) {
104+ HashMap <String , Object > jsonObject = new HashMap <String , Object >();
101105 SimpleDateFormat sdf = new SimpleDateFormat (context .getParameter (ES_TIMESTAMP ));
102106
103107 //add all the default SampleResult parameters
@@ -121,15 +125,15 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
121125 jsonObject .put ("Timestamp" , sdf .format (new Date (sr .getTimeStamp ())));
122126 jsonObject .put ("BuildNumber" , this .buildNumber );
123127 jsonObject .put ("ElapsedTime" , getElapsedDate ());
124- jsonObject .put ("ResponseCode" , (sr .isResponseCodeOK () && StringUtils .isNumeric (sr .getResponseCode ())) ? sr .getResponseCode () : context .getParameter (ES_STATUSCODE ));
128+ jsonObject .put ("ResponseCode" , (sr .isResponseCodeOK () && StringUtils .isNumeric (sr .getResponseCode ())) ? sr .getResponseCode () : context .getParameter (ES_STATUS_CODE ));
125129
126130 //all assertions
127131 AssertionResult [] assertionResults = sr .getAssertionResults ();
128132 if (assertionResults != null ) {
129- Map <String , Object > [] assertionArray = new HashMap [assertionResults .length ];
133+ HashMap <String , Object > [] assertionArray = new HashMap [assertionResults .length ];
130134 Integer i = 0 ;
131135 for (AssertionResult assertionResult : assertionResults ) {
132- Map <String , Object > assertionMap = new HashMap <String , Object >();
136+ HashMap <String , Object > assertionMap = new HashMap <String , Object >();
133137 boolean failure = assertionResult .isFailure () || assertionResult .isError ();
134138 assertionMap .put ("failure" , failure );
135139 assertionMap .put ("failureMessage" , assertionResult .getFailureMessage ());
@@ -139,17 +143,6 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
139143 }
140144 }
141145
142- //For each custom property (starting with "esfield_")
143- // Delirius325, 2017/12/11: This functionnality is not working yet. Working on a fix to be out ASAP.
144- /*Properties props = JMeterUtils.getJMeterProperties();
145- for(String key : props.stringPropertyNames()) {
146- String propValue = props.getProperty(key);
147- if(propValue.startsWith("es_field_")) {
148- key = key.replace("es_field_","");
149- jsonObject.put(key, propValue);
150- }
151- }*/
152-
153146 return jsonObject ;
154147 }
155148
0 commit comments