1 /*
2 * Copyright (c) 2021-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 <string>
18 #include <vector>
19 #include <cmath>
20 #include <cstdio>
21 #include <gtest/gtest.h>
22 #include <securec.h>
23 #include "hdf_base.h"
24 #include "osal_time.h"
25 #include "v1_1/ivibrator_interface.h"
26 #include "parameters.h"
27
28 using namespace OHOS::HDI::Vibrator::V1_1;
29 using namespace testing::ext;
30 using namespace std;
31
32 namespace {
33 uint32_t g_duration = 1;
34 uint32_t g_sleepTime1 = 1;
35 uint32_t g_sleepTime2 = 2;
36 int32_t g_intensity1 = 3;
37 int32_t g_frequency1 = 20;
38 std::string g_timeSequence = "haptic.clock.timer";
39 std::string g_builtIn = "haptic.default.effect";
40 std::string g_arbitraryStr = "arbitraryString";
41 const std::string DEVICETYPE_KEY = "const.product.devicetype";
42 const std::string DEVICETYPE_TYPE = "phone";
43 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
44
45 class vibratorBenchmarkTest : public benchmark::Fixture {
46 public:
47 void SetUp(const ::benchmark::State &state);
48 void TearDown(const ::benchmark::State &state);
49 };
50
SetUp(const::benchmark::State & state)51 void vibratorBenchmarkTest::SetUp(const ::benchmark::State &state)
52 {
53 g_vibratorInterface = IVibratorInterface::Get();
54 }
55
TearDown(const::benchmark::State & state)56 void vibratorBenchmarkTest::TearDown(const ::benchmark::State &state)
57 {
58 }
59
60 /**
61 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0010
62 * @tc.desc: Benchmarktest for interface StartOnce
63 * Controls this vibrator to perform a one-shot vibrator at a given duration.
64 * @tc.type: FUNC
65 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0010)66 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0010)(benchmark::State &st)
67 {
68 ASSERT_NE(nullptr, g_vibratorInterface);
69
70 int32_t startRet;
71 int32_t endRet;
72 for (auto _ : st) {
73 startRet = g_vibratorInterface->StartOnce(g_duration);
74 EXPECT_EQ(startRet, HDF_SUCCESS);
75
76 OsalMSleep(g_sleepTime1);
77
78 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
79 EXPECT_EQ(endRet, HDF_SUCCESS);
80 }
81 }
82
83 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0010)->
84 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
85
86 /**
87 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0020
88 * @tc.desc: Benchmarktest for interface Start
89 * Controls this Performing Time Series Vibrator Effects.
90 * @tc.type: FUNC
91 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0020)92 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0020)(benchmark::State &st)
93 {
94 ASSERT_NE(nullptr, g_vibratorInterface);
95
96 int32_t startRet;
97 int32_t endRet;
98 for (auto _ : st) {
99 startRet = g_vibratorInterface->Start(g_timeSequence);
100 EXPECT_EQ(startRet, HDF_SUCCESS);
101
102 OsalMSleep(g_sleepTime2);
103
104 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
105 EXPECT_EQ(endRet, HDF_SUCCESS);
106 }
107 }
108
109 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0020)->
110 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
111
112 /**
113 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0030
114 * @tc.desc: Benchmarktest for interface Stop
115 * Controls this Performing built-in Vibrator Effects.
116 * @tc.type: FUNC
117 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0030)118 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0030)(benchmark::State &st)
119 {
120 ASSERT_NE(nullptr, g_vibratorInterface);
121 int32_t startRet;
122 int32_t endRet;
123
124 for (auto _ : st) {
125 startRet = g_vibratorInterface->Start(g_builtIn);
126 EXPECT_EQ(startRet, HDF_SUCCESS);
127
128 OsalMSleep(g_sleepTime1);
129
130 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
131 EXPECT_EQ(endRet, HDF_SUCCESS);
132 }
133 }
134
135 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0030)->
136 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
137
138 /**
139 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0040
140 * @tc.desc: Benchmarktest for interface GetVibratorInfo.
141 * Controls this Performing Time Series Vibrator Effects.
142 * @tc.type: FUNC
143 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0040)144 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0040)(benchmark::State &state)
145 {
146 uint32_t majorVer;
147 uint32_t minorVer;
148 uint32_t ret;
149
150 ASSERT_NE(nullptr, g_vibratorInterface);
151
152 ret = g_vibratorInterface->GetVersion(majorVer, minorVer);
153 ASSERT_EQ(HDF_SUCCESS, ret);
154
155 ASSERT_LT(0, minorVer);
156 ASSERT_LT(0, majorVer);
157
158 std::vector<HdfVibratorInfo> info;
159
160 for (auto _ : state) {
161 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
162 EXPECT_EQ(startRet, HDF_SUCCESS);
163 }
164 }
165
166 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0040)->
167 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
168
169 /**
170 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0050
171 * @tc.desc: Benchmarktest for interface EnableVibratorModulation.
172 * Controls this Performing built-in Vibrator Effects.
173 * @tc.type: FUNC
174 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0050)175 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0050)(benchmark::State &state)
176 {
177 uint32_t majorVer;
178 uint32_t minorVer;
179 if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
180 return;
181 }
182
183 if (majorVer > 0 && minorVer <= 0) {
184 return;
185 }
186 ASSERT_NE(nullptr, g_vibratorInterface);
187
188 std::vector<HdfVibratorInfo> info;
189
190 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
191 EXPECT_EQ(startRet, HDF_SUCCESS);
192
193 for (auto _ : state) {
194 if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
195 printf("vibratot benchmarktest successed!\n\t");
196 EXPECT_GT(g_duration, 0);
197 EXPECT_GE(g_intensity1, info[0].intensityMinValue);
198 EXPECT_LE(g_intensity1, info[0].intensityMaxValue);
199 EXPECT_GE(g_frequency1, info[0].frequencyMinValue);
200 EXPECT_LE(g_frequency1, info[0].frequencyMaxValue);
201 printf("vibratot benchmark test successed!\n\t");
202 startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1);
203 EXPECT_EQ(startRet, HDF_SUCCESS);
204 OsalMSleep(g_sleepTime1);
205 startRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
206 EXPECT_EQ(startRet, HDF_SUCCESS);
207 }
208 }
209 }
210
211 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0050)->
212 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
213
214 /**
215 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0060
216 * @tc.desc: Start periodic vibration with custom composite effect
217 * @tc.type: FUNC
218 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0060)219 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0060)(benchmark::State &state)
220 {
221
222 PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
223 PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
224 PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
225 CompositeEffect effect1 = {
226 .primitiveEffect = primitiveEffect1
227 };
228 CompositeEffect effect2 = {
229 .primitiveEffect = primitiveEffect2
230 };
231 CompositeEffect effect3 = {
232 .primitiveEffect = primitiveEffect3
233 };
234 std::vector<CompositeEffect> vec;
235 vec.push_back(effect1);
236 vec.push_back(effect2);
237 vec.push_back(effect3);
238 HdfCompositeEffect effect;
239 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
240 effect.compositeEffects = vec;
241 int32_t ret;
242 for (auto _ : state) {
243 ret = g_vibratorInterface -> EnableCompositeEffect(effect);
244 }
245 OsalMSleep(2);
246 }
247 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0060)->
248 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
249
250 /**
251 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0070
252 * @tc.desc: Get effect information with the given effect type
253 * @tc.type: FUNC
254 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0070)255 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0070)(benchmark::State &state)
256 {
257 ASSERT_NE(nullptr, g_vibratorInterface);
258 HdfEffectInfo effectInfo;
259 int32_t ret;
260 for (auto _ : state) {
261 ret = g_vibratorInterface -> GetEffectInfo("haptic.pattern.type1", effectInfo);
262 }
263 EXPECT_EQ(HDF_SUCCESS, ret);
264 }
265 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0070)->
266 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
267
268 /**
269 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0080
270 * @tc.desc: Get vibration status.
271 * @tc.type: FUNC
272 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0080)273 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0080)(benchmark::State &state)
274 {
275 ASSERT_NE(nullptr, g_vibratorInterface);
276 bool stat {false};
277 for (auto _ : state) {
278 g_vibratorInterface -> IsVibratorRunning(stat);
279 }
280 }
281 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0080)->
282 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
283 }
284
285 BENCHMARK_MAIN();