blob: 812afd8dcc33639127f0c80ab785731e06a0816d [file] [log] [blame]
Zhizhou Yange5986902017-08-10 17:37:53 -07001#!/usr/bin/env python2
2#
3# Copyright 2017 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6#
7# pylint: disable=cros-logging-import
8
9"""Script to help generate json format report from raw data."""
10from __future__ import print_function
11
12import argparse
13import config
14import json
15import logging
16import sys
17
18# Turn the logging level to INFO before importing other autotest
19# code, to avoid having failed import logging messages confuse the
20# test_droid user.
21logging.basicConfig(level=logging.INFO)
22
23
24def _parse_arguments_internal(argv):
Zhizhou Yang62362922017-08-30 16:04:36 -070025 parser = argparse.ArgumentParser(description='Convert result to JSON'
26 'format')
Zhizhou Yange5986902017-08-10 17:37:53 -070027
Zhizhou Yang62362922017-08-30 16:04:36 -070028 parser.add_argument(
29 '-b', '--bench', help='Generate JSON format file for which benchmark.')
Zhizhou Yange5986902017-08-10 17:37:53 -070030
Zhizhou Yang62362922017-08-30 16:04:36 -070031 parser.add_argument(
32 '-i', '--input', help='Specify the input result file name.')
Zhizhou Yange5986902017-08-10 17:37:53 -070033
Zhizhou Yang62362922017-08-30 16:04:36 -070034 parser.add_argument(
35 '-o', '--output', help='Specify the output JSON format result file')
Zhizhou Yange5986902017-08-10 17:37:53 -070036
Zhizhou Yang62362922017-08-30 16:04:36 -070037 parser.add_argument(
38 '-p',
39 '--platform',
40 help='Indicate the platform(experiment or device) name '
41 'to be shown in JSON')
Zhizhou Yange5986902017-08-10 17:37:53 -070042
Zhizhou Yang62362922017-08-30 16:04:36 -070043 parser.add_argument(
44 '--iterations',
45 type=int,
46 help='How many iterations does the result include.')
47 return parser.parse_args(argv)
Zhizhou Yange5986902017-08-10 17:37:53 -070048
49# Collect data and generate JSON {} tuple from benchmark result
50def collect_data(infile, bench, it):
Zhizhou Yang62362922017-08-30 16:04:36 -070051 result_dict = {}
52 with open(infile + str(it)) as fin:
53 if bench not in config.bench_parser_dict:
54 logging.error('Please input the correct benchmark name.')
55 raise ValueError('Wrong benchmark name: %s' % bench)
56 parse = config.bench_parser_dict[bench]
57 result_dict = parse(bench, fin)
58 return result_dict
Zhizhou Yange5986902017-08-10 17:37:53 -070059
60# If there is no original output file, create a new one and init it.
61def create_outfile(outfile, bench):
Zhizhou Yang62362922017-08-30 16:04:36 -070062 with open(outfile, 'w') as fout:
63 obj_null = {'data': {bench.lower(): []}, 'platforms': []}
64 json.dump(obj_null, fout)
Zhizhou Yange5986902017-08-10 17:37:53 -070065
66# Seek the original output file and try to add new result into it.
67def get_outfile(outfile, bench):
Zhizhou Yang62362922017-08-30 16:04:36 -070068 try:
69 return open(outfile)
70 except IOError:
71 create_outfile(outfile, bench)
72 return open(outfile)
Zhizhou Yange5986902017-08-10 17:37:53 -070073
74def main(argv):
Zhizhou Yang62362922017-08-30 16:04:36 -070075 arguments = _parse_arguments_internal(argv)
Zhizhou Yange5986902017-08-10 17:37:53 -070076
Zhizhou Yang62362922017-08-30 16:04:36 -070077 bench = arguments.bench
78 infile = arguments.input
79 outfile = arguments.output
80 platform = arguments.platform
81 iteration = arguments.iterations
Zhizhou Yange5986902017-08-10 17:37:53 -070082
Zhizhou Yang62362922017-08-30 16:04:36 -070083 result = []
84 for i in xrange(iteration):
85 result += collect_data(infile, bench, i)
Zhizhou Yange5986902017-08-10 17:37:53 -070086
Zhizhou Yang62362922017-08-30 16:04:36 -070087 with get_outfile(outfile, bench) as fout:
88 obj = json.load(fout)
89 obj['platforms'].append(platform)
90 obj['data'][bench.lower()].append(result)
91 with open(outfile, 'w') as fout:
92 json.dump(obj, fout)
Zhizhou Yange5986902017-08-10 17:37:53 -070093
94
95if __name__ == '__main__':
Zhizhou Yang62362922017-08-30 16:04:36 -070096 main(sys.argv[1:])