summaryrefslogtreecommitdiff
path: root/android/scripts/common.sh
blob: bc4a52610431615aa71c1c788ff3136892d03319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/system/bin/sh

G_LOOP_COUNT=12
G_RECORD_LOCAL_CSV=TRUE
G_VERBOSE_OUTPUT=FALSE
F_RAW_DATA_CSV="/data/local/tmp/lava_test_shell_raw_data.csv"
F_STATISTIC_DATA_CSV="/data/local/tmp/lava_test_shell_statistic_data.csv"

## Description:
##    output the max value of the passed 2 parameters
## Usage:
##    f_max "${val1}" "${val2}"
## Example:
##    max=$(f_max "1.5" "2.0")
f_max(){
    local val1=$1
    local val2=$2
    [ -z "$val1" ] && echo $val2
    [ -z "$val2" ] && echo $val1

    echo "$val1,$val2"|awk -F, '{if($1<$2) print $2; else print $1}'
}

## Description:
##    output the min value of the passed 2 parameters
## Usage:
##    f_min "${val1}" "${val2}"
## Example:
##    min=$(f_min "1.5" "2.0")
f_min(){
    local val1=$1
    local val2=$2
    [ -z "$val1" ] && echo $val1
    [ -z "$val2" ] && echo $val2

    echo "$val1,$val2"|awk -F, '{if($1>$2) print $2; else print $1}'
}

## Description:
##   calculate the average value for specified csv file.
##   The first field of that csv file should be the key/name of that line,
##   Lines have the same key should be together.
## Usage:
##    statistic "${csv_file_path}" "${file_number}"
## Example:
##    statistic "$f_res_starttime" 2
## Note:
##    if less than 4 samples for that key/item there, average will be calculated as total/count
##    if 4 or more samples for that key/item there, average will be calculated with max and min excluded
statistic(){
    local f_data=$1
    if ! [ -f "$f_data" ]; then
        return
    fi
    local field_no=$2
    if [ -z "$field_no" ]; then
        field_no=2
    fi
    local total=0
    local max=0
    local min=0
    local old_key=""
    local new_key=""
    local count=0
    local units=""
    sort ${f_data} >${f_data}.sort
    for line in $(cat "${f_data}.sort" |tr ' ' '~'); do
        if ! echo "$line"|grep -q ,; then
            continue
        fi
        new_key=$(echo $line|cut -d, -f1)
        local measurement_units=$(echo $line|cut -d, -f${field_no})
        if echo ${measurement_units}|grep -q '~'; then
            value=$(echo ${measurement_units}|cut -d~ -f1)
        else
            value=${measurement_units}
        fi

        if [ "X${new_key}" = "X${old_key}" ]; then
            total=$(echo "${total},${value}"|awk -F, '{printf "%.2f",$1+$2;}')
            count=$(echo "${count},1"|awk -F, '{printf $1+$2;}')
            max=$(f_max "$max" "$value")
            min=$(f_min "$min" "$value")
        else
            if [ "X${old_key}" != "X" ]; then
                if [ $count -ge 4 ]; then
                    average=$(echo "${total},${max},${min},$count"|awk -F, '{printf "%.2f",($1-$2-$3)/($4-2);}')
                else
                    average=$(echo "${total},$count"|awk -F, '{printf "%.2f",$1/$2;}')
                fi
                if [ -z "${units}" ]; then
                    echo "${old_key}=${average}"
                else
                    echo "${old_key}=${average},${units}"
                fi
            fi
            total="${value}"
            max="${value}"
            min="${value}"
            old_key="${new_key}"
            count=1
            if echo ${measurement_units}|grep -q '~'; then
                units=$(echo ${measurement_units}|cut -d~ -f2)
            else
                units=""
            fi
        fi
    done
    if [ "X${new_key}" != "X" ]; then
        if [ $count -ge 4 ]; then
            average=$(echo "${total},${max},${min},$count"|awk -F, '{printf "%.2f",($1-$2-$3)/($4-2);}')
        else
            average=$(echo "${total},$count"|awk -F, '{printf "%.2f",$1/$2;}')
        fi
        if [ -z "${units}" ]; then
            echo "${new_key}=${average}"
        else
            echo "${new_key}=${average},${units}"
        fi
    fi
    rm "${f_data}.sort"
}

## Description:
##   output the test result to console and add for lava-test-shell,
##   also write into one csv file for comparing manually
## Usage:
##    output_test_result $test_name $result [ $measurement [ $units ] ]
## Note:
##    G_RECORD_LOCAL_CSV: when this environment variant is set to "TRUE",
##         the result will be recorded in a csv file in the following path:
##              rawdata/final_result.csv
##    G_VERBOSE_OUTPUT: when this environment variant is set to "TRUE", and only it is TRUE,
##         the verbose informain about the result will be outputed
output_test_result(){
    local test_name=$1
    local result=$2
    local measurement=$3
    local units=$4

    if [ -z "${test_name}" ] || [ -z "$result" ]; then
        return
    fi
    local output=""
    local lava_paras=""
    local output_csv=""
    if [ -z "$units" ]; then
        units="points"
    fi
    if [ -z "${measurement}" ]; then
        output="${test_name}=${result}"
        lava_paras="${test_name} --result ${result}"
    else
        output="${test_name}=${measurement} ${units}"
        lava_paras="${test_name} --result ${result} --measurement ${measurement} --units ${units}"
        output_csv="${test_name},${measurement} ${units}"
    fi

    if [ "X${G_VERBOSE_OUTPUT}" = "XTRUE" ];then
        echo "${output}"
    fi

    local cmd="lava-test-case"
    if [ -n "$(which $cmd)" ];then
        $cmd ${lava_paras}
    elif [ "X${G_VERBOSE_OUTPUT}" = "XTRUE" ];then
        echo "$cmd ${lava_paras}"
    fi
    if [ "X${G_RECORD_LOCAL_CSV}" = "XTRUE" ]; then
        if [ -n "${output_csv}" ]; then
            echo "${output_csv}">>${F_RAW_DATA_CSV}
        fi
    fi
}

func_print_usage_common(){
    echo "$(basename $0) [--record-csv TRUE|others] [--loop-count LOOP_COUNT]"
    echo "     --record-csv: specify if record the result in csv format in file ${F_RAW_DATA_CSV}"
    echo "                   Only record the file when TRUE is specified."
    echo "     --loop-count: specify the number that how many times should be run for each application to get the average result, default is 12"
    echo "     --verbose-output: output the result and lava-test-case command for each test case each time it is run"
    echo "$(basename $0) [--help|-h]"
    echo "     print out this usage."
}

func_parse_parameters_common(){
    local para_loop_count=""
    while [ -n "$1" ]; do
        case "X$1" in
            X--record-csv)
                para_record_csv=$2
                if [ -z "${para_record_csv}" ]; then
                    echo "Please specify the value for --record-csv option"
                    exit 1
                fi
                shift 2
                ;;
            X--verbose-output)
                para_verbose_output=$2
                if [ -z "${para_verbose_output}" ]; then
                    echo "Please specify the value for --verbose-output option"
                    exit 1
                fi
                shift 2
                ;;
            X--loop-count)
                para_loop_count=$2
                if [ -z "${para_loop_count}" ]; then
                    echo "Please specify the value for --loop-count option"
                    exit 1
                fi
                shift 2
                ;;
            X-h|X--help)
                func_print_usage_common
                exit 1
                ;;
            X-*)
                echo "Unknown option: $1"
                func_print_usage_common
                exit 1
                ;;
            X*)
                func_print_usage_common
                exit 1
                ;;
        esac
    done

    if [ -n "${para_loop_count}" ]; then
        local tmp_str=$(echo ${para_loop_count}|tr -d '[:digit:]')
        if [ -z "${tmp_str}" ]; then
            G_LOOP_COUNT=${para_loop_count}
        else
            echo "The specified LOOP_COUNT(${para_loop_count}) is not valid"
            exit 1
        fi
    fi

    if [ -n "${para_record_csv}" ] && [ "X${para_record_csv}" = "XTRUE" ];then
        G_RECORD_LOCAL_CSV=TRUE
    elif [ -n "${para_record_csv}" ];then
        G_RECORD_LOCAL_CSV=FALSE
    fi

    if [ -n "${para_verbose_output}" ] && [ "X${para_verbose_output}" = "XTRUE" ];then
        G_VERBOSE_OUTPUT=TRUE
    elif [ -n "${para_record_csv}" ];then
        G_VERBOSE_OUTPUT=FALSE
    fi
}

## Description:
##   common framework to run test multiple times
##   also write result into one csv file for comparing manually
## Usage:
##    output_test_result $test_name $result [ $measurement [ $units ] ]
## Note:
##    RECORD_RESULT_LOCAL: when this environment variant is set to "TRUE",
##         the result will be recorded in a csv file in the following path:
##              rawdata/final_result.csv
run_test(){
    func_parse_parameters_common "$@"
    if [ "X${G_RECORD_LOCAL_CSV}" = "XTRUE" ]; then
        [ -f "${F_RAW_DATA_CSV}" ] && rm "${F_RAW_DATA_CSV}"
        [ -f "${F_STATISTIC_DATA_CSV}" ] && rm "${F_STATISTIC_DATA_CSV}"
        mkdir -p $(dirname ${F_RAW_DATA_CSV})
    fi

    loop_index=0
    while [ ${loop_index} -lt ${G_LOOP_COUNT} ]; do
        if [ -n "${var_test_func}" ]; then
            ${var_test_func}
        fi
        loop_index=$((loop_index + 1))
    done

    if [ "X${G_RECORD_LOCAL_CSV}" = "XTRUE" ]; then
        statistic ${F_RAW_DATA_CSV} 2 |tee ${F_STATISTIC_DATA_CSV}
        sed -i 's/=/,/' "${F_STATISTIC_DATA_CSV}"

        attach_cmd="lava-test-run-attach"
        if [ -n "$(which ${attach_cmd})" ]; then
            if [ -f "${F_RAW_DATA_CSV}" ]; then
                ${attach_cmd} ${F_RAW_DATA_CSV} text/plain
            fi
            if [ -f "${F_STATISTIC_DATA_CSV}" ]; then
                ${attach_cmd} ${F_STATISTIC_DATA_CSV} text/plain
            fi
        fi
    fi
}