xref: /aosp_15_r20/build/bazel/scripts/incremental_build/perf_metrics_test.py (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1#!/usr/bin/env python3
2
3# Copyright (C) 2022 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16import dataclasses
17import unittest
18
19from perf_metrics import _get_column_headers
20
21
22def to_row(concatenated_keys: str) -> dict:
23    return {c: None for c in concatenated_keys}
24
25
26class PerfMetricsTest(unittest.TestCase):
27    """Tests utility functions. This is not Perf Test itself."""
28
29    def test_get_column_headers(self):
30        @dataclasses.dataclass
31        class Example:
32            # each string = concatenated keys of the row object
33            row_keysets: list[str]
34            # concatenated headers
35            expected_headers: str
36
37        examples: list[Example] = [
38            Example(["a"], "a"),
39            Example(["ac", "bd"], "abcd"),
40            Example(["abe", "cde"], "abcde"),
41            Example(["ab", "ba"], "ab"),
42            Example(["abcde", "edcba"], "abcde"),
43            Example(["ac", "abc"], "abc"),
44        ]
45        for e in examples:
46            rows = [to_row(kz) for kz in e.row_keysets]
47            expected_headers = [*e.expected_headers]
48            with self.subTest(rows=rows, expected_headers=expected_headers):
49                self.assertEqual(
50                    _get_column_headers(rows, allow_cycles=True), expected_headers
51                )
52
53    def test_cycles(self):
54        examples = [(["ab", "ba"], "a->b->a"), (["abcd", "db"], "b->c->d->b")]
55        for e, cycle in examples:
56            rows = [to_row(kz) for kz in e]
57            with self.subTest(rows=rows, cycle=cycle):
58                with self.assertRaisesRegex(
59                    ValueError, f"event ordering has a cycle {cycle}"
60                ):
61                    _get_column_headers(rows, allow_cycles=False)
62
63
64if __name__ == "__main__":
65    unittest.main()
66