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
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 = 1;
33 uint32_t g_sleepTime1 = 1;
34 uint32_t g_sleepTime2 = 2;
35 std::string g_timeSequence = "haptic.clock.timer";
36 std::string g_builtIn = "haptic.default.effect";
37 std::string g_arbitraryStr = "arbitraryString";
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: SUB_DriverSystem_VibratorBenchmark_0010
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,SUB_DriverSystem_VibratorBenchmark_0010)61 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0010)(benchmark::State &st)
62 {
63 ASSERT_NE(nullptr, g_vibratorInterface);
64
65 int32_t startRet;
66 int32_t endRet;
67 for (auto _ : st) {
68 startRet = g_vibratorInterface->StartOnce(g_duration);
69 EXPECT_EQ(startRet, HDF_SUCCESS);
70
71 OsalMSleep(g_sleepTime1);
72
73 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_ONCE);
74 EXPECT_EQ(endRet, HDF_SUCCESS);
75 }
76 }
77
78 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0010)->
79 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
80
81 /**
82 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0020
83 * @tc.desc: Benchmarktest for interface Start
84 * Controls this Performing Time Series Vibrator Effects.
85 * @tc.type: FUNC
86 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0020)87 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0020)(benchmark::State &st)
88 {
89 ASSERT_NE(nullptr, g_vibratorInterface);
90
91 int32_t startRet;
92 int32_t endRet;
93 for (auto _ : st) {
94 startRet = g_vibratorInterface->Start(g_timeSequence);
95 EXPECT_EQ(startRet, HDF_SUCCESS);
96
97 OsalMSleep(g_sleepTime2);
98
99 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
100 EXPECT_EQ(endRet, HDF_SUCCESS);
101 }
102 }
103
104 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0020)->
105 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
106
107 /**
108 * @tc.name: SUB_DriverSystem_VibratorBenchmark_0030
109 * @tc.desc: Benchmarktest for interface Stop
110 * Controls this Performing built-in Vibrator Effects.
111 * @tc.type: FUNC
112 */
BENCHMARK_F(vibratorBenchmarkTest,SUB_DriverSystem_VibratorBenchmark_0030)113 BENCHMARK_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0030)(benchmark::State &st)
114 {
115 ASSERT_NE(nullptr, g_vibratorInterface);
116 int32_t startRet;
117 int32_t endRet;
118
119 for (auto _ : st) {
120 startRet = g_vibratorInterface->Start(g_builtIn);
121 EXPECT_EQ(startRet, HDF_SUCCESS);
122
123 OsalMSleep(g_sleepTime1);
124
125 endRet = g_vibratorInterface->Stop(HDF_VIBRATOR_MODE_PRESET);
126 EXPECT_EQ(endRet, HDF_SUCCESS);
127 }
128 }
129
130 BENCHMARK_REGISTER_F(vibratorBenchmarkTest, SUB_DriverSystem_VibratorBenchmark_0030)->
131 Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
132 }
133
134 BENCHMARK_MAIN();