aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar borenet <borenet@google.com>2015-07-06 11:18:45 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-07-06 11:18:45 -0700
commit334e588d9ed5390efb82beb37329b56a380f1d0e (patch)
tree2ad686b026e65070bad301ee3ecac5710e007213
parentd390110a39f11cd9f5b07621df1f3e1d3064c277 (diff)
Add nanobench-style JSON output to llvm_coverage_run
Runs the build and run scripts for coverage, then dumps the results into a file in nanobench-compatible format. BUG=skia:2430 Review URL: https://codereview.chromium.org/1227523004
-rwxr-xr-xtools/llvm_coverage_run.py76
1 files changed, 72 insertions, 4 deletions
diff --git a/tools/llvm_coverage_run.py b/tools/llvm_coverage_run.py
index 902b1c6b04..676e670fcf 100755
--- a/tools/llvm_coverage_run.py
+++ b/tools/llvm_coverage_run.py
@@ -10,6 +10,7 @@
import argparse
import json
import os
+import re
import shlex
import subprocess
import sys
@@ -96,11 +97,78 @@ def run_coverage(cmd):
return results
+def _testname(filename):
+ """Transform the file name into an ingestible test name."""
+ return re.sub(r'[^a-zA-Z0-9]', '_', filename)
+
+
+def _nanobench_json(results, properties, key):
+ """Return the results in JSON format like that produced by nanobench."""
+ rv = {}
+ # Copy over the properties first, then set the 'key' and 'results' keys,
+ # in order to avoid bad formatting in case the user passes in a properties
+ # dict containing those keys.
+ rv.update(properties)
+ rv['key'] = key
+ rv['results'] = {
+ _testname(f): {
+ 'coverage': {
+ 'percent': percent,
+ 'options': {
+ 'fullname': f,
+ 'dir': os.path.dirname(f),
+ },
+ },
+ } for percent, f in results
+ }
+ return rv
+
+
+def _parse_key_value(kv_list):
+ """Return a dict whose key/value pairs are derived from the given list.
+
+ For example:
+
+ ['k1', 'v1', 'k2', 'v2']
+ becomes:
+
+ {'k1': 'v1',
+ 'k2': 'v2'}
+ """
+ if len(kv_list) % 2 != 0:
+ raise Exception('Invalid key/value pairs: %s' % kv_list)
+
+ rv = {}
+ for i in xrange(len(kv_list) / 2):
+ rv[kv_list[i*2]] = kv_list[i*2+1]
+ return rv
+
+
def main():
- res = run_coverage(sys.argv[1:])
- print '% Covered\tFilename'
- for percent, f in res:
- print '%f\t%s' % (percent, f)
+ """Run coverage and generate a report."""
+ # Parse args.
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--outResultsFile')
+ parser.add_argument(
+ '--key', metavar='key_or_value', nargs='+',
+ help='key/value pairs identifying this bot.')
+ parser.add_argument(
+ '--properties', metavar='key_or_value', nargs='+',
+ help='key/value pairs representing properties of this build.')
+ args, cmd = parser.parse_known_args()
+ key = _parse_key_value(args.key)
+ properties = _parse_key_value(args.properties)
+
+ # Run coverage.
+ results = run_coverage(cmd)
+
+ # Write results.
+ format_results = _nanobench_json(results, properties, key)
+ if args.outResultsFile:
+ with open(args.outResultsFile, 'w') as f:
+ json.dump(format_results, f)
+ else:
+ print json.dumps(format_results, indent=4, sort_keys=True)
if __name__ == '__main__':