1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "subcommand_dump_test.h"
17
18 #include <algorithm>
19 #include <chrono>
20 #include <cinttypes>
21 #include <sched.h>
22 #include <sstream>
23 #include <thread>
24
25 #include "command.h"
26 #include "debug_logger.h"
27 #include "utilities.h"
28
29 using namespace std::literals::chrono_literals;
30 using namespace testing::ext;
31 using namespace std;
32 using namespace OHOS::HiviewDFX;
33 namespace OHOS {
34 namespace Developtools {
35 namespace HiPerf {
36 class SubCommandDumpTest : public testing::Test {
37 public:
38 static void SetUpTestCase(void);
39 static void TearDownTestCase(void);
40 void SetUp();
41 void TearDown();
42
43 void TestDumpCommand(const std::string &option, bool expect = true) const;
44 };
45
SetUpTestCase()46 void SubCommandDumpTest::SetUpTestCase()
47 {
48 SubCommand::ClearSubCommands();
49 }
50
TearDownTestCase()51 void SubCommandDumpTest::TearDownTestCase() {}
52
SetUp()53 void SubCommandDumpTest::SetUp()
54 {
55 // clear the subCommands left from other UT
56 SubCommand::ClearSubCommands();
57 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
58 SubCommandDump::RegisterSubCommandDump();
59 ASSERT_EQ(SubCommand::GetSubCommands().size(), 1u);
60 }
61
TearDown()62 void SubCommandDumpTest::TearDown()
63 {
64 ASSERT_EQ(SubCommand::GetSubCommands().size(), 1u);
65 SubCommand::ClearSubCommands();
66 ASSERT_EQ(SubCommand::GetSubCommands().size(), 0u);
67 }
68
TestDumpCommand(const std::string & option,bool expect) const69 void SubCommandDumpTest::TestDumpCommand(const std::string &option, bool expect) const
70 {
71 StdoutRecord stdoutRecord;
72
73 std::string cmdString = "dump";
74 cmdString += " " + option + " ";
75
76 // it need load some symbols and much more log
77
78 ScopeDebugLevel tempLogLevel {LEVEL_DEBUG};
79
80 stdoutRecord.Start();
81 const auto startTime = chrono::steady_clock::now();
82 bool ret = Command::DispatchCommand(cmdString);
83 const auto costMs = std::chrono::duration_cast<std::chrono::milliseconds>(
84 chrono::steady_clock::now() - startTime);
85 std::string stringOut = stdoutRecord.Stop();
86
87 printf("command : %s(run %" PRId64 " ms) return %s(expect %s)\n", cmdString.c_str(),
88 (uint64_t)costMs.count(), ret ? "true" : "false", expect ? "true" : "false");
89 EXPECT_EQ(expect, ret);
90 if (expect) {
91 EXPECT_EQ(SubStringCount(stringOut, "HILOG/E"), 0u);
92 }
93 }
94
95 /**
96 * @tc.name:
97 * @tc.desc: record
98 * @tc.type: FUNC
99 */
100
101 HWTEST_F(SubCommandDumpTest, Test_LibReport_Success, TestSize.Level1)
102 {
103 StdoutRecord stdoutRecord;
104 stdoutRecord.Start();
105 std::string cmdString = "dump -i /data/test/resource/testdata/report/perf.data.libreport";
106 EXPECT_EQ(Command::DispatchCommand(cmdString), true);
107 std::string stringOut = stdoutRecord.Stop();
108 size_t symbolsCount = 39;
109 size_t buildIdCount = 32;
110 size_t sampleCount = 1000;
111 size_t featureCount = 10;
112
113 EXPECT_EQ(stringOut.find("magic: PERFILE2") != std::string::npos, true);
114 EXPECT_EQ(SubStringCount(stringOut, "fileid:"), symbolsCount);
115 EXPECT_EQ(SubStringCount(stringOut, "buildId:"), buildIdCount);
116 EXPECT_EQ(SubStringCount(stringOut, "record sample:"), sampleCount);
117 EXPECT_EQ(SubStringCount(stringOut, "feature:"), featureCount);
118 }
119
120 HWTEST_F(SubCommandDumpTest, DumpInputFilename1, TestSize.Level1)
121 {
122 TestDumpCommand("/data/test/resource/testdata/perf.data ", false);
123 }
124
125 HWTEST_F(SubCommandDumpTest, DumpInputFilename2, TestSize.Level1)
126 {
127 TestDumpCommand("-i /data/test/resource/testdata/perf.data ");
128 }
129
130 HWTEST_F(SubCommandDumpTest, DumpInputFilenamErr, TestSize.Level1)
131 {
132 TestDumpCommand("-i whatfile ", false);
133 }
134
135 HWTEST_F(SubCommandDumpTest, DumpHeaderAttrs, TestSize.Level1)
136 {
137 TestDumpCommand("-i /data/test/resource/testdata/perf.data --head ");
138 }
139
140 HWTEST_F(SubCommandDumpTest, DumpData, TestSize.Level1)
141 {
142 TestDumpCommand("-i /data/test/resource/testdata/perf.data -d ");
143 }
144
145 HWTEST_F(SubCommandDumpTest, DumpFeatures, TestSize.Level1)
146 {
147 TestDumpCommand("-i /data/test/resource/testdata/perf.data -f ");
148 }
149
150 HWTEST_F(SubCommandDumpTest, DumpSympath, TestSize.Level1)
151 {
152 TestDumpCommand("-i /data/test/resource/testdata/perf.data --sympath ./ ");
153 }
154
155 HWTEST_F(SubCommandDumpTest, DumpSympathErr, TestSize.Level1)
156 {
157 TestDumpCommand("-i /data/test/resource/testdata/perf.data --sympath where ", false);
158 }
159
160 HWTEST_F(SubCommandDumpTest, DumpExportUserdata0, TestSize.Level1)
161 {
162 TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 0");
163 }
164
165 HWTEST_F(SubCommandDumpTest, DumpExportUserdata1, TestSize.Level1)
166 {
167 TestDumpCommand("-i /data/test/resource/testdata/perf.data --export 1");
168 }
169
170 HWTEST_F(SubCommandDumpTest, DumpElffile, TestSize.Level1)
171 {
172 TestDumpCommand("--elf /data/test/resource/testdata/elf_test ");
173 }
174
175 HWTEST_F(SubCommandDumpTest, DumpElffileErr, TestSize.Level1)
176 {
177 TestDumpCommand("--elf whatfile ", false);
178 }
179
180 HWTEST_F(SubCommandDumpTest, DumpInputElfConflict, TestSize.Level1)
181 {
182 TestDumpCommand("perf.data --elf elffile ", false);
183 }
184
185 #if HAVE_PROTOBUF
186 HWTEST_F(SubCommandDumpTest, DumpProtofile, TestSize.Level1)
187 {
188 TestDumpCommand("--proto /data/test/resource/testdata/proto_test ");
189 }
190
191 HWTEST_F(SubCommandDumpTest, DumpProtofileErr, TestSize.Level1)
192 {
193 TestDumpCommand("--proto whatfile ", false);
194 }
195
196 HWTEST_F(SubCommandDumpTest, DumpInputProtoConflict, TestSize.Level1)
197 {
198 TestDumpCommand("perf.data --proto ptotofile ", false);
199 }
200
201 HWTEST_F(SubCommandDumpTest, DumpElfProtoConflict, TestSize.Level1)
202 {
203 TestDumpCommand("--elf elffile --proto ptotofile ", false);
204 }
205 #endif
206 } // namespace HiPerf
207 } // namespace Developtools
208 } // namespace OHOS
209