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 HWTEST_F(SubCommandDumpTest, DumpInputFilename, TestSize.Level1)
101 {
102 TestDumpCommand("/data/test/resource/testdata/perf.data ");
103 }
104
105 HWTEST_F(SubCommandDumpTest, DumpInputFilenamErr, TestSize.Level1)
106 {
107 TestDumpCommand("whatfile ", false);
108 }
109
110 HWTEST_F(SubCommandDumpTest, DumpHeaderAttrs, TestSize.Level1)
111 {
112 TestDumpCommand("/data/test/resource/testdata/perf.data --head ");
113 }
114
115 HWTEST_F(SubCommandDumpTest, DumpData, TestSize.Level1)
116 {
117 TestDumpCommand("/data/test/resource/testdata/perf.data -d ");
118 }
119
120 HWTEST_F(SubCommandDumpTest, DumpFeatures, TestSize.Level1)
121 {
122 TestDumpCommand("/data/test/resource/testdata/perf.data -f ");
123 }
124
125 HWTEST_F(SubCommandDumpTest, DumpSympath, TestSize.Level1)
126 {
127 TestDumpCommand("/data/test/resource/testdata/perf.data --sympath ./ ");
128 }
129
130 HWTEST_F(SubCommandDumpTest, DumpSympathErr, TestSize.Level1)
131 {
132 TestDumpCommand("/data/test/resource/testdata/perf.data --sympath where ", false);
133 }
134
135 HWTEST_F(SubCommandDumpTest, DumpExportUserdata0, TestSize.Level1)
136 {
137 TestDumpCommand("/data/test/resource/testdata/perf.data --export 0");
138 }
139
140 HWTEST_F(SubCommandDumpTest, DumpExportUserdata1, TestSize.Level1)
141 {
142 TestDumpCommand("/data/test/resource/testdata/perf.data --export 1");
143 }
144
145 HWTEST_F(SubCommandDumpTest, DumpElffile, TestSize.Level1)
146 {
147 TestDumpCommand("--elf /data/test/resource/testdata/elf_test ");
148 }
149
150 HWTEST_F(SubCommandDumpTest, DumpElffileErr, TestSize.Level1)
151 {
152 TestDumpCommand("--elf whatfile ", false);
153 }
154
155 HWTEST_F(SubCommandDumpTest, DumpInputElfConflict, TestSize.Level1)
156 {
157 TestDumpCommand("perf.data --elf elffile ", false);
158 }
159
160 #if HAVE_PROTOBUF
161 HWTEST_F(SubCommandDumpTest, DumpProtofile, TestSize.Level1)
162 {
163 TestDumpCommand("--proto /data/test/resource/testdata/proto_test ");
164 }
165
166 HWTEST_F(SubCommandDumpTest, DumpProtofileErr, TestSize.Level1)
167 {
168 TestDumpCommand("--proto whatfile ", false);
169 }
170
171 HWTEST_F(SubCommandDumpTest, DumpInputProtoConflict, TestSize.Level1)
172 {
173 TestDumpCommand("perf.data --proto ptotofile ", false);
174 }
175
176 HWTEST_F(SubCommandDumpTest, DumpElfProtoConflict, TestSize.Level1)
177 {
178 TestDumpCommand("--elf elffile --proto ptotofile ", false);
179 }
180 #endif
181 } // namespace HiPerf
182 } // namespace Developtools
183 } // namespace OHOS
184