• 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 #include <fstream>
17 #include "perf_test_strategy.h"
18 #include "ipc_skeleton.h"
19 
20 namespace OHOS::perftest {
21     using namespace std;
22 
PerfTestStrategy(set<PerfMetric> metrics,string actionCodeRef,string resetCodeRef,string bundleName,int32_t iterations,int32_t timeout,ApiCallErr & error)23     PerfTestStrategy::PerfTestStrategy(set<PerfMetric> metrics, string actionCodeRef, string resetCodeRef,
24                                        string bundleName, int32_t iterations, int32_t timeout, ApiCallErr &error)
25     {
26         perfMetrics_ = metrics;
27         actionCodeRef_ = actionCodeRef;
28         resetCodeRef_ = resetCodeRef;
29         bundleName_ = bundleName;
30         iterations_ = iterations;
31         timeout_ = timeout;
32         GetBundleNameByPid(error);
33         CreateDataCollections();
34     }
35 
GetPerfMetrics()36     set<PerfMetric> PerfTestStrategy::GetPerfMetrics()
37     {
38         return perfMetrics_;
39     }
40 
GetActionCodeRef()41     string PerfTestStrategy::GetActionCodeRef()
42     {
43         return actionCodeRef_;
44     }
45 
GetResetCodeRef()46     string PerfTestStrategy::GetResetCodeRef()
47     {
48         return resetCodeRef_;
49     }
50 
GetBundleName()51     string PerfTestStrategy::GetBundleName()
52     {
53         return bundleName_;
54     }
55 
GetIterations()56     int32_t PerfTestStrategy::GetIterations()
57     {
58         return iterations_;
59     }
60 
GetTimeout()61     int32_t PerfTestStrategy::GetTimeout()
62     {
63         return timeout_;
64     }
65 
GetDataCollections()66     map<PerfMetric, shared_ptr<DataCollection>> PerfTestStrategy::GetDataCollections()
67     {
68         return dataCollections_;
69     }
70 
GetBundleNameByPid(ApiCallErr & error)71     void PerfTestStrategy::GetBundleNameByPid(ApiCallErr &error)
72     {
73         if (bundleName_ != "") {
74             return;
75         }
76         int32_t callingPid = IPCSkeleton::GetCallingPid();
77         string filePath = "/proc/" + to_string(callingPid) + "/cmdline";
78         ifstream inFile(filePath.c_str());
79         if (!inFile) {
80             LOG_E("Get bundleName by pid failed");
81             error = ApiCallErr(ERR_INITIALIZE_FAILED, "Get current application bundleName failed");
82             return;
83         }
84         getline(inFile, bundleName_);
85         inFile.close();
86         size_t endIndex = bundleName_.find_last_not_of('\0');
87         if (endIndex != std::string::npos) {
88             bundleName_.erase(endIndex + 1);
89         }
90         LOG_I("GetBundleNameByPid, pid: %{public}d, bundleName: %{public}s", callingPid, bundleName_.c_str());
91     }
92 
CreateDataCollections()93     void PerfTestStrategy::CreateDataCollections()
94     {
95         dataCollections_.clear();
96         for (auto perfMetric : perfMetrics_) {
97             if (g_dataCollectionMap.find(perfMetric) == g_dataCollectionMap.end()) {
98                 LOG_W("The DataCollection of PerfMetric::%{public}d is not existed", perfMetric);
99                 continue;
100             }
101             shared_ptr<DataCollection> dataCollection = g_dataCollectionMap.at(perfMetric)(perfMetric);
102             if (dataCollection == nullptr) {
103                 LOG_W("Get DataCollection of PerfMetric::%{public}d failed", perfMetric);
104                 continue;
105             }
106             dataCollection -> SetBundleName(bundleName_);
107             dataCollections_[perfMetric] = dataCollection;
108         }
109     }
110 } // namespace OHOS::perftest
111