xref: /aosp_15_r20/system/extras/simpleperf/cmd_merge_test.cpp (revision 288bf5226967eb3dac5cce6c939ccc2a7f2b4fe5)
1*288bf522SAndroid Build Coastguard Worker /*
2*288bf522SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*288bf522SAndroid Build Coastguard Worker  *
4*288bf522SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*288bf522SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*288bf522SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*288bf522SAndroid Build Coastguard Worker  *
8*288bf522SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*288bf522SAndroid Build Coastguard Worker  *
10*288bf522SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*288bf522SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*288bf522SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*288bf522SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*288bf522SAndroid Build Coastguard Worker  * limitations under the License.
15*288bf522SAndroid Build Coastguard Worker  */
16*288bf522SAndroid Build Coastguard Worker 
17*288bf522SAndroid Build Coastguard Worker #include <gtest/gtest.h>
18*288bf522SAndroid Build Coastguard Worker 
19*288bf522SAndroid Build Coastguard Worker #include <optional>
20*288bf522SAndroid Build Coastguard Worker 
21*288bf522SAndroid Build Coastguard Worker #include <android-base/file.h>
22*288bf522SAndroid Build Coastguard Worker 
23*288bf522SAndroid Build Coastguard Worker #include "command.h"
24*288bf522SAndroid Build Coastguard Worker #include "get_test_data.h"
25*288bf522SAndroid Build Coastguard Worker #include "record.h"
26*288bf522SAndroid Build Coastguard Worker #include "record_file.h"
27*288bf522SAndroid Build Coastguard Worker #include "test_util.h"
28*288bf522SAndroid Build Coastguard Worker #include "utils.h"
29*288bf522SAndroid Build Coastguard Worker 
30*288bf522SAndroid Build Coastguard Worker using namespace simpleperf;
31*288bf522SAndroid Build Coastguard Worker 
MergeCmd()32*288bf522SAndroid Build Coastguard Worker static std::unique_ptr<Command> MergeCmd() {
33*288bf522SAndroid Build Coastguard Worker   return CreateCommandInstance("merge");
34*288bf522SAndroid Build Coastguard Worker }
35*288bf522SAndroid Build Coastguard Worker 
GetReport(const std::string & record_file)36*288bf522SAndroid Build Coastguard Worker static std::string GetReport(const std::string& record_file) {
37*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmpfile;
38*288bf522SAndroid Build Coastguard Worker   close(tmpfile.release());
39*288bf522SAndroid Build Coastguard Worker   if (!CreateCommandInstance("report")->Run({"-i", record_file, "-g", "-o", tmpfile.path})) {
40*288bf522SAndroid Build Coastguard Worker     return "";
41*288bf522SAndroid Build Coastguard Worker   }
42*288bf522SAndroid Build Coastguard Worker   std::string data;
43*288bf522SAndroid Build Coastguard Worker   if (!android::base::ReadFileToString(tmpfile.path, &data)) {
44*288bf522SAndroid Build Coastguard Worker     return "";
45*288bf522SAndroid Build Coastguard Worker   }
46*288bf522SAndroid Build Coastguard Worker   return data;
47*288bf522SAndroid Build Coastguard Worker }
48*288bf522SAndroid Build Coastguard Worker 
49*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(merge_cmd,input_output_options)50*288bf522SAndroid Build Coastguard Worker TEST(merge_cmd, input_output_options) {
51*288bf522SAndroid Build Coastguard Worker   // missing arguments
52*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(MergeCmd()->Run({}));
53*288bf522SAndroid Build Coastguard Worker   // missing input files
54*288bf522SAndroid Build Coastguard Worker   std::string input_file = GetTestData("perf.data");
55*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(MergeCmd()->Run({"-i", input_file}));
56*288bf522SAndroid Build Coastguard Worker   // missing output file
57*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmpfile;
58*288bf522SAndroid Build Coastguard Worker   close(tmpfile.release());
59*288bf522SAndroid Build Coastguard Worker   ASSERT_FALSE(MergeCmd()->Run({"-o", tmpfile.path}));
60*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-o", tmpfile.path}));
61*288bf522SAndroid Build Coastguard Worker   // input files separated by comma
62*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file + "," + input_file, "-o", tmpfile.path}));
63*288bf522SAndroid Build Coastguard Worker   // input files in different -i options
64*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-i", input_file, "-o", tmpfile.path}));
65*288bf522SAndroid Build Coastguard Worker }
66*288bf522SAndroid Build Coastguard Worker 
67*288bf522SAndroid Build Coastguard Worker // @CddTest = 6.1/C-0-2
TEST(merge_cmd,merge_two_files)68*288bf522SAndroid Build Coastguard Worker TEST(merge_cmd, merge_two_files) {
69*288bf522SAndroid Build Coastguard Worker   std::string input_file1 = GetTestData("perf_merge1.data");
70*288bf522SAndroid Build Coastguard Worker   std::string input_file2 = GetTestData("perf_merge2.data");
71*288bf522SAndroid Build Coastguard Worker 
72*288bf522SAndroid Build Coastguard Worker   std::string report = GetReport(input_file1);
73*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("Samples: 27"), std::string::npos);
74*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("malloc"), std::string::npos);
75*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(report.find("sleep_main"), std::string::npos);
76*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("toybox_main"), std::string::npos);
77*288bf522SAndroid Build Coastguard Worker 
78*288bf522SAndroid Build Coastguard Worker   report = GetReport(input_file2);
79*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("Samples: 31"), std::string::npos);
80*288bf522SAndroid Build Coastguard Worker   ASSERT_EQ(report.find("malloc"), std::string::npos);
81*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("sleep_main"), std::string::npos);
82*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("toybox_main"), std::string::npos);
83*288bf522SAndroid Build Coastguard Worker 
84*288bf522SAndroid Build Coastguard Worker   TemporaryFile tmpfile;
85*288bf522SAndroid Build Coastguard Worker   close(tmpfile.release());
86*288bf522SAndroid Build Coastguard Worker   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file1 + "," + input_file2, "-o", tmpfile.path}));
87*288bf522SAndroid Build Coastguard Worker   report = GetReport(tmpfile.path);
88*288bf522SAndroid Build Coastguard Worker   // sum of sample counts in input files
89*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("Samples: 58"), std::string::npos);
90*288bf522SAndroid Build Coastguard Worker   // union of symbols in input files
91*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("malloc"), std::string::npos);
92*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("sleep_main"), std::string::npos);
93*288bf522SAndroid Build Coastguard Worker   ASSERT_NE(report.find("toybox_main"), std::string::npos);
94*288bf522SAndroid Build Coastguard Worker }
95