xref: /aosp_15_r20/system/extras/simpleperf/scripts/test/kotlin_app_test.py (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1#!/usr/bin/env python3
2#
3# Copyright (C) 2021 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.
16
17import os
18import unittest
19
20from simpleperf_utils import remove
21from . app_test import TestExampleBase
22from . test_utils import INFERNO_SCRIPT, TestHelper
23
24
25class TestExampleKotlin(TestExampleBase):
26    @classmethod
27    def setUpClass(cls):
28        cls.prepare("SimpleperfExampleKotlin",
29                    "simpleperf.example.kotlin",
30                    ".MainActivity")
31
32    def test_app_profiler(self):
33        self.common_test_app_profiler()
34
35    def test_app_profiler_profile_from_launch(self):
36        self.run_app_profiler(start_activity=True, build_binary_cache=False)
37        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
38        self.check_strings_in_file("report.txt", [
39            "simpleperf.example.kotlin.MainActivity$createBusyThread$1." +
40            "run", "__start_thread"])
41
42    def test_report(self):
43        self.common_test_report()
44        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
45        self.check_strings_in_file("report.txt", [
46            "simpleperf.example.kotlin.MainActivity$createBusyThread$1." +
47            "run", "__start_thread"])
48
49    def test_annotate(self):
50        if not self.use_compiled_java_code:
51            return
52        self.common_test_annotate()
53        self.check_file_under_dir("annotated_files", "MainActivity.kt")
54        summary_file = os.path.join("annotated_files", "summary")
55        self.check_annotation_summary(summary_file, [
56            ("MainActivity.kt", 80, 80),
57            ("run", 80, 0),
58            ("callFunction", 0, 0),
59            ("line 19", 80, 0),
60            ("line 25", 0, 0)])
61
62    def test_report_sample(self):
63        self.common_test_report_sample([
64            "simpleperf.example.kotlin.MainActivity$createBusyThread$1." +
65            "run", "__start_thread"])
66
67    def test_pprof_proto_generator(self):
68        check_strings_with_lines = []
69        if self.use_compiled_java_code:
70            check_strings_with_lines = [
71                "simpleperf/example/kotlin/MainActivity.kt",
72                "run"]
73        self.common_test_pprof_proto_generator(
74            check_strings_with_lines=check_strings_with_lines,
75            check_strings_without_lines=[
76                'simpleperf.example.kotlin.MainActivity$createBusyThread$1.run'])
77
78    def test_inferno(self):
79        self.common_test_inferno()
80        self.run_app_profiler()
81        self.run_cmd([INFERNO_SCRIPT, "-sc"])
82        self.check_inferno_report_html([
83            ('simpleperf.example.kotlin.MainActivity$createBusyThread$1.run', 80)])
84
85    def test_report_html(self):
86        self.common_test_report_html()
87
88
89class TestExampleKotlinProfileableApk(TestExampleKotlin):
90    """ Test profiling a profileable released apk."""
91    @classmethod
92    def setUpClass(cls):
93        if TestHelper.android_version >= 10:
94            cls.prepare("SimpleperfExampleKotlin",
95                        "simpleperf.example.kotlin",
96                        ".MainActivity", apk_name='app-release.apk')
97
98    def setUp(self):
99        if TestHelper().android_version < 10:
100            raise unittest.SkipTest("Profileable apk isn't supported on Android < Q.")
101        super().setUp()
102
103
104class TestExampleKotlinRoot(TestExampleBase):
105    @classmethod
106    def setUpClass(cls):
107        cls.prepare("SimpleperfExampleKotlin",
108                    "simpleperf.example.kotlin",
109                    ".MainActivity",
110                    adb_root=True)
111
112    def test_app_profiler(self):
113        self.common_test_app_profiler()
114
115
116class TestExampleKotlinTraceOffCpu(TestExampleBase):
117    @classmethod
118    def setUpClass(cls):
119        cls.prepare("SimpleperfExampleKotlin",
120                    "simpleperf.example.kotlin",
121                    ".SleepActivity")
122
123    def test_smoke(self):
124        self.run_app_profiler(record_arg="-g -f 1000 --duration 10 -e cpu-clock:u --trace-offcpu")
125        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
126        function_prefix = 'simpleperf.example.kotlin.SleepActivity$createRunSleepThread$1.'
127        self.check_strings_in_file("report.txt", [
128            function_prefix + "run",
129            function_prefix + "RunFunction",
130            function_prefix + "SleepFunction"
131        ])
132        if self.use_compiled_java_code:
133            remove("annotated_files")
134            self.run_cmd(["annotate.py", "-s", self.example_path, '--summary-width', '1000'])
135            self.check_exist(dirname="annotated_files")
136            self.check_file_under_dir("annotated_files", "SleepActivity.kt")
137            summary_file = os.path.join("annotated_files", "summary")
138            self.check_annotation_summary(summary_file, [
139                ("SleepActivity.kt", 80, 20),
140                ("run", 80, 0),
141                ("RunFunction", 20, 20),
142                ("SleepFunction", 20, 0),
143                ("line 23", 20, 0),
144                ("line 31", 20, 0)])
145
146        self.run_cmd([INFERNO_SCRIPT, "-sc"])
147        self.check_inferno_report_html([
148            (function_prefix + 'run', 80),
149            (function_prefix + 'RunFunction', 20),
150            (function_prefix + 'SleepFunction', 20)])
151