2828import java .util .Map ;
2929import java .util .Properties ;
3030import java .util .concurrent .TimeUnit ;
31+ import java .util .logging .Level ;
32+ import java .util .logging .Logger ;
3133
3234import org .apache .commons .cli .CommandLine ;
3335import org .apache .commons .cli .CommandLineParser ;
3941import org .apache .commons .cli .ParseException ;
4042import org .apache .commons .csv .CSVFormat ;
4143import org .apache .commons .csv .CSVRecord ;
42- import org .apache .log4j .BasicConfigurator ;
43- import org .apache .log4j .Level ;
44- import org .apache .log4j .LogManager ;
45- import org .apache .log4j .Logger ;
44+
4645import org .influxdb .InfluxDB ;
4746import org .influxdb .InfluxDB .ConsistencyLevel ;
4847import org .influxdb .InfluxDBFactory ;
5251
5352/**
5453*
55- * <h1>JMeter results file to Influxdb</h1>
54+ * <h1>JMeter results file to Influxdb 1.x </h1>
5655* <p>
5756* Read a result JMeter file in csv format and
5857* import all results in Influxdb Database
@@ -103,14 +102,13 @@ public class ImportJMeterLogIntoInfluxdb {
103102
104103 public static final int K_WRITE_MODULO = 1000 ; // write informations to InfluxDB every K_WRITE_MODULO lines
105104
106- private static final Logger LOGGER = LogManager .getLogger (ImportJMeterLogIntoInfluxdb .class );
105+ private static final Logger LOGGER = Logger .getLogger ( ImportJMeterLogIntoInfluxdb .class . getPackage (). getName () );
107106
108107 public static void main (String [] args ) {
109-
110108 System .out .println ("Begin main" );
111109
112- BasicConfigurator . configure ();
113- Logger . getRootLogger (). setLevel (Level .WARN );
110+ long startMain = System . currentTimeMillis ();
111+ LOGGER . setLevel (Level .WARNING );
114112
115113 Options options = createOptions ();
116114 Properties parseProperties = null ;
@@ -138,8 +136,23 @@ public static void main(String[] args) {
138136 boolean bMultiplyValueBy = false ;
139137 double dMutiplyCoef = 0.001 ;
140138
139+
141140 String sTmp = "" ;
141+ sTmp = (String ) parseProperties .get (K_LEVEL_TRACE_OPT );
142+ if (sTmp != null ) {
143+ String traceLevel = sTmp ;
144+ if (traceLevel .equalsIgnoreCase ("WARN" )) {
145+ LOGGER .setLevel (Level .WARNING );
146+ }
147+
148+ if (traceLevel .equalsIgnoreCase ("INFO" )) {
149+ LOGGER .setLevel (Level .INFO );
150+ }
142151
152+ if (traceLevel .equalsIgnoreCase ("DEBUG" )) {
153+ LOGGER .setLevel (Level .FINE );
154+ }
155+ }
143156 sTmp = (String ) parseProperties .get (K_JMETER_FILE_IN_OPT );
144157 if (sTmp != null ) {
145158 jmeterFileIn = sTmp ;
@@ -200,37 +213,34 @@ public static void main(String[] args) {
200213 bMultiplyValueBy = true ;
201214 }
202215
203-
204-
205- sTmp = (String ) parseProperties .get (K_LEVEL_TRACE_OPT );
206- if (sTmp != null ) {
207- String traceLevel = sTmp ;
208- if (traceLevel .equalsIgnoreCase ("WARN" )) {
209- Logger .getRootLogger ().setLevel (Level .WARN );
210- }
211-
212- if (traceLevel .equalsIgnoreCase ("INFO" )) {
213- Logger .getRootLogger ().setLevel (Level .INFO );
214- }
215-
216- if (traceLevel .equalsIgnoreCase ("DEBUG" )) {
217- Logger .getRootLogger ().setLevel (Level .DEBUG );
218- }
219- }
220-
216+ int nbLines = 0 ;
221217 try {
222218 ImportJMeterLogIntoInfluxdb importJMeterLogIntoInfluxdb = new ImportJMeterLogIntoInfluxdb ();
223- importJMeterLogIntoInfluxdb .readJMeterFileAndWriteInInfluxDB (jmeterFileIn , jmeter_save_saveservice_timestamp_format , delimiter ,
219+ nbLines = importJMeterLogIntoInfluxdb .readJMeterFileAndWriteInInfluxDB (jmeterFileIn , jmeter_save_saveservice_timestamp_format , delimiter ,
224220 influxdbUrl , influxdbUser , influxdbPassword , dbName , testLabel , application , bParseResponseMessage , bMultiplyValueBy , dMutiplyCoef );
225221 } catch (Exception ex ) {
226- LOGGER .error (ex );
222+ LOGGER .severe (ex .toString ());
223+ System .out .println ("End main Error (exit 1)" );
224+ System .exit (1 );
225+ }
226+ if (nbLines == 0 ) {
227+ System .out .println ("End main Error (exit 1)" );
227228 System .exit (1 );
228229 }
229- System .out .println ("End main (exit 0)" );
230+
231+ // compute duration
232+ long endMain = System .currentTimeMillis ();
233+ long durationMs = endMain - startMain ;
234+ double averageLineMs = 0 ;
235+ if (nbLines > 0 ) {
236+ averageLineMs = ((double ) durationMs / nbLines );
237+ }
238+ LOGGER .warning ("Duration = " + durationMs + " ms (" + Utils .timeFormatDuration (durationMs ) + ") for number of lines = " + nbLines + ", average ms by line = " + String .format ( "%.3f" ,averageLineMs ));
239+ System .out .println ("End main OK (exit 0)" );
230240 System .exit (0 );
231241 }
232242
233- public void readJMeterFileAndWriteInInfluxDB (String jmeterFileIn , String timeFormat , char delimiter ,
243+ public int readJMeterFileAndWriteInInfluxDB (String jmeterFileIn , String timeFormat , char delimiter ,
234244 String influxdbUrl , String influxdbUser , String influxdbPassword , String dbName , String testLabel ,
235245 String application , boolean bParseResponseMessage , boolean bMultiplyValueBy , double dMutiplyCoef ) throws Exception {
236246 InfluxDB influxDB = InfluxDBFactory .connect (influxdbUrl , influxdbUser , influxdbPassword );
@@ -253,14 +263,12 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
253263 DecimalFormat decimalFormat = new DecimalFormat (pattern , symbols );
254264 decimalFormat .setParseBigDecimal (true );
255265
256-
266+ int lineNumber = 1 ;
257267 try {
258268 in = new FileReader (jmeterFileIn );
259269 Iterable <CSVRecord > records = CSVFormat .RFC4180 .withFirstRecordAsHeader ().withDelimiter (delimiter )
260270 .parse (in );
261271
262- int lineNumber = 1 ;
263-
264272 for (CSVRecord record : records ) {
265273
266274 JMeterSimpleSampler jsSampler = new JMeterSimpleSampler ();
@@ -339,7 +347,7 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
339347 }
340348 catch (Exception ex ) {
341349 jsSampler .setResponseCode (responseCodeTmp );
342- LOGGER .error ("Put Response Code Error 500 because i can't parse Integer for ResponseCode :<" + sTmp + ">" );
350+ LOGGER .severe ("Put Response Code Error 500 because i can't parse Integer for ResponseCode :<" + sTmp + ">" );
343351 }
344352 }
345353 }
@@ -367,7 +375,7 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
367375 catch (Exception ex ) {
368376 withResponseMessageLong = false ;
369377 jsSampler .setResponseMessageLong (responseMessageLong );
370- LOGGER .error ("Put Response Message Long format, i can't parse Long for ResponseMessage :<" + sTmp + ">" );
378+ LOGGER .severe ("Put Response Message Long format, i can't parse Long for ResponseMessage :<" + sTmp + ">" );
371379 }
372380 }
373381 }
@@ -396,7 +404,7 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
396404 }
397405 catch (Exception ex ) {
398406 jsSampler .setSuccess (successTmp );
399- LOGGER .error ("Put success to false because i can't parse Boolean for success :<" + sTmp + ">" );
407+ LOGGER .severe ("Put success to false because i can't parse Boolean for success :<" + sTmp + ">" );
400408 }
401409 }
402410
@@ -532,7 +540,7 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
532540 batchPoints .point (point );
533541
534542 if ((lineNumber % K_WRITE_MODULO ) == 0 ) {
535- LOGGER .warn ("write batchPoints, lineNumber = " + lineNumber );
543+ LOGGER .warning ("write batchPoints, lineNumber = " + lineNumber );
536544
537545 influxDB .write (batchPoints );
538546
@@ -552,11 +560,11 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
552560 batchPoints .point (pointEvt );
553561 }
554562
555- LOGGER .warn ("End write batchPoints, lineNumber = " + lineNumber );
563+ LOGGER .warning ("End write batchPoints, lineNumber = " + lineNumber );
556564 influxDB .write (batchPoints );
557565
558566 } catch (Exception ex ) {
559- LOGGER .error (ex );
567+ LOGGER .severe (ex . toString () );
560568 throw ex ;
561569 } finally {
562570 if (in != null ) {
@@ -575,6 +583,7 @@ public void readJMeterFileAndWriteInInfluxDB(String jmeterFileIn, String timeFor
575583 }
576584 }
577585 }
586+ return lineNumber ;
578587 }
579588
580589 private static Options createOptions () {
0 commit comments