• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "form_constants.h"
21 #include "form_host_client.h"
22 #include "form_mgr.h"
23 #include "hilog_wrapper.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::AAFwk;
28 using namespace OHOS::AppExecFwk;
29 
30 namespace {
31 static const int32_t NUM_THREADS = 4;
32 
33 class FormManagerTest : public benchmark::Fixture {
34 public:
FormManagerTest()35     FormManagerTest()
36     {
37         Iterations(iterations);
38         Repetitions(repetitions);
39         ReportAggregatesOnly();
40     }
41 
42     ~FormManagerTest() override = default;
43 
SetUp(const::benchmark::State & state)44     void SetUp(const ::benchmark::State &state) override
45     {
46     }
47 
TearDown(const::benchmark::State & state)48     void TearDown(const ::benchmark::State &state) override
49     {
50     }
51 
52 protected:
53     const int32_t repetitions = 3;
54     const int32_t iterations = 1000;
55     // sleep 100ms
56     const int32_t usleepTime = 1000 * 100;
57     std::string bundleName = "ohos.samples.FormApplication";
58     std::string moduleName = "entry";
59 };
60 
BENCHMARK_F(FormManagerTest,GetAllFormsInfoTestCase)61 BENCHMARK_F(FormManagerTest, GetAllFormsInfoTestCase)(benchmark::State &state)
62 {
63     while (state.KeepRunning()) {
64         std::vector<FormInfo> formInfos {};
65         ErrCode errCode = FormMgr::GetInstance().GetAllFormsInfo(formInfos);
66         if (errCode != ERR_OK) {
67             HILOG_ERROR("%{public}s error, failed to GetAllFormsInfoTestCase, error code is %{public}d.", __func__,
68                 errCode);
69             state.SkipWithError("GetAllFormsInfoTestCase failed, return error.");
70         }
71         if (formInfos.empty()) {
72             HILOG_ERROR("%{public}s error, failed to GetAllFormsInfoTestCase, formInfos empty.", __func__);
73             state.SkipWithError("GetAllFormsInfoTestCase failed, formInfos empty.");
74         }
75     }
76 }
77 
BENCHMARK_F(FormManagerTest,GetFormsInfoByAppTestCase)78 BENCHMARK_F(FormManagerTest, GetFormsInfoByAppTestCase)(benchmark::State &state)
79 {
80     while (state.KeepRunning()) {
81         std::vector<FormInfo> formInfos {};
82         ErrCode errCode = FormMgr::GetInstance().GetFormsInfoByApp(bundleName, formInfos);
83         if (errCode != ERR_OK) {
84             HILOG_ERROR("%{public}s error, failed to GetFormsInfoByAppTestCase, error code is %{public}d.", __func__,
85                 errCode);
86             state.SkipWithError("GetFormsInfoByAppTestCase failed, return error.");
87         }
88         if (formInfos.empty()) {
89             HILOG_ERROR("%{public}s error, failed to GetFormsInfoByAppTestCase, formInfos empty.", __func__);
90             state.SkipWithError("GetFormsInfoByAppTestCase failed, formInfos empty.");
91         }
92     }
93 }
94 
BENCHMARK_F(FormManagerTest,GetFormsInfoByModuleTestCase)95 BENCHMARK_F(FormManagerTest, GetFormsInfoByModuleTestCase)(benchmark::State &state)
96 {
97     while (state.KeepRunning()) {
98         std::vector<FormInfo> formInfos {};
99         ErrCode errCode = FormMgr::GetInstance().GetFormsInfoByModule(bundleName, moduleName, formInfos);
100         if (errCode != ERR_OK) {
101             HILOG_ERROR("%{public}s error, failed to GetFormsInfoByModuleTestCase, error code is %{public}d.", __func__,
102                 errCode);
103             state.SkipWithError("GetFormsInfoByAppTestCase failed, return error.");
104         }
105         if (formInfos.empty()) {
106             HILOG_ERROR("%{public}s error, failed to GetFormsInfoByModuleTestCase, formInfos empty.", __func__);
107             state.SkipWithError("GetFormsInfoByAppTestCase failed, formInfos empty.");
108         }
109     }
110 }
111 
112 BENCHMARK_REGISTER_F(FormManagerTest, GetAllFormsInfoTestCase)->Threads(NUM_THREADS);
113 BENCHMARK_REGISTER_F(FormManagerTest, GetFormsInfoByAppTestCase)->Threads(NUM_THREADS);
114 BENCHMARK_REGISTER_F(FormManagerTest, GetFormsInfoByModuleTestCase)->Threads(NUM_THREADS);
115 
116 class FormManagerTestAddForm : public benchmark::Fixture {
117 public:
FormManagerTestAddForm()118     FormManagerTestAddForm()
119     {
120         Iterations(iterations);
121         Repetitions(repetitions);
122         ReportAggregatesOnly();
123     }
124 
125     ~FormManagerTestAddForm() override = default;
126 
SetUp(const::benchmark::State & state)127     void SetUp(const ::benchmark::State &state) override
128     {
129         int32_t numFormsDeleted = 0;
130         std::vector<int64_t> validFormIds {};
131         FormMgr::GetInstance().DeleteInvalidForms(validFormIds, formHostClient, numFormsDeleted);
132         std::string deviceId;
133         std::string bundleName = "ohos.samples.FormApplication";
134         std::string moduleName = "entry";
135         std::string abilityName = "FormAbility";
136         std::string formName = "widget";
137         int32_t dimension = 3;
138 
139         want.SetElementName(deviceId, bundleName, abilityName);
140         want.SetParam(Constants::PARAM_MODULE_NAME_KEY, moduleName);
141         want.SetParam(Constants::PARAM_FORM_NAME_KEY, formName);
142         want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, dimension);
143     }
144 
TearDown(const::benchmark::State & state)145     void TearDown(const ::benchmark::State &state) override
146     {
147         for (int64_t formId: formIds) {
148             FormMgr::GetInstance().DeleteForm(formId, formHostClient);
149         }
150     }
151 
152 protected:
153     const int32_t repetitions = 2;
154     const int32_t iterations = 8;
155     // sleep 100ms
156     const int32_t usleepTime = 1000 * 100;
157     Want want;
158     std::vector<int64_t> formIds {};
159     sptr<FormHostClient> formHostClient = FormHostClient::GetInstance();
160 };
161 
BENCHMARK_F(FormManagerTestAddForm,AddFormTestCase)162 BENCHMARK_F(FormManagerTestAddForm, AddFormTestCase)(benchmark::State &state)
163 {
164     while (state.KeepRunning()) {
165         FormJsInfo formInfo;
166         FormMgr::GetInstance().AddForm(0, want, formHostClient, formInfo);
167         formIds.push_back(formInfo.formId);
168     }
169 }
170 }
171 
172 // Run the benchmark
173 BENCHMARK_MAIN();
174