run_test_build: Add debugging option to report slow commands

Test: time m art-run-test-{host,target,jvm}-data
Change-Id: Ib131e0d713380176a4b10a199a5411f6f2927dbb
diff --git a/test/run_test_build.py b/test/run_test_build.py
index 75cd64f..eb2de12 100755
--- a/test/run_test_build.py
+++ b/test/run_test_build.py
@@ -24,6 +24,7 @@
 import glob
 import os
 import pathlib
+import re
 import shlex
 import shutil
 import subprocess
@@ -31,9 +32,9 @@
 import zipfile
 
 from argparse import ArgumentParser
+from concurrent.futures import ThreadPoolExecutor
 from fcntl import lockf, LOCK_EX, LOCK_NB
 from importlib.machinery import SourceFileLoader
-from concurrent.futures import ThreadPoolExecutor
 from os import environ, getcwd, chdir, cpu_count, chmod
 from os.path import relpath
 from pathlib import Path
@@ -53,6 +54,9 @@
   "979-const-method-handle",  # b/228312861: RBE uses wrong inputs.
 }
 
+# Debug option. Report commands that are taking a lot of user CPU time.
+REPORT_SLOW_COMMANDS = False
+
 class BuildTestContext:
   def __init__(self, args, android_build_top, test_dir):
     self.android_build_top = android_build_top.absolute()
@@ -114,6 +118,8 @@
   def run(self, executable: pathlib.Path, args: List[Union[pathlib.Path, str]]):
     assert isinstance(executable, pathlib.Path), executable
     cmd: List[Union[pathlib.Path, str]] = []
+    if REPORT_SLOW_COMMANDS:
+      cmd += ["/usr/bin/time"]
     if executable.suffix == ".sh":
       cmd += ["/bin/bash"]
     cmd += [executable]
@@ -136,6 +142,14 @@
                        env=self.bash_env,
                        stderr=subprocess.STDOUT,
                        stdout=subprocess.PIPE)
+    if REPORT_SLOW_COMMANDS:
+      m = re.search("([0-9\.]+)user", p.stdout)
+      assert m, p.stdout
+      t = float(m.group(1))
+      if t > 1.0:
+        cmd_text = " ".join(map(str, cmd[1:]))[:100]
+        print(f"[{self.test_name}] Command took {t:.2f}s: {cmd_text}")
+
     if p.returncode != 0:
       raise Exception("Command failed with exit code {}\n$ {}\n{}".format(
                       p.returncode, " ".join(map(str, cmd)), p.stdout))