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