github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/metrics/network/iperf3_kubernetes/k8s-network-metrics-iperf3.sh (about) 1 #!/bin/bash 2 # 3 # Copyright (c) 2021 Intel Corporation 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 # This test measures the following network essentials: 8 # - bandwith simplex 9 # - jitter 10 # 11 # These metrics/results will be got from the interconnection between 12 # a client and a server using iperf3 tool. 13 # The following cases are covered: 14 # 15 # case 1: 16 # container-server <----> container-client 17 # 18 # case 2" 19 # container-server <----> host-client 20 21 set -e 22 23 SCRIPT_PATH=$(dirname "$(readlink -f "$0")") 24 25 source "${SCRIPT_PATH}/../../../.ci/lib.sh" 26 source "${SCRIPT_PATH}/../../lib/common.bash" 27 iperf_file=$(mktemp iperfresults.XXXXXXXXXX) 28 TEST_NAME="${TEST_NAME:-network-iperf3}" 29 COLLECT_ALL="${COLLECT_ALL:-false}" 30 CI_JOB="${CI_JOB:-}" 31 32 function remove_tmp_file() { 33 rm -rf "${iperf_file}" 34 } 35 36 trap remove_tmp_file EXIT 37 38 function iperf3_all_collect_results() { 39 metrics_json_init 40 metrics_json_start_array 41 local json="$(cat << EOF 42 { 43 "bandwidth": { 44 "Result" : $bandwidth_result, 45 "Units" : "$bandwidth_units" 46 }, 47 "jitter": { 48 "Result" : $jitter_result, 49 "Units" : "$jitter_units" 50 }, 51 "cpu": { 52 "Result" : $cpu_result, 53 "Units" : "$cpu_units" 54 }, 55 "parallel": { 56 "Result" : $parallel_result, 57 "Units" : "$parallel_units" 58 } 59 } 60 EOF 61 )" 62 metrics_json_add_array_element "$json" 63 metrics_json_end_array "Results" 64 } 65 66 function iperf3_bandwidth() { 67 # Start server 68 local transmit_timeout="30" 69 70 kubectl exec -i "$client_pod_name" -- sh -c "iperf3 -J -c ${server_ip_add} -t ${transmit_timeout}" | jq '.end.sum_received.bits_per_second' > "${iperf_file}" 71 export bandwidth_result=$(cat "${iperf_file}") 72 export bandwidth_units="bits per second" 73 74 if [ "$COLLECT_ALL" == "true" ]; then 75 iperf3_all_collect_results 76 else 77 metrics_json_init 78 metrics_json_start_array 79 80 local json="$(cat << EOF 81 { 82 "bandwidth": { 83 "Result" : $bandwidth_result, 84 "Units" : "$bandwidth_units" 85 } 86 } 87 EOF 88 )" 89 metrics_json_add_array_element "$json" 90 metrics_json_end_array "Results" 91 fi 92 } 93 94 function iperf3_jitter() { 95 # Start server 96 local transmit_timeout="30" 97 98 kubectl exec -i "$client_pod_name" -- sh -c "iperf3 -J -c ${server_ip_add} -u -t ${transmit_timeout}" | jq '.end.sum.jitter_ms' > "${iperf_file}" 99 result=$(cat "${iperf_file}") 100 export jitter_result=$(printf "%0.3f\n" $result) 101 export jitter_units="ms" 102 103 if [ "$COLLECT_ALL" == "true" ]; then 104 iperf3_all_collect_results 105 else 106 metrics_json_init 107 metrics_json_start_array 108 109 local json="$(cat << EOF 110 { 111 "jitter": { 112 "Result" : $jitter_result, 113 "Units" : "ms" 114 } 115 } 116 EOF 117 )" 118 metrics_json_add_array_element "$json" 119 metrics_json_end_array "Results" 120 fi 121 } 122 123 function iperf3_parallel() { 124 # This will measure four parallel connections with iperf3 125 kubectl exec -i "$client_pod_name" -- sh -c "iperf3 -J -c ${server_ip_add} -P 4" | jq '.end.sum_received.bits_per_second' > "${iperf_file}" 126 export parallel_result=$(cat "${iperf_file}") 127 export parallel_units="bits per second" 128 129 if [ "$COLLECT_ALL" == "true" ]; then 130 iperf3_all_collect_results 131 else 132 metrics_json_init 133 metrics_json_start_array 134 135 local json="$(cat << EOF 136 { 137 "parallel": { 138 "Result" : $parallel_result, 139 "Units" : "$parallel_units" 140 } 141 } 142 EOF 143 )" 144 metrics_json_add_array_element "$json" 145 metrics_json_end_array "Results" 146 fi 147 } 148 149 function iperf3_cpu() { 150 # Start server 151 local transmit_timeout="80" 152 153 kubectl exec -i "$client_pod_name" -- sh -c "iperf3 -J -c ${server_ip_add} -t ${transmit_timeout}" | jq '.end.cpu_utilization_percent.host_total' > "${iperf_file}" 154 export cpu_result=$(cat "${iperf_file}") 155 export cpu_units="percent" 156 157 if [ "$COLLECT_ALL" == "true" ]; then 158 iperf3_all_collect_results 159 else 160 metrics_json_init 161 metrics_json_start_array 162 163 local json="$(cat << EOF 164 { 165 "cpu": { 166 "Result" : $cpu_result, 167 "Units" : "$cpu_units" 168 } 169 } 170 EOF 171 )" 172 173 metrics_json_add_array_element "$json" 174 metrics_json_end_array "Results" 175 fi 176 } 177 178 function iperf3_start_deployment() { 179 cmds=("bc" "jq") 180 check_cmds "${cmds[@]}" 181 182 # Check no processes are left behind 183 check_processes 184 185 if [ -z "${CI_JOB}" ]; then 186 # Start kubernetes 187 start_kubernetes 188 fi 189 190 export KUBECONFIG="$HOME/.kube/config" 191 export service="iperf3-server" 192 export deployment="iperf3-server-deployment" 193 194 wait_time=20 195 sleep_time=2 196 197 # Create deployment 198 kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/iperf3-deployment.yaml" 199 200 # Check deployment creation 201 local cmd="kubectl wait --for=condition=Available deployment/${deployment}" 202 waitForProcess "$wait_time" "$sleep_time" "$cmd" 203 204 # Create DaemonSet 205 kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/iperf3-daemonset.yaml" 206 207 # Expose deployment 208 kubectl expose deployment/"${deployment}" 209 210 # Get the names of the server pod 211 export server_pod_name=$(kubectl get pods -o name | grep server | cut -d '/' -f2) 212 213 # Verify the server pod is working 214 local cmd="kubectl get pod $server_pod_name -o yaml | grep 'phase: Running'" 215 waitForProcess "$wait_time" "$sleep_time" "$cmd" 216 217 # Get the names of client pod 218 export client_pod_name=$(kubectl get pods -o name | grep client | cut -d '/' -f2) 219 220 # Verify the client pod is working 221 local cmd="kubectl get pod $client_pod_name -o yaml | grep 'phase: Running'" 222 waitForProcess "$wait_time" "$sleep_time" "$cmd" 223 224 # Get the ip address of the server pod 225 export server_ip_add=$(kubectl get pod "$server_pod_name" -o jsonpath='{.status.podIP}') 226 } 227 228 function iperf3_deployment_cleanup() { 229 kubectl delete pod "$server_pod_name" "$client_pod_name" 230 kubectl delete ds iperf3-clients 231 kubectl delete deployment "$deployment" 232 kubectl delete service "$deployment" 233 if [ -z "${CI_JOB}" ]; then 234 end_kubernetes 235 check_processes 236 fi 237 } 238 239 function help() { 240 echo "$(cat << EOF 241 Usage: $0 "[options]" 242 Description: 243 This script implements a number of network metrics 244 using iperf3. 245 246 Options: 247 -a Run all tests 248 -b Run bandwidth tests 249 -c Run cpu metrics tests 250 -h Help 251 -j Run jitter tests 252 EOF 253 )" 254 } 255 256 function main() { 257 init_env 258 iperf3_start_deployment 259 260 local OPTIND 261 while getopts ":abcjph:" opt 262 do 263 case "$opt" in 264 a) # all tests 265 test_all="1" 266 ;; 267 b) # bandwith test 268 test_bandwith="1" 269 ;; 270 c) 271 # run cpu tests 272 test_cpu="1" 273 ;; 274 h) 275 help 276 exit 0; 277 ;; 278 j) # jitter tests 279 test_jitter="1" 280 ;; 281 p) 282 # run parallel tests 283 test_parallel="1" 284 ;; 285 :) 286 echo "Missing argument for -$OPTARG"; 287 help 288 exit 1; 289 ;; 290 esac 291 done 292 shift $((OPTIND-1)) 293 294 [[ -z "$test_bandwith" ]] && \ 295 [[ -z "$test_jitter" ]] && \ 296 [[ -z "$test_cpu" ]] && \ 297 [[ -z "$test_parallel" ]] && \ 298 [[ -z "$test_all" ]] && \ 299 help && die "Must choose at least one test" 300 301 if [ "$test_bandwith" == "1" ]; then 302 iperf3_bandwidth 303 fi 304 305 if [ "$test_jitter" == "1" ]; then 306 iperf3_jitter 307 fi 308 309 if [ "$test_cpu" == "1" ]; then 310 iperf3_cpu 311 fi 312 313 if [ "$test_parallel" == "1" ]; then 314 iperf3_parallel 315 fi 316 317 if [ "$test_all" == "1" ]; then 318 export COLLECT_ALL=true && iperf3_bandwidth && iperf3_jitter && iperf3_cpu && iperf3_parallel 319 fi 320 321 metrics_json_save 322 iperf3_deployment_cleanup 323 } 324 325 main "$@"