Store JSON perf results for low_bandwidth_audio_test.

Bug: chromium:755660
Change-Id: I87c19f8dde14772e75f93c723f11408702ca8c8e
Reviewed-on: https://webrtc-review.googlesource.com/39515
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Commit-Queue: Edward Lemur <ehmaldonado@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21615}
diff --git a/audio/test/low_bandwidth_audio_test.py b/audio/test/low_bandwidth_audio_test.py
index 05dcf47..9e25977 100755
--- a/audio/test/low_bandwidth_audio_test.py
+++ b/audio/test/low_bandwidth_audio_test.py
@@ -16,6 +16,7 @@
 
 import argparse
 import collections
+import json
 import logging
 import os
 import re
@@ -54,6 +55,8 @@
   parser.add_argument('--android', action='store_true',
       help='Perform the test on a connected Android device instead.')
   parser.add_argument('--adb-path', help='Path to adb binary.', default='adb')
+  parser.add_argument('--chartjson-result-file',
+      help='Where to store perf results in chartjson format.', default=None)
 
   # Ignore Chromium-specific flags
   parser.add_argument('--isolated-script-test-output',
@@ -192,6 +195,15 @@
   return {'polqa_mos_lqo': (mos_lqo, 'score')}
 
 
+def _AddChart(charts, metric, test_name, value, units):
+  chart = charts.setdefault(metric, {})
+  chart[test_name] = {
+      "type": "scalar",
+      "value": value,
+      "units": units,
+  }
+
+
 Analyzer = collections.namedtuple('Analyzer', ['func', 'executable',
                                                'sample_rate_hz'])
 
@@ -220,6 +232,8 @@
   if polqa_path and _RunPolqa(polqa_path, example_path, example_path):
     analyzers.append(Analyzer(_RunPolqa, polqa_path, 48000))
 
+  charts = {}
+
   for analyzer in analyzers:
     # Start the test executable that produces audio files.
     test_process = subprocess.Popen(
@@ -245,6 +259,7 @@
         for metric, (value, units) in analyzer_results.items():
           # Output a result for the perf dashboard.
           print 'RESULT %s: %s= %s %s' % (metric, test_name, value, units)
+          _AddChart(charts, metric, test_name, value, units)
 
         if args.remove:
           os.remove(reference_file)
@@ -252,6 +267,10 @@
     finally:
       test_process.terminate()
 
+  if args.chartjson_result_file:
+    with open(args.chartjson_result_file, 'w') as f:
+      json.dump({"format_version": "1.0", "charts": charts}, f)
+
   return test_process.wait()