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 <unistd.h>
23 #include <vector>
24 #include "hdf_base.h"
25 #include "osal_time.h"
26 #include "v1_1/imotion_interface.h"
27 #include "motion_callback_impl.h"
28
29 using namespace OHOS::HDI::Motion::V1_1;
30 using namespace testing::ext;
31 using namespace std;
32
33 #define DATA_NUM 12
34 #define DATA_VALUE 6
35
36 namespace {
37 sptr<OHOS::HDI::Motion::V1_1::IMotionInterface> g_motionInterface = nullptr;
38 sptr<IMotionCallback> g_motionCallback = new MotionCallbackImpl();
39 sptr<IMotionCallback> g_motionCallbackUnregistered = new MotionCallbackImpl();
40 std::vector<uint8_t> g_motionConfigData(DATA_NUM, DATA_VALUE);
41
42
43 class MotionBenchmarkTest : public benchmark::Fixture {
44 public:
45 void SetUp(const ::benchmark::State &state);
46 void TearDown(const ::benchmark::State &state);
47 };
48
SetUp(const::benchmark::State & state)49 void MotionBenchmarkTest::SetUp(const ::benchmark::State &state)
50 {
51 g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get();
52 }
53
TearDown(const::benchmark::State & state)54 void MotionBenchmarkTest::TearDown(const ::benchmark::State &state)
55 {
56 }
57
58 /**
59 * @tc.name: DriverSystem_MotionBenchmark_EnableMotion
60 * @tc.desc: Benchmarktest for interface EnableMotion
61 * Obtains information about all motion in the system
62 * @tc.type: FUNC
63 */
BENCHMARK_F(MotionBenchmarkTest,EnableMotion)64 BENCHMARK_F(MotionBenchmarkTest, EnableMotion)(benchmark::State &state)
65 {
66 ASSERT_NE(nullptr, g_motionInterface);
67
68 for (auto _ : state) {
69 int32_t motionType = OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_WRIST_DOWN;
70 int32_t ret = g_motionInterface->EnableMotion(motionType);
71 EXPECT_NE(HDF_SUCCESS, ret);
72 ret = g_motionInterface->DisableMotion(motionType);
73 EXPECT_NE(HDF_SUCCESS, ret);
74 }
75 }
76
77 BENCHMARK_REGISTER_F(MotionBenchmarkTest, EnableMotion)->
78 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
79
80
81 /**
82 * @tc.name: DriverSystem_MotionBenchmark_Register
83 * @tc.desc: Benchmarktest for interface Register
84 * Obtains information about all motion in the system
85 * @tc.type: FUNC
86 */
BENCHMARK_F(MotionBenchmarkTest,Register)87 BENCHMARK_F(MotionBenchmarkTest, Register)(benchmark::State &state)
88 {
89 ASSERT_NE(nullptr, g_motionInterface);
90
91 for (auto _ : state) {
92 int32_t ret = g_motionInterface->Register(g_motionCallback);
93 EXPECT_EQ(HDF_SUCCESS, ret);
94 ret = g_motionInterface->Unregister(g_motionCallback);
95 EXPECT_EQ(0, ret);
96 }
97 }
98
99 BENCHMARK_REGISTER_F(MotionBenchmarkTest, Register)->
100 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
101
102 /**
103 * @tc.name: DriverSystem_MotionBenchmark_SetMotionConfig
104 * @tc.desc: Benchmarktest for interface SetMotionConfig
105 * Obtains information about all motion in the system
106 * @tc.type: FUNC
107 */
BENCHMARK_F(MotionBenchmarkTest,SetMotionConfig)108 BENCHMARK_F(MotionBenchmarkTest, SetMotionConfig)(benchmark::State &state)
109 {
110 ASSERT_NE(nullptr, g_motionInterface);
111
112 for (auto _ : state) {
113 int32_t motionType = -1;
114 int32_t ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
115 EXPECT_NE(HDF_SUCCESS, ret);
116 }
117 }
118
119 BENCHMARK_REGISTER_F(MotionBenchmarkTest, SetMotionConfig)->
120 Iterations(ITERATION_FREQUENCY)->Repetitions(REPETITION_FREQUENCY)->ReportAggregatesOnly();
121 }
122 BENCHMARK_MAIN();
123