3535 * @source_2: https://github.com/zumo64/ELK_POC
3636 */
3737public class ElasticsearchBackend extends AbstractBackendListenerClient {
38- private static final String BUILD_NUMBER = "BuildNumber" ;
39- private static final String ES_SCHEME = "es.scheme" ;
40- private static final String ES_HOST = "es.host" ;
41- private static final String ES_PORT = "es.transport.port" ;
42- private static final String ES_INDEX = "es.index" ;
43- private static final String ES_TIMESTAMP = "es.timestamp" ;
44- private static final String ES_STATUS_CODE = "es.status.code" ;
45- private static final String ES_BULK_SIZE = "es.bulk.size" ;
46- private static final String ES_TIMEOUT_MS = "es.timout.ms" ;
38+ private static final String BUILD_NUMBER = "BuildNumber" ;
39+ private static final String ES_SCHEME = "es.scheme" ;
40+ private static final String ES_HOST = "es.host" ;
41+ private static final String ES_PORT = "es.transport.port" ;
42+ private static final String ES_INDEX = "es.index" ;
43+ private static final String ES_TIMESTAMP = "es.timestamp" ;
44+ private static final String ES_STATUS_CODE = "es.status.code" ;
45+ private static final String ES_BULK_SIZE = "es.bulk.size" ;
46+ private static final String ES_TIMEOUT_MS = "es.timout.ms" ;
47+ private static final String ES_SAMPLE_FILTER = "es.sample.filter" ;
4748 private static final long DEFAULT_TIMEOUT_MS = 200L ;
4849 private static final Logger logger = LoggerFactory .getLogger (ElasticsearchBackend .class );
4950
5051 private List <String > bulkRequestList ;
52+ private List <String > filters ;
5153 private RestClient client ;
5254 private String index ;
5355 private int buildNumber ;
@@ -65,23 +67,21 @@ public Arguments getDefaultParameters() {
6567 parameters .addArgument (ES_STATUS_CODE , "531" );
6668 parameters .addArgument (ES_BULK_SIZE , "100" );
6769 parameters .addArgument (ES_TIMEOUT_MS , Long .toString (DEFAULT_TIMEOUT_MS ));
70+ parameters .addArgument (ES_SAMPLE_FILTER , null );
6871 return parameters ;
6972 }
7073
7174 @ Override
7275 public void setupTest (BackendListenerContext context ) throws Exception {
7376 try {
74- //
7577 String host = context .getParameter (ES_HOST );
78+ this .filters = new LinkedList <String >();
7679 int port = Integer .parseInt (context .getParameter (ES_PORT ));
7780 this .index = context .getParameter (ES_INDEX );
7881 this .bulkSize = Integer .parseInt (context .getParameter (ES_BULK_SIZE ));
7982 this .timeoutMs = JMeterUtils .getPropDefault (ES_TIMEOUT_MS , DEFAULT_TIMEOUT_MS );
8083 this .buildNumber = (JMeterUtils .getProperty (ElasticsearchBackend .BUILD_NUMBER ) != null && JMeterUtils .getProperty (ElasticsearchBackend .BUILD_NUMBER ).trim () != "" )
8184 ? Integer .parseInt (JMeterUtils .getProperty (ElasticsearchBackend .BUILD_NUMBER )) : 0 ;
82-
83- // Build RestHighLevelClient & BulkRequest
84-
8585 this .client = RestClient .builder (new HttpHost (context .getParameter (ES_HOST ), port , context .getParameter (ES_SCHEME )))
8686 .setRequestConfigCallback (requestConfigBuilder -> requestConfigBuilder .setConnectTimeout (5000 )
8787 .setSocketTimeout ((int ) timeoutMs ))
@@ -94,7 +94,12 @@ public void onFailure(HttpHost host) {
9494 .setMaxRetryTimeoutMillis (60000 )
9595 .build ();
9696 this .bulkRequestList = new LinkedList <String >();
97-
97+ String [] filterArray = (context .getParameter (ES_SAMPLE_FILTER ).contains (";" )) ? context .getParameter (ES_SAMPLE_FILTER ).split (";" ) : new String [] {context .getParameter (ES_SAMPLE_FILTER )};
98+ if (filterArray .length >= 1 && filterArray [0 ].trim () != "" ) {
99+ for (String filter : filterArray ) {
100+ this .filters .add (filter );
101+ }
102+ }
98103 super .setupTest (context );
99104 } catch (Exception e ) {
100105 throw new IllegalStateException ("Unable to setup connectivity to ES" , e );
@@ -104,9 +109,24 @@ public void onFailure(HttpHost host) {
104109 @ Override
105110 public void handleSampleResults (List <SampleResult > results , BackendListenerContext context ) {
106111 for (SampleResult sr : results ) {
107- Gson gson = new Gson ();
108- String json = gson .toJson (this .getElasticData (sr , context ));
109- this .bulkRequestList .add (json );
112+ boolean validSample = false ;
113+
114+ if (this .filters == null ) {
115+ validSample = true ;
116+ } else {
117+ for (String filter : this .filters ) {
118+ if (filter .toLowerCase ().trim ().equals (sr .getSampleLabel ().toLowerCase ().trim ())) {
119+ validSample = true ;
120+ break ;
121+ }
122+ }
123+ }
124+
125+ if (validSample ) {
126+ Gson gson = new Gson ();
127+ String json = gson .toJson (this .getElasticData (sr , context ));
128+ this .bulkRequestList .add (json );
129+ }
110130 }
111131
112132 if (this .bulkRequestList .size () >= this .bulkSize ) {
0 commit comments