1 /*
2 * Copyright (c) 2024 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 <gtest/gtest.h>
17 #include <iostream>
18 #include <regex>
19 #include <unordered_set>
20
21 #include "cpu_decorator.h"
22 #include "file_util.h"
23 #include "gpu_decorator.h"
24 #include "hiebpf_decorator.h"
25 #include "hilog_decorator.h"
26 #include "io_decorator.h"
27 #include "memory_decorator.h"
28 #include "network_decorator.h"
29 #include "trace_decorator.h"
30 #include "trace_manager.h"
31 #include "wm_decorator.h"
32 #ifdef HAS_HIPROFILER
33 #include "mem_profiler_decorator.h"
34 #include "native_memory_profiler_sa_client_manager.h"
35 #endif
36 #ifdef HAS_HIPERF
37 #include "perf_decorator.h"
38 #endif
39
40 using namespace testing::ext;
41 using namespace OHOS::HiviewDFX;
42 using namespace OHOS::HiviewDFX::UCollectUtil;
43 using namespace OHOS::Developtools::NativeDaemon;
44
45 namespace {
46 constexpr uint32_t TEST_LINE_NUM = 100;
47 constexpr int TEST_DURATION = 10;
48 constexpr int TEST_INTERVAL = 1;
49 TraceManager g_traceManager;
50 const std::vector<std::regex> REGEXS = {
51 std::regex("^Date:$"),
52 std::regex("^\\d{4}-\\d{2}-\\d{2}$"),
53 std::regex("API statistics:"),
54 std::regex("API TotalCall FailCall AvgLatency\\(us\\) MaxLatency\\(us\\) TotalTimeSpent\\(us\\)"),
55 // eg: MemProfilerCollector::Start-1 1 1 144708 144708 144708
56 std::regex("\\w{1,}::\\w{1,}(-\\d)?\\s\\d+\\s\\d+\\s\\d+\\s\\d+\\s\\d+"),
57 std::regex("Hitrace API detail statistics:"),
58 std::regex("Caller FailCall OverCall TotalCall AvgLatency\\(us\\) MaxLatency\\(us\\) TotalTimeSpent\\(us\\)"),
59 // eg: OTHER 0 0 1 127609 127609 127609
60 std::regex("\\w{1,}\\s\\d+\\s\\d+\\s\\d+\\s\\d+\\s\\d+\\s\\d+"),
61 std::regex("Hitrace Traffic statistics:"),
62 std::regex("Caller TraceFile TimeSpent\\(us\\) RawSize\\(b\\) UsedSize\\(b\\) TimeStamp\\(us\\)"),
63 // eg: OTHER /data/Other_trace_2024051200504@14036-90732232.sys 176129 25151 127609 1715446244066654
64 std::regex("\\w{1,}\\s.{1,}\\.(sys|zip)\\s\\d+\\s\\d+\\s\\d+\\s\\d{16}"),
65 std::regex("Hitrace Traffic Compress Ratio:"),
66 std::regex("^0.142800$")
67 };
68
69 std::unordered_set<std::string> COLLECTOR_NAMES = {
70 "CpuCollector", "GpuCollector", "HiebpfCollector", "HilogCollector",
71 "IoCollector", "MemoryCollector", "NetworkCollector", "TraceCollector", "WmCollector",
72 };
73
CallCollectorFuncs()74 void CallCollectorFuncs()
75 {
76 auto cpuCollector = CpuCollector::Create();
77 (void)cpuCollector->CollectSysCpuUsage();
78 auto gpuCollector = GpuCollector::Create();
79 (void)gpuCollector->CollectSysGpuLoad();
80 auto hiebpfCollector = HiebpfCollector::Create();
81 (void)hiebpfCollector->StartHiebpf(5, "com.ohos.launcher", "/data/local/tmp/ebpf.txt"); // 5 : test duration
82 auto hilogCollector = HilogCollector::Create();
83 (void)hilogCollector->CollectLastLog(getpid(), TEST_LINE_NUM);
84 auto ioCollector = IoCollector::Create();
85 (void)ioCollector->CollectRawDiskStats();
86 #ifdef HAS_HIPROFILER
87 auto memProfilerCollector = MemProfilerCollector::Create();
88 memProfilerCollector->Start(NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY,
89 0, TEST_DURATION, TEST_INTERVAL);
90 #endif
91 auto memCollector = MemoryCollector::Create();
92 (void)memCollector->CollectSysMemory();
93 auto networkCollector = NetworkCollector::Create();
94 (void)networkCollector->CollectRate();
95 #ifdef HAS_HIPERF
96 auto perfCollector = PerfCollector::Create();
97 (void)perfCollector->StartPerf("/data/local/tmp/");
98 #endif
99 auto traceCollector = TraceCollector::Create();
100 UCollect::TraceCaller caller = UCollect::TraceCaller::OTHER;
101 const std::vector<std::string> tagGroups = {"scene_performance"};
102 (void)g_traceManager.OpenSnapshotTrace(tagGroups);
103 CollectResult<std::vector<std::string>> resultDumpTrace = traceCollector->DumpTrace(caller);
104 (void)g_traceManager.CloseTrace();
105 (void)traceCollector->TraceOff();
106 auto wmCollector = WmCollector::Create();
107 (void)wmCollector->ExportWindowsInfo();
108 }
109
CallStatFuncs()110 void CallStatFuncs()
111 {
112 CpuDecorator::SaveStatCommonInfo();
113 GpuDecorator::SaveStatCommonInfo();
114 HiebpfDecorator::SaveStatCommonInfo();
115 HilogDecorator::SaveStatCommonInfo();
116 IoDecorator::SaveStatCommonInfo();
117 MemoryDecorator::SaveStatCommonInfo();
118 NetworkDecorator::SaveStatCommonInfo();
119 TraceDecorator::SaveStatCommonInfo();
120 #ifdef HAS_HIPROFILER
121 MemProfilerDecorator::SaveStatCommonInfo();
122 #endif
123 #ifdef HAS_HIPERF
124 PerfDecorator::SaveStatCommonInfo();
125 #endif
126 WmDecorator::SaveStatCommonInfo();
127 TraceDecorator::SaveStatSpecialInfo();
128 }
129
IsMatchAnyRegex(const std::string & line,const std::vector<std::regex> & regs)130 bool IsMatchAnyRegex(const std::string& line, const std::vector<std::regex>& regs)
131 {
132 return std::any_of(regs.begin(), regs.end(), [line](std::regex reg) {return regex_match(line, reg);});
133 }
134
RemoveCollectorNameIfMatched(const std::string & line,std::unordered_set<std::string> & collectorNames)135 void RemoveCollectorNameIfMatched(const std::string& line, std::unordered_set<std::string>& collectorNames)
136 {
137 for (const auto& name : collectorNames) {
138 if (strncmp(line.c_str(), name.c_str(), strlen(name.c_str())) == 0) {
139 collectorNames.erase(name);
140 return;
141 }
142 }
143 }
144
CheckContent(const std::string & fileName,const std::vector<std::regex> & regs,std::unordered_set<std::string> & collectorNames)145 bool CheckContent(const std::string& fileName, const std::vector<std::regex>& regs,
146 std::unordered_set<std::string>& collectorNames)
147 {
148 std::ifstream file;
149 file.open(fileName.c_str());
150 if (!file.is_open()) {
151 return false;
152 }
153 std::string line;
154 while (getline(file, line)) {
155 if (line.size() > 0 && line[line.size() - 1] == '\r') {
156 line.erase(line.size() - 1, 1);
157 }
158 if (line.size() == 0) {
159 continue;
160 }
161 if (!IsMatchAnyRegex(line, regs)) {
162 file.close();
163 std::cout << "line:" << line << " not match" << std::endl;
164 return false;
165 }
166 RemoveCollectorNameIfMatched(line, collectorNames);
167 }
168 file.close();
169 return collectorNames.empty() ? true : false;
170 }
171 }
172
173 class DecoratorTest : public testing::Test {
174 public:
SetUp()175 void SetUp() {};
TearDown()176 void TearDown() {};
SetUpTestCase()177 static void SetUpTestCase()
178 {
179 #ifdef HAS_HIPROFILER
180 COLLECTOR_NAMES.insert("MemProfilerCollector");
181 #endif
182 #ifdef HAS_HIPERF
183 COLLECTOR_NAMES.insert("PerfCollector");
184 #endif
185 };
TearDownTestCase()186 static void TearDownTestCase() {};
187 };
188
189 /**
190 * @tc.name: DecoratorTest001
191 * @tc.desc: used to test ucollection stat
192 * @tc.type: FUNC
193 */
194 HWTEST_F(DecoratorTest, DecoratorTest001, TestSize.Level1)
195 {
196 CallCollectorFuncs();
197 CallStatFuncs();
198 bool res = CheckContent(UC_STAT_LOG_PATH, REGEXS, COLLECTOR_NAMES);
199 ASSERT_TRUE(res);
200 if (FileUtil::FileExists(UC_STAT_LOG_PATH)) {
201 FileUtil::RemoveFile(UC_STAT_LOG_PATH);
202 }
203 }
204