87aec86b30a0c76c8ee516d4f0ee1f25d965d5c1
[platform/core/ml/nnfw.git] / tests / scripts / common.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #    http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 MY_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
18
19 function get_result_of_benchmark_test()
20 {
21     local DRIVER_BIN=$1
22     local MODEL=$2
23     local LOG_FILE=$3
24
25     local RET=0
26     $MY_PATH/models/run_test.sh --driverbin="$DRIVER_BIN  -r 5 -w 3" $MODEL > $LOG_FILE 2>&1
27     RET=$?
28     if [[ $RET -ne 0 ]]; then
29         echo "Testing $MODEL aborted... exit code: $RET"
30         exit $RET
31     fi
32
33     local RESULT=`grep -E '^- Mean:' $LOG_FILE | sed -e 's/ms//g' | awk '{print $3}'`
34     echo "$RESULT"
35 }
36
37 function print_result_of_benchmark_test()
38 {
39     local NAME=$1
40     local RESULT=$2
41     local RESULT_FILE=$3
42
43     echo "$NAME $RESULT" > $RESULT_FILE
44 }
45
46 function print_with_dots()
47 {
48     PRINT_WIDTH=45
49     local MSG="$@"
50     pad=$(printf '%0.1s' "."{1..45})
51     padlength=$((PRINT_WIDTH- ${#MSG}))
52     printf '%s' "$MSG"
53     printf '%*.*s ' 0 $padlength "$pad"
54 }
55
56
57 function run_benchmark_and_print()
58 {
59     local WRITE_FILE_NAME=$1
60     local MSG=$2
61     local MODEL=$3
62     local REPORT_MODEL_DIR=$4
63     local PAUSE_TIME_IN_SEC=$5
64     local DRIVER_BIN=$6
65     local BENCHMARK_RUN_TEST_SH=$7
66
67     LOG_FILE=$REPORT_MODEL_DIR/$WRITE_FILE_NAME.txt
68     RESULT_FILE=$REPORT_MODEL_DIR/$WRITE_FILE_NAME.result
69     print_with_dots $MSG
70     RESULT=$(get_result_of_benchmark_test $DRIVER_BIN $MODEL $LOG_FILE)
71     echo "$RESULT ms"
72     print_result_of_benchmark_test "$MSG" "$RESULT" $RESULT_FILE
73     sleep $PAUSE_TIME_IN_SEC
74 }