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 #ifdef HAS_HIPERF
16 #include <iostream>
17 
18 #include "perf_collector.h"
19 #include "file_util.h"
20 #include "time_util.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 PerfCollectorTest : 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 /**
38  * @tc.name: PerfCollectorTest001
39  * @tc.desc: used to test PerfCollector.StartPerf
40  * @tc.type: FUNC
41 */
42 HWTEST_F(PerfCollectorTest, PerfCollectorTest001, TestSize.Level1)
43 {
44     std::shared_ptr<UCollectUtil::PerfCollector> perfCollector = UCollectUtil::PerfCollector::Create();
45     vector<pid_t> selectPids = {getpid()};
46     std::string filedir = "/data/local/tmp/";
47     std::string filename = "hiperf-";
48     filename += TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
49     "%Y%m%d%H%M%S");
50     filename += ".data";
51     perfCollector->SetOutputFilename(filename);
52     perfCollector->SetSelectPids(selectPids);
53     perfCollector->SetTimeStopSec(3);
54     CollectResult<bool> data = perfCollector->StartPerf(filedir);
55     std::cout << "collect perf data result" << data.retCode << std::endl;
56     ASSERT_TRUE(data.retCode == UcError::SUCCESS);
57 }
58 
59 /**
60  * @tc.name: PerfCollectorTest002
61  * @tc.desc: used to test PerfCollector.StartPerf
62  * @tc.type: FUNC
63 */
64 HWTEST_F(PerfCollectorTest, PerfCollectorTest002, TestSize.Level1)
65 {
66     std::shared_ptr<UCollectUtil::PerfCollector> perfCollector = UCollectUtil::PerfCollector::Create();
67     vector<pid_t> selectPids = {getpid()};
68     std::string filedir = "/data/local/tmp/";
69     std::string filename = "hiperf-";
70     filename += TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
71     "%Y%m%d%H%M%S");
72     filename += ".data";
73     std::string filepath = filedir + filename;
74     perfCollector->SetOutputFilename(filename);
75     perfCollector->SetSelectPids(selectPids);
76     perfCollector->SetTimeStopSec(3);
77     CollectResult<bool> data = perfCollector->StartPerf(filedir);
78     std::cout << "collect perf data result" << data.retCode << std::endl;
79     ASSERT_EQ(FileUtil::FileExists(filepath), true);
80 }
81 
82 /**
83  * @tc.name: PerfCollectorTest003
84  * @tc.desc: used to test PerfCollector.SetTargetSystemWide
85  * @tc.type: FUNC
86 */
87 HWTEST_F(PerfCollectorTest, PerfCollectorTest003, TestSize.Level1)
88 {
89     std::shared_ptr<UCollectUtil::PerfCollector> perfCollector = UCollectUtil::PerfCollector::Create();
90     vector<pid_t> selectPids = {getpid()};
91     std::string filedir = "/data/local/tmp/";
92     std::string filename = "hiperf-";
93     filename += TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
94     "%Y%m%d%H%M%S");
95     filename += ".data";
96     std::string filepath = filedir + filename;
97     perfCollector->SetOutputFilename(filename);
98     perfCollector->SetTargetSystemWide(true);
99     perfCollector->SetCallGraph("fp");
100     std::vector<std::string> selectEvents = {"hw-cpu-cycles", "hw-instructions"};
101     perfCollector->SetSelectEvents(selectEvents);
102     CollectResult<bool> data = perfCollector->Prepare(filedir);
103     ASSERT_TRUE(data.retCode == UcError::SUCCESS);
104     TimeUtil::Sleep(1);
105     data = perfCollector->StartRun();
106     ASSERT_TRUE(data.retCode == UcError::SUCCESS);
107     TimeUtil::Sleep(1);
108     data = perfCollector->Stop();
109     ASSERT_TRUE(data.retCode == UcError::SUCCESS);
110     std::cout << "collect perf data result" << data.retCode << std::endl;
111     ASSERT_EQ(FileUtil::FileExists(filepath), true);
112 }
113 
114 /**
115  * @tc.name: PerfCollectorTest004
116  * @tc.desc: used to test PerfCollector.SetCpuPercent
117  * @tc.type: FUNC
118 */
119 HWTEST_F(PerfCollectorTest, PerfCollectorTest004, TestSize.Level1)
120 {
121     std::shared_ptr<UCollectUtil::PerfCollector> perfCollector = UCollectUtil::PerfCollector::Create();
122     vector<pid_t> selectPids = {getpid()};
123     std::string filedir = "/data/local/tmp/";
124     std::string filename = "hiperf-";
125     filename += TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
126     "%Y%m%d%H%M%S");
127     filename += ".data";
128     std::string filepath = filedir + filename;
129     perfCollector->SetOutputFilename(filename);
130     perfCollector->SetSelectPids(selectPids);
131     perfCollector->SetFrequency(100);
132     perfCollector->SetTimeStopSec(2);
133     perfCollector->SetCpuPercent(100);
134     CollectResult<bool> data = perfCollector->StartPerf(filedir);
135     std::cout << "collect perf data result" << data.retCode << std::endl;
136     ASSERT_EQ(FileUtil::FileExists(filepath), true);
137 }
138 
139 /**
140  * @tc.name: PerfCollectorTest005
141  * @tc.desc: used to test PerfCollector.SetReport
142  * @tc.type: FUNC
143 */
144 HWTEST_F(PerfCollectorTest, PerfCollectorTest005, TestSize.Level1)
145 {
146     std::shared_ptr<UCollectUtil::PerfCollector> perfCollector = UCollectUtil::PerfCollector::Create();
147     vector<pid_t> selectPids = {getpid()};
148     std::string filedir = "/data/local/tmp/";
149     std::string filename = "hiperf-";
150     filename += TimeUtil::TimestampFormatToDate(TimeUtil::GetMilliseconds() / TimeUtil::SEC_TO_MILLISEC,
151     "%Y%m%d%H%M%S");
152     filename += ".data";
153     std::string filepath = filedir + filename;
154     perfCollector->SetOutputFilename(filename);
155     perfCollector->SetSelectPids(selectPids);
156     perfCollector->SetTimeStopSec(3);
157     perfCollector->SetReport(true);
158     CollectResult<bool> data = perfCollector->StartPerf(filedir);
159     std::cout << "collect perf data result" << data.retCode << std::endl;
160     ASSERT_EQ(FileUtil::FileExists(filepath), true);
161 }
162 #endif // HAS_HIPERF
163