@@ -22,20 +22,27 @@ CMD="sleep 10; cat /proc/meminfo"
22
22
# which makes later direct comparison easier.
23
23
MEMSIZE=${MEMSIZE:- $((2048* 1024))}
24
24
25
+ # this variable determines the number of attempts when a test
26
+ # result is considered not valid (a zero value or a negative value)
27
+ MAX_FAILED_ATTEMPTS=3
25
28
memtotalAvg=0
26
29
units_memtotal=" "
27
30
memfreeAvg=0
28
31
units_memfree=" "
29
32
memavailableAvg=0
30
33
units_memavailable=" "
31
- total_iters=0
32
- header=0
33
34
35
+ # count_iters: is the index of the current iteration
36
+ count_iters=0
37
+
38
+ # valid_result: if value stored is '1' the result is valid, '0' otherwise
39
+ valid_result=0
34
40
35
41
parse_results () {
36
42
local raw_results=" ${1} "
37
43
38
- # variables that receive cummulative memory values for json case, or init to zero for csv case
44
+ # Variables used for sum cummulative values in the case of two or more reps.
45
+ # and used to compute average results for 'json' output format.
39
46
local memtotal_acu=" ${2:- 0} "
40
47
local memfree_acu=" ${3:- 0} "
41
48
local memavailable_acu=" ${4:- 0} "
@@ -49,21 +56,26 @@ parse_results() {
49
56
local memavailable=$( echo " $raw_results " | awk ' /MemAvailable/ {print $2}' )
50
57
units_memavailable=$( echo " $raw_results " | awk ' /MemAvailable/ {print $3}' )
51
58
59
+ # check results: if any result is zero or negative, it is considered as invalid, and the test will be repeated.
60
+ if (( $(echo "$memtotal <= 0 " | bc - l) )) || (( $(echo "$memfree <= 0 " | bc - l) )) || (( $(echo "$memavailable <= 0 " | bc - l) )) ; then
61
+ MAX_FAILED_ATTEMPTS=$(( MAX_FAILED_ATTEMPTS- 1 ))
62
+ valid_result=0
63
+ info " Skipping invalid result: memtotal: $memtotal memfree: $memfree memavailable: $memavailable "
64
+ return 0
65
+ fi
66
+
52
67
memtotalAvg=$(( memtotal+ memtotal_acu))
53
68
memfreeAvg=$(( memfree+ memfree_acu))
54
69
memavailableAvg=$(( memavailable+ memavailable_acu))
55
-
56
- let " total_iters=total_iters+1"
57
-
58
- echo " Iteration# $total_iters memtotal: $memtotal memfree: $memfree memavailable: $memavailable "
70
+ valid_result=1
71
+ info " Iteration# $count_iters memtotal: $memtotal memfree: $memfree memavailable: $memavailable "
59
72
}
60
73
61
-
62
74
store_results_json () {
63
75
metrics_json_start_array
64
- memtotalAvg=$(( memtotalAvg / total_iters ) )
65
- memfreeAvg=$(( memfreeAvg / total_iters ) )
66
- memavailableAvg=$(( memavailableAvg / total_iters ) )
76
+ memtotalAvg=$( echo " scale=4; $ memtotalAvg / $count_iters " | bc )
77
+ memfreeAvg=$( echo " scale=4; $ memfreeAvg / $count_iters " | bc )
78
+ memavailableAvg=$( echo " scale=4; $ memavailableAvg / $count_iters " | bc )
67
79
68
80
local json=" $( cat << EOF
69
81
{
@@ -84,7 +96,7 @@ store_results_json() {
84
96
"Units" : "${units_memavailable} "
85
97
},
86
98
"repetitions": {
87
- "Result" : ${total_iters }
99
+ "Result" : ${count_iters }
88
100
}
89
101
}
90
102
EOF
94
106
metrics_json_save
95
107
}
96
108
97
- store_results_csv () {
98
- local filename=${1}
99
-
100
- if [ $header -eq 0 ]; then
101
- echo " memtotal,units_memtotal,memfree,units_memfree,memavailable,units_memavailable" > $filename
102
- header=1
103
- fi
104
- echo " $memtotalAvg ,$units_memtotal ,$memfreeAvg ,$units_memfree ,$memavailableAvg ,$units_memavailable " >> $filename
105
- }
106
-
107
109
function main() {
108
110
# switch to select output format
109
- local output_format=${1:- json}
110
- local iterations=${2:- 1}
111
- local -r csv_filename=" mem-usage-inside-container-$iterations -iters-$( date +" %Y_%m_%d_%H-%M" ) .csv"
112
- echo " Output format: $output_format "
113
- echo " Iterations: $iterations "
111
+ local num_iterations=${1:- 1}
112
+ info " Iterations: $num_iterations "
114
113
115
114
# Check tools/commands dependencies
116
115
cmds=(" awk" " ctr" )
117
-
118
116
init_env
119
117
check_cmds " ${cmds[@]} "
120
118
check_images " ${IMAGE} "
121
-
122
119
metrics_json_init
123
120
124
- for (( i = 0 ; i < $iterations ; i ++ )) do
121
+ while [ $count_iters -lt $num_iterations ] ; do
125
122
local output=$( sudo -E " ${CTR_EXE} " run --memory-limit $(( MEMSIZE* 1024 )) --rm --runtime=$CTR_RUNTIME $IMAGE busybox sh -c " $CMD " 2>&1 )
123
+ parse_results " ${output} " " ${memtotalAvg} " " ${memfreeAvg} " " ${memavailableAvg} "
126
124
127
- if [ ${output_format} = " json" ]; then
128
- parse_results " ${output} " " ${memtotalAvg} " " ${memfreeAvg} " " ${memavailableAvg} "
129
-
130
- # Record results per iteration
131
- elif [ ${output_format} = " csv" ]; then
132
- parse_results " ${output} "
133
- store_results_csv ${csv_filename}
134
- fi
135
- sleep 0.5
125
+ # quit if number of attempts exceeds the allowed value.
126
+ [ ${MAX_FAILED_ATTEMPTS} -eq 0 ] && die " Max number of attempts exceeded."
127
+ [ ${valid_result} -eq 1 ] && count_iters=$(( count_iters+ 1 ))
136
128
done
137
-
138
- if [ $output_format = " json" ]; then
139
- store_results_json
140
- fi
141
-
129
+ store_results_json
142
130
clean_env_ctr
143
131
}
144
132
145
133
# Parameters
146
- # @1: output format [json/csv]
147
- # @2: iterations {integer}
134
+ # @1: num_iterations {integer}
148
135
main " $@ "
0 commit comments