1111import org .slf4j .Logger ;
1212import org .slf4j .LoggerFactory ;
1313
14- import java .util .LinkedList ;
15- import java .util .List ;
14+ import java .util .*;
1615
1716public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
1817 private static final String BUILD_NUMBER = "BuildNumber" ;
@@ -30,6 +29,7 @@ public class ElasticsearchBackendClient extends AbstractBackendListenerClient {
3029 private static final Logger logger = LoggerFactory .getLogger (ElasticsearchBackendClient .class );
3130
3231 private ElasticSearchMetricSender sender ;
32+ private Set <String > modes ;
3333 private List <String > filters ;
3434 private RestClient client ;
3535 private int buildNumber ;
@@ -56,6 +56,7 @@ public Arguments getDefaultParameters() {
5656 public void setupTest (BackendListenerContext context ) throws Exception {
5757 try {
5858 this .filters = new LinkedList <String >();
59+ this .modes = new HashSet <>(Arrays .asList ("info" ,"debug" ,"error" ,"quiet" ));
5960 this .bulkSize = Integer .parseInt (context .getParameter (ES_BULK_SIZE ));
6061 this .timeoutMs = JMeterUtils .getPropDefault (ES_TIMEOUT_MS , DEFAULT_TIMEOUT_MS );
6162 this .buildNumber = (JMeterUtils .getProperty (ElasticsearchBackendClient .BUILD_NUMBER ) != null && JMeterUtils .getProperty (ElasticsearchBackendClient .BUILD_NUMBER ).trim () != "" ) ? Integer .parseInt (JMeterUtils .getProperty (ElasticsearchBackendClient .BUILD_NUMBER )) : 0 ;
@@ -72,6 +73,8 @@ public void onFailure(HttpHost host) {
7273 .build ();
7374 this .sender = new ElasticSearchMetricSender (this .client , context .getParameter (ES_INDEX ).toLowerCase ());
7475 this .sender .createIndex ();
76+
77+ checkTestMode (context .getParameter (ES_TEST_MODE ));
7578
7679 String [] filterArray = (context .getParameter (ES_SAMPLE_FILTER ).contains (";" )) ? context .getParameter (ES_SAMPLE_FILTER ).split (";" ) : new String [] {context .getParameter (ES_SAMPLE_FILTER )};
7780 if (filterArray .length >= 1 && filterArray [0 ].trim () != "" ) {
@@ -131,7 +134,20 @@ public void teardownTest(BackendListenerContext context) throws Exception {
131134 if (this .sender .getListSize () > 0 ) {
132135 this .sender .sendRequest ();
133136 }
134- client . close ();
137+ this . sender . closeConnection ();
135138 super .teardownTest (context );
136139 }
140+
141+ /**
142+ * This method checks if the test mode is valid
143+ * @param mode The test mode as String
144+ */
145+ private void checkTestMode (String mode ) {
146+ if (!this .modes .contains (mode )) {
147+ logger .warn ("The parameter \" es.test.mode\" isn't set properly. Three modes are allowed: debug ,info, and quiet." );
148+ logger .warn (" -- \" debug\" : sends request and response details to ElasticSearch. Info only sends the details if the response has an error." );
149+ logger .warn (" -- \" info\" : should be used in production" );
150+ logger .warn (" -- \" quiet\" : should be used if you don't care to have the details." );
151+ }
152+ }
137153}
0 commit comments