xref: /aosp_15_r20/build/bazel/scripts/incremental_build/pretty_test.py (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14import io
15import textwrap
16import unittest
17from typing import TextIO
18
19from pretty import Aggregation
20from pretty import summarize_helper
21
22
23class PrettyTest(unittest.TestCase):
24    metrics: TextIO
25
26    def setUp(self) -> None:
27        self.metrics = io.StringIO(
28            textwrap.dedent(
29                """\
30                build_result,build_type,description,targets,a,ab,ac
31                SUCCESS,B1,WARMUP,nothing,1,10,1:00
32                SUCCESS,B1,do it,something,10,200,
33                SUCCESS,B1,rebuild-1,something,4,,1:04
34                SUCCESS,B1,rebuild-2,something,6,55,1:07
35                TEST_FAILURE,B2,do it,something,601,,
36                TEST_FAILURE,B2,do it,nothing,3600,,
37                TEST_FAILURE,B2,undo it,something,240,,
38                FAILED,B3,,,70000,70000,7:00:00
39                """
40            )
41        )
42
43    def test_summarize_single_prop(self):
44        result = summarize_helper(self.metrics, "a$", Aggregation.MAX)
45        self.assertEqual(len(result), 1)
46        self.assertEqual(
47            textwrap.dedent(
48                """\
49                cuj,targets,B1,B2
50                WARMUP,nothing,1,
51                do it,something,10,601
52                do it,nothing,,3600
53                rebuild,something,6[N=2],
54                undo it,something,,240
55                """
56            ),
57            result["a"],
58        )
59
60    def test_summarize_multiple_props(self):
61        result = summarize_helper(self.metrics, "a.$", Aggregation.MEDIAN)
62        self.assertEqual(len(result), 2)
63        self.assertEqual(
64            textwrap.dedent(
65                """\
66                cuj,targets,B1,B2
67                WARMUP,nothing,10,
68                do it,something,200,
69                do it,nothing,,
70                rebuild,something,55,
71                undo it,something,,
72                """
73            ),
74            result["ab"],
75        )
76        self.assertEqual(
77            textwrap.dedent(
78                """\
79                cuj,targets,B1,B2
80                WARMUP,nothing,01:00,
81                do it,something,,
82                do it,nothing,,
83                rebuild,something,01:06[N=2],
84                undo it,something,,
85                """
86            ),
87            result["ac"],
88        )
89
90    def test_summarize_loose_pattern(self):
91        result = summarize_helper(self.metrics, "^a", Aggregation.MEDIAN)
92        self.assertEqual(len(result), 3)
93
94
95if __name__ == "__main__":
96    unittest.main()
97