• 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 <thread>
17 #include <chrono>
18 #include "page_switch_time_collection.h"
19 
20 namespace OHOS::perftest {
21     using namespace std;
22     const string EVENT_DOMAIN = "PERFORMANCE";
23     const string EVENT_NAME = "ABILITY_OR_PAGE_SWITCH";
24     const int32_t LISTEN_TIMEOUT = 10000;
25     const int32_t LISTEN_TIMEOUT_UNIT = 500;
26     const string COMPLETE_TIME_PARAM = "E2E_LATENCY";
27 
PageSwitchTimeCollection(PerfMetric perfMetric)28     PageSwitchTimeCollection::PageSwitchTimeCollection(PerfMetric perfMetric) : DataCollection(perfMetric)
29     {
30         completeTime_ = INVALID_VALUE;
31     }
32 
StartCollection(ApiCallErr & error)33     void PageSwitchTimeCollection::StartCollection(ApiCallErr &error)
34     {
35         pageSwitchListener_ = std::make_shared<TimeListener>(bundleName_);
36         ListenerRule domainNameRule(EVENT_DOMAIN, EVENT_NAME, RuleType::WHOLE_WORD);
37         vector<ListenerRule> sysRules;
38         sysRules.push_back(domainNameRule);
39         auto ret = HiSysEventManager::AddListener(pageSwitchListener_, sysRules);
40         if (ret != ZERO) {
41             error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Start page switch measure failed");
42         }
43     }
44 
StopCollectionAndGetResult(ApiCallErr & error)45     double PageSwitchTimeCollection::StopCollectionAndGetResult(ApiCallErr &error)
46     {
47         int32_t sleepTime = ZERO;
48         do {
49             this_thread::sleep_for(chrono::milliseconds(LISTEN_TIMEOUT_UNIT));
50             sleepTime += LISTEN_TIMEOUT_UNIT;
51         } while (!pageSwitchListener_->GetEvent() && sleepTime < LISTEN_TIMEOUT);
52         HiSysEventManager::RemoveListener(pageSwitchListener_);
53         if (!pageSwitchListener_->GetEvent()) {
54             LOG_E("Get event of page switch failed");
55             return INVALID_VALUE;
56         }
57         shared_ptr<HiviewDFX::HiSysEventRecord> eventRecord = pageSwitchListener_->GetEvent();
58         uint64_t res;
59         if (eventRecord->GetParamValue(COMPLETE_TIME_PARAM, res) != ZERO) {
60             LOG_E("Get the E2E_LATENCY of HiSysEventRecord failed");
61             return completeTime_;
62         }
63         completeTime_ = static_cast<double>(res);
64         return completeTime_;
65     }
66 }