• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "hdf_base.h"
22 #include "usb_utils.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class UsbHostPerformanceTest : public testing::Test {
29 protected:
SetUpTestCase(void)30     static void SetUpTestCase(void)
31     {
32         printf("------start UsbHostPerformanceTest------\n");
33     }
TearDownTestCase(void)34     static void TearDownTestCase(void)
35     {
36         printf("------end UsbHostPerformanceTest------\n");
37     }
38 };
39 
40 /**
41  * @tc.number    : H_Lx_H_Sub_usb_performance_005
42  * @tc.name      : Host SDK ROM占用<60K
43  * @tc.type      : PERF
44  * @tc.level     : Level 1
45  */
46 HWTEST_F(UsbHostPerformanceTest, CheckHostSdkRom, TestSize.Level1)
47 {
48     printf("------start CheckHostSdkRom------\n");
49     const char *hostSdkPath = HDF_LIBRARY_FULL_PATH("libusb_ddk_host");
50     long 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 CheckHostSdkRom------\n");
57 }
58 
59 /**
60  * @tc.number    : H_Lx_H_Sub_usb_performance_006, H_Lx_H_Sub_usb_performance_007,
61  *                 H_Lx_H_Sub_usb_performance_008
62  * @tc.name      : Host SDK RAM占用峰值<40K,RAM占用均值<30K;Host SDK CPU占用峰值<10%,CPU占用均值<15%;
63  *                 Host SDK驱动框架下单进程加载SDK,最大并发线程数<5
64  * @tc.type      : PERF
65  * @tc.level     : Level 1
66  */
67 HWTEST_F(UsbHostPerformanceTest, CheckHostSdkProcInfo, TestSize.Level1)
68 {
69     printf("------start CheckHostSdkProcInfo------\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 + " pnp_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("usbhost_ddk_test -AW $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 = atoi(pch);
92     printf("try to kill usb_watch_process.sh, pid: %d\n", watchPid);
93     kill(watchPid, SIGKILL);
94     CalcProcInfoFromFile(info, logFile);
95     EXPECT_LT(info.cpuPeak, 15) << "ErrInfo: cpu peak is not less than 15%";
96     EXPECT_LT(info.cpuAvg, 10) << "ErrInfo: average cpu is not less than 10%";
97     res = popen("ps -ef | grep 'pnp_host' | grep -v grep | wc -l", "r");
98     pch = ParseSysCmdResult(*res, 1, 1);
99     processCount = atoi(pch);
100     EXPECT_EQ(processCount, 1) << "ErrInfo: host sdk process count is not equal to 1";
101     printf("------end CheckHostSdkProcInfo------\n");
102 }
103 }
104