xref: /aosp_15_r20/system/extras/simpleperf/cmd_merge_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 <optional>
20 
21 #include <android-base/file.h>
22 
23 #include "command.h"
24 #include "get_test_data.h"
25 #include "record.h"
26 #include "record_file.h"
27 #include "test_util.h"
28 #include "utils.h"
29 
30 using namespace simpleperf;
31 
MergeCmd()32 static std::unique_ptr<Command> MergeCmd() {
33   return CreateCommandInstance("merge");
34 }
35 
GetReport(const std::string & record_file)36 static std::string GetReport(const std::string& record_file) {
37   TemporaryFile tmpfile;
38   close(tmpfile.release());
39   if (!CreateCommandInstance("report")->Run({"-i", record_file, "-g", "-o", tmpfile.path})) {
40     return "";
41   }
42   std::string data;
43   if (!android::base::ReadFileToString(tmpfile.path, &data)) {
44     return "";
45   }
46   return data;
47 }
48 
49 // @CddTest = 6.1/C-0-2
TEST(merge_cmd,input_output_options)50 TEST(merge_cmd, input_output_options) {
51   // missing arguments
52   ASSERT_FALSE(MergeCmd()->Run({}));
53   // missing input files
54   std::string input_file = GetTestData("perf.data");
55   ASSERT_FALSE(MergeCmd()->Run({"-i", input_file}));
56   // missing output file
57   TemporaryFile tmpfile;
58   close(tmpfile.release());
59   ASSERT_FALSE(MergeCmd()->Run({"-o", tmpfile.path}));
60   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-o", tmpfile.path}));
61   // input files separated by comma
62   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file + "," + input_file, "-o", tmpfile.path}));
63   // input files in different -i options
64   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-i", input_file, "-o", tmpfile.path}));
65 }
66 
67 // @CddTest = 6.1/C-0-2
TEST(merge_cmd,merge_two_files)68 TEST(merge_cmd, merge_two_files) {
69   std::string input_file1 = GetTestData("perf_merge1.data");
70   std::string input_file2 = GetTestData("perf_merge2.data");
71 
72   std::string report = GetReport(input_file1);
73   ASSERT_NE(report.find("Samples: 27"), std::string::npos);
74   ASSERT_NE(report.find("malloc"), std::string::npos);
75   ASSERT_EQ(report.find("sleep_main"), std::string::npos);
76   ASSERT_NE(report.find("toybox_main"), std::string::npos);
77 
78   report = GetReport(input_file2);
79   ASSERT_NE(report.find("Samples: 31"), std::string::npos);
80   ASSERT_EQ(report.find("malloc"), std::string::npos);
81   ASSERT_NE(report.find("sleep_main"), std::string::npos);
82   ASSERT_NE(report.find("toybox_main"), std::string::npos);
83 
84   TemporaryFile tmpfile;
85   close(tmpfile.release());
86   ASSERT_TRUE(MergeCmd()->Run({"-i", input_file1 + "," + input_file2, "-o", tmpfile.path}));
87   report = GetReport(tmpfile.path);
88   // sum of sample counts in input files
89   ASSERT_NE(report.find("Samples: 58"), std::string::npos);
90   // union of symbols in input files
91   ASSERT_NE(report.find("malloc"), std::string::npos);
92   ASSERT_NE(report.find("sleep_main"), std::string::npos);
93   ASSERT_NE(report.find("toybox_main"), std::string::npos);
94 }
95