xref: /aosp_15_r20/external/autotest/server/cros/telemetry_runner_unittest.py (revision 9c5db1993ded3edbeafc8092d69fe5de2ee02df7)
1#!/usr/bin/python3
2# Copyright 2019 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6import json
7import unittest
8
9import common
10from autotest_lib.server.cros import telemetry_runner
11
12histograms_sample = [
13    {
14        'values': [
15            'story1'
16        ],
17        'guid': '00000001-...',
18        'type': 'GenericSet'
19    },
20    {
21        'values': [
22            'story2'
23        ],
24        'guid': '00000002-...',
25        'type': 'GenericSet'
26    },
27    {
28        'values': [
29            'benchmark1'
30        ],
31        'guid': 'a0000001-...',
32        'type': 'GenericSet'
33    },
34    {
35        'values': [
36            'benchmark_desc1'
37        ],
38        'guid': 'b0000001-...',
39        'type': 'GenericSet'
40    },
41    {
42        'sampleValues': [1.0, 2.0],
43        'name': 'metric1',
44        'diagnostics': {
45            'stories': '00000001-...',
46            'benchmarks': 'a0000001-...',
47            'benchmarkDescriptions': 'b0000001-...'
48        },
49        'unit': 'ms_smallerIsBetter'
50    },
51    {
52        'sampleValues': [1.0, 2.0],
53        'name': 'metric1',
54        'diagnostics': {
55            'stories': '00000002-...',
56            'benchmarks': 'a0000001-...',
57            'benchmarkDescriptions': 'b0000001-...'
58        },
59        'unit': 'ms_smallerIsBetter'
60    }
61]
62
63chartjson_sample = {
64    'format_version': 1.0,
65    'benchmark_name': 'benchmark1',
66    'benchmark_description': 'benchmark_desc1',
67    'benchmark_metadata': {
68        'type': 'telemetry_benchmark',
69        'name': 'benchmark1',
70        'description': 'benchmark_desc1'
71    },
72    'charts': {
73        'metric1': {
74            'story1': {
75                'std': 0.5,
76                'name': 'metric1',
77                'type': 'list_of_scalar_values',
78                'values': [1.0, 2.0],
79                'units': 'ms',
80                'improvement_direction': 'down'
81            },
82            'story2': {
83                'std': 0.5,
84                'name': 'metric1',
85                'type': 'list_of_scalar_values',
86                'values': [1.0, 2.0],
87                'units': 'ms',
88                'improvement_direction': 'down'
89            },
90            'summary': {
91                'std': 0.5,
92                'name': 'metric1',
93                'type': 'list_of_scalar_values',
94                'values': [1.0, 1.0, 2.0, 2.0],
95                'units': 'ms',
96                'improvement_direction': 'down'
97            }
98        },
99    }
100}
101
102class TelemetryRunnerTestCase(unittest.TestCase):
103    """Test telemetry runner module."""
104
105    def test_convert_chart_json(self):
106        # Deep comparison of 2 objects with json dumps.
107        converted = telemetry_runner.TelemetryRunner.convert_chart_json(
108            histograms_sample)
109        chartjson_dumps = json.dumps(chartjson_sample, sort_keys=True)
110        chartjson_dumps2 = json.dumps(converted, sort_keys=True)
111        self.assertEqual(chartjson_dumps, chartjson_dumps2)
112
113if __name__ == '__main__':
114  unittest.main()
115