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