/
JMeter.java
1458 lines (1348 loc) · 71.1 KB
/
JMeter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.Authenticator;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.apache.commons.cli.avalon.CLArgsParser;
import org.apache.commons.cli.avalon.CLOption;
import org.apache.commons.cli.avalon.CLOptionDescriptor;
import org.apache.commons.cli.avalon.CLUtil;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.control.ReplaceableController;
import org.apache.jmeter.engine.ClientJMeterEngine;
import org.apache.jmeter.engine.DistributedRunner;
import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.engine.RemoteJMeterEngineImpl;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.engine.TreeCloner;
import org.apache.jmeter.exceptions.IllegalUserActionException;
import org.apache.jmeter.gui.action.LoadRecentProject;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.gui.tree.JMeterTreeNode;
import org.apache.jmeter.plugin.JMeterPlugin;
import org.apache.jmeter.plugin.PluginManager;
import org.apache.jmeter.report.config.ConfigurationException;
import org.apache.jmeter.report.dashboard.ReportGenerator;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.rmi.RmiUtils;
import org.apache.jmeter.samplers.Remoteable;
import org.apache.jmeter.samplers.SampleEvent;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.services.FileServer;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestElementSchema;
import org.apache.jmeter.testelement.TestStateListener;
import org.apache.jmeter.threads.RemoteThreadsListenerTestElement;
import org.apache.jmeter.util.BeanShellInterpreter;
import org.apache.jmeter.util.BeanShellServer;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.util.SecurityProviderLoader;
import org.apache.jmeter.util.ShutdownClient;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.SearchByClass;
import org.apache.jorphan.reflect.ClassTools;
import org.apache.jorphan.util.HeapDumper;
import org.apache.jorphan.util.JMeterException;
import org.apache.jorphan.util.JOrphanUtils;
import org.apache.jorphan.util.ThreadDumper;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Main JMeter class; processes options and starts the GUI, non-GUI or server as appropriate.
*/
public class JMeter implements JMeterPlugin {
private static final String JSR223_INIT_FILE = "jsr223.init.file";
private static final Logger log = LoggerFactory.getLogger(JMeter.class);
public static final int UDP_PORT_DEFAULT = ShutdownClient.UDP_PORT_DEFAULT;
public static final String HTTP_PROXY_PASS = "http.proxyPass"; // $NON-NLS-1$
public static final String HTTP_PROXY_USER = "http.proxyUser"; // $NON-NLS-1$
public static final String JMETER_NON_GUI = "JMeter.NonGui"; // $NON-NLS-1$
public static final String JMETER_REPORT_OUTPUT_DIR_PROPERTY =
"jmeter.reportgenerator.outputdir"; //$NON-NLS-1$
// Icons size in the JMeter tree
public static final String TREE_ICON_SIZE = "jmeter.tree.icons.size"; //$NON-NLS-1$
public static final String DEFAULT_TREE_ICON_SIZE = "19x19"; //$NON-NLS-1$
protected static final String KEY_SIZE = "<SIZE>"; //$NON-NLS-1$
// If the -t flag is to "LAST", then the last loaded file (if any) is used
private static final String USE_LAST_JMX = "LAST";
// If the -j or -l flag is set to LAST or LAST.log|LAST.jtl, then the last loaded file name is used to
// generate the log file name by removing .JMX and replacing it with .log|.jtl
private static final int PROXY_PASSWORD = 'a';// $NON-NLS-1$
private static final int JMETER_HOME_OPT = 'd';// $NON-NLS-1$
private static final int HELP_OPT = 'h';// $NON-NLS-1$
private static final int OPTIONS_OPT = '?';// $NON-NLS-1$
// logging configuration file
private static final int JMLOGCONF_OPT = 'i';// $NON-NLS-1$
// jmeter.log
private static final int JMLOGFILE_OPT = 'j';// $NON-NLS-1$
// sample result log file
private static final int LOGFILE_OPT = 'l';// $NON-NLS-1$
private static final int NONGUI_OPT = 'n';// $NON-NLS-1$
private static final int PROPFILE_OPT = 'p';// $NON-NLS-1$
private static final int PROPFILE2_OPT = 'q';// $NON-NLS-1$
private static final int REMOTE_OPT = 'r';// $NON-NLS-1$
private static final int SERVER_OPT = 's';// $NON-NLS-1$
private static final int TESTFILE_OPT = 't';// $NON-NLS-1$
private static final int PROXY_USERNAME = 'u';// $NON-NLS-1$
private static final int VERSION_OPT = 'v';// $NON-NLS-1$
private static final int REPORT_GENERATING_OPT = 'g';// $NON-NLS-1$
private static final int REPORT_AT_END_OPT = 'e';// $NON-NLS-1$
private static final int REPORT_OUTPUT_FOLDER_OPT = 'o';// $NON-NLS-1$
private static final int FORCE_DELETE_RESULT_FILE = 'f';// $NON-NLS-1$
private static final int SYSTEM_PROPERTY = 'D';// $NON-NLS-1$
private static final int JMETER_GLOBAL_PROP = 'G';// $NON-NLS-1$
private static final int PROXY_SCHEME = 'E';// $NON-NLS-1$
private static final int PROXY_HOST = 'H';// $NON-NLS-1$
private static final int JMETER_PROPERTY = 'J';// $NON-NLS-1$
private static final int LOGLEVEL = 'L';// $NON-NLS-1$
private static final int NONPROXY_HOSTS = 'N';// $NON-NLS-1$
private static final int PROXY_PORT = 'P';// $NON-NLS-1$
private static final int REMOTE_OPT_PARAM = 'R';// $NON-NLS-1$
private static final int SYSTEM_PROPFILE = 'S';// $NON-NLS-1$
private static final int REMOTE_STOP = 'X';// $NON-NLS-1$
private static final String JMX_SUFFIX = ".JMX"; // $NON-NLS-1$
private static final String PACKAGE_PREFIX = "org.apache."; //$NON_NLS-1$
/**
* Define the understood options. Each CLOptionDescriptor contains:
* <ul>
* <li>The "long" version of the option. Eg, "help" means that "--help"
* will be recognised.</li>
* <li>The option flags, governing the option's argument(s).</li>
* <li>The "short" version of the option. Eg, 'h' means that "-h" will be
* recognised.</li>
* <li>A description of the option.</li>
* </ul>
*/
private static final CLOptionDescriptor D_OPTIONS_OPT =
new CLOptionDescriptor("?", CLOptionDescriptor.ARGUMENT_DISALLOWED, OPTIONS_OPT,
"print command line options and exit");
private static final CLOptionDescriptor D_HELP_OPT =
new CLOptionDescriptor("help", CLOptionDescriptor.ARGUMENT_DISALLOWED, HELP_OPT,
"print usage information and exit");
private static final CLOptionDescriptor D_VERSION_OPT =
new CLOptionDescriptor("version", CLOptionDescriptor.ARGUMENT_DISALLOWED, VERSION_OPT,
"print the version information and exit");
private static final CLOptionDescriptor D_PROPFILE_OPT =
new CLOptionDescriptor("propfile", CLOptionDescriptor.ARGUMENT_REQUIRED, PROPFILE_OPT,
"the jmeter property file to use");
private static final CLOptionDescriptor D_PROPFILE2_OPT =
new CLOptionDescriptor("addprop", CLOptionDescriptor.ARGUMENT_REQUIRED
| CLOptionDescriptor.DUPLICATES_ALLOWED, PROPFILE2_OPT,
"additional JMeter property file(s)");
private static final CLOptionDescriptor D_TESTFILE_OPT =
new CLOptionDescriptor("testfile", CLOptionDescriptor.ARGUMENT_REQUIRED, TESTFILE_OPT,
"the jmeter test(.jmx) file to run. \"-t LAST\" will load last used file");
private static final CLOptionDescriptor D_LOGFILE_OPT =
new CLOptionDescriptor("logfile", CLOptionDescriptor.ARGUMENT_REQUIRED, LOGFILE_OPT,
"the file to log samples to");
private static final CLOptionDescriptor D_JMLOGCONF_OPT =
new CLOptionDescriptor("jmeterlogconf", CLOptionDescriptor.ARGUMENT_REQUIRED, JMLOGCONF_OPT,
"jmeter logging configuration file (log4j2.xml)");
private static final CLOptionDescriptor D_JMLOGFILE_OPT =
new CLOptionDescriptor("jmeterlogfile", CLOptionDescriptor.ARGUMENT_REQUIRED, JMLOGFILE_OPT,
"jmeter run log file (jmeter.log)");
private static final CLOptionDescriptor D_NONGUI_OPT =
new CLOptionDescriptor("nongui", CLOptionDescriptor.ARGUMENT_DISALLOWED, NONGUI_OPT,
"run JMeter in nongui mode");
private static final CLOptionDescriptor D_SERVER_OPT =
new CLOptionDescriptor("server", CLOptionDescriptor.ARGUMENT_DISALLOWED, SERVER_OPT,
"run the JMeter server");
private static final CLOptionDescriptor D_PROXY_SCHEME =
new CLOptionDescriptor("proxyScheme", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_SCHEME,
"Set a proxy scheme to use for the proxy server");
private static final CLOptionDescriptor D_PROXY_HOST =
new CLOptionDescriptor("proxyHost", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_HOST,
"Set a proxy server for JMeter to use");
private static final CLOptionDescriptor D_PROXY_PORT =
new CLOptionDescriptor("proxyPort", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_PORT,
"Set proxy server port for JMeter to use");
private static final CLOptionDescriptor D_NONPROXY_HOSTS =
new CLOptionDescriptor("nonProxyHosts", CLOptionDescriptor.ARGUMENT_REQUIRED, NONPROXY_HOSTS,
"Set nonproxy host list (e.g. *.apache.org|localhost)");
private static final CLOptionDescriptor D_PROXY_USERNAME =
new CLOptionDescriptor("username", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_USERNAME,
"Set username for proxy server that JMeter is to use");
private static final CLOptionDescriptor D_PROXY_PASSWORD =
new CLOptionDescriptor("password", CLOptionDescriptor.ARGUMENT_REQUIRED, PROXY_PASSWORD,
"Set password for proxy server that JMeter is to use");
private static final CLOptionDescriptor D_JMETER_PROPERTY =
new CLOptionDescriptor("jmeterproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, JMETER_PROPERTY,
"Define additional JMeter properties");
private static final CLOptionDescriptor D_JMETER_GLOBAL_PROP =
new CLOptionDescriptor("globalproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, JMETER_GLOBAL_PROP,
"Define Global properties (sent to servers)\n\t\te.g. -Gport=123 or -Gglobal.properties");
private static final CLOptionDescriptor D_SYSTEM_PROPERTY =
new CLOptionDescriptor("systemproperty", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, SYSTEM_PROPERTY,
"Define additional system properties");
private static final CLOptionDescriptor D_SYSTEM_PROPFILE =
new CLOptionDescriptor("systemPropertyFile", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENT_REQUIRED, SYSTEM_PROPFILE,
"additional system property file(s)");
private static final CLOptionDescriptor D_LOGLEVEL =
new CLOptionDescriptor("loglevel", CLOptionDescriptor.DUPLICATES_ALLOWED
| CLOptionDescriptor.ARGUMENTS_REQUIRED_2, LOGLEVEL,
"[category=]level e.g. jorphan=INFO, jmeter.util=DEBUG or com.example.foo=WARN");
private static final CLOptionDescriptor D_REMOTE_OPT =
new CLOptionDescriptor("runremote", CLOptionDescriptor.ARGUMENT_DISALLOWED, REMOTE_OPT,
"Start remote servers (as defined in remote_hosts)");
private static final CLOptionDescriptor D_REMOTE_OPT_PARAM =
new CLOptionDescriptor("remotestart", CLOptionDescriptor.ARGUMENT_REQUIRED, REMOTE_OPT_PARAM,
"Start these remote servers (overrides remote_hosts)");
private static final CLOptionDescriptor D_JMETER_HOME_OPT =
new CLOptionDescriptor("homedir", CLOptionDescriptor.ARGUMENT_REQUIRED, JMETER_HOME_OPT,
"the jmeter home directory to use");
private static final CLOptionDescriptor D_REMOTE_STOP =
new CLOptionDescriptor("remoteexit", CLOptionDescriptor.ARGUMENT_DISALLOWED, REMOTE_STOP,
"Exit the remote servers at end of test (non-GUI)");
private static final CLOptionDescriptor D_REPORT_GENERATING_OPT =
new CLOptionDescriptor("reportonly",
CLOptionDescriptor.ARGUMENT_REQUIRED, REPORT_GENERATING_OPT,
"generate report dashboard only, from a test results file",
new CLOptionDescriptor[]{ D_NONGUI_OPT, D_REMOTE_OPT, D_REMOTE_OPT_PARAM, D_LOGFILE_OPT }); // disallowed
private static final CLOptionDescriptor D_REPORT_AT_END_OPT =
new CLOptionDescriptor("reportatendofloadtests",
CLOptionDescriptor.ARGUMENT_DISALLOWED, REPORT_AT_END_OPT,
"generate report dashboard after load test");
private static final CLOptionDescriptor D_REPORT_OUTPUT_FOLDER_OPT =
new CLOptionDescriptor("reportoutputfolder",
CLOptionDescriptor.ARGUMENT_REQUIRED, REPORT_OUTPUT_FOLDER_OPT,
"output folder for report dashboard");
private static final CLOptionDescriptor D_FORCE_DELETE_RESULT_FILE =
new CLOptionDescriptor("forceDeleteResultFile",
CLOptionDescriptor.ARGUMENT_DISALLOWED, FORCE_DELETE_RESULT_FILE,
"force delete existing results files and web report folder if present before starting the test");
private static final String[][] DEFAULT_ICONS = {
{ "org.apache.jmeter.control.gui.TestPlanGui", "org/apache/jmeter/images/beaker.gif" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.timers.gui.AbstractTimerGui", "org/apache/jmeter/images/timer.gif" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.threads.gui.ThreadGroupGui", "org/apache/jmeter/images/thread.gif" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.visualizers.gui.AbstractListenerGui", "org/apache/jmeter/images/meter.png" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.config.gui.AbstractConfigGui", "org/apache/jmeter/images/testtubes.png" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.processor.gui.AbstractPreProcessorGui", "org/apache/jmeter/images/leafnode.gif"}, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.processor.gui.AbstractPostProcessorGui","org/apache/jmeter/images/leafnodeflip.gif"},//$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.control.gui.AbstractControllerGui", "org/apache/jmeter/images/knob.gif" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.control.gui.WorkBenchGui", "org/apache/jmeter/images/clipboard.gif" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.samplers.gui.AbstractSamplerGui", "org/apache/jmeter/images/pipet.png" }, //$NON-NLS-1$ $NON-NLS-2$
{ "org.apache.jmeter.assertions.gui.AbstractAssertionGui", "org/apache/jmeter/images/question.gif"} //$NON-NLS-1$ $NON-NLS-2$
};
private static final CLOptionDescriptor[] options = new CLOptionDescriptor[] {
D_OPTIONS_OPT,
D_HELP_OPT,
D_VERSION_OPT,
D_PROPFILE_OPT,
D_PROPFILE2_OPT,
D_TESTFILE_OPT,
D_LOGFILE_OPT,
D_JMLOGCONF_OPT,
D_JMLOGFILE_OPT,
D_NONGUI_OPT,
D_SERVER_OPT,
D_PROXY_SCHEME,
D_PROXY_HOST,
D_PROXY_PORT,
D_NONPROXY_HOSTS,
D_PROXY_USERNAME,
D_PROXY_PASSWORD,
D_JMETER_PROPERTY,
D_JMETER_GLOBAL_PROP,
D_SYSTEM_PROPERTY,
D_SYSTEM_PROPFILE,
D_FORCE_DELETE_RESULT_FILE,
D_LOGLEVEL,
D_REMOTE_OPT,
D_REMOTE_OPT_PARAM,
D_JMETER_HOME_OPT,
D_REMOTE_STOP,
D_REPORT_GENERATING_OPT,
D_REPORT_AT_END_OPT,
D_REPORT_OUTPUT_FOLDER_OPT,
};
/** Properties to be sent to remote servers */
private Properties remoteProps;
/** should remote engines be stopped at end of non-GUI test? */
private boolean remoteStop;
/** should delete result file / report folder before start ? */
private boolean deleteResultFile = false;
public JMeter() {
super();
}
/**
* Takes the command line arguments and uses them to determine how to
* startup JMeter.
*
* Called reflectively by {@link NewDriver#main(String[])}
* @param args The arguments for JMeter
*/
public void start(String[] args) {
CLArgsParser parser = new CLArgsParser(args, options);
String error = parser.getErrorString();
if (error == null){// Check option combinations
boolean gui = parser.getArgumentById(NONGUI_OPT)==null;
boolean nonGuiOnly = parser.getArgumentById(REMOTE_OPT)!=null
|| parser.getArgumentById(REMOTE_OPT_PARAM)!=null
|| parser.getArgumentById(REMOTE_STOP)!=null;
if (gui && nonGuiOnly) {
error = "-r and -R and -X are only valid in non-GUI mode";
}
}
if (null != error) {
System.err.println("Error: " + error);//NOSONAR
System.out.println("Usage");//NOSONAR
System.out.println(CLUtil.describeOptions(options).toString());//NOSONAR
// repeat the error so no need to scroll back past the usage to see it
System.out.println("Error: " + error);//NOSONAR
return;
}
try {
initializeProperties(parser); // Also initialises JMeter logging
SecurityProviderLoader.addSecurityProvider(JMeterUtils.getJMeterProperties());
Thread.setDefaultUncaughtExceptionHandler(
(Thread t, Throwable e) -> {
if (!(e instanceof ThreadDeath)) {
log.error("Uncaught exception in thread {}", t, e);
System.err.println("Uncaught Exception " + e + " in thread " + t + ". See log file for details.");//NOSONAR
}
});
if (log.isInfoEnabled()) {
log.info(JMeterUtils.getJMeterCopyright());
log.info("Version {}", JMeterUtils.getJMeterVersion());
log.info("java.version={}", System.getProperty("java.version"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("java.vm.name={}", System.getProperty("java.vm.name"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("os.name={}", System.getProperty("os.name"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("os.arch={}", System.getProperty("os.arch"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("os.version={}", System.getProperty("os.version"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("file.encoding={}", System.getProperty("file.encoding"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("java.awt.headless={}", System.getProperty("java.awt.headless"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("Max memory ={}", Runtime.getRuntime().maxMemory());
log.info("Available Processors ={}", Runtime.getRuntime().availableProcessors());
log.info("Default Locale={}", Locale.getDefault().getDisplayName());
log.info("JMeter Locale={}", JMeterUtils.getLocale().getDisplayName());
log.info("JMeterHome={}", JMeterUtils.getJMeterHome());
log.info("user.dir ={}", System.getProperty("user.dir"));//$NON-NLS-1$ //$NON-NLS-2$
log.info("PWD ={}", new File(".").getCanonicalPath());//$NON-NLS-1$
log.info("IP: {} Name: {} FullName: {}", JMeterUtils.getLocalHostIP(), JMeterUtils.getLocalHostName(),
JMeterUtils.getLocalHostFullName());
}
setProxy(parser);
updateClassLoader();
if (log.isDebugEnabled())
{
String jcp=System.getProperty("java.class.path");// $NON-NLS-1$
String[] bits = jcp.split(File.pathSeparator);
log.debug("ClassPath");
for(String bit : bits){
log.debug(bit);
}
}
// Set some (hopefully!) useful properties
Instant now = Instant.now();
JMeterUtils.setProperty("START.MS",Long.toString(now.toEpochMilli()));// $NON-NLS-1$
JMeterUtils.setProperty("START.YMD", getFormatter("yyyyMMdd").format(now));// $NON-NLS-1$ $NON-NLS-2$
JMeterUtils.setProperty("START.HMS", getFormatter("HHmmss").format(now));// $NON-NLS-1$ $NON-NLS-2$
// For unknown reason, TestElementSchema might fail to initialize in remote execution mode
// It reproduces with Java 11.0.13, and the error is StackOverflowError with the following stacktrace
// The workaround is to initialize Kotlin reflection before deserializing the test plan.
// at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174) ~[?:?]
// at java.net.URLClassLoader.defineClass(URLClassLoader.java:555) ~[?:?]
// at java.net.URLClassLoader$1.run(URLClassLoader.java:458) ~[?:?]
// at java.net.URLClassLoader$1.run(URLClassLoader.java:452) ~[?:?]
// at java.security.AccessController.doPrivileged(Native Method) ~[?:?]
// at java.net.URLClassLoader.findClass(URLClassLoader.java:451) ~[?:?]
// at java.lang.ClassLoader.loadClass(ClassLoader.java:589) ~[?:?]
// at org.apache.jmeter.DynamicClassLoader.loadClass(DynamicClassLoader.java:81) ~[ApacheJMeter.jar:5.5.1-SNAPSHOT]
// at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]
// at kotlin.jvm.internal.ClassReference.<clinit>(ClassReference.kt:156) ~[kotlin-stdlib-1.8.21.jar:1.8.21-release-380(1.8.21)]
// at kotlin.jvm.internal.ReflectionFactory.getOrCreateKotlinClass(ReflectionFactory.java:30) ~[kotlin-stdlib-1.8.21.jar)]
// at kotlin.jvm.internal.Reflection.getOrCreateKotlinClass(Reflection.java:60) ~[kotlin-stdlib-1.8.21.jar:1.8.21-release-380(1.8.21)]
// at org.apache.jmeter.testelement.TestElementSchema.<init>(TestElementSchema.kt:33) ~[ApacheJMeter_core.jar:5.5.1-SNAPSHOT]
// at org.apache.jmeter.testelement.TestElementSchema$INSTANCE.<init>(TestElementSchema.kt:26) ~[ApacheJMeter_core.jar:5.5.1-SNAPSHOT]
// at org.apache.jmeter.testelement.TestElementSchema$INSTANCE.<init>(TestElementSchema.kt) ~[ApacheJMeter_core.jar:5.5.1-SNAPSHOT]
// at org.apache.jmeter.testelement.TestElementSchema.<clinit>(TestElementSchema.kt) ~[ApacheJMeter_core.jar:5.5.1-SNAPSHOT]
// at org.apache.jmeter.protocol.java.sampler.BeanShellSampler.<clinit>(BeanShellSampler.java:53) ~[ApacheJMeter_java.jar:5.5.1-SNAPSHOT]
// at jdk.internal.misc.Unsafe.ensureClassInitialized0(Native Method) ~[?:?]
TestElementSchema.INSTANCE.getGuiClass();
if (parser.getArgumentById(VERSION_OPT) != null) {
displayAsciiArt();
} else if (parser.getArgumentById(HELP_OPT) != null) {
displayAsciiArt();
System.out.println(JMeterUtils.getResourceFileAsText("org/apache/jmeter/help.txt"));//NOSONAR $NON-NLS-1$
} else if (parser.getArgumentById(OPTIONS_OPT) != null) {
displayAsciiArt();
System.out.println(CLUtil.describeOptions(options).toString());//NOSONAR
} else if (parser.getArgumentById(SERVER_OPT) != null) {
// Start the server
try {
RemoteJMeterEngineImpl.startServer(RmiUtils.getRmiRegistryPort()); // $NON-NLS-1$
startOptionalServers();
} catch (Exception ex) {
System.err.println("Server failed to start: "+ex);//NOSONAR
log.error("Giving up, as server failed with:", ex);
throw ex;
}
} else {
String testFile=null;
CLOption testFileOpt = parser.getArgumentById(TESTFILE_OPT);
if (testFileOpt != null){
testFile = testFileOpt.getArgument();
if (USE_LAST_JMX.equals(testFile)) {
testFile = LoadRecentProject.getRecentFile(0);// most recent
}
}
CLOption testReportOpt = parser.getArgumentById(REPORT_GENERATING_OPT);
if (testReportOpt != null) { // generate report from existing file
String reportFile = testReportOpt.getArgument();
extractAndSetReportOutputFolder(parser, deleteResultFile);
ReportGenerator generator = new ReportGenerator(reportFile, null);
generator.generate();
} else if (parser.getArgumentById(NONGUI_OPT) == null) { // not non-GUI => GUI
PluginManager.install(this, true);
String initialTestFile = testFile;
JMeterGuiLauncher.startGui(initialTestFile);
startOptionalServers();
} else { // NON-GUI must be true
extractAndSetReportOutputFolder(parser, deleteResultFile);
CLOption remoteTest = parser.getArgumentById(REMOTE_OPT_PARAM);
if (remoteTest == null) {
remoteTest = parser.getArgumentById(REMOTE_OPT);
}
CLOption jtl = parser.getArgumentById(LOGFILE_OPT);
String jtlFile = null;
if (jtl != null) {
jtlFile = processLAST(jtl.getArgument(), ".jtl"); // $NON-NLS-1$
}
CLOption reportAtEndOpt = parser.getArgumentById(REPORT_AT_END_OPT);
if(reportAtEndOpt != null && jtlFile == null) {
throw new IllegalUserActionException(
"Option -"+ ((char)REPORT_AT_END_OPT)+" requires -"+((char)LOGFILE_OPT )+ " option");
}
startNonGui(testFile, jtlFile, remoteTest, reportAtEndOpt != null);
startOptionalServers();
}
}
} catch (IllegalUserActionException e) {// NOSONAR
System.out.println("Incorrect Usage:"+e.getMessage());//NOSONAR
System.out.println(CLUtil.describeOptions(options).toString());//NOSONAR
} catch (Throwable e) { // NOSONAR
log.error("An error occurred: ", e);
System.out.println("An error occurred: " + e.getMessage());//NOSONAR
// FIXME Should we exit here ? If we are called by Maven or Jenkins
System.exit(1);
}
}
private static DateTimeFormatter getFormatter(String pattern) {
return DateTimeFormatter.ofPattern(pattern).withZone(ZoneId.systemDefault());
}
/**
* Extract option JMeter#REPORT_OUTPUT_FOLDER_OPT and if defined sets property
* {@link JMeter#JMETER_REPORT_OUTPUT_DIR_PROPERTY} after checking folder can
* be safely written to
* @param parser {@link CLArgsParser}
* @param deleteReportFolder true means delete report folder
* @throws IllegalArgumentException
*/
private static void extractAndSetReportOutputFolder(CLArgsParser parser, boolean deleteReportFolder) {
CLOption reportOutputFolderOpt = parser
.getArgumentById(REPORT_OUTPUT_FOLDER_OPT);
if(reportOutputFolderOpt != null) {
String reportOutputFolder = parser.getArgumentById(REPORT_OUTPUT_FOLDER_OPT).getArgument();
File reportOutputFolderAsFile = new File(reportOutputFolder);
JOrphanUtils.canSafelyWriteToFolder(reportOutputFolderAsFile, deleteReportFolder);
final String reportOutputFolderAbsPath = reportOutputFolderAsFile.getAbsolutePath();
log.info("Setting property '{}' to:'{}'", JMETER_REPORT_OUTPUT_DIR_PROPERTY, reportOutputFolderAbsPath);
JMeterUtils.setProperty(JMETER_REPORT_OUTPUT_DIR_PROPERTY, reportOutputFolderAbsPath);
}
}
/**
* Displays as ASCII Art Apache JMeter version + Copyright notice
*/
private static void displayAsciiArt() {
try (InputStream inputStream = JMeter.class.getResourceAsStream("jmeter_as_ascii_art.txt")) {
if(inputStream != null) {
String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
System.out.println(text);//NOSONAR
}
} catch (Exception e1) { //NOSONAR No logging here
System.out.println(JMeterUtils.getJMeterCopyright());//NOSONAR
System.out.println("Version " + JMeterUtils.getJMeterVersion());//NOSONAR
}
}
// Update classloader if necessary
private static void updateClassLoader() throws MalformedURLException {
updatePath("search_paths",";", true); //$NON-NLS-1$//$NON-NLS-2$
updatePath("user.classpath",File.pathSeparator, true);//$NON-NLS-1$
updatePath("plugin_dependency_paths",";", false);//$NON-NLS-1$
}
private static void updatePath(String property, String sep, boolean cp) throws MalformedURLException {
String userpath= JMeterUtils.getPropDefault(property,"");// $NON-NLS-1$
if (userpath.length() <= 0) {
return;
}
log.info("{}={}", property, userpath); //$NON-NLS-1$
StringTokenizer tok = new StringTokenizer(userpath, sep);
while(tok.hasMoreTokens()) {
String path=tok.nextToken();
File f=new File(path);
if (!f.canRead() && !f.isDirectory()) {
log.warn("Can't read {}", path);
} else {
if (cp) {
log.info("Adding to classpath and loader: {}", path);
NewDriver.addPath(path);
} else {
log.info("Adding to loader: {}", path);
NewDriver.addURL(path);
}
}
}
}
/**
*
*/
private static void startOptionalServers() {
int bshport = JMeterUtils.getPropDefault("beanshell.server.port", 0);// $NON-NLS-1$
String bshfile = JMeterUtils.getPropDefault("beanshell.server.file", "");// $NON-NLS-1$ $NON-NLS-2$
if (bshport > 0) {
log.info("Starting Beanshell server ({},{})", bshport, bshfile);
Runnable t = new BeanShellServer(bshport, bshfile);
t.run(); // NOSONAR we just evaluate some code here
}
runInitScripts();
int mirrorPort=JMeterUtils.getPropDefault("mirror.server.port", 0);// $NON-NLS-1$
if (mirrorPort > 0){
log.info("Starting Mirror server ({})", mirrorPort);
try {
Object instance = ClassTools.construct(
"org.apache.jmeter.protocol.http.control.HttpMirrorControl",// $NON-NLS-1$
mirrorPort);
ClassTools.invoke(instance,"startHttpMirror");
} catch (JMeterException e) {
log.warn("Could not start Mirror server",e);
}
}
}
/**
* Runs user configured init scripts
*/
static void runInitScripts() {
// Should we run a beanshell script on startup?
String bshinit = JMeterUtils.getProperty("beanshell.init.file");// $NON-NLS-1$
if (bshinit != null){
log.info("Running Beanshell on file: {}", bshinit);
try {
BeanShellInterpreter bsi = new BeanShellInterpreter();
bsi.source(bshinit);
} catch (ClassNotFoundException|JMeterException e) {
if (log.isWarnEnabled()) {
log.warn("Could not process Beanshell file: {}", e.getMessage());
}
}
}
// Should we run a JSR223 script on startup?
String jsr223Init = JMeterUtils.getProperty(JSR223_INIT_FILE);// $NON-NLS-1$
if (jsr223Init != null){
log.info("Running JSR-223 init script in file: {}", jsr223Init);
File file = new File(jsr223Init);
if(file.exists() && file.canRead()) {
String extension = StringUtils.defaultIfBlank(FilenameUtils.getExtension(jsr223Init), "Groovy");
try (Reader reader = Files.newBufferedReader(file.toPath())) {
ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
ScriptEngine engine = scriptEngineManager.getEngineByExtension(extension);
if (engine == null) {
log.warn(
"No script engine found for [{}]. Will try to use Groovy. Possible engines and their extensions are: {}",
extension, getEnginesAndExtensions(scriptEngineManager));
extension = "Groovy";
engine = scriptEngineManager.getEngineByName(extension);
}
Bindings bindings = engine.createBindings();
final Logger logger = LoggerFactory.getLogger(JSR223_INIT_FILE);
bindings.put("log", logger); // $NON-NLS-1$ (this name is fixed)
Properties props = JMeterUtils.getJMeterProperties();
bindings.put("props", props); // $NON-NLS-1$ (this name is fixed)
// For use in debugging:
bindings.put("OUT", System.out); // NOSONAR $NON-NLS-1$ (this name is fixed)
engine.eval(reader, bindings);
} catch (IOException | ScriptException ex) {
log.error("Error running init script {} with engine for {}: {}", jsr223Init, extension, ex);
}
} else {
log.error("Script {} referenced by property {} is not readable or does not exist", file.getAbsolutePath(), JSR223_INIT_FILE);
}
}
}
private static Map<String, List<String>> getEnginesAndExtensions(ScriptEngineManager scriptEngineManager) {
return scriptEngineManager.getEngineFactories().stream()
.collect(Collectors.toMap(
f -> f.getLanguageName() + " (" + f.getLanguageVersion() + ")",
ScriptEngineFactory::getExtensions));
}
/**
* Sets a proxy server for the JVM if the command line arguments are
* specified.
*/
private static void setProxy(CLArgsParser parser) throws IllegalUserActionException {
if (parser.getArgumentById(PROXY_USERNAME) != null) {
Properties jmeterProps = JMeterUtils.getJMeterProperties();
if (parser.getArgumentById(PROXY_PASSWORD) != null) {
String u = parser.getArgumentById(PROXY_USERNAME).getArgument();
String p = parser.getArgumentById(PROXY_PASSWORD).getArgument();
Authenticator.setDefault(new ProxyAuthenticator(u, p));
log.info("Set Proxy login: {}/{}", u, p);
jmeterProps.setProperty(HTTP_PROXY_USER, u);//for Httpclient
jmeterProps.setProperty(HTTP_PROXY_PASS, p);//for Httpclient
} else {
String u = parser.getArgumentById(PROXY_USERNAME).getArgument();
Authenticator.setDefault(new ProxyAuthenticator(u, ""));
log.info("Set Proxy login: {}", u);
jmeterProps.setProperty(HTTP_PROXY_USER, u);
}
}
if (parser.getArgumentById(PROXY_HOST) != null && parser.getArgumentById(PROXY_PORT) != null) {
String h = parser.getArgumentById(PROXY_HOST).getArgument();
String p = parser.getArgumentById(PROXY_PORT).getArgument();
System.setProperty("http.proxyHost", h );// $NON-NLS-1$
System.setProperty("https.proxyHost", h);// $NON-NLS-1$
System.setProperty("http.proxyPort", p);// $NON-NLS-1$
System.setProperty("https.proxyPort", p);// $NON-NLS-1$
String proxyScheme = null;
if (parser.getArgumentById(PROXY_SCHEME) != null) {
proxyScheme = parser.getArgumentById(PROXY_SCHEME).getArgument();
if(!StringUtils.isBlank(proxyScheme)){
System.setProperty("http.proxyScheme", proxyScheme );// $NON-NLS-1$
}
}
if(log.isInfoEnabled()) {
log.info("Set proxy Host: {}, Port: {}, Scheme: {}", h, p, proxyScheme != null ? proxyScheme : "Not set");
}
} else if (parser.getArgumentById(PROXY_HOST) != null || parser.getArgumentById(PROXY_PORT) != null) {
throw new IllegalUserActionException(JMeterUtils.getResString("proxy_cl_error"));// $NON-NLS-1$
}
if (parser.getArgumentById(NONPROXY_HOSTS) != null) {
String n = parser.getArgumentById(NONPROXY_HOSTS).getArgument();
System.setProperty("http.nonProxyHosts", n );// $NON-NLS-1$
System.setProperty("https.nonProxyHosts", n );// $NON-NLS-1$
log.info("Set http[s].nonProxyHosts: {}", n);
}
}
private void initializeProperties(CLArgsParser parser) {
if (parser.getArgumentById(PROPFILE_OPT) != null) {
JMeterUtils.loadJMeterProperties(parser.getArgumentById(PROPFILE_OPT).getArgument());
} else {
JMeterUtils.loadJMeterProperties(NewDriver.getJMeterDir() + File.separator
+ "bin" + File.separator // $NON-NLS-1$
+ "jmeter.properties");// $NON-NLS-1$
}
JMeterUtils.initLocale();
// Bug 33845 - allow direct override of Home dir
if (parser.getArgumentById(JMETER_HOME_OPT) == null) {
JMeterUtils.setJMeterHome(NewDriver.getJMeterDir());
} else {
JMeterUtils.setJMeterHome(parser.getArgumentById(JMETER_HOME_OPT).getArgument());
}
Properties jmeterProps = JMeterUtils.getJMeterProperties();
remoteProps = new Properties();
// Add local JMeter properties, if the file is found
String userProp = JMeterUtils.getPropDefault("user.properties",""); //$NON-NLS-1$
if (userProp.length() > 0){ //$NON-NLS-1$
File file = JMeterUtils.findFile(userProp);
if (file.canRead()){
try (FileInputStream fis = new FileInputStream(file)){
log.info("Loading user properties from: {}", file);
Properties tmp = new Properties();
tmp.load(fis);
jmeterProps.putAll(tmp);
} catch (IOException e) {
log.warn("Error loading user property file: {}", userProp, e);
}
}
}
// Add local system properties, if the file is found
String sysProp = JMeterUtils.getPropDefault("system.properties",""); //$NON-NLS-1$
if (sysProp.length() > 0){
File file = JMeterUtils.findFile(sysProp);
if (file.canRead()) {
try (FileInputStream fis = new FileInputStream(file)){
log.info("Loading system properties from: {}", file);
System.getProperties().load(fis);
} catch (IOException e) {
log.warn("Error loading system property file: {}", sysProp, e);
}
}
}
// Process command line property definitions
// These can potentially occur multiple times
List<CLOption> clOptions = parser.getArguments();
for (CLOption option : clOptions) {
String name = option.getArgument(0);
String value = option.getArgument(1);
switch (option.getDescriptor().getId()) {
// Should not have any text arguments
case CLOption.TEXT_ARGUMENT:
throw new IllegalArgumentException("Unknown arg: " + option.getArgument());
case PROPFILE2_OPT: // Bug 33920 - allow multiple props
log.info("Loading additional properties from: {}", name);
try (FileInputStream fis = new FileInputStream(new File(name))){
Properties tmp = new Properties();
tmp.load(fis);
jmeterProps.putAll(tmp);
} catch (FileNotFoundException e) { // NOSONAR
log.warn("Can't find additional property file: {}", name, e);
} catch (IOException e) { // NOSONAR
log.warn("Error loading additional property file: {}", name, e);
}
break;
case SYSTEM_PROPFILE:
log.info("Setting System properties from file: {}", name);
try (FileInputStream fis = new FileInputStream(new File(name))){
System.getProperties().load(fis);
} catch (IOException e) { // NOSONAR
if (log.isWarnEnabled()) {
log.warn("Cannot find system property file. {}", e.getLocalizedMessage());
}
}
break;
case SYSTEM_PROPERTY:
if (value.length() > 0) { // Set it
log.info("Setting System property: {}={}", name, value);
System.getProperties().setProperty(name, value);
} else { // Reset it
log.warn("Removing System property: {}", name);
System.getProperties().remove(name);
}
break;
case JMETER_PROPERTY:
if (value.length() > 0) { // Set it
log.info("Setting JMeter property: {}={}", name, value);
jmeterProps.setProperty(name, value);
} else { // Reset it
log.warn("Removing JMeter property: {}", name);
jmeterProps.remove(name);
}
break;
case JMETER_GLOBAL_PROP:
if (value.length() > 0) { // Set it
log.info("Setting Global property: {}={}", name, value);
remoteProps.setProperty(name, value);
} else {
File propFile = new File(name);
if (propFile.canRead()) {
log.info("Setting Global properties from the file {}", name);
try (FileInputStream fis = new FileInputStream(propFile)){
remoteProps.load(fis);
} catch (FileNotFoundException e) { // NOSONAR
if (log.isWarnEnabled()) {
log.warn("Could not find properties file: {}", e.getLocalizedMessage());
}
} catch (IOException e) { // NOSONAR
if (log.isWarnEnabled()) {
log.warn("Could not load properties file: {}", e.getLocalizedMessage());
}
}
}
}
break;
case LOGLEVEL:
if (value.length() > 0) { // Set category
log.info("LogLevel: {}={}", name, value);
final Level logLevel = Level.getLevel(value);
if (logLevel != null) {
String loggerName = name;
if (name.startsWith("jmeter") || name.startsWith("jorphan")) {
loggerName = PACKAGE_PREFIX + name;
}
Configurator.setAllLevels(loggerName, logLevel);
} else {
log.warn("Invalid log level, '{}' for '{}'.", value, name);
}
} else { // Set root level
log.warn("LogLevel: {}", name);
final Level logLevel = Level.getLevel(name);
if (logLevel != null) {
Configurator.setRootLevel(logLevel);
} else {
log.warn("Invalid log level, '{}', for the root logger.", name);
}
}
break;
case REMOTE_STOP:
remoteStop = true;
break;
case FORCE_DELETE_RESULT_FILE:
deleteResultFile = true;
break;
default:
// ignored
break;
}
}
String sampleVariables = (String) jmeterProps.get(SampleEvent.SAMPLE_VARIABLES);
if (sampleVariables != null){
remoteProps.put(SampleEvent.SAMPLE_VARIABLES, sampleVariables);
}
jmeterProps.put("jmeter.version", JMeterUtils.getJMeterVersion());
}
/*
* Checks for LAST or LASTsuffix.
* Returns the LAST name with .JMX replaced by suffix.
*/
private static String processLAST(final String jmlogfile, final String suffix) {
if (USE_LAST_JMX.equals(jmlogfile) || USE_LAST_JMX.concat(suffix).equals(jmlogfile)){
String last = LoadRecentProject.getRecentFile(0);// most recent
if (last.toUpperCase(Locale.ENGLISH).endsWith(JMX_SUFFIX)){
return last.substring(0, last.length() - JMX_SUFFIX.length()).concat(suffix);
}
}
return jmlogfile;
}
private void startNonGui(String testFile, String logFile, CLOption remoteStart, boolean generateReportDashboard)
throws IllegalUserActionException, ConfigurationException {
// add a system property so samplers can check to see if JMeter
// is running in NonGui mode
System.setProperty(JMETER_NON_GUI, "true");// $NON-NLS-1$
JMeter driver = new JMeter();// TODO - why does it create a new instance?
driver.remoteProps = this.remoteProps;
driver.remoteStop = this.remoteStop;
driver.deleteResultFile = this.deleteResultFile;
PluginManager.install(this, false);
String remoteHostsString = null;
if (remoteStart != null) {
remoteHostsString = remoteStart.getArgument();
if (remoteHostsString == null) {
remoteHostsString = JMeterUtils.getPropDefault(
"remote_hosts", //$NON-NLS-1$
"127.0.0.1");//NOSONAR $NON-NLS-1$
}
}
if (testFile == null) {
throw new IllegalUserActionException("Non-GUI runs require a test plan");
}
driver.runNonGui(testFile, logFile, remoteStart != null, remoteHostsString, generateReportDashboard);
}
// run test in batch mode
@SuppressWarnings("JdkObsolete")
void runNonGui(String testFile, String logFile, boolean remoteStart, String remoteHostsString, boolean generateReportDashboard)
throws ConfigurationException {
try {
File f = new File(testFile);
if (!f.exists() || !f.isFile()) {
throw new ConfigurationException("The file " + f.getAbsolutePath() + " doesn't exist or can't be opened");
}
FileServer.getFileServer().setBaseForScript(f);
HashTree tree = SaveService.loadTree(f);
@SuppressWarnings("deprecation") // Deliberate use of deprecated ctor
JMeterTreeModel treeModel = new JMeterTreeModel(new Object());// NOSONAR Create non-GUI version to avoid headless problems
JMeterTreeNode root = (JMeterTreeNode) treeModel.getRoot();
treeModel.addSubTree(tree, root);
// Hack to resolve ModuleControllers in non GUI mode
SearchByClass<ReplaceableController> replaceableControllers =
new SearchByClass<>(ReplaceableController.class);
tree.traverse(replaceableControllers);
Collection<ReplaceableController> replaceableControllersRes = replaceableControllers.getSearchResults();
for (ReplaceableController replaceableController : replaceableControllersRes) {
replaceableController.resolveReplacementSubTree(root);
}
// Ensure tree is interpreted (ReplaceableControllers are replaced)
// For GUI runs this is done in Start.java
HashTree clonedTree = convertSubTree(tree, true);
Summariser summariser = null;
String summariserName = JMeterUtils.getPropDefault("summariser.name", "");//$NON-NLS-1$
if (summariserName.length() > 0) {
log.info("Creating summariser <{}>", summariserName);
println("Creating summariser <" + summariserName + ">");
summariser = new Summariser(summariserName);
}
ResultCollector resultCollector = null;
if (logFile != null) {
resultCollector = new ResultCollector(summariser);
resultCollector.setFilename(logFile);
clonedTree.add(clonedTree.getArray()[0], resultCollector);
}
else {
// only add Summariser if it can not be shared with the ResultCollector
if (summariser != null) {
clonedTree.add(clonedTree.getArray()[0], summariser);
}
}
if (deleteResultFile) {
SearchByClass<ResultCollector> resultListeners = new SearchByClass<>(ResultCollector.class);
clonedTree.traverse(resultListeners);
for (ResultCollector rc : resultListeners.getSearchResults()) {
File resultFile = new File(rc.getFilename());
if (resultFile.exists() && !resultFile.delete()) {
throw new IllegalStateException("Could not delete results file " + resultFile.getAbsolutePath()