10
10
import org .apache .jmeter .visualizers .backend .BackendListenerContext ;
11
11
import org .elasticsearch .action .bulk .BulkRequestBuilder ;
12
12
import org .elasticsearch .client .Client ;
13
- //import org.elasticsearch.client.transport.TransportClient;
14
13
import org .elasticsearch .common .transport .InetSocketTransportAddress ;
15
14
import org .elasticsearch .common .settings .Settings ;
16
15
import org .elasticsearch .common .xcontent .XContentType ;
@@ -33,9 +32,9 @@ public class ElasticsearchBackend extends AbstractBackendListenerClient {
33
32
private static final String ES_INDEX = "es.index" ;
34
33
private static final String ES_INDEX_TYPE = "es.indexType" ;
35
34
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 " ;
37
36
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 " ;
39
38
40
39
private Client client ;
41
40
private Settings settings ;
@@ -44,6 +43,8 @@ public class ElasticsearchBackend extends AbstractBackendListenerClient {
44
43
private String host ;
45
44
private Integer port ;
46
45
private Integer buildNumber ;
46
+ private Integer bulkSize ;
47
+ private BulkRequestBuilder bulkRequest ;
47
48
48
49
@ Override
49
50
public Arguments getDefaultParameters () {
@@ -54,9 +55,9 @@ public Arguments getDefaultParameters() {
54
55
parameters .addArgument (ES_INDEX , null );
55
56
parameters .addArgument (ES_INDEX_TYPE , "SampleResult" );
56
57
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" );
58
59
parameters .addArgument (ES_CLUSTER , "elasticsearch" );
59
- // parameters.addArgument(ES_TRUSTALL_SSL , "false ");
60
+ parameters .addArgument (ES_BULK_SIZE , "100 " );
60
61
return parameters ;
61
62
}
62
63
@@ -66,11 +67,12 @@ public void setupTest(BackendListenerContext context) throws Exception {
66
67
this .index = context .getParameter (ES_INDEX );
67
68
this .indexType = context .getParameter (ES_INDEX_TYPE );
68
69
this .host = context .getParameter (ES_HOST );
70
+ this .bulkSize = Integer .parseInt (context .getParameter (ES_BULK_SIZE ));
69
71
this .port = Integer .parseInt (context .getParameter (ES_PORT ));
70
72
this .buildNumber = (JMeterUtils .getProperty ("BuildNumber" ) != null && JMeterUtils .getProperty ("BuildNumber" ).trim () != "" ) ? Integer .parseInt (JMeterUtils .getProperty ("BuildNumber" )) : 0 ;
71
73
this .settings = Settings .builder ().put ("cluster.name" , context .getParameter (ES_CLUSTER )).build ();
72
74
this .client = new PreBuiltTransportClient (this .settings ).addTransportAddress (new InetSocketTransportAddress (InetAddress .getByName (this .host ), this .port ));
73
-
75
+ this . bulkRequest = this . client . prepareBulk ();
74
76
super .setupTest (context );
75
77
} catch (Exception e ) {
76
78
e .printStackTrace ();
@@ -79,25 +81,27 @@ public void setupTest(BackendListenerContext context) throws Exception {
79
81
80
82
@ Override
81
83
public void handleSampleResults (List <SampleResult > results , BackendListenerContext context ) {
82
- BulkRequestBuilder bulkRequest = client .prepareBulk ();
83
-
84
84
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 ));
87
86
}
88
87
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
+ }
91
92
}
92
93
93
94
@ Override
94
95
public void teardownTest (BackendListenerContext context ) throws Exception {
96
+ if (this .bulkRequest .numberOfActions () > 0 )
97
+ this .bulkRequest .get ();
98
+
95
99
this .client .close ();
96
100
super .teardownTest (context );
97
101
}
98
102
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 >();
101
105
SimpleDateFormat sdf = new SimpleDateFormat (context .getParameter (ES_TIMESTAMP ));
102
106
103
107
//add all the default SampleResult parameters
@@ -121,15 +125,15 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
121
125
jsonObject .put ("Timestamp" , sdf .format (new Date (sr .getTimeStamp ())));
122
126
jsonObject .put ("BuildNumber" , this .buildNumber );
123
127
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 ));
125
129
126
130
//all assertions
127
131
AssertionResult [] assertionResults = sr .getAssertionResults ();
128
132
if (assertionResults != null ) {
129
- Map <String , Object > [] assertionArray = new HashMap [assertionResults .length ];
133
+ HashMap <String , Object > [] assertionArray = new HashMap [assertionResults .length ];
130
134
Integer i = 0 ;
131
135
for (AssertionResult assertionResult : assertionResults ) {
132
- Map <String , Object > assertionMap = new HashMap <String , Object >();
136
+ HashMap <String , Object > assertionMap = new HashMap <String , Object >();
133
137
boolean failure = assertionResult .isFailure () || assertionResult .isError ();
134
138
assertionMap .put ("failure" , failure );
135
139
assertionMap .put ("failureMessage" , assertionResult .getFailureMessage ());
@@ -139,17 +143,6 @@ public Map<String, Object> getElasticData(SampleResult sr, BackendListenerContex
139
143
}
140
144
}
141
145
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
-
153
146
return jsonObject ;
154
147
}
155
148
0 commit comments