1 /*
2  * Copyright (c) 2021-2023 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 <cmath>
17 #include <cstdio>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include "hdf_base.h"
21 #include "osal_time.h"
22 #include "v2_0/sensor_interface_proxy.h"
23 #include "sensor_callback_impl.h"
24 
25 using namespace testing::ext;
26 using namespace OHOS::HDI::Sensor::V2_0;
27 
28 namespace {
29     const int32_t SENSOR_ID = 0;
30     const int32_t SENSOR_INTERVAL = 1000000000;
31     const int32_t SENSOR_POLL_TIME = 5;
32     const int32_t SENSOR_USEC_TIME = 1000000;
33     const int32_t SENSOR_MSEC_TIME = 1000;
34     const int32_t SENSOR_COMMON_TIME = 2000;
35     sptr<ISensorInterface>  g_sensorInterface = nullptr;
36     sptr<ISensorCallback> g_callback = new SensorCallbackImpl();
37 }
38 
39 class HdfSensorHdiPerformanceTest : public testing::Test {
40 public:
41     static void SetUpTestCase();
42     static void TearDownTestCase();
43     void SetUp();
44     void TearDown();
45 };
46 
SetUpTestCase()47 void HdfSensorHdiPerformanceTest::SetUpTestCase()
48 {
49     g_sensorInterface = ISensorInterface::Get();
50     if (g_sensorInterface == nullptr) {
51         printf("test sensorHdi get Module instance failed\n\r");
52     }
53 }
54 
TearDownTestCase()55 void HdfSensorHdiPerformanceTest::TearDownTestCase()
56 {
57 }
58 
SetUp()59 void HdfSensorHdiPerformanceTest::SetUp()
60 {
61 }
62 
TearDown()63 void HdfSensorHdiPerformanceTest::TearDown()
64 {
65 }
66 
67 /**
68   * @tc.name: SensorHdiRegister001
69   * @tc.desc: Interface performance test.
70   * @tc.type: FUNC
71   * @tc.require: AR000F869N
72   */
73 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiRegister001, TestSize.Level1)
74 {
75     int timeUsed = 0;
76     struct timespec tv1 = (struct timespec){0};
77     struct timespec tv2 = (struct timespec){0};
78 
79     clock_gettime(CLOCK_REALTIME, &tv1);
80     int ret = g_sensorInterface->Register(0, g_callback);
81     clock_gettime(CLOCK_REALTIME, &tv2);
82     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
83         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
84     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
85     EXPECT_EQ(0, ret);
86 }
87 
88 /**
89   * @tc.name: SensorHdiGetAllSensors001
90   * @tc.desc: Interface performance test.
91   * @tc.type: FUNC
92   * @tc.require: AR000F869O
93   */
94 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiGetAllSensors001, TestSize.Level1)
95 {
96     std::vector<HdfSensorInformation> info;
97     int timeUsed = 0;
98     struct timespec tv1 = (struct timespec){0};
99     struct timespec tv2 = (struct timespec){0};
100 
101     clock_gettime(CLOCK_REALTIME, &tv1);
102     int ret = g_sensorInterface->GetAllSensorInfo(info);
103     clock_gettime(CLOCK_REALTIME, &tv2);
104     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
105         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
106     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
107     EXPECT_EQ(0, ret);
108 }
109 
110 /**
111   * @tc.name: SensorHdiEnable001
112   * @tc.desc: Interface performance test.
113   * @tc.type: FUNC
114   * @tc.require: AR000F869P
115   */
116 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiEnable001, TestSize.Level1)
117 {
118     int timeUsed = 0;
119     struct timespec tv1 = (struct timespec){0};
120     struct timespec tv2 = (struct timespec){0};
121 
122     clock_gettime(CLOCK_REALTIME, &tv1);
123     int ret = g_sensorInterface->Enable(SENSOR_ID);
124     clock_gettime(CLOCK_REALTIME, &tv2);
125     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
126         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
127     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
128     EXPECT_EQ(0, ret);
129 }
130 
131 /**
132   * @tc.name: SensorHdiSetBatch001
133   * @tc.desc: Interface performance test.
134   * @tc.type: FUNC
135   * @tc.require: AR000F869Q
136   */
137 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiSetBatch001, TestSize.Level1)
138 {
139     int timeUsed = 0;
140     struct timespec tv1 = (struct timespec){0};
141     struct timespec tv2 = (struct timespec){0};
142 
143     clock_gettime(CLOCK_REALTIME, &tv1);
144     int ret = g_sensorInterface->SetBatch(SENSOR_ID, SENSOR_INTERVAL, SENSOR_POLL_TIME);
145     clock_gettime(CLOCK_REALTIME, &tv2);
146     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
147         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
148 
149     OsalSleep(SENSOR_POLL_TIME);
150 
151     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
152     EXPECT_EQ(0, ret);
153 }
154 
155 /**
156   * @tc.name: SensorHdiDisable001
157   * @tc.desc: Interface performance test.
158   * @tc.type: FUNC
159   * @tc.require: AR000F869U
160   */
161 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiDisable001, TestSize.Level1)
162 {
163     int timeUsed = 0;
164     struct timespec tv1 = (struct timespec){0};
165     struct timespec tv2 = (struct timespec){0};
166 
167     clock_gettime(CLOCK_REALTIME, &tv1);
168     int ret = g_sensorInterface->Disable(SENSOR_ID);
169     clock_gettime(CLOCK_REALTIME, &tv2);
170     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
171         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
172     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
173     EXPECT_EQ(0, ret);
174 }
175 
176 /**
177   * @tc.name: SensorHdiSetMode001
178   * @tc.desc: Interface performance test.
179   * @tc.type: FUNC
180   * @tc.require: AR000F869R
181   */
182 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiSetMode001, TestSize.Level1)
183 {
184     int timeUsed = 0;
185     struct timespec tv1 = (struct timespec){0};
186     struct timespec tv2 = (struct timespec){0};
187 
188     clock_gettime(CLOCK_REALTIME, &tv1);
189     int ret = g_sensorInterface->SetMode(SENSOR_ID, 1);
190     clock_gettime(CLOCK_REALTIME, &tv2);
191     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
192         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
193     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
194     EXPECT_EQ(0, ret);
195 }
196 
197 /**
198   * @tc.name: SensorHdiSetOption001
199   * @tc.desc: Interface performance test.
200   * @tc.type: FUNC
201   * @tc.require: AR000F869S
202   */
203 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiSetOption001, TestSize.Level1)
204 {
205     int timeUsed = 0;
206     struct timespec tv1 = (struct timespec){0};
207     struct timespec tv2 = (struct timespec){0};
208 
209     clock_gettime(CLOCK_REALTIME, &tv1);
210     int ret = g_sensorInterface->SetOption(SENSOR_ID, 1);
211     clock_gettime(CLOCK_REALTIME, &tv2);
212     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
213         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
214     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
215     EXPECT_EQ(0, ret);
216 }
217 
218 /**
219   * @tc.name: SensorHdiRegister001
220   * @tc.desc: Interface performance test.
221   * @tc.type: FUNC
222   * @tc.require: AR000F869T
223   */
224 HWTEST_F(HdfSensorHdiPerformanceTest, SensorHdiUnregister001, TestSize.Level1)
225 {
226     int timeUsed = 0;
227     struct timespec tv1 = (struct timespec){0};
228     struct timespec tv2 = (struct timespec){0};
229 
230     clock_gettime(CLOCK_REALTIME, &tv1);
231     int ret = g_sensorInterface->Unregister(0, g_callback);
232     clock_gettime(CLOCK_REALTIME, &tv2);
233     timeUsed = ((tv2.tv_sec * SENSOR_USEC_TIME + tv2.tv_nsec / SENSOR_MSEC_TIME) -
234         (tv1.tv_sec * SENSOR_USEC_TIME + tv1.tv_nsec / SENSOR_MSEC_TIME));
235     EXPECT_GE(SENSOR_COMMON_TIME, timeUsed);
236     EXPECT_EQ(0, ret);
237 }
238