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 "app_start_time_collection.h" 19 20 namespace OHOS::perftest { 21 using namespace std; 22 const string EVENT_DOMAIN = "PERFORMANCE"; 23 const string EVENT_NAME = "APP_START"; 24 const int32_t LISTEN_TIMEOUT = 10000; 25 const int32_t LISTEN_TIMEOUT_UNIT = 500; 26 const string RESPONSE_TIME_PARAM = "RESPONSE_LATENCY"; 27 const string COMPLETE_TIME_PARAM = "E2E_LATENCY"; 28 AppStartTimeCollection(PerfMetric perfMetric)29 AppStartTimeCollection::AppStartTimeCollection(PerfMetric perfMetric) : DataCollection(perfMetric) 30 { 31 isCollecting_ = false; 32 responseTime_ = INVALID_VALUE; 33 completeTime_ = INVALID_VALUE; 34 } 35 StartCollection(ApiCallErr & error)36 void AppStartTimeCollection::StartCollection(ApiCallErr &error) 37 { 38 appStartListener_ = std::make_shared<TimeListener>(bundleName_); 39 ListenerRule domainNameRule(EVENT_DOMAIN, EVENT_NAME, RuleType::WHOLE_WORD); 40 vector<ListenerRule> sysRules; 41 sysRules.push_back(domainNameRule); 42 auto ret = HiSysEventManager::AddListener(appStartListener_, sysRules); 43 if (ret != ZERO) { 44 error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Start app start measure failed"); 45 } 46 isCollecting_ = true; 47 } 48 StopCollectionAndGetResult(ApiCallErr & error)49 double AppStartTimeCollection::StopCollectionAndGetResult(ApiCallErr &error) 50 { 51 if (isCollecting_) { 52 int32_t sleepTime = ZERO; 53 do { 54 this_thread::sleep_for(chrono::milliseconds(LISTEN_TIMEOUT_UNIT)); 55 sleepTime += LISTEN_TIMEOUT_UNIT; 56 } while ((!appStartListener_->GetEvent()) && sleepTime < LISTEN_TIMEOUT); 57 HiSysEventManager::RemoveListener(appStartListener_); 58 if (!appStartListener_->GetEvent()) { 59 LOG_E("Get event of app start failed"); 60 isCollecting_ = false; 61 return INVALID_VALUE; 62 } 63 shared_ptr<HiviewDFX::HiSysEventRecord> eventRecord = appStartListener_->GetEvent(); 64 uint64_t responseRes; 65 if (eventRecord->GetParamValue(RESPONSE_TIME_PARAM, responseRes) != ZERO) { 66 LOG_E("Get the RESPONSE_LATENCY of HiSysEventRecord failed"); 67 isCollecting_ = false; 68 return responseTime_; 69 } 70 responseTime_ = static_cast<double>(responseRes); 71 uint64_t completeRes; 72 if (eventRecord->GetParamValue(COMPLETE_TIME_PARAM, completeRes) != ZERO) { 73 LOG_E("Get the E2E_LATENCY of HiSysEventRecord failed"); 74 isCollecting_ = false; 75 return completeTime_; 76 } 77 completeTime_ = static_cast<double>(completeRes); 78 isCollecting_ = false; 79 } 80 switch (perfMetric_) { 81 case APP_START_RESPONSE_TIME: 82 return responseTime_; 83 case APP_START_COMPLETE_TIME: 84 return completeTime_; 85 default: 86 return INVALID_VALUE; 87 } 88 return INVALID_VALUE; 89 } 90 }