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 
17 #include <csignal>
18 #include <cstdio>
19 #include <cstring>
20 #include <gtest/gtest.h>
21 #include <unistd.h>
22 #include "hdf_base.h"
23 #include "usb_utils.h"
24 
25 using namespace std;
26 using namespace testing::ext;
27 
28 namespace {
29 class UsbHostPerformanceTest : public testing::Test {
30 protected:
SetUpTestCase(void)31     static void SetUpTestCase(void)
32     {
33         printf("------start UsbHostPerformanceTest------\n");
34     }
TearDownTestCase(void)35     static void TearDownTestCase(void)
36     {
37         printf("------end UsbHostPerformanceTest------\n");
38     }
39 };
40 
41 /**
42  * @tc.number    : H_Lx_H_Sub_usb_performance_005
43  * @tc.name      : Host SDK ROM占用<60K
44  * @tc.type      : PERF
45  * @tc.level     : Level 1
46  */
47 HWTEST_F(UsbHostPerformanceTest, CheckHostSdkRom, TestSize.Level1)
48 {
49     printf("------start CheckHostSdkRom------\n");
50     const char *hostSdkPath = HDF_LIBRARY_FULL_PATH("libusb_ddk_host");
51     int64_t size = 0;
52     FILE *fp = fopen(hostSdkPath, "rb");
53     fseek(fp, 0, SEEK_END);
54     size = ftell(fp);
55     fclose(fp);
56     EXPECT_LT(size, 60 * 1024);
57     printf("------end CheckHostSdkRom------\n");
58 }
59 
60 /**
61  * @tc.number    : H_Lx_H_Sub_usb_performance_006, H_Lx_H_Sub_usb_performance_007,
62  *                 H_Lx_H_Sub_usb_performance_008
63  * @tc.name      : Host SDK RAM占用峰值<40K,RAM占用均值<30K;Host SDK CPU占用峰值<10%,CPU占用均值<15%;
64  *                 Host SDK驱动框架下单进程加载SDK,最大并发线程数<5
65  * @tc.type      : PERF
66  * @tc.level     : Level 1
67  */
68 HWTEST_F(UsbHostPerformanceTest, CheckHostSdkProcInfo, TestSize.Level1)
69 {
70     printf("------start CheckHostSdkProcInfo------\n");
71     const string logFile = "/data/usb_proclog.txt";
72     const string script = "usb_watch_process.sh";
73     int32_t processCount;
74     pid_t watchPid = 0;
75     char *pch = nullptr;
76     FILE *res = nullptr;
77     struct ProcInfo info = {0, 0, 0, 0, 0};
78     ASSERT_EQ(access(script.c_str(), F_OK), 0) << "ErrInfo: shell script not exists";
79     if (access(script.c_str(), X_OK) == -1) {
80         system(("chmod +x " + script).c_str());
81     }
82     printf("try to start usb_watch_process.sh...\n");
83     ASSERT_EQ(system(("nohup sh " + script + " pnp_host > /data/nohup.out &").c_str()), 0);
84     printf("usb_watch_process.sh is running...\n");
85     for (int32_t i = 0; i < 1000; i++) {
86         system("usbhost_ddk_test -AW $RANDOM");
87         printf("Write data %d times\n", i);
88         usleep(100 * 1000);
89     }
90     res = popen("ps -ef | grep 'usb_watch_process.sh' | grep -v grep | cut -F 2", "r");
91     pch = ParseSysCmdResult(*res, 1, 1);
92     watchPid = stoi(pch);
93     printf("try to kill usb_watch_process.sh, pid: %d\n", watchPid);
94     kill(watchPid, SIGKILL);
95     CalcProcInfoFromFile(info, logFile);
96     EXPECT_LT(info.cpuPeak, 15) << "ErrInfo: cpu peak is not less than 15%";
97     EXPECT_LT(info.cpuAvg, 10) << "ErrInfo: average cpu is not less than 10%";
98     res = popen("ps -ef | grep 'pnp_host' | grep -v grep | wc -l", "r");
99     pch = ParseSysCmdResult(*res, 1, 1);
100     processCount = stoi(pch);
101     EXPECT_EQ(processCount, 1) << "ErrInfo: host sdk process count is not equal to 1";
102     printf("------end CheckHostSdkProcInfo------\n");
103 }
104 } // namespace
105