1 /*
2 * Copyright (c) 2022 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 <benchmark/benchmark.h>
17 #include <unistd.h>
18 #include <vector>
19
20 #include "ability_manager_client.h"
21 #include "hilog_wrapper.h"
22
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::AAFwk;
26
27 namespace {
28 class AbilityManagerTest : public benchmark::Fixture {
29 public:
AbilityManagerTest()30 AbilityManagerTest()
31 {
32 Iterations(iterations);
33 Repetitions(repetitions);
34 ReportAggregatesOnly();
35 }
36
37 ~AbilityManagerTest() override = default;
38
SetUp(const::benchmark::State & state)39 void SetUp(const ::benchmark::State &state) override
40 {
41 AbilityManagerClient::GetInstance()->CleanAllMissions();
42 usleep(usleepTime);
43 }
44
TearDown(const::benchmark::State & state)45 void TearDown(const ::benchmark::State &state) override
46 {
47 AbilityManagerClient::GetInstance()->CleanAllMissions();
48 usleep(usleepTime);
49 }
50
51 protected:
52 const int32_t repetitions = 3;
53 const int32_t iterations = 1000;
54 const int32_t upperLimit = 32;
55 // sleep 100ms
56 const int32_t usleepTime = 1000 * 100;
57 };
58
BENCHMARK_F(AbilityManagerTest,GetProcessRunningInfosTestCase)59 BENCHMARK_F(AbilityManagerTest, GetProcessRunningInfosTestCase)(
60 benchmark::State &state)
61 {
62 while (state.KeepRunning()) {
63 std::vector<AppExecFwk::RunningProcessInfo> info {};
64 ErrCode errCode = AbilityManagerClient::GetInstance()->GetProcessRunningInfos(info);
65 if (errCode != ERR_OK) {
66 state.SkipWithError("GetProcessRunningInfosTestCase failed.");
67 }
68 }
69 }
70
BENCHMARK_F(AbilityManagerTest,GetAbilityRunningInfosTestCase)71 BENCHMARK_F(AbilityManagerTest, GetAbilityRunningInfosTestCase)(
72 benchmark::State &state)
73 {
74 while (state.KeepRunning()) {
75 std::vector<AbilityRunningInfo> info {};
76 ErrCode errCode = AbilityManagerClient::GetInstance()->GetAbilityRunningInfos(info);
77 if (errCode != ERR_OK) {
78 state.SkipWithError("GetAbilityRunningInfosTestCase failed.");
79 }
80 }
81 }
82
BENCHMARK_F(AbilityManagerTest,GetExtensionRunningInfosTestCase)83 BENCHMARK_F(AbilityManagerTest, GetExtensionRunningInfosTestCase)(
84 benchmark::State &state)
85 {
86 while (state.KeepRunning()) {
87 std::vector<ExtensionRunningInfo> info {};
88 ErrCode errCode = AbilityManagerClient::GetInstance()->GetExtensionRunningInfos(upperLimit, info);
89 if (errCode != ERR_OK) {
90 state.SkipWithError("GetExtensionRunningInfosTestCase failed.");
91 }
92 }
93 }
94 }
95
96 // Run the benchmark
97 BENCHMARK_MAIN();
98