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 <benchmark/benchmark.h>
17 #include <cmath>
18 #include <cstdio>
19 #include <gtest/gtest.h>
20 #include <securec.h>
21 #include <string>
22 #include <vector>
23 #include "hdf_base.h"
24 #include "osal_time.h"
25 #include "v2_0/ivibrator_interface.h"
26
27 using namespace OHOS::HDI::Vibrator;
28 using namespace OHOS::HDI::Vibrator::V2_0;
29 using namespace testing::ext;
30 using namespace std;
31
32 namespace {
33 constexpr int32_t ITERATION_FREQUENCY = 100;
34 constexpr int32_t REPETITION_FREQUENCY = 3;
35 uint32_t g_duration = 100;
36 int32_t g_intensity1 = 30;
37 int32_t g_frequency1 = 200;
38 uint32_t g_sleepTime1 = 200;
39 V2_0::HapticPaket g_hapticPaket = {434, 1, {{V2_0::TRANSIENT, 0, 149, 100, 50, 0, 4,
40 {{0, 0, 0}, {1, 1, 0}, {32, 1, -39}, {149, 0, -39}}}}};
41 V2_0::VibratorPackage g_vibPackage = {434, 149, {{434, 1, {{V2_0::TRANSIENT, 0, 149, 100, 50, 0, 4,
42 {{0, 0, 0}, {1, 1, 0}, {32, 1, -39}, {149, 0, -39}}}}}}};
43 int32_t g_sessionId = 1;
44 std::string g_timeSequence = "haptic.clock.timer";
45 std::string g_builtIn = "haptic.default.effect";
46 sptr<IVibratorInterface> g_vibratorInterface = nullptr;
47 std::vector<HdfVibratorInfo> g_vibratorInfo;
48
49 class VibratorBenchmarkTest : public benchmark::Fixture {
50 public:
51 void SetUp(const ::benchmark::State &state);
52 void TearDown(const ::benchmark::State &state);
53 };
54
SetUp(const::benchmark::State & state)55 void VibratorBenchmarkTest::SetUp(const ::benchmark::State &state)
56 {
57 g_vibratorInterface = IVibratorInterface::Get();
58 if (g_vibratorInterface == nullptr) {
59 printf("g_vibratorInterface is nullptr");
60 GTEST_SKIP() << "g_vibratorInterface is nullptr" << std::endl;
61 return;
62 }
63 g_vibratorInterface->GetVibratorInfo(g_vibratorInfo);
64 }
65
TearDown(const::benchmark::State & state)66 void VibratorBenchmarkTest::TearDown(const ::benchmark::State &state)
67 {
68 }
69
70 /**
71 * @tc.name: DriverSystem_VibratorBenchmark_StartOnce
72 * @tc.desc: Benchmarktest for interface StartOnce
73 * Controls this vibrator to perform a one-shot vibrator at a given duration
74 * @tc.type: FUNC
75 */
BENCHMARK_F(VibratorBenchmarkTest,StartOnce)76 BENCHMARK_F(VibratorBenchmarkTest, StartOnce)(benchmark::State &state)
77 {
78 ASSERT_NE(nullptr, g_vibratorInterface);
79
80 int32_t startRet;
81 int32_t endRet;
82 for (auto _ : state) {
83 for (auto it : g_vibratorInfo) {
84 startRet = g_vibratorInterface->StartOnce({it.deviceId, it.vibratorId}, g_duration);
85 EXPECT_EQ(startRet, HDF_SUCCESS);
86
87 endRet = g_vibratorInterface->Stop({it.deviceId, it.vibratorId}, HDF_VIBRATOR_MODE_ONCE);
88 EXPECT_EQ(endRet, HDF_SUCCESS);
89 }
90 }
91 }
92
93 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, StartOnce)->
94 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
95
96 /**
97 * @tc.name: DriverSystem_VibratorBenchmark_Start
98 * @tc.desc: Benchmarktest for interface Start
99 * Controls this Performing Time Series Vibrator Effects
100 * @tc.type: FUNC
101 */
BENCHMARK_F(VibratorBenchmarkTest,Start)102 BENCHMARK_F(VibratorBenchmarkTest, Start)(benchmark::State &state)
103 {
104 ASSERT_NE(nullptr, g_vibratorInterface);
105
106 int32_t startRet;
107 int32_t endRet;
108 for (auto _ : state) {
109 for (auto it : g_vibratorInfo) {
110 HdfEffectInfo effectInfo;
111 g_vibratorInterface->GetEffectInfo({it.deviceId, it.vibratorId}, g_timeSequence, effectInfo);
112 if (effectInfo.isSupportEffect == true) {
113 startRet = g_vibratorInterface->Start({it.deviceId, it.vibratorId}, g_timeSequence);
114 EXPECT_EQ(startRet, HDF_SUCCESS);
115
116 endRet = g_vibratorInterface->Stop({it.deviceId, it.vibratorId}, HDF_VIBRATOR_MODE_PRESET);
117 EXPECT_EQ(endRet, HDF_SUCCESS);
118 }
119 }
120 }
121 }
122
123 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, Start)->
124 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
125
126 /**
127 * @tc.name: DriverSystem_VibratorBenchmark_Stop
128 * @tc.desc: Benchmarktest for interface Stop
129 * Controls this Performing built-in Vibrator Effects
130 * @tc.type: FUNC
131 */
BENCHMARK_F(VibratorBenchmarkTest,Stop)132 BENCHMARK_F(VibratorBenchmarkTest, Stop)(benchmark::State &state)
133 {
134 ASSERT_NE(nullptr, g_vibratorInterface);
135 int32_t startRet;
136 int32_t endRet;
137
138 for (auto _ : state) {
139 for (auto it : g_vibratorInfo) {
140 HdfEffectInfo effectInfo;
141 g_vibratorInterface->GetEffectInfo({it.deviceId, it.vibratorId}, g_builtIn, effectInfo);
142 if (effectInfo.isSupportEffect == true) {
143 startRet = g_vibratorInterface->Start({it.deviceId, it.vibratorId}, g_builtIn);
144 EXPECT_EQ(startRet, HDF_SUCCESS);
145
146 endRet = g_vibratorInterface->Stop({it.deviceId, it.vibratorId}, HDF_VIBRATOR_MODE_PRESET);
147 EXPECT_EQ(endRet, HDF_SUCCESS);
148 }
149 }
150 }
151 }
152
153 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, Stop)->
154 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
155
156 /**
157 * @tc.name: DriverSystem_VibratorBenchmark_GetVibratorInfo
158 * @tc.desc: Benchmarktest for interface GetVibratorInfo
159 * Controls this Performing Time Series Vibrator Effects
160 * @tc.type: FUNC
161 */
BENCHMARK_F(VibratorBenchmarkTest,GetVibratorInfo)162 BENCHMARK_F(VibratorBenchmarkTest, GetVibratorInfo)(benchmark::State &state)
163 {
164 ASSERT_NE(nullptr, g_vibratorInterface);
165
166 std::vector<HdfVibratorInfo> info;
167
168 for (auto _ : state) {
169 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
170 EXPECT_EQ(startRet, HDF_SUCCESS);
171 }
172 }
173
174 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, GetVibratorInfo)->
175 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
176
177 /**
178 * @tc.name: DriverSystem_VibratorBenchmark_EnableVibratorModulation
179 * @tc.desc: Benchmarktest for interface EnableVibratorModulation
180 * Controls this Performing built-in Vibrator Effects
181 * @tc.type: FUNC
182 */
BENCHMARK_F(VibratorBenchmarkTest,EnableVibratorModulation)183 BENCHMARK_F(VibratorBenchmarkTest, EnableVibratorModulation)(benchmark::State &state)
184 {
185 ASSERT_NE(nullptr, g_vibratorInterface);
186
187 std::vector<HdfVibratorInfo> info;
188
189 int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
190 EXPECT_EQ(startRet, HDF_SUCCESS);
191
192 for (auto _ : state) {
193 for (auto it : g_vibratorInfo) {
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({it.deviceId, it.vibratorId}, g_duration,
203 g_intensity1, g_frequency1);
204 EXPECT_EQ(startRet, HDF_SUCCESS);
205 OsalMSleep(g_sleepTime1);
206 startRet = g_vibratorInterface->Stop({it.deviceId, it.vibratorId}, HDF_VIBRATOR_MODE_ONCE);
207 EXPECT_EQ(startRet, HDF_SUCCESS);
208 }
209 }
210 }
211 }
212
213 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, EnableVibratorModulation)->
214 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
215
216 /**
217 * @tc.name: SUB_DriverSystem_VibratorBenchmark_EnableCompositeEffect
218 * @tc.desc: Start periodic vibration with custom composite effect
219 * @tc.type: FUNC
220 */
BENCHMARK_F(VibratorBenchmarkTest,EnableCompositeEffect)221 BENCHMARK_F(VibratorBenchmarkTest, EnableCompositeEffect)(benchmark::State &state)
222 {
223 ASSERT_NE(nullptr, g_vibratorInterface);
224
225 for (auto _ : state) {
226 PrimitiveEffect primitiveEffect1 {0, 60007, 0};
227 PrimitiveEffect primitiveEffect2 {1000, 60007, 0};
228 PrimitiveEffect primitiveEffect3 {1000, 60007, 0};
229 CompositeEffect effect1 = {
230 .primitiveEffect = primitiveEffect1
231 };
232 CompositeEffect effect2 = {
233 .primitiveEffect = primitiveEffect2
234 };
235 CompositeEffect effect3 = {
236 .primitiveEffect = primitiveEffect3
237 };
238 std::vector<CompositeEffect> vec;
239 vec.push_back(effect1);
240 vec.push_back(effect2);
241 vec.push_back(effect3);
242 HdfCompositeEffect effect;
243 effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
244 effect.compositeEffects = vec;
245 int32_t ret;
246 for (auto it : g_vibratorInfo) {
247 HapticCapacity hapticCapacity;
248 ret = g_vibratorInterface->GetHapticCapacity({it.deviceId, it.vibratorId}, hapticCapacity);
249 EXPECT_EQ(ret, HDF_SUCCESS);
250 if (hapticCapacity.isSupportPresetMapping) {
251 ret = g_vibratorInterface->EnableCompositeEffect({it.deviceId, it.vibratorId}, effect);
252 EXPECT_EQ(HDF_SUCCESS, ret);
253 }
254 }
255 EXPECT_EQ(HDF_SUCCESS, ret);
256 }
257 OsalMSleep(2);
258 }
259
260 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, EnableCompositeEffect)->
261 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
262
263 /**
264 * @tc.name: SUB_DriverSystem_VibratorBenchmark_GetEffectInfo
265 * @tc.desc: Get effect information with the given effect type
266 * @tc.type: FUNC
267 */
BENCHMARK_F(VibratorBenchmarkTest,GetEffectInfo)268 BENCHMARK_F(VibratorBenchmarkTest, GetEffectInfo)(benchmark::State &state)
269 {
270 ASSERT_NE(nullptr, g_vibratorInterface);
271 HdfEffectInfo effectInfo;
272 int32_t ret;
273 for (auto _ : state) {
274 for (auto it : g_vibratorInfo) {
275 ret = g_vibratorInterface->GetEffectInfo({it.deviceId, it.vibratorId}, "haptic.pattern.type1", effectInfo);
276 }
277 }
278 EXPECT_EQ(HDF_SUCCESS, ret);
279 }
280
281 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, GetEffectInfo)->
282 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
283
284 /**
285 * @tc.name: SUB_DriverSystem_VibratorBenchmark_IsVibratorRunning
286 * @tc.desc: Get vibration status.
287 * @tc.type: FUNC
288 */
BENCHMARK_F(VibratorBenchmarkTest,IsVibratorRunning)289 BENCHMARK_F(VibratorBenchmarkTest, IsVibratorRunning)(benchmark::State &state)
290 {
291 ASSERT_NE(nullptr, g_vibratorInterface);
292 bool stat {false};
293 int32_t ret;
294 for (auto _ : state) {
295 for (auto it : g_vibratorInfo) {
296 ret = g_vibratorInterface->IsVibratorRunning({it.deviceId, it.vibratorId}, stat);
297 }
298 }
299 EXPECT_EQ(HDF_SUCCESS, ret);
300 }
301
302 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, IsVibratorRunning)->
303 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
304 }
305
306 BENCHMARK_MAIN();
307
308 /**
309 * @tc.name: DriverSystem_VibratorBenchmark_PlayPatternBySessionId
310 * @tc.desc: Benchmarktest for interface PlayPatternBySessionId
311 * Control vibrator perform and stop by sessionID
312 * @tc.type: FUNC
313 */
BENCHMARK_F(VibratorBenchmarkTest,PlayPatternBySessionId)314 BENCHMARK_F(VibratorBenchmarkTest, PlayPatternBySessionId)(benchmark::State &state)
315 {
316 ASSERT_NE(nullptr, g_vibratorInterface);
317
318 int32_t startRet = 0;
319 int32_t endRet = 0;
320 for (auto _ : state) {
321 startRet = g_vibratorInterface->PlayPatternBySessionId(
322 {-1, 1}, g_sessionId, g_hapticPaket);
323 EXPECT_EQ(startRet, HDF_SUCCESS);
324
325 endRet = g_vibratorInterface->StopVibrateBySessionId({-1, 1}, g_sessionId);
326 EXPECT_EQ(endRet, HDF_SUCCESS);
327 }
328 }
329
330 /**
331 * @tc.name: DriverSystem_VibratorBenchmark_PlayPackageBySession
332 * @tc.desc: Benchmarktest for interface PlayPackageBySession
333 * Control vibrator perform and stop by sessionID
334 * @tc.type: FUNC
335 */
BENCHMARK_F(VibratorBenchmarkTest,PlayPackageBySession)336 BENCHMARK_F(VibratorBenchmarkTest, PlayPackageBySession)(benchmark::State &state)
337 {
338 ASSERT_NE(nullptr, g_vibratorInterface);
339
340 int32_t startRet = 0;
341 int32_t endRet = 0;
342 for (auto _ : state) {
343 startRet = g_vibratorInterface->PlayPackageBySession(
344 {-1, 1}, g_sessionId, g_vibPackage);
345 EXPECT_EQ(startRet, HDF_SUCCESS);
346
347 endRet = g_vibratorInterface->StopVibrateBySessionId({-1, 1}, g_sessionId);
348 EXPECT_EQ(endRet, HDF_SUCCESS);
349 }
350 }
351
352