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
TEST(merge_cmd,input_output_options)49 TEST(merge_cmd, input_output_options) {
50 // missing arguments
51 ASSERT_FALSE(MergeCmd()->Run({}));
52 // missing input files
53 std::string input_file = GetTestData("perf.data");
54 ASSERT_FALSE(MergeCmd()->Run({"-i", input_file}));
55 // missing output file
56 TemporaryFile tmpfile;
57 close(tmpfile.release());
58 ASSERT_FALSE(MergeCmd()->Run({"-o", tmpfile.path}));
59 ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-o", tmpfile.path}));
60 // input files separated by comma
61 ASSERT_TRUE(MergeCmd()->Run({"-i", input_file + "," + input_file, "-o", tmpfile.path}));
62 // input files in different -i options
63 ASSERT_TRUE(MergeCmd()->Run({"-i", input_file, "-i", input_file, "-o", tmpfile.path}));
64 }
65
TEST(merge_cmd,merge_two_files)66 TEST(merge_cmd, merge_two_files) {
67 std::string input_file1 = GetTestData("perf_merge1.data");
68 std::string input_file2 = GetTestData("perf_merge2.data");
69
70 std::string report = GetReport(input_file1);
71 ASSERT_NE(report.find("Samples: 27"), std::string::npos);
72 ASSERT_NE(report.find("malloc"), std::string::npos);
73 ASSERT_EQ(report.find("sleep_main"), std::string::npos);
74 ASSERT_NE(report.find("toybox_main"), std::string::npos);
75
76 report = GetReport(input_file2);
77 ASSERT_NE(report.find("Samples: 31"), std::string::npos);
78 ASSERT_EQ(report.find("malloc"), std::string::npos);
79 ASSERT_NE(report.find("sleep_main"), std::string::npos);
80 ASSERT_NE(report.find("toybox_main"), std::string::npos);
81
82 TemporaryFile tmpfile;
83 close(tmpfile.release());
84 ASSERT_TRUE(MergeCmd()->Run({"-i", input_file1 + "," + input_file2, "-o", tmpfile.path}));
85 report = GetReport(tmpfile.path);
86 // sum of sample counts in input files
87 ASSERT_NE(report.find("Samples: 58"), std::string::npos);
88 // union of symbols in input files
89 ASSERT_NE(report.find("malloc"), std::string::npos);
90 ASSERT_NE(report.find("sleep_main"), std::string::npos);
91 ASSERT_NE(report.find("toybox_main"), std::string::npos);
92 }
93