1# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4"""Helper functions to parse result collected from device"""
5
6from __future__ import print_function
7from fix_skia_results import _TransformBenchmarks
8
9import json
10
11def normalize(bench, dict_list):
12    bench_base = {
13        'Panorama': 1,
14        'Dex2oat': 1,
15        'Hwui': 10000,
16        'Skia': 1,
17        'Synthmark': 1,
18        'Binder': 0.001
19    }
20    result_dict = dict_list[0]
21    for key in result_dict:
22        result_dict[key] = result_dict[key] / bench_base[bench]
23    return [result_dict]
24
25
26# Functions to parse benchmark result for data collection.
27def parse_Panorama(bench, fin):
28    result_dict = {}
29    for line in fin:
30        words = line.split()
31        if 'elapsed' in words:
32            #TODO: Need to restructure the embedded word counts.
33            result_dict['total_time_s'] = float(words[3])
34            result_dict['retval'] = 0
35            return normalize(bench, [result_dict])
36    raise ValueError('You passed the right type of thing, '
37                     'but it didn\'t have the expected contents.')
38
39
40def parse_Synthmark(bench, fin):
41    result_dict = {}
42    accum = 0
43    cnt = 0
44    for line in fin:
45        words = line.split()
46        if 'normalized' in words:
47            #TODO: Need to restructure the embedded word counts.
48            accum += float(words[-1])
49            cnt += 1
50    if accum != 0:
51        result_dict['total_voices'] = accum / cnt
52        result_dict['retval'] = 0
53        return normalize(bench, [result_dict])
54    raise ValueError('You passed the right type of thing, '
55                     'but it didn\'t have the expected contents.')
56
57
58def parse_Binder(bench, fin):
59    result_dict = {}
60    accum = 0
61    cnt = 0
62    for line in fin:
63        words = line.split()
64        for word in words:
65            if 'average' in word:
66                #TODO: Need to restructure the embedded word counts.
67                accum += float(word[8:-2])
68                cnt += 1
69    if accum != 0:
70        result_dict['avg_time_ms'] = accum / cnt
71        result_dict['retval'] = 0
72        return normalize(bench, [result_dict])
73    raise ValueError('You passed the right type of thing, '
74                     'but it didn\'t have the expected contents.')
75
76
77def parse_Dex2oat(bench, fin):
78    result_dict = {}
79    cnt = 0
80    for line in fin:
81        words = line.split()
82        if 'elapsed' in words:
83            cnt += 1
84            #TODO: Need to restructure the embedded word counts.
85            if cnt == 1:
86                # First 'elapsed' time is for microbench 'Chrome'
87                result_dict['chrome_s'] = float(words[3])
88            elif cnt == 2:
89                # Second 'elapsed' time is for microbench 'Camera'
90                result_dict['camera_s'] = float(words[3])
91
92                result_dict['retval'] = 0
93                # Two results found, return
94                return normalize(bench, [result_dict])
95    raise ValueError('You passed the right type of thing, '
96                     'but it didn\'t have the expected contents.')
97
98
99def parse_Hwui(bench, fin):
100    result_dict = {}
101    for line in fin:
102        words = line.split()
103        if 'elapsed' in words:
104            #TODO: Need to restructure the embedded word counts.
105            result_dict['total_time_s'] = float(words[3])
106            result_dict['retval'] = 0
107            return normalize(bench, [result_dict])
108    raise ValueError('You passed the right type of thing, '
109                     'but it didn\'t have the expected contents.')
110
111
112def parse_Skia(bench, fin):
113    obj = json.load(fin)
114    return normalize(bench, _TransformBenchmarks(obj))
115