• 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     uint32_t g_duration = 100;
33     int32_t g_intensity1 = 30;
34     int32_t g_frequency1 = 200;
35     uint32_t g_sleepTime1 = 200;
36     std::string g_timeSequence = "haptic.clock.timer";
37     std::string g_builtIn = "haptic.default.effect";
38     sptr<IVibratorInterface> g_vibratorInterface = nullptr;
39 
40 class VibratorBenchmarkTest : public benchmark::Fixture {
41 public:
42     void SetUp(const ::benchmark::State &state);
43     void TearDown(const ::benchmark::State &state);
44 };
45 
SetUp(const::benchmark::State & state)46 void VibratorBenchmarkTest::SetUp(const ::benchmark::State &state)
47 {
48     g_vibratorInterface = IVibratorInterface::Get();
49 }
50 
TearDown(const::benchmark::State & state)51 void VibratorBenchmarkTest::TearDown(const ::benchmark::State &state)
52 {
53 }
54 
55 /**
56   * @tc.name: DriverSystem_VibratorBenchmark_001
57   * @tc.desc: Benchmarktest for interface StartOnce
58   * Controls this vibrator to perform a one-shot vibrator at a given duration.
59   * @tc.type: FUNC
60   */
BENCHMARK_F(VibratorBenchmarkTest,DriverSystem_VibratorBenchmark_001)61 BENCHMARK_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_001)(benchmark::State &state)
62 {
63     ASSERT_NE(nullptr, g_vibratorInterface);
64 
65     int32_t startRet;
66     int32_t endRet;
67     for (auto _ : state) {
68         startRet = g_vibratorInterface->StartOnce(g_duration);
69         EXPECT_EQ(startRet, HDF_SUCCESS);
70 
71         endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
72         EXPECT_EQ(endRet, HDF_SUCCESS);
73     }
74 }
75 
76 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_001)->
77     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
78 
79 /**
80   * @tc.name: DriverSystem_VibratorBenchmark_002
81   * @tc.desc: Benchmarktest for interface Start
82   * Controls this Performing Time Series Vibrator Effects.
83   * @tc.type: FUNC
84   */
BENCHMARK_F(VibratorBenchmarkTest,DriverSystem_VibratorBenchmark_002)85 BENCHMARK_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_002)(benchmark::State &state)
86 {
87     ASSERT_NE(nullptr, g_vibratorInterface);
88 
89     int32_t startRet;
90     int32_t endRet;
91     for (auto _ : state) {
92         startRet = g_vibratorInterface->Start(g_timeSequence);
93         EXPECT_EQ(startRet, HDF_SUCCESS);
94 
95         endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
96         EXPECT_EQ(endRet, HDF_SUCCESS);
97     }
98 }
99 
100 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_002)->
101     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
102 
103 /**
104   * @tc.name: DriverSystem_VibratorBenchmark_003
105   * @tc.desc: Benchmarktest for interface Stop
106   * Controls this Performing built-in Vibrator Effects.
107   * @tc.type: FUNC
108   */
BENCHMARK_F(VibratorBenchmarkTest,DriverSystem_VibratorBenchmark_003)109 BENCHMARK_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_003)(benchmark::State &state)
110 {
111     ASSERT_NE(nullptr, g_vibratorInterface);
112     int32_t startRet;
113     int32_t endRet;
114 
115     for (auto _ : state) {
116         startRet = g_vibratorInterface->Start(g_builtIn);
117         EXPECT_EQ(startRet, HDF_SUCCESS);
118 
119         endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
120         EXPECT_EQ(endRet, HDF_SUCCESS);
121     }
122 }
123 
124 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_003)->
125     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
126 
127 /**
128   * @tc.name: DriverSystem_VibratorBenchmark_004
129   * @tc.desc: Benchmarktest for interface GetVibratorInfo.
130   * Controls this Performing Time Series Vibrator Effects.
131   * @tc.type: FUNC
132   */
BENCHMARK_F(VibratorBenchmarkTest,DriverSystem_VibratorBenchmark_004)133 BENCHMARK_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_004)(benchmark::State &state)
134 {
135     uint32_t majorVer;
136     uint32_t minorVer;
137     uint32_t ret;
138 
139     ASSERT_NE(nullptr, g_vibratorInterface);
140 
141     ret = g_vibratorInterface->GetVersion(majorVer, minorVer);
142     ASSERT_EQ(HDF_SUCCESS, ret);
143 
144     ASSERT_LT(0, minorVer);
145     ASSERT_LT(0, majorVer);
146 
147     std::vector<HdfVibratorInfo> info;
148 
149     for (auto _ : state) {
150         int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
151         EXPECT_EQ(startRet, HDF_SUCCESS);
152     }
153 }
154 
155 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_004)->
156     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
157 
158 /**
159   * @tc.name: DriverSystem_VibratorBenchmark_005
160   * @tc.desc: Benchmarktest for interface EnableVibratorModulation.
161   * Controls this Performing built-in Vibrator Effects.
162   * @tc.type: FUNC
163   */
BENCHMARK_F(VibratorBenchmarkTest,DriverSystem_VibratorBenchmark_005)164 BENCHMARK_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_005)(benchmark::State &state)
165 {
166     uint32_t majorVer;
167     uint32_t minorVer;
168     if (g_vibratorInterface->GetVersion(majorVer, minorVer) != HDF_SUCCESS) {
169         return;
170     }
171 
172     if (majorVer > 0 && minorVer <= 0) {
173         return;
174     }
175     ASSERT_NE(nullptr, g_vibratorInterface);
176 
177     std::vector<HdfVibratorInfo> info;
178 
179     int32_t startRet = g_vibratorInterface->GetVibratorInfo(info);
180     EXPECT_EQ(startRet, HDF_SUCCESS);
181 
182     for (auto _ : state) {
183         if ((info[0].isSupportIntensity == 1) || (info[0].isSupportFrequency == 1)) {
184             printf("vibratot benchmarktest successed!\n\t");
185             EXPECT_GT(g_duration, 0);
186             EXPECT_GE(g_intensity1, info[0].intensityMinValue);
187             EXPECT_LE(g_intensity1, info[0].intensityMaxValue);
188             EXPECT_GE(g_frequency1, info[0].frequencyMinValue);
189             EXPECT_LE(g_frequency1, info[0].frequencyMaxValue);
190             printf("vibratot benchmark test successed!\n\t");
191             startRet = g_vibratorInterface->EnableVibratorModulation(g_duration, g_intensity1, g_frequency1);
192             EXPECT_EQ(startRet, HDF_SUCCESS);
193             OsalMSleep(g_sleepTime1);
194             startRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
195             EXPECT_EQ(startRet, HDF_SUCCESS);
196         }
197     }
198 }
199 
200 BENCHMARK_REGISTER_F(VibratorBenchmarkTest, DriverSystem_VibratorBenchmark_005)->
201     Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
202 }
203 
204 BENCHMARK_MAIN();
205