cros: Upload command stats in parallel.
Add background command stats uploading functionality to CrosCommand
instances.
The functionality is disabled by default, and can be enabled by
overriding the 'upload_stats' property. Also adds a
'upload_stats_timeout' property to allow customization of the timeout.
The timeout for stats uploading is set at 1 second, which provides for a
fair bit of margin. From manual timing, upload_command_stats takes
between 0.35s-0.45s in MTV.
BUG=None
TEST=Ran 'cros chrome-sdk' and verified stats were uploading.
Change-Id: I09cf4c005c80aa2464981266d44c0055e0e295ab
Reviewed-on: https://gerrit.chromium.org/gerrit/47088
Commit-Queue: Ryan Cui <rcui@chromium.org>
Reviewed-by: Ryan Cui <rcui@chromium.org>
Tested-by: Ryan Cui <rcui@chromium.org>
diff --git a/scripts/cros.py b/scripts/cros.py
index b2b22f0..4392821 100644
--- a/scripts/cros.py
+++ b/scripts/cros.py
@@ -2,8 +2,11 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import sys
+
from chromite.cros import commands
from chromite.lib import commandline
+from chromite.lib import stats
def GetOptions(my_commands):
@@ -23,6 +26,11 @@
return parser
+def _RunSubCommand(subcommand):
+ """Helper function for testing purposes."""
+ subcommand.Run()
+
+
def main(args):
parser = GetOptions(commands.ListCommands())
# Cros currently does nothing without a subcmd. Print help if no args are
@@ -33,5 +41,13 @@
namespace = parser.parse_args(args)
subcommand = namespace.cros_class(namespace)
- subcommand.Run()
+ with stats.UploadContext() as queue:
+ cmd_base = subcommand.options.cros_class.command_name
+ cmd_stats = stats.Stats(cmd_line=sys.argv, cmd_base=cmd_base)
+ queue.put([cmd_stats, stats.StatsUploader.URL,
+ subcommand.upload_stats_timeout])
+ # TODO: to make command completion faster, send an interrupt signal to the
+ # stats uploader task after the subcommand completes.
+ _RunSubCommand(subcommand)
+
return 0