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
18
19from simpleperf_utils import remove
20from . app_test import TestExampleBase
21from . test_utils import INFERNO_SCRIPT
22
23
24class TestExampleOfKotlin(TestExampleBase):
25    @classmethod
26    def setUpClass(cls):
27        cls.prepare("SimpleperfExampleOfKotlin",
28                    "com.example.simpleperf.simpleperfexampleofkotlin",
29                    ".MainActivity")
30
31    def test_app_profiler(self):
32        self.common_test_app_profiler()
33
34    def test_app_profiler_profile_from_launch(self):
35        self.run_app_profiler(start_activity=True, build_binary_cache=False)
36        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
37        self.check_strings_in_file("report.txt", [
38            "com.example.simpleperf.simpleperfexampleofkotlin.MainActivity$createBusyThread$1." +
39            "run", "__start_thread"])
40
41    def test_report(self):
42        self.common_test_report()
43        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
44        self.check_strings_in_file("report.txt", [
45            "com.example.simpleperf.simpleperfexampleofkotlin.MainActivity$createBusyThread$1." +
46            "run", "__start_thread"])
47
48    def test_annotate(self):
49        if not self.use_compiled_java_code:
50            return
51        self.common_test_annotate()
52        self.check_file_under_dir("annotated_files", "MainActivity.kt")
53        summary_file = os.path.join("annotated_files", "summary")
54        self.check_annotation_summary(summary_file, [
55            ("MainActivity.kt", 80, 80),
56            ("run", 80, 0),
57            ("callFunction", 0, 0),
58            ("line 19", 80, 0),
59            ("line 25", 0, 0)])
60
61    def test_report_sample(self):
62        self.common_test_report_sample([
63            "com.example.simpleperf.simpleperfexampleofkotlin.MainActivity$createBusyThread$1." +
64            "run", "__start_thread"])
65
66    def test_pprof_proto_generator(self):
67        check_strings_with_lines = []
68        if self.use_compiled_java_code:
69            check_strings_with_lines = [
70                "com/example/simpleperf/simpleperfexampleofkotlin/MainActivity.kt",
71                "run"]
72        self.common_test_pprof_proto_generator(
73            check_strings_with_lines=check_strings_with_lines,
74            check_strings_without_lines=["com.example.simpleperf.simpleperfexampleofkotlin." +
75                                         "MainActivity$createBusyThread$1.run"])
76
77    def test_inferno(self):
78        self.common_test_inferno()
79        self.run_app_profiler()
80        self.run_cmd([INFERNO_SCRIPT, "-sc"])
81        self.check_inferno_report_html([('com.example.simpleperf.simpleperfexampleofkotlin.' +
82                                         'MainActivity$createBusyThread$1.run', 80)])
83
84    def test_report_html(self):
85        self.common_test_report_html()
86
87
88class TestExampleOfKotlinRoot(TestExampleBase):
89    @classmethod
90    def setUpClass(cls):
91        cls.prepare("SimpleperfExampleOfKotlin",
92                    "com.example.simpleperf.simpleperfexampleofkotlin",
93                    ".MainActivity",
94                    adb_root=True)
95
96    def test_app_profiler(self):
97        self.common_test_app_profiler()
98
99
100class TestExampleOfKotlinTraceOffCpu(TestExampleBase):
101    @classmethod
102    def setUpClass(cls):
103        cls.prepare("SimpleperfExampleOfKotlin",
104                    "com.example.simpleperf.simpleperfexampleofkotlin",
105                    ".SleepActivity")
106
107    def test_smoke(self):
108        self.run_app_profiler(record_arg="-g -f 1000 --duration 10 -e cpu-cycles:u --trace-offcpu")
109        self.run_cmd(["report.py", "-g", "-o", "report.txt"])
110        function_prefix = "com.example.simpleperf.simpleperfexampleofkotlin." + \
111                          "SleepActivity$createRunSleepThread$1."
112        self.check_strings_in_file("report.txt", [
113            function_prefix + "run",
114            function_prefix + "RunFunction",
115            function_prefix + "SleepFunction"
116        ])
117        if self.use_compiled_java_code:
118            remove("annotated_files")
119            self.run_cmd(["annotate.py", "-s", self.example_path])
120            self.check_exist(dirname="annotated_files")
121            self.check_file_under_dir("annotated_files", "SleepActivity.kt")
122            summary_file = os.path.join("annotated_files", "summary")
123            self.check_annotation_summary(summary_file, [
124                ("SleepActivity.kt", 80, 20),
125                ("run", 80, 0),
126                ("RunFunction", 20, 20),
127                ("SleepFunction", 20, 0),
128                ("line 24", 20, 0),
129                ("line 32", 20, 0)])
130
131        self.run_cmd([INFERNO_SCRIPT, "-sc"])
132        self.check_inferno_report_html([
133            (function_prefix + 'run', 80),
134            (function_prefix + 'RunFunction', 20),
135            (function_prefix + 'SleepFunction', 20)])
136