1 /*
2  * Copyright (c) 2021 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 <cstring>
17 #ifdef __LINUX__
18 #   include <unistd.h>
19 #   include <fcntl.c>
20 #endif
21 
22 #include "gtest/gtest.h"
23 
24 #include "client_executor/include/i_aie_client.inl"
25 #include "platform/time/include/time.h"
26 #include "protocol/retcode_inner/aie_retcode_inner.h"
27 #include "service_dead_cb.h"
28 #include "utils/aie_macros.h"
29 #include "utils/log/aie_log.h"
30 
31 using namespace OHOS::AI;
32 using namespace testing::ext;
33 
34 namespace {
35     const int REQUEST_ID = 1;
36     const int OPERATE_ID = 2;
37     const long long CLIENT_INFO_VERSION = 1;
38     const int SESSION_ID = -1;
39     const long long ALGORITHM_INFO_CLIENT_VERSION = 1;
40     const int ALGORITHM_SYNC_TYPE = 0;
41     const long long ALGORITHM_VERSION = 1;
42     const int EXECUTE_TIMES = 100;
43     const int COUNT = 20;
44     const int CHAR_TYPE = 4;
45     const int DESCRIPTION_LENGTH = 129;
46     const int ALPHABET_LENGTH = 26;
47     const int DIGIT = 10;
48     const int UPPER_POSITION = 1;
49     const int LOWER_POSITION = 2;
50     const int SPACE_POSITION = 3;
51     const char MIN_UPPER_CASE_CHAR = 'A';
52     const char MIN_LOWER_CASE_CHAR = 'a';
53     const char MIN_NUMERIC_CHAR = '0';
54     const char TRAILING_CHAR = '\0';
55     const char * const PREPARE_INPUT_SYNC = "Sync prepare inputData";
56     const int EXCEPTED_SYNC_PROCESS_TIME = 30;
57 }
58 
59 class SyncProcessTimeTest : public testing::Test {
60 public:
61     // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()62     static void SetUpTestCase() {};
63 
64     // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()65     static void TearDownTestCase() {};
66 
67     // SetUp:Execute before each test case
SetUp()68     void SetUp() {};
69 
70     // TearDown:Execute after each test case
TearDown()71     void TearDown() {};
72 };
73 
Random(void)74 static int Random(void)
75 {
76 #ifndef __LINUX__
77     return -1;
78 #else
79 #ifndef O_RDONLY
80 #define O_RDONLY 0u
81 #endif
82     int r = -1;
83     int fd = open("/dev/random", O_RDONLY);
84     if (fd > 0) {
85         (void)read(fd, &r, sizeof(int));
86     }
87     (void)close(fd);
88 
89     return r;
90 #endif
91 }
92 
RandStr(const int len,char * str)93 static void RandStr(const int len, char *str)
94 {
95     int i;
96     for (i = 0; i < len - 1; ++i) {
97         switch (Random() % CHAR_TYPE) {
98             case UPPER_POSITION:
99                 str[i] = MIN_UPPER_CASE_CHAR + Random() % ALPHABET_LENGTH;
100                 break;
101             case LOWER_POSITION:
102                 str[i] = MIN_LOWER_CASE_CHAR + Random() % ALPHABET_LENGTH;
103                 break;
104             case SPACE_POSITION:
105                 str[i] = ' ';
106                 break;
107             default:
108                 str[i] = MIN_NUMERIC_CHAR + Random() % DIGIT;
109                 break;
110         }
111     }
112 
113     str[i] = TRAILING_CHAR;
114 }
115 
116 /**
117  * @tc.name: TestSyncTime001
118  * @tc.desc: Test Time Consumption of Sync Process Interface in AI System.
119  * @tc.type: PERF
120  * @tc.require: AR000F77MI
121  */
122 HWTEST_F(SyncProcessTimeTest, TestSyncTime001, TestSize.Level0)
123 {
124     HILOGI("[Test]SyncProcessTimeTest001.");
125     long long processTotalTime = 0;
126 
127     const char *str = PREPARE_INPUT_SYNC;
128     char *inputData = const_cast<char*>(str);
129     int len = strlen(str) + 1;
130 
131     for (int i = 0; i < COUNT; ++i) {
132         HILOGI("[Test]Test delay times:[%d]", i);
133         char config[DESCRIPTION_LENGTH];
134         RandStr(DESCRIPTION_LENGTH, config);
135         ConfigInfo configInfo {.description = config};
136         ClientInfo clientInfo = {
137             .clientVersion = CLIENT_INFO_VERSION,
138             .clientId = INVALID_CLIENT_ID,
139             .sessionId = SESSION_ID,
140             .serverUid = INVALID_UID,
141             .clientUid = INVALID_UID,
142             .extendLen = len,
143             .extendMsg = reinterpret_cast<unsigned char*>(inputData),
144         };
145 
146         AlgorithmInfo algoInfo = {
147             .clientVersion = ALGORITHM_INFO_CLIENT_VERSION,
148             .isAsync = false,
149             .algorithmType = ALGORITHM_SYNC_TYPE,
150             .algorithmVersion = ALGORITHM_VERSION,
151             .isCloud = true,
152             .operateId = OPERATE_ID,
153             .requestId = REQUEST_ID,
154             .extendLen = len,
155             .extendMsg = reinterpret_cast<unsigned char*>(inputData),
156         };
157 
158         ServiceDeadCb *cb = nullptr;
159         AIE_NEW(cb, ServiceDeadCb());
160         ASSERT_NE(cb, nullptr);
161         int resultCode = AieClientInit(configInfo, clientInfo, algoInfo, cb);
162         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
163 
164         DataInfo inputInfo = {
165             .data = reinterpret_cast<unsigned char*>(inputData),
166             .length = len,
167         };
168         DataInfo outputInfo;
169         resultCode = AieClientPrepare(clientInfo, algoInfo, inputInfo, outputInfo, nullptr);
170         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
171 
172         int optionType = 0;
173         resultCode = AieClientSetOption(clientInfo, optionType, inputInfo);
174         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
175 
176         outputInfo = {
177             .data = nullptr,
178             .length = 0
179         };
180         resultCode = AieClientGetOption(clientInfo, optionType, inputInfo, outputInfo);
181         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
182 
183         std::time_t processStartTime = GetCurTimeMillSec();
184         resultCode = AieClientSyncProcess(clientInfo, algoInfo, inputInfo, outputInfo);
185         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
186         std::time_t processEndTime = GetCurTimeMillSec();
187         processTotalTime += processEndTime - processStartTime;
188 
189         resultCode = AieClientRelease(clientInfo, algoInfo, inputInfo);
190         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
191 
192         resultCode = AieClientDestroy(clientInfo);
193         ASSERT_EQ(resultCode, RETCODE_SUCCESS);
194         AIE_DELETE(cb);
195     }
196     std::time_t duration = processTotalTime / EXECUTE_TIMES;
197     HILOGI("[Test][CheckTimeSyncProcess][%lld]", duration);
198     ASSERT_TRUE((duration > 0) && (duration <= EXCEPTED_SYNC_PROCESS_TIME));
199 }
200