@@ -55,6 +55,10 @@ def setOutputLevel(cls, namespace, dest, value):
5555 setattr (namespace , "test_output" , value )
5656
5757
58+ _TIME_TESTS_OPT = "--time-tests"
59+ _TIME_TESTS_SLOWEST_DEFAULT = 20
60+ _TIME_TESTS_PREFIX = f"{ _TIME_TESTS_OPT } ="
61+
5862class AliasAction (argparse .Action ):
5963 def __init__ (self , option_strings , dest , nargs = None , ** kwargs ):
6064 self .expansion = kwargs .pop ("alias" , None )
@@ -382,9 +386,12 @@ def parse_args():
382386 action = "store_true" ,
383387 )
384388 execution_test_time_group .add_argument (
385- "--time-tests" ,
386- help = "Track elapsed wall time for each test printed in a histogram" ,
389+ _TIME_TESTS_OPT ,
390+ help = "Track elapsed wall time for each test and print a histogram; "
391+ "optionally limit the slowest-test list with =N or report all with =all "
392+ f"(default slowest count: { _TIME_TESTS_SLOWEST_DEFAULT } )" ,
387393 action = "store_true" ,
394+ default = None ,
388395 )
389396
390397 selection_group = parser .add_argument_group ("Test Selection" )
@@ -515,8 +522,21 @@ def parse_args():
515522
516523 # LIT is special: environment variables override command line arguments.
517524 env_args = shlex .split (os .environ .get ("LIT_OPTS" , "" ))
518- args = sys .argv [1 :] + env_args
525+ try :
526+ # --time-tests is preprocessed here: bare --time-tests defaults to 20,
527+ # and --time-tests=N / --time-tests=all set the slowest-test limit.
528+ # Values must use = (e.g. --time-tests=all), since `lit --time-tests all`
529+ # could mean "show all slow tests" or "run the test directory all".
530+ args , time_tests = _extract_time_tests_args (sys .argv [1 :] + env_args )
531+ except argparse .ArgumentTypeError as exc :
532+ parser .error (str (exc ))
519533 opts = parser .parse_args (args )
534+ opts .time_tests = time_tests
535+
536+ if opts .time_tests is not None and opts .skip_test_time_recording :
537+ parser .error (
538+ f"argument --skip-test-time-recording: not allowed with argument { _TIME_TESTS_OPT } "
539+ )
520540
521541 # Validate command line options
522542 if opts .incremental :
@@ -555,6 +575,28 @@ def _positive_int(arg):
555575 return _int (arg , "positive" , lambda i : i > 0 )
556576
557577
578+ def _time_tests_count (arg ):
579+ if arg .lower () == "all" :
580+ return "all"
581+ return _positive_int (arg )
582+
583+
584+ def _extract_time_tests_args (args ):
585+ processed = []
586+ time_tests = None
587+ for arg in args :
588+ if arg == _TIME_TESTS_OPT :
589+ time_tests = _TIME_TESTS_SLOWEST_DEFAULT
590+ elif arg .startswith (_TIME_TESTS_PREFIX ):
591+ value = arg [len (_TIME_TESTS_PREFIX ) :]
592+ if not value :
593+ raise _error (f"argument { _TIME_TESTS_OPT } requires a value after '='" )
594+ time_tests = _time_tests_count (value )
595+ else :
596+ processed .append (arg )
597+ return processed , time_tests
598+
599+
558600def _non_negative_int (arg ):
559601 return _int (arg , "non-negative" , lambda i : i >= 0 )
560602
0 commit comments