Time kgtk command execution.
-import sys
+import datetime
import importlib
-import pkgutil
import itertools
+import pkgutil
+import sys
+import time
from kgtk import cli
from kgtk.exceptions import KGTKExceptionHandler, KGTKArgumentParseException
@@ -34,6 +36,9 @@ def cli_entry(*args):
"""
global ret_code
+ # Capture the initial time for timing measurements.
+ start_time: float = time.time()
+
# get all arguments
if not args:
args = tuple(sys.argv)
@@ -51,6 +56,7 @@ def cli_entry(*args):
shared_args.add_argument('--debug', dest='_debug', action='store_true', default=False, help='enable debug mode')
shared_args.add_argument('--expert', dest='_expert', action='store_true', default=False, help='enable expert mode')
shared_args.add_argument('--pipedebug', dest='_pipedebug', action='store_true', default=False, help='enable pipe debug mode')
+ shared_args.add_argument('--timing', dest='_timing', action='store_true', default=False, help='enable timing measurements')
add_shared_arguments(shared_args)
# parse shared arguments
@@ -58,6 +64,7 @@ def cli_entry(*args):
shared_args = tuple(filter(lambda a: a not in rest_args, args))
args = tuple(rest_args)
+
# complete parser, load sub-parser of each module
parser = KGTKArgumentParser(
parents=[base_parser], prog='kgtk',
@@ -145,7 +152,7 @@ def cli_entry(*args):
concat_cmd_str = 'sh.kgtk({}, {}, _piped=True, _err=sys.stderr)'.format(concat_cmd_str, cmd_str)
try:
if parsed_shared_args._pipedebug:
- print("eval: %s" % concat_cmd_str)
+ print("eval: %s" % concat_cmd_str, file=sys.stderr, flush=True)
process = eval(concat_cmd_str)
process.wait()
except sh.SignalException_SIGPIPE:
@@ -154,5 +161,10 @@ def cli_entry(*args):
# mimic parser exit
parser.exit(KGTKArgumentParseException.return_code, e.stderr.decode('utf-8'))
+ if parsed_shared_args._timing:
+ end_time: float = time.time()
+ elapsed_seconds = end_time - start_time
+ print("Time taken: %s (%s)" % (str(datetime.timedelta(seconds=elapsed_seconds)), " ".join(args)), file=sys.stderr, flush=True)
+
return ret_code
