1/*
2 * Copyright (c) 2024 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#include <benchmark/benchmark.h>
16#include <string>
17#include <vector>
18
19#include <cmath>
20#include <cstdio>
21#include <unistd.h>
22#include <gtest/gtest.h>
23#include <securec.h>
24#include <vector>
25#include "hdf_base.h"
26#include "osal_time.h"
27#include "v1_1/imotion_interface.h"
28#include "../hdi/motion_callback_impl.h"
29
30#define DATA_NUM 12
31#define DATA_VALUE 6
32
33using namespace OHOS::HDI::Motion::V1_1;
34using namespace testing::ext;
35
36namespace {
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
43class HdfMotionTest : public testing::Test {
44public:
45    static void SetUpTestCase();
46    static void TearDownTestCase();
47    void SetUp();
48    void TearDown();
49};
50
51void HdfMotionTest::SetUpTestCase()
52{
53    g_motionInterface = OHOS::HDI::Motion::V1_1::IMotionInterface::Get();
54}
55
56void HdfMotionTest::TearDownTestCase()
57{
58}
59
60void HdfMotionTest::SetUp()
61{
62}
63
64void HdfMotionTest::TearDown()
65{
66}
67
68/**
69  * @tc.name: SUB_Driver_Motion_BenchMark_0100
70  * @tc.desc: Benchmarktest for interface Register.
71  * @tc.type: FUNC
72  */
73BENCHMARK_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0100)(benchmark::State &st)
74{
75    if (g_motionInterface == nullptr) {
76        ASSERT_NE(nullptr, g_motionInterface);
77        return;
78    }
79
80    int32_t ret;
81    for (auto _ : st) {
82    ret =g_motionInterface->Register(g_motionCallback);
83    g_motionInterface->Unregister(g_motionCallback);
84    }
85    EXPECT_EQ(SENSOR_SUCCESS, ret);
86}
87
88BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0100)->
89    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
90
91
92/**
93  * @tc.name: SUB_Driver_Motion_BenchMark_0200
94  * @tc.desc: Benchmarktest for interface Unregister.
95  * @tc.type: FUNC
96  */
97BENCHMARK_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0200)(benchmark::State &st)
98{
99    if (g_motionInterface == nullptr) {
100        ASSERT_NE(nullptr, g_motionInterface);
101        return;
102    }
103
104    int32_t ret;
105    for (auto _ : st) {
106    g_motionInterface->Register(g_motionCallback);
107    ret = g_motionInterface->Unregister(g_motionCallback);
108    }
109    EXPECT_EQ(SENSOR_SUCCESS, ret);
110}
111
112BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0200)->
113    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
114
115
116/**
117  * @tc.name: SUB_Driver_Motion_BenchMark_0300
118  * @tc.desc: Benchmarktest for interface EnableMotion.
119  * @tc.type: FUNC
120  */
121BENCHMARK_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0300)(benchmark::State &st)
122{
123    if (g_motionInterface == nullptr) {
124        ASSERT_NE(nullptr, g_motionInterface);
125        return;
126    }
127    vector<int32_t> vec;
128    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
129    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
130    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
131    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
132    int32_t ret;
133    ret = g_motionInterface->Register(g_motionCallback);
134    EXPECT_EQ(HDF_SUCCESS, ret);
135
136    for (auto _ : st) {
137    ret = g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
138    g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
139    }
140    EXPECT_NE(HDF_SUCCESS, ret);
141
142    ret = g_motionInterface->Unregister(g_motionCallback);
143    EXPECT_EQ(HDF_SUCCESS, ret);
144}
145BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0300)->
146    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
147
148/**
149  * @tc.name: SUB_Driver_Motion_BenchMark_0400
150  * @tc.desc: Benchmarktest for interface DisableMotion.
151  * @tc.type: FUNC
152  */
153BENCHMARK_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0400)(benchmark::State &st)
154{
155    if (g_motionInterface == nullptr) {
156        ASSERT_NE(nullptr, g_motionInterface);
157        return;
158    }
159    vector<int32_t> vec;
160    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_PICKUP);
161    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_FLIP);
162    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_SHAKE);
163    vec.push_back(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_ROTATION);
164    int32_t ret;
165    ret = g_motionInterface->Register(g_motionCallback);
166    EXPECT_EQ(HDF_SUCCESS, ret);
167
168    for (auto _ : st) {
169    g_motionInterface->EnableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
170    ret = g_motionInterface->DisableMotion(OHOS::HDI::Motion::V1_1::HDF_MOTION_TYPE_MAX);
171    }
172    EXPECT_NE(HDF_SUCCESS, ret);
173
174    ret = g_motionInterface->Unregister(g_motionCallback);
175    EXPECT_EQ(HDF_SUCCESS, ret);
176}
177BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0400)->
178    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();
179
180/**
181  * @tc.name: SUB_Driver_Motion_BenchMark_0500
182  * @tc.desc: Benchmarktest for interface SetMotionConfig.
183  * @tc.type: FUNC
184  */
185BENCHMARK_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0500)(benchmark::State &st)
186{
187if (g_motionInterface == nullptr) {
188        ASSERT_NE(nullptr, g_motionInterface);
189        return;
190    }
191    int32_t motionType = -1;
192    int32_t ret;
193    for (auto _ : st) {
194    ret = g_motionInterface->SetMotionConfig(motionType, g_motionConfigData);
195    }
196    EXPECT_NE(HDF_SUCCESS, ret);
197}
198BENCHMARK_REGISTER_F(sensorBenchmarkTest, SUB_Driver_Motion_BenchMark_0500)->
199    Iterations(100)->Repetitions(3)->ReportAggregatesOnly();