1#!/bin/bash
2# Copyright (c) 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# usage: bash run_ffi_workload_test.sh $run_mode $openharmony_path  $daily_report_folder_path $hdc_machine_ip 
16# eg:  bash run_ffi_workload_test.sh 1 /home/codes out 172.24.32.1:8710 
17#
18set -e
19declare -i ret_ok=0
20declare -i ret_error=1
21
22function init() {
23    LOOP_NUM=5                            # execute workload times
24    WORKLOAD_BINARY_NAME='FFIWorkLoadTest'       # workload test binary name
25    SO_NAME="libark_jsruntime_test.so"
26    CUR_PATH=$(dirname "$(readlink -f "$0")")
27    WORK_PATH=$CUR_PATH/tmp/
28    LOCAL_XML_PATH=$WORK_PATH/xmls # local xml result files path
29}
30
31function check_command_exist() {
32    command=$1
33    if type "$command" >/dev/null 2>&1; then return $ret_ok; else return $ret_error; fi
34}
35
36function check_pip_component() {
37    pip3 list | grep "$1"
38    return $?
39}
40
41function compile_workload_test() {
42    if [ ! -f "$WORKLOAD_BINARY" ] || [ ! -f "$WORKLOAD_SO_PATH" ]; then
43        echo "enter comple_worklaod_test()"
44        "$OPENHARMONY_ROOT_PATH"/build.sh --product-name rk3568 --build-target ark_js_unittest --fast-rebuild > "$WORK_PATH"/test.log
45        ret=$(grep "build  successful" "$CUR_PATH"/test.log)
46        if [[ "$ret" == *"build successful"* ]];then
47            echo "ut build success"
48        else
49            echo "ut build failed"
50        fi
51    fi
52
53    if [ ! -f "$WORKLOAD_BINARY" ] || [ ! -f "$WORKLOAD_SO_PATH" ]; then
54        echo "ut build fail. please chceck it!"
55        return "$ret_error"
56    fi
57
58    return "$ret_ok"
59}
60
61
62function push_and_run_test_objs_on_development_board() {
63    [ -f "$WORKLOAD_SO_PATH" ] || {
64        echo "no libark_jsruntime_test.so, please check it"
65        return "$ret_error"
66    }
67    [ -f "$WORKLOAD_BINARY" ] || {
68        echo "no libark_jsruntime_test binary file, please check it"
69        return "$ret_error"
70    }
71
72    hdc -s "$HDC_MACHINE_IP" shell [ -d "$HDC_TEST_PATH" ] &&  rm -rf "$HDC_TEST_PATH"
73    hdc -s "$HDC_MACHINE_IP" shell mkdir -p "$HDC_TEST_PATH"
74    hdc -s "$HDC_MACHINE_IP" shell chmod u+x "$HDC_TEST_PATH"
75
76    hdc -s "$HDC_MACHINE_IP" file send "$WORKLOAD_SO_PATH" "$HDC_TEST_PATH"
77    hdc -s "$HDC_MACHINE_IP" file send "$WORKLOAD_BINARY" "$HDC_TEST_PATH"
78
79    hdc -s "$HDC_MACHINE_IP" shell chmod u+x "$HDC_TEST_PATH"/"$WORKLOAD_BINARY_NAME"
80    hdc -s "$HDC_MACHINE_IP" shell chmod u+x "$HDC_TEST_PATH"/"$SO_NAME"
81
82    # excute FFIWorkLoadTest
83    hdc -s "$HDC_MACHINE_IP" shell "cd $HDC_TEST_PATH;./$WORKLOAD_BINARY_NAME;./$WORKLOAD_BINARY_NAME;\
84        ./$WORKLOAD_BINARY_NAME;./$WORKLOAD_BINARY_NAME;./$WORKLOAD_BINARY_NAME"
85
86    return "$ret_ok"
87}
88
89function get_xml_files() {
90    echo "enter get_xml_files"
91    for ((i=0;i<"$LOOP_NUM";i++)); do
92        if [ "$i" == 0 ]; then
93            xml_name="FFIWorkLoadTest"
94        else
95            xml_name="FFIWorkLoadTest_$i"
96        fi
97        xml_file_path=$HDC_TEST_PATH/$xml_name".xml"
98        echo "xml_file_path: $xml_file_path"
99        echo "LOCAL_XML_PATH: $LOCAL_XML_PATH"
100        hdc  -s "$HDC_MACHINE_IP" file recv "$xml_file_path" "$LOCAL_XML_PATH"
101    done
102    return "$ret_ok"
103}
104
105
106function get_report() {
107    work_path=$1
108    report_path=$2
109    python3 ./get_ffi_workload_report.py -w "$work_path"  -o "$report_path"
110}
111
112
113main() {
114    init
115    para_num=$#
116    if [ "$para_num" -lt 4 ]; then
117        echo "bad parameters. please check them."
118        return "$ret_error"
119    fi
120    # 1--open network;2-yellow zone(network restricted zone)
121    run_mode=$1
122    if [[ $run_mode -eq "1" || $run_mode -eq "2" ]];then
123        echo "normal parameter run_mode. run_mode: $run_mode"
124    else
125        return "$ret_error"
126    fi
127
128    OPENHARMONY_ROOT_PATH=$2
129    if [[ "$3" = /* ]]; then
130        FFI_WORKLOAD_TEST_ARCHIVE_PATH=$3
131    else
132        FFI_WORKLOAD_TEST_ARCHIVE_PATH=$(readlink -f "$3")
133    fi
134    WORKLOAD_BINARY=$OPENHARMONY_ROOT_PATH/out/rk3568/tests/unittest/arkcompiler/ets_runtime/$WORKLOAD_BINARY_NAME
135    WORKLOAD_SO_PATH=$OPENHARMONY_ROOT_PATH/out/rk3568/test/test/$SO_NAME
136    sub_path="data/local/tmp/workload_test"
137    HDC_TEST_PATH=/"$sub_path"
138
139    if [ "$para_num" -eq 4 ]; then
140        HDC_MACHINE_IP=$4
141    fi
142
143    if [ -d "$LOCAL_XML_PATH" ];then
144        rm -rf "$LOCAL_XML_PATH"
145    fi
146    mkdir -p "$LOCAL_XML_PATH"
147    [ ! -d "$FFI_WORKLOAD_TEST_ARCHIVE_PATH" ] || { mkdir -p "$FFI_WORKLOAD_TEST_ARCHIVE_PATH"; }
148    check_command_exist "unzip" || {
149        echo "unzip is unavailable, please check it!"
150        return "$ret_error"
151    }
152    check_command_exist "hdc" || { return "$ret_error";}
153    check_command_exist python3 || {
154        echo "python3 is not available"
155        return "$ret_error"
156    }
157    check_pip_component "openpyxl" || { pip3 install openpyxl; }
158
159    [ -f "$CUR_PATH/get_ffi_workload_report.py" ] || {
160        echo "no get_ffi_workload_report.py, please check it"
161        return "$ret_error"
162    }
163
164 
165    # when run_mode is 2, no need compile unittest
166    if [ "$run_mode" -eq 1 ];then
167        compile_workload_test || {
168            echo "fail to compile workload test "
169            return "$ret_error"
170        }
171    fi
172    
173    push_and_run_test_objs_on_development_board || {
174        echo "fail to push and run test objects to development board"
175        return "$ret_error"
176    }
177
178    get_xml_files || {
179        echo "fail to run workload tests in development board"
180        return "$ret_error"
181    }
182
183    time_str=$(date "+%Y%m%d")
184    output_path=$FFI_WORKLOAD_TEST_ARCHIVE_PATH/ffi_workload_daily_report_$time_str".xlsx"
185
186    get_report "$WORK_PATH" "$FFI_WORKLOAD_TEST_ARCHIVE_PATH" || {
187        echo "fail to get report"
188        return "$ret_error"
189    }
190    if [ -d "$WORK_PATH" ];then
191        rm -rf "$WORK_PATH"
192    fi
193    if [ -f "$output_path" ];then
194        echo "run workload test successfully and get daily report. report path: $output_path"
195    else
196        echo "run workload test failed."
197    fi
198}   
199
200main "$@"
201exit $?
202