1 /*
2 * Copyright (c) 2023 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 #include <fstream>
16 #include <iostream>
17 #include <regex>
18 #include <string>
19
20 #include "io_collector.h"
21
22 #include <gtest/gtest.h>
23
24 using namespace testing::ext;
25 using namespace OHOS::HiviewDFX;
26 using namespace OHOS::HiviewDFX::UCollectUtil;
27 using namespace OHOS::HiviewDFX::UCollect;
28
29 class IoCollectorTest : public testing::Test {
30 public:
SetUp()31 void SetUp() {};
TearDown()32 void TearDown() {};
SetUpTestCase()33 static void SetUpTestCase() {};
TearDownTestCase()34 static void TearDownTestCase() {};
35 };
36
37 namespace {
38 // %-13s\t%12s\t%12s\t%12s\t%12s\t%12s\t%12s\t%20s\t%20s
39 const std::regex ALL_PROC_IO_STATS1("^\\d{1,}\\s{1,}[\\w/\\.:-]{1,}\\s{1,}\\d{1,}(\\s{1,}\\d{1,}\\.\\d{2}){6}$");
40 const std::regex ALL_PROC_IO_STATS2("^[\\d\\s]{12}[\\s\\w/\\.:-]{13,}[\\s\\d]{13}([\\s\\d\\.]{13}){6}$");
41 // %-13s\t%20s\t%20s\t%20s\t%20s\t%12s\t%12s\t%12s
42 const std::regex DISK_STATS1("^\\w{1,}(\\s{1,}\\d{1,}\\.\\d{2}){4}(\\s{1,}\\d{1,}\\.\\d{4}){2}\\s{1,}\\d{1,}$");
43 const std::regex DISK_STATS2("^[\\w\\s]{13,}([\\s\\d\\.]{13}){4}([\\s\\d\\.]{13}){2}[\\s\\d]{13}$");
44 // %-15s\t%15s\t%15s\t%15s\t%15s
45 const std::regex EMMC_INFO1("^[\\w\\.]{1,}\\s{1,}\\w{1,}\\s{1,}[\\w\\s]{1,}\\w{1,}\\s{1,}\\d{1,}\\.\\d{2}$");
46 const std::regex EMMC_INFO2("^[\\w\\.\\s]{15,}([\\w\\s]{10}){3}[\\s\\d\\.]{12}$");
47 // %-12.2f\t%12.2f\t%12.2f\t%12.2f\t%12.2f\t%12.2f
48 const std::regex SYS_IO_STATS1("^\\d{1,}\\.\\d{2}(\\s{1,}\\d{1,}\\.\\d{2}){5}$");
49 const std::regex SYS_IO_STATS2("^[\\d\\s\\.]{12}([\\d\\s\\.]{13}){5}$");
50
CheckFormat(const std::string & fileName,const std::regex & reg1,const std::regex & reg2)51 bool CheckFormat(const std::string &fileName, const std::regex ®1, const std::regex ®2)
52 {
53 std::ifstream file;
54 file.open(fileName.c_str());
55 if (!file.is_open()) {
56 return false;
57 }
58 std::string line;
59 getline(file, line);
60 while (getline(file, line)) {
61 if (line.size() > 0 && line[line.size() - 1] == '\r') {
62 line.erase(line.size() - 1, 1);
63 }
64 if (line.size() == 0) {
65 continue;
66 }
67 if (!regex_match(line, reg1) || !regex_match(line, reg2)) {
68 file.close();
69 return false;
70 }
71 }
72 file.close();
73 return true;
74 }
75 }
76
77 /**
78 * @tc.name: IoCollectorTest001
79 * @tc.desc: used to test IoCollector.CollectProcessIo
80 * @tc.type: FUNC
81 */
82 HWTEST_F(IoCollectorTest, IoCollectorTest001, TestSize.Level1)
83 {
84 std::shared_ptr<IoCollector> collector = IoCollector::Create();
85 CollectResult<ProcessIo> data = collector->CollectProcessIo(1000);
86 std::cout << "collect process io result" << data.retCode << std::endl;
87 ASSERT_TRUE(data.retCode == UcError::SUCCESS);
88 }
89
90 /**
91 * @tc.name: IoCollectorTest002
92 * @tc.desc: used to test IoCollector.CollectRawDiskStats
93 * @tc.type: FUNC
94 */
95 HWTEST_F(IoCollectorTest, IoCollectorTest002, TestSize.Level1)
96 {
97 std::shared_ptr<IoCollector> collect = IoCollector::Create();
98 auto result = collect->CollectRawDiskStats();
99 std::cout << "collect raw disk stats result " << result.retCode << std::endl;
100 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
101 }
102
103 /**
104 * @tc.name: IoCollectorTest003
105 * @tc.desc: used to test IoCollector.CollectDiskStats
106 * @tc.type: FUNC
107 */
108 HWTEST_F(IoCollectorTest, IoCollectorTest003, TestSize.Level1)
109 {
110 std::shared_ptr<IoCollector> collect = IoCollector::Create();
__anond6c36c300202(const DiskStats &stats) 111 auto result = collect->CollectDiskStats([] (const DiskStats &stats) {
112 return false;
113 });
114 std::cout << "collect disk stats result " << result.retCode << std::endl;
115 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
116 }
117
118 /**
119 * @tc.name: IoCollectorTest004
120 * @tc.desc: used to test IoCollector.ExportDiskStats
121 * @tc.type: FUNC
122 */
123 HWTEST_F(IoCollectorTest, IoCollectorTest004, TestSize.Level1)
124 {
125 std::shared_ptr<IoCollector> collect = IoCollector::Create();
__anond6c36c300302(const DiskStats &stats) 126 auto result = collect->ExportDiskStats([] (const DiskStats &stats) {
127 return false;
128 });
129 std::cout << "export disk stats result " << result.retCode << std::endl;
130 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
131 bool flag = CheckFormat(result.data, DISK_STATS1, DISK_STATS2);
132 ASSERT_TRUE(flag);
133
134 sleep(3);
135 auto nextResult = collect->ExportDiskStats();
136 std::cout << "export disk stats nextResult " << nextResult.retCode << std::endl;
137 ASSERT_TRUE(nextResult.retCode == UcError::SUCCESS);
138 flag = CheckFormat(nextResult.data, DISK_STATS1, DISK_STATS2);
139 ASSERT_TRUE(flag);
140 }
141
142 /**
143 * @tc.name: IoCollectorTest005
144 * @tc.desc: used to test IoCollector.ExportDiskStats
145 * @tc.type: FUNC
146 */
147 HWTEST_F(IoCollectorTest, IoCollectorTest005, TestSize.Level1)
148 {
149 std::shared_ptr<IoCollector> collect = IoCollector::Create();
__anond6c36c300402(const DiskStats &stats) 150 auto result = collect->CollectDiskStats([] (const DiskStats &stats) {
151 return false;
152 }, true);
153 std::cout << "export disk stats result " << result.retCode << std::endl;
154 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
155
156 sleep(3);
157 auto nextResult = collect->ExportDiskStats();
158 std::cout << "export disk stats nextResult " << nextResult.retCode << std::endl;
159 ASSERT_TRUE(nextResult.retCode == UcError::SUCCESS);
160 bool flag = CheckFormat(nextResult.data, DISK_STATS1, DISK_STATS2);
161 ASSERT_TRUE(flag);
162 }
163
164 /**
165 * @tc.name: IoCollectorTest006
166 * @tc.desc: used to test IoCollector.CollectEMMCInfo
167 * @tc.type: FUNC
168 */
169 HWTEST_F(IoCollectorTest, IoCollectorTest006, TestSize.Level1)
170 {
171 std::shared_ptr<IoCollector> collect = IoCollector::Create();
172 auto result = collect->CollectEMMCInfo();
173 std::cout << "collect emmc info result " << result.retCode << std::endl;
174 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
175 }
176
177 /**
178 * @tc.name: IoCollectorTest007
179 * @tc.desc: used to test IoCollector.ExportEMMCInfo
180 * @tc.type: FUNC
181 */
182 HWTEST_F(IoCollectorTest, IoCollectorTest007, TestSize.Level1)
183 {
184 std::shared_ptr<IoCollector> collect = IoCollector::Create();
185 auto result = collect->ExportEMMCInfo();
186 std::cout << "export emmc info result " << result.retCode << std::endl;
187 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
188 bool flag = CheckFormat(result.data, EMMC_INFO1, EMMC_INFO2);
189 ASSERT_TRUE(flag);
190 }
191
192 /**
193 * @tc.name: IoCollectorTest008
194 * @tc.desc: used to test IoCollector.CollectAllProcIoStats
195 * @tc.type: FUNC
196 */
197 HWTEST_F(IoCollectorTest, IoCollectorTest008, TestSize.Level1)
198 {
199 std::shared_ptr<IoCollector> collect = IoCollector::Create();
200 auto result = collect->CollectAllProcIoStats();
201 std::cout << "collect all proc io stats result " << result.retCode << std::endl;
202 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
203 }
204
205 /**
206 * @tc.name: IoCollectorTest009
207 * @tc.desc: used to test IoCollector.ExportAllProcIoStats
208 * @tc.type: FUNC
209 */
210 HWTEST_F(IoCollectorTest, IoCollectorTest009, TestSize.Level1)
211 {
212 std::shared_ptr<IoCollector> collect = IoCollector::Create();
213 auto result = collect->ExportAllProcIoStats();
214 std::cout << "export all proc io stats result " << result.retCode << std::endl;
215 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
216 bool flag = CheckFormat(result.data, ALL_PROC_IO_STATS1, ALL_PROC_IO_STATS2);
217 ASSERT_TRUE(flag);
218 }
219
220 /**
221 * @tc.name: IoCollectorTest010
222 * @tc.desc: used to test IoCollector.ExportAllProcIoStats
223 * @tc.type: FUNC
224 */
225 HWTEST_F(IoCollectorTest, IoCollectorTest010, TestSize.Level1)
226 {
227 std::shared_ptr<IoCollector> collect = IoCollector::Create();
228 auto result = collect->ExportAllProcIoStats();
229 std::cout << "export all proc io stats result " << result.retCode << std::endl;
230 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
231 bool flag = CheckFormat(result.data, ALL_PROC_IO_STATS1, ALL_PROC_IO_STATS2);
232 ASSERT_TRUE(flag);
233
234 sleep(3);
235 auto nextResult = collect->ExportAllProcIoStats();
236 std::cout << "export all proc io stats nextResult " << nextResult.retCode << std::endl;
237 ASSERT_TRUE(nextResult.retCode == UcError::SUCCESS);
238 flag = CheckFormat(nextResult.data, ALL_PROC_IO_STATS1, ALL_PROC_IO_STATS2);
239 ASSERT_TRUE(flag);
240 }
241
242 /**
243 * @tc.name: IoCollectorTest011
244 * @tc.desc: used to test IoCollector.CollectSysIoStats
245 * @tc.type: FUNC
246 */
247 HWTEST_F(IoCollectorTest, IoCollectorTest011, TestSize.Level1)
248 {
249 std::shared_ptr<IoCollector> collect = IoCollector::Create();
250 auto result = collect->CollectSysIoStats();
251 std::cout << "collect sys io stats result " << result.retCode << std::endl;
252 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
253 }
254
255 /**
256 * @tc.name: IoCollectorTest012
257 * @tc.desc: used to test IoCollector.ExportSysIoStats
258 * @tc.type: FUNC
259 */
260 HWTEST_F(IoCollectorTest, IoCollectorTest012, TestSize.Level1)
261 {
262 std::shared_ptr<IoCollector> collect = IoCollector::Create();
263 auto result = collect->ExportSysIoStats();
264 std::cout << "export sys io stats result " << result.retCode << std::endl;
265 ASSERT_TRUE(result.retCode == UcError::SUCCESS);
266 bool flag = CheckFormat(result.data, SYS_IO_STATS1, SYS_IO_STATS2);
267 ASSERT_TRUE(flag);
268 }
269