xref: /aosp_15_r20/system/extras/simpleperf/cmd_api_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 
19 #include <stdio.h>
20 
21 #include "command.h"
22 #include "get_test_data.h"
23 #include "record_file.h"
24 #include "test_util.h"
25 #include "utils.h"
26 
27 using namespace simpleperf;
28 
29 #if defined(__ANDROID__)
30 
WaitUntilAppExit(const std::string & package_name)31 static bool WaitUntilAppExit(const std::string& package_name) {
32   while (true) {
33     std::unique_ptr<FILE, decltype(&pclose)> fp(popen("ps -e", "re"), pclose);
34     if (!fp) {
35       return false;
36     }
37     std::string s;
38     if (!android::base::ReadFdToString(fileno(fp.get()), &s)) {
39       return false;
40     }
41     if (s.find(package_name) == std::string::npos) {
42       break;
43     }
44     sleep(1);
45   }
46   return true;
47 }
48 
CheckPerfDataFile(const std::string & filename)49 static void CheckPerfDataFile(const std::string& filename) {
50   auto reader = RecordFileReader::CreateInstance(filename);
51   ASSERT_TRUE(reader);
52   bool has_sample = false;
53   ASSERT_TRUE(reader->ReadDataSection([&](std::unique_ptr<Record> r) {
54     if (r->type() == PERF_RECORD_SAMPLE) {
55       has_sample = true;
56     }
57     return true;
58   }));
59   ASSERT_TRUE(has_sample);
60 }
61 
RecordApp(const std::string & package_name,const std::string & apk_path)62 static void RecordApp(const std::string& package_name, const std::string& apk_path) {
63   // 1. Install apk.
64   AppHelper app_helper;
65   ASSERT_TRUE(app_helper.InstallApk(apk_path, package_name));
66 
67   // 2. Prepare recording.
68   ASSERT_TRUE(CreateCommandInstance("api-prepare")->Run({"--app", package_name, "--days", "1"}));
69 
70   // 3. Start the app.
71   ASSERT_TRUE(app_helper.StartApp("am start " + package_name + "/.MainActivity"));
72 
73   // 4. Wait until the app stops.
74   sleep(3);
75   ASSERT_TRUE(WaitUntilAppExit(package_name));
76 
77   // 5. Collect perf.data.
78   SetRunInAppToolForTesting(true, true);
79   TemporaryFile tmpfile;
80   ASSERT_TRUE(
81       CreateCommandInstance("api-collect")->Run({"--app", package_name, "-o", tmpfile.path}));
82 
83   // 6. Verify perf.data.
84   TemporaryDir tmpdir;
85   ASSERT_TRUE(Workload::RunCmd({"unzip", "-d", tmpdir.path, tmpfile.path}));
86   for (const std::string& filename : GetEntriesInDir(tmpdir.path)) {
87     CheckPerfDataFile(std::string(tmpdir.path) + "/" + filename);
88   }
89 }
90 
91 #endif  // defined(__ANDROID__)
92 
93 // @CddTest = 6.1/C-0-2
TEST(cmd_api,java_app)94 TEST(cmd_api, java_app) {
95 #if defined(__ANDROID__)
96   RecordApp("simpleperf.demo.java_api", GetTestData("java_api.apk"));
97 #else
98   GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
99 #endif
100 }
101 
102 // @CddTest = 6.1/C-0-2
TEST(cmd_api,native_app)103 TEST(cmd_api, native_app) {
104 #if defined(__ANDROID__)
105   RecordApp("simpleperf.demo.cpp_api", GetTestData("cpp_api.apk"));
106 #else
107   GTEST_LOG_(INFO) << "This test tests recording apps on Android.";
108 #endif
109 }
110