1 /*
2 * Copyright (c) 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 "devattestclient_performance_test.h"
17
18 #include <string>
19 #include <unistd.h>
20 #include <vector>
21 #include <sys/timeb.h>
22 #include <ipc_skeleton.h>
23 #include "iservice_registry.h"
24 #include "devattest_errno.h"
25 #include "devattest_client.h"
26 #include "devattest_service_proxy.h"
27
28 using namespace OHOS;
29 using namespace testing::ext;
30 using namespace OHOS::DevAttest;
31 namespace {
32 static const int SA_ID_DEVICE_ATTEST_SERVICE = 5501;
33 static const int PERFORMANCE_TEST_REPEAT_TIMES = 2000;
34 static const int MS_PER_SECOND = 1000;
35 static const long long PERFORMANCE_TEST_MAX_UNWIND_TIME_MS = 10;
36 static long long phaseConsumeTimeArray[static_cast<int>(AttestPhaseType::ATTEST_PHASE_MAX_TYPE)] = {0};
37
GetSysTime()38 long long GetSysTime()
39 {
40 struct timeb t;
41 ftime(&t);
42 return MS_PER_SECOND * t.time + t.millitm;
43 }
44
SetPhaseConsumeTime(AttestPhaseType type,long long time)45 void SetPhaseConsumeTime(AttestPhaseType type, long long time)
46 {
47 phaseConsumeTimeArray[static_cast<int>(type)] = time;
48 return;
49 }
50 }
51
SetUpTestCase(void)52 void DevAttestClientPerformanceTest::SetUpTestCase(void)
53 {
54 }
55
TearDownTestCase(void)56 void DevAttestClientPerformanceTest::TearDownTestCase(void)
57 {
58 int phaseType = 0;
59 double diffTime = 0;
60 double repeatTimes = static_cast<double>(PERFORMANCE_TEST_REPEAT_TIMES);
61
62 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_FIRST_CALL);
63 if (phaseConsumeTimeArray[phaseType] != 0) {
64 GTEST_LOG_(INFO) << "first call consume: " << phaseConsumeTimeArray[phaseType] << "ms";
65 } else {
66 GTEST_LOG_(ERROR) << "first call consume: " << "SA already exists";
67 }
68
69 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_CLIENT);
70 diffTime = phaseConsumeTimeArray[phaseType] / repeatTimes;
71 GTEST_LOG_(INFO) << "interface of client consume: " << diffTime << "ms";
72
73 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_GetSAManager);
74 diffTime = phaseConsumeTimeArray[phaseType];
75 GTEST_LOG_(INFO) << "interface of GetSAManager consume: " << diffTime << "ms";
76
77 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_CheckDevAttestSA);
78 diffTime = phaseConsumeTimeArray[phaseType];
79 GTEST_LOG_(INFO) << "interface of CheckDevAttestSA consume: " << diffTime << "ms";
80
81 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_PROXY);
82 diffTime = phaseConsumeTimeArray[phaseType] / repeatTimes;
83 GTEST_LOG_(INFO) << "interface of proxy consume: " << diffTime << "ms";
84
85 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_IPC);
86 diffTime = phaseConsumeTimeArray[phaseType] / repeatTimes;
87 GTEST_LOG_(INFO) << "interface of IPC consume: " << diffTime << "ms";
88
89 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_MARSHALLING);
90 diffTime = phaseConsumeTimeArray[phaseType] / repeatTimes;
91 GTEST_LOG_(INFO) << "interface of marshalling consume: " << diffTime << "ms";
92
93 phaseType = static_cast<int>(AttestPhaseType::ATTEST_PHASE_UNMARSHALLING);
94 diffTime = phaseConsumeTimeArray[phaseType] / repeatTimes;
95 GTEST_LOG_(INFO) << "interface of unmarshalling consume: " << diffTime << "ms";
96 return;
97 }
98
SetUp(void)99 void DevAttestClientPerformanceTest::SetUp(void)
100 {
101 }
102
TearDown(void)103 void DevAttestClientPerformanceTest::TearDown(void)
104 {
105 }
106
TestIPCConsume(void)107 int32_t DevAttestServiceProxyTest::TestIPCConsume(void)
108 {
109 MessageParcel data;
110 MessageParcel reply;
111 MessageOption option;
112
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 GTEST_LOG_(ERROR) << "write interface token failed ";
115 return DEVATTEST_FAIL;
116 }
117
118 sptr<IRemoteObject> remote = Remote();
119 if (remote == nullptr) {
120 GTEST_LOG_(ERROR) << "remote service null ";
121 return DEVATTEST_FAIL;
122 }
123 int ret = remote->SendRequest(GET_AUTH_RESULT, data, reply, option);
124 if (ret != DEVATTEST_SUCCESS) {
125 GTEST_LOG_(ERROR) << "call SendRequest failed " << ret;
126 return DEVATTEST_FAIL;
127 }
128 return DEVATTEST_SUCCESS;
129 }
130
GetAttestStatus(AttestResultInfo & attestResultInfo)131 int32_t DevAttestServiceProxyTest::GetAttestStatus(AttestResultInfo &attestResultInfo)
132 {
133 GTEST_LOG_(INFO) << "Entry GetAttestStatus, something wrong!";
134 return DEVATTEST_SUCCESS;
135 }
136
137 namespace {
138 /**
139 * @tc.name: CheckSystemAbilityTest001
140 * @tc.desc: Calculating the time spent calling the samgr external interface.
141 * @tc.type: FUNC
142 * @tc.require: Issue I7JVMY
143 */
144 HWTEST_F (DevAttestClientPerformanceTest, CheckSystemAbilityTest001, TestSize.Level0)
145 {
146 sptr<ISystemAbilityManager> samgrProxy;
147 long long startTime = GetSysTime();
148 samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
149 long long endTime = GetSysTime();
150 long long diffTime = (endTime - startTime);
151 ASSERT_NE(nullptr, samgrProxy);
152 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_GetSAManager, diffTime);
153
154 startTime = GetSysTime();
155 sptr<IRemoteObject> object = samgrProxy->CheckSystemAbility(SA_ID_DEVICE_ATTEST_SERVICE);
156 endTime = GetSysTime();
157 diffTime = (endTime - startTime);
158 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_CheckDevAttestSA, diffTime);
159 }
160
161 /**
162 * @tc.name: GetAttestStatusTest001
163 * @tc.desc: Calculating the time spent calling the client external interface when SA does not exist.
164 * @tc.type: FUNC
165 * @tc.require: Issue I7JVMY
166 */
167 HWTEST_F (DevAttestClientPerformanceTest, GetAttestStatusTest001, TestSize.Level0)
168 {
169 long long startTime = GetSysTime();
170 AttestResultInfo attestResultInfo;
171 int ret = DelayedSingleton<DevAttestClient>::GetInstance()->GetAttestStatus(attestResultInfo);
172 long long endTime = GetSysTime();
173 long long diffTime = (endTime - startTime);
174 ASSERT_EQ(DEVATTEST_SUCCESS, ret);
175 if (diffTime < PERFORMANCE_TEST_MAX_UNWIND_TIME_MS) {
176 GTEST_LOG_(ERROR) << "This result is not the first time that SA has been pulled up, "
177 << "please kill devattest_service process, then test again.";
178 } else {
179 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_FIRST_CALL, diffTime);
180 }
181 }
182
183 /**
184 * @tc.name: GetAttestStatusTest002
185 * @tc.desc: Calculating the time spent calling the client external interface when SA exists.
186 * @tc.type: FUNC
187 * @tc.require: Issue I7JVMY
188 */
189 HWTEST_F (DevAttestClientPerformanceTest, GetAttestStatusTest002, TestSize.Level0)
190 {
191 long long startTime = GetSysTime();
192 AttestResultInfo attestResultInfo;
193 int ret = 0;
194 for (int i = 0; i < PERFORMANCE_TEST_REPEAT_TIMES; i++) {
195 ret = DelayedSingleton<DevAttestClient>::GetInstance()->GetAttestStatus(attestResultInfo);
196 }
197 long long endTime = GetSysTime();
198 long long diffTime = (endTime - startTime);
199 ASSERT_EQ(DEVATTEST_SUCCESS, ret);
200 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_CLIENT, diffTime);
201 }
202
203 /**
204 * @tc.name: GetAttestStatusTest003
205 * @tc.desc: Calculating the time spent calling the client_proxy external interface when SA exists.
206 * @tc.type: FUNC
207 * @tc.require: Issue I7JVMY
208 */
209 HWTEST_F (DevAttestClientPerformanceTest, GetAttestStatusTest003, TestSize.Level0)
210 {
211 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
212 ASSERT_NE(nullptr, samgrProxy);
213 sptr<IRemoteObject> object = samgrProxy->CheckSystemAbility(SA_ID_DEVICE_ATTEST_SERVICE);
214 ASSERT_NE(nullptr, object);
215 DevAttestServiceProxy devAttestServiceProxy(object);
216
217 long long startTime = GetSysTime();
218 AttestResultInfo attestResultInfo;
219 int ret = 0;
220 for (int i = 0; i < PERFORMANCE_TEST_REPEAT_TIMES; i++) {
221 ret = devAttestServiceProxy.GetAttestStatus(attestResultInfo);
222 }
223 long long endTime = GetSysTime();
224 long long diffTime = (endTime - startTime);
225 ASSERT_EQ(DEVATTEST_SUCCESS, ret);
226 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_PROXY, diffTime);
227 }
228
229 /**
230 * @tc.name: GetAttestStatusTest004
231 * @tc.desc: Calculating the time spent calling the IPC remote interface when SA exists.
232 * @tc.type: FUNC
233 * @tc.require: Issue I7JVMY
234 */
235 HWTEST_F (DevAttestClientPerformanceTest, GetAttestStatusTest004, TestSize.Level0)
236 {
237 sptr<ISystemAbilityManager> samgrProxy =
238 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
239 ASSERT_NE(nullptr, samgrProxy);
240 sptr<IRemoteObject> object =
241 samgrProxy->CheckSystemAbility(SA_ID_DEVICE_ATTEST_SERVICE);
242 ASSERT_NE(nullptr, object);
243 DevAttestServiceProxyTest devAttestServiceProxyTest(object);
244
245 long long startTime = GetSysTime();
246 int ret = 0;
247 for (int i = 0; i < PERFORMANCE_TEST_REPEAT_TIMES; i++) {
248 ret = devAttestServiceProxyTest.TestIPCConsume();
249 }
250 long long endTime = GetSysTime();
251 long long diffTime = (endTime - startTime);
252 ASSERT_EQ(DEVATTEST_SUCCESS, ret);
253 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_IPC, diffTime);
254 }
255
256 /**
257 * @tc.name: ParcelTest001
258 * @tc.desc: Calculating the time spent calling the Parcel interface.
259 * @tc.type: FUNC
260 * @tc.require: Issue I7JVMY
261 */
262 HWTEST_F (DevAttestClientPerformanceTest, ParcelTest001, TestSize.Level0)
263 {
264 MessageParcel data;
265 AttestResultInfo attestResultInfo;
266 bool retBool = false;
267 long long startTime = GetSysTime();
268 for (int i = 0; i < PERFORMANCE_TEST_REPEAT_TIMES; i++) {
269 retBool = attestResultInfo.Marshalling(data);
270 }
271 long long endTime = GetSysTime();
272 long long diffTime = (endTime - startTime);
273 ASSERT_EQ(true, retBool);
274 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_MARSHALLING, diffTime);
275
276 sptr<AttestResultInfo> attestResultInfoPtr;
277 startTime = GetSysTime();
278 for (int i = 0; i < PERFORMANCE_TEST_REPEAT_TIMES; i++) {
279 attestResultInfoPtr = AttestResultInfo::Unmarshalling(data);
280 }
281 endTime = GetSysTime();
282 diffTime = (endTime - startTime);
283 SetPhaseConsumeTime(AttestPhaseType::ATTEST_PHASE_UNMARSHALLING, diffTime);
284 }
285 }
286