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