• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef DATA_COLLECTION_H
17 #define DATA_COLLECTION_H
18 
19 #include "common_utils.h"
20 #include "frontend_api_defines.h"
21 #include "hisysevent_manager.h"
22 
23 namespace OHOS::perftest {
24     using namespace std;
25     using namespace OHOS::HiviewDFX;
26     static const double INITIAL_VALUE = 0.00;
27     static const double INVALID_VALUE = -1.00;
28 
29     enum PerfMetric: int32_t {
30         DURATION,
31         CPU_LOAD,
32         CPU_USAGE,
33         MEMORY_RSS,
34         MEMORY_PSS,
35         APP_START_RESPONSE_TIME,
36         APP_START_COMPLETE_TIME,
37         PAGE_SWITCH_COMPLETE_TIME,
38         LIST_SWIPE_FPS,
39         METRIC_COUNT
40     };
41 
42     class DataCollection {
43     public:
DataCollection(PerfMetric perfMetric)44         DataCollection(PerfMetric perfMetric) : perfMetric_(perfMetric) {};
45         DataCollection() = default;
46         virtual ~DataCollection() = default;
47         virtual void StartCollection(ApiCallErr &error) = 0;
48         virtual double StopCollectionAndGetResult(ApiCallErr &error) = 0;
49         void SetPid(int32_t pid);
50         void SetBundleName(string bundleName);
51         int32_t GetPidByBundleName(string bundleName);
52         bool IsProcessExist(int32_t pid);
53         bool ExecuteCommand(const string command, string& result);
54     protected:
55         int32_t pid_;
56         string bundleName_;
57         PerfMetric perfMetric_;
58     };
59 
60     class TimeListener : public HiviewDFX::HiSysEventListener {
61     public:
TimeListener(string bundleName)62         TimeListener(string bundleName) : bundleName_(bundleName) {}
63         void OnEvent(shared_ptr<HiSysEventRecord> record);
64         void OnServiceDied();
65         shared_ptr<HiviewDFX::HiSysEventRecord> GetEvent();
66     private:
67         string bundleName_;
68         shared_ptr<HiviewDFX::HiSysEventRecord> lastEvent_ = nullptr;
69     };
70 } // namespace OHOS::perftest
71 
72 #endif