• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <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_2/ivibrator_interface.h"
26 #include "parameters.h"
27 
28 using namespace OHOS::HDI::Vibrator;
29 using namespace OHOS::HDI::Vibrator::V1_2;
30 using namespace testing::ext;
31 using namespace std;
32 
33 namespace {
34     uint32_t g_duration = 1;
35     uint32_t g_sleepTime1 = 1;
36     uint32_t g_sleepTime2 = 2;
37     int32_t g_intensity1 = 3;
38     int32_t g_frequency1 = 20;
39     int32_t g_intensity3 = 60;
40     V1_2::HapticPaket g_pkg = {434, 1, {{V1_2::CONTINUOUS, 0, 149, 100, 50, 0, 4,
41         {{0, 0, 0}, {1, 1, 0}, {32, 1, -39}, {149, 0, -39}}}}};
42     std::string g_timeSequence = "haptic.clock.timer";
43     std::string g_builtIn = "haptic.default.effect";
44     std::string g_arbitraryStr = "arbitraryString";
45     std::string g_effect1 = "haptic.long_press.light";
46     sptr<IVibratorInterface> g_vibratorInterface = nullptr;
47 
48 class vibratorBenchmarkTest : public benchmark::Fixture {
49 public:
50     void SetUp(const ::benchmark::State &state);
51     void TearDown(const ::benchmark::State &state);
52 };
53 
SetUp(const::benchmark::State & state)54 void vibratorBenchmarkTest::SetUp(const ::benchmark::State &state)
55 {
56     g_vibratorInterface = IVibratorInterface::Get();
57 }
58 
TearDown(const::benchmark::State & state)59 void vibratorBenchmarkTest::TearDown(const ::benchmark::State &state)
60 {
61 }
62 
63 /**
64   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0010
65   * @tc.desc: Benchmarktest for interface StartOnce
66   * Controls this vibrator to perform a one-shot vibrator at a given duration.
67   * @tc.type: FUNC
68   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0100)69 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0100)(benchmark::State &st)
70 {
71     ASSERT_NE(nullptr, g_vibratorInterface);
72 
73     int32_t startRet;
74     int32_t endRet;
75     for (auto _ : st) {
76         startRet = g_vibratorInterface->StartOnce({-1, 1}, g_duration);
77         EXPECT_EQ(startRet, HDF_SUCCESS);
78 
79         OsalMSleep(g_sleepTime1);
80 
81         endRet = g_vibratorInterface->Stop({-1, 1}, HDF_VIBRATOR_MODE_ONCE);
82         EXPECT_EQ(endRet, HDF_SUCCESS);
83     }
84 }
85 
86 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0100)->
87     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
88 
89 /**
90   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0020
91   * @tc.desc: Benchmarktest for interface Start
92   * Controls this Performing Time Series Vibrator Effects.
93   * @tc.type: FUNC
94   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0200)95 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0200)(benchmark::State &st)
96 {
97     ASSERT_NE(nullptr, g_vibratorInterface);
98 
99     int32_t startRet;
100     int32_t endRet;
101     for (auto _ : st) {
102         startRet = g_vibratorInterface->Start({-1, 1}, g_timeSequence);
103         EXPECT_EQ(startRet, HDF_SUCCESS);
104 
105         OsalMSleep(g_sleepTime2);
106 
107         endRet = g_vibratorInterface->Stop({-1, 1}, HDF_VIBRATOR_MODE_PRESET);
108         EXPECT_EQ(endRet, HDF_SUCCESS);
109     }
110 }
111 
112 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0200)->
113     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
114 
115 /**
116   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0030
117   * @tc.desc: Benchmarktest for interface Stop
118   * Controls this Performing built-in Vibrator Effects.
119   * @tc.type: FUNC
120   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0300)121 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0300)(benchmark::State &st)
122 {
123     ASSERT_NE(nullptr, g_vibratorInterface);
124     int32_t startRet;
125     int32_t endRet;
126 
127     for (auto _ : st) {
128         startRet = g_vibratorInterface->Start({-1, 1}, g_builtIn);
129         EXPECT_EQ(startRet, HDF_SUCCESS);
130 
131         OsalMSleep(g_sleepTime1);
132 
133         endRet = g_vibratorInterface->Stop({-1, 1}, HDF_VIBRATOR_MODE_PRESET);
134         EXPECT_EQ(endRet, HDF_SUCCESS);
135     }
136 }
137 
138 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0300)->
139     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
140 
141 /**
142   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0040
143   * @tc.desc: Benchmarktest for interface GetVibratorInfo.
144   * Controls this Performing Time Series Vibrator Effects.
145   * @tc.type: FUNC
146   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0400)147 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0400)(benchmark::State &state)
148 {
149     uint32_t majorVer;
150     uint32_t minorVer;
151     uint32_t ret;
152 
153     ASSERT_NE(nullptr, g_vibratorInterface);
154 
155     ret = g_vibratorInterface->GetVersion(majorVer, minorVer);
156     ASSERT_EQ(HDF_SUCCESS, ret);
157 
158     ASSERT_LT(0, minorVer);
159     ASSERT_LT(0, majorVer);
160 
161     std::vector<HdfVibratorInfo> info;
162 
163     for (auto _ : state) {
164         int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
165         EXPECT_EQ(startRet, HDF_SUCCESS);
166     }
167 }
168 
169 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0400)->
170     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
171 
172 /**
173   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0050
174   * @tc.desc: Benchmarktest for interface EnableVibratorModulation.
175   * Controls this Performing built-in Vibrator Effects.
176   * @tc.type: FUNC
177   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0500)178 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0500)(benchmark::State &state)
179 {
180     uint32_t majorVer;
181     uint32_t minorVer;
182     if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
183         return;
184     }
185 
186     if (majorVer > 0 && minorVer <= 0) {
187         return;
188     }
189     ASSERT_NE(nullptr, g_vibratorInterface);
190 
191     std::vector<HdfVibratorInfo> info;
192 
193     int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
194     EXPECT_EQ(startRet, HDF_SUCCESS);
195 
196     for (auto _ : state) {
197         if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
198             printf("vibratot benchmarktest successed!\n\t");
199             EXPECT_GT(g_duration, 0);
200             EXPECT_GE(g_intensity1, info[0].intensityMinValue);
201             EXPECT_LE(g_intensity1, info[0].intensityMaxValue);
202             EXPECT_GE(g_frequency1, info[0].frequencyMinValue);
203             EXPECT_LE(g_frequency1, info[0].frequencyMaxValue);
204             printf("vibratot benchmark test successed!\n\t");
205             startRet = g_vibratorInterface->EnableVibratorModulation({-1, 1}, g_duration, g_intensity1, g_frequency1);
206             EXPECT_EQ(startRet, HDF_SUCCESS);
207             OsalMSleep(g_sleepTime1);
208             startRet = g_vibratorInterface->Stop({-1, 1}, HDF_VIBRATOR_MODE_ONCE);
209             EXPECT_EQ(startRet, HDF_SUCCESS);
210         }
211     }
212 }
213 
214 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0500)->
215     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
216 
217 /**
218   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0060
219   * @tc.desc: Start periodic vibration with custom composite effect
220   * @tc.type: FUNC
221   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0600)222 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0600)(benchmark::State &state)
223 {
224         PrimitiveEffect primitiveEffect1 { 0, 60007, 0};
225         PrimitiveEffect primitiveEffect2 { 1000, 60007, 0};
226         PrimitiveEffect primitiveEffect3 { 1000, 60007, 0};
227         CompositeEffect effect1 = {
228         .primitiveEffect = primitiveEffect1
229         };
230         CompositeEffect effect2 = {
231         .primitiveEffect = primitiveEffect2
232         };
233         CompositeEffect effect3 = {
234         .primitiveEffect = primitiveEffect3
235         };
236         std::vector<CompositeEffect> vec;
237         vec.push_back(effect1);
238         vec.push_back(effect2);
239         vec.push_back(effect3);
240         HdfCompositeEffect effect;
241         effect.type = HDF_EFFECT_TYPE_PRIMITIVE;
242         effect.compositeEffects = vec;
243         int32_t ret;
244         for (auto _ : state) {
245         ret = g_vibratorInterface -> EnableCompositeEffect(effect);
246         }
247         OsalMSleep(2);
248 }
249 
250 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0600)->
251     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
252 
253 /**
254   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0070
255   * @tc.desc: Get effect information with the given effect type
256   * @tc.type: FUNC
257   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0700)258 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0700)(benchmark::State &state)
259 {
260     ASSERT_NE(nullptr, g_vibratorInterface);
261     HdfEffectInfo effectInfo;
262     int32_t ret;
263     for (auto _ : state) {
264     ret = g_vibratorInterface -> GetEffectInfo("haptic.pattern.type1", effectInfo);
265     }
266     EXPECT_EQ(HDF_SUCCESS, ret);
267 }
268 
269 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0700)->
270     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
271 
272 /**
273   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0080
274   * @tc.desc: Get vibration status.
275   * @tc.type: FUNC
276   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0800)277 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0800)(benchmark::State &state)
278 {
279     ASSERT_NE(nullptr, g_vibratorInterface);
280     bool stat {false};
281     for (auto _ : state) {
282     g_vibratorInterface -> IsVibratorRunning(stat);
283     }
284 }
285 
286 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0800)->
287     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
288 
289 /**
290   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0090
291   * @tc.desc: Get vibration status.
292   * @tc.type: FUNC
293   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_0900)294 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0900)(benchmark::State &state)
295 {
296     ASSERT_NE(nullptr, g_vibratorInterface);
297     int32_t startRet;
298     for (auto _ : state) {
299     startRet = g_vibratorInterface->StartByIntensity({-1, 1}, g_effect1, g_intensity3);
300     }
301     EXPECT_EQ(startRet, HDF_SUCCESS);
302 
303     int32_t endRet = g_vibratorInterface->StopV1_2(HdfVibratorModeV1_2::HDF_VIBRATOR_MODE_PRESET);
304     EXPECT_EQ(endRet, HDF_SUCCESS);
305 }
306 
307 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_0900)->
308     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
309 
310 /**
311   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0100
312   * @tc.desc: Get vibration status.
313   * @tc.type: FUNC
314   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_1000)315 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1000)(benchmark::State &state)
316 {
317     ASSERT_NE(nullptr, g_vibratorInterface);
318     int32_t startRet;
319     for (auto _ : state) {
320     startRet = g_vibratorInterface->PlayHapticPattern({-1, 1}, g_pkg);
321     }
322     EXPECT_EQ(startRet, HDF_SUCCESS);
323 
324     int32_t endRet = g_vibratorInterface->StopV1_2(HdfVibratorModeV1_2::HDF_VIBRATOR_MODE_ONCE);
325     EXPECT_EQ(endRet, HDF_SUCCESS);
326 }
327 
328 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1000)->
329     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
330 
331 /**
332   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0110
333   * @tc.desc: Get vibration status.
334   * @tc.type: FUNC
335   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_1100)336 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1100)(benchmark::State &state)
337 {
338     ASSERT_NE(nullptr, g_vibratorInterface);
339     OHOS::HDI::Vibrator::V1_2::HapticCapacity hapticCapacity;
340     int32_t startRet;
341     for (auto _ : state) {
342     startRet = g_vibratorInterface->GetHapticCapacity({-1, 1}, hapticCapacity);
343     }
344     EXPECT_EQ(startRet, HDF_SUCCESS);
345     printf("hapticCapacity.isSupportHdHaptic = %d\n", hapticCapacity.isSupportHdHaptic);
346     printf("hapticCapacity.isSupportPresetMapping = %d\n", hapticCapacity.isSupportPresetMapping);
347     printf("hapticCapacity.isSupportTimeDelay = %d\n", hapticCapacity.isSupportTimeDelay);
348 
349     int32_t endRet = g_vibratorInterface->StopV1_2(HdfVibratorModeV1_2::HDF_VIBRATOR_MODE_ONCE);
350     EXPECT_EQ(endRet, HDF_SUCCESS);
351 }
352 
353 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1100)->
354     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
355 
356 /**
357   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0120
358   * @tc.desc: Get vibration status.
359   * @tc.type: FUNC
360   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_1200)361 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1200)(benchmark::State &state)
362 {
363     ASSERT_NE(nullptr, g_vibratorInterface);
364     int32_t startUpTime;
365     int32_t mode = 0;
366     int32_t startRet;
367     for (auto _ : state) {
368     startRet = g_vibratorInterface->GetHapticStartUpTime({-1, 1}, mode, startUpTime);
369     }
370     EXPECT_EQ(startRet, HDF_SUCCESS);
371     printf("startUpTime = %d\n", startUpTime);
372 
373     int32_t endRet = g_vibratorInterface->StopV1_2(HdfVibratorModeV1_2::HDF_VIBRATOR_MODE_ONCE);
374     EXPECT_EQ(endRet, HDF_SUCCESS);
375 }
376 
377 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1200)->
378     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
379 
380 /**
381   * @tc.name: SUB_DriverSystem_VibratorBenchmark_0130
382   * @tc.desc: Get vibration status.
383   * @tc.type: FUNC
384   */
BENCHMARK_F(vibratorBenchmarkTest,SUB_Driver_Sensor_VibaratorPerf_1300)385 BENCHMARK_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1300)(benchmark::State &state)
386 {
387     ASSERT_NE(nullptr, g_vibratorInterface);
388     int32_t startRet;
389     int32_t endRet;
390     for (auto _ : st) {
391     startRet = g_vibratorInterface->StartOnce({-1, 1}, g_duration);
392 
393     OsalMSleep(g_sleepTime1);
394 
395     endRet = g_vibratorInterface->StopV1_2(HdfVibratorModeV1_2::HDF_VIBRATOR_MODE_ONCE);
396     }
397     EXPECT_EQ(startRet, HDF_SUCCESS);
398     EXPECT_EQ(endRet, HDF_SUCCESS);
399 }
400 
401 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_Driver_Sensor_VibaratorPerf_1300)->
402     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
403 }
404 BENCHMARK_MAIN();
405