github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/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 test_repo="${test_repo:-github.com/kata-containers/tests}" 28 iperf_file=$(mktemp iperfresults.XXXXXXXXXX) 29 30 function remove_tmp_file() { 31 rm -rf "${iperf_file}" 32 } 33 34 trap remove_tmp_file EXIT 35 36 function iperf3_bandwidth() { 37 iperf3_start_deployment 38 local TEST_NAME="network iperf3 bandwidth" 39 metrics_json_init 40 41 # Start server 42 local transmit_timeout="30" 43 44 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}" 45 result=$(cat "${iperf_file}") 46 47 local json="$(cat << EOF 48 { 49 "bandwidth": { 50 "Result" : $result, 51 "Units" : "bits per second" 52 } 53 } 54 EOF 55 )" 56 57 metrics_json_add_array_element "$json" 58 metrics_json_end_array "Results" 59 60 metrics_json_save 61 iperf3_deployment_cleanup 62 } 63 64 function iperf3_utc_jitter() { 65 iperf3_start_deployment 66 local TEST_NAME="network iperf3 utc jitter" 67 metrics_json_init 68 69 # Start server 70 local transmit_timeout="30" 71 72 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}" 73 result=$(cat "${iperf_file}") 74 75 local json="$(cat << EOF 76 { 77 "jitter": { 78 "Result" : $result, 79 "Units" : "ms" 80 } 81 } 82 EOF 83 )" 84 85 metrics_json_add_array_element "$json" 86 metrics_json_end_array "Results" 87 88 metrics_json_save 89 iperf3_deployment_cleanup 90 } 91 92 function cpu_metrics_iperf3() { 93 cmd=("awk") 94 check_cmds "${cmds[@]}" 95 96 iperf3_start_deployment 97 local TEST_NAME="cpu metrics running iperf3" 98 99 # Start server 100 local transmit_timeout="80" 101 102 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}" 103 result=$(cat "${iperf_file}") 104 105 metrics_json_init 106 107 local json="$(cat << EOF 108 { 109 "cpu utilization host total": { 110 "Result" : $result, 111 "Units" : "percent" 112 } 113 } 114 EOF 115 )" 116 117 metrics_json_add_array_element "$json" 118 metrics_json_end_array "Results" 119 120 metrics_json_save 121 iperf3_deployment_cleanup 122 } 123 124 function iperf3_start_deployment() { 125 cmds=("bc" "jq") 126 check_cmds "${cmds[@]}" 127 128 # Check no processes are left behind 129 check_processes 130 131 # Start kubernetes 132 start_kubernetes 133 134 export KUBECONFIG="$HOME/.kube/config" 135 export service="iperf3-server" 136 export deployment="iperf3-server-deployment" 137 138 wait_time=20 139 sleep_time=2 140 141 # Create deployment 142 kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/iperf3-deployment.yaml" 143 144 # Check deployment creation 145 local cmd="kubectl wait --for=condition=Available deployment/${deployment}" 146 waitForProcess "$wait_time" "$sleep_time" "$cmd" 147 148 # Create DaemonSet 149 kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/iperf3-daemonset.yaml" 150 151 # Expose deployment 152 kubectl expose deployment/"${deployment}" 153 154 # Get the names of the server pod 155 export server_pod_name=$(kubectl get pods -o name | grep server | cut -d '/' -f2) 156 157 # Verify the server pod is working 158 local cmd="kubectl get pod $server_pod_name -o yaml | grep 'phase: Running'" 159 waitForProcess "$wait_time" "$sleep_time" "$cmd" 160 161 # Get the names of client pod 162 export client_pod_name=$(kubectl get pods -o name | grep client | cut -d '/' -f2) 163 164 # Verify the client pod is working 165 local cmd="kubectl get pod $client_pod_name -o yaml | grep 'phase: Running'" 166 waitForProcess "$wait_time" "$sleep_time" "$cmd" 167 168 # Get the ip address of the server pod 169 export server_ip_add=$(kubectl get pod "$server_pod_name" -o jsonpath='{.status.podIP}') 170 } 171 172 function iperf3_deployment_cleanup() { 173 kubectl delete deployment "$deployment" 174 kubectl delete service "$deployment" 175 end_kubernetes 176 check_processes 177 } 178 179 function start_kubernetes() { 180 info "Start k8s" 181 pushd "${GOPATH}/src/${test_repo}/integration/kubernetes" 182 bash ./init.sh 183 popd 184 } 185 186 function end_kubernetes() { 187 info "End k8s" 188 pushd "${GOPATH}/src/${test_repo}/integration/kubernetes" 189 bash ./cleanup_env.sh 190 popd 191 } 192 193 function help() { 194 echo "$(cat << EOF 195 Usage: $0 "[options]" 196 Description: 197 This script implements a number of network metrics 198 using iperf3. 199 200 Options: 201 -a Run all tests 202 -b Run bandwidth tests 203 -c Run cpu metrics tests 204 -h Help 205 -j Run jitter tests 206 EOF 207 )" 208 } 209 210 function main() { 211 local OPTIND 212 while getopts ":abcjh:" opt 213 do 214 case "$opt" in 215 a) # all tests 216 test_bandwidth="1" 217 test_jitter="1" 218 ;; 219 b) # bandwith test 220 test_bandwith="1" 221 ;; 222 c) 223 # run cpu tests 224 test_cpu="1" 225 ;; 226 h) 227 help 228 exit 0; 229 ;; 230 j) # jitter tests 231 test_jitter="1" 232 ;; 233 :) 234 echo "Missing argument for -$OPTARG"; 235 help 236 exit 1; 237 ;; 238 esac 239 done 240 shift $((OPTIND-1)) 241 242 [[ -z "$test_bandwith" ]] && \ 243 [[ -z "$test_jitter" ]] && \ 244 [[ -z "$test_cpu" ]] && \ 245 help && die "Must choose at least one test" 246 247 if [ "$test_bandwith" == "1" ]; then 248 iperf3_bandwidth 249 fi 250 251 if [ "$test_jitter" == "1" ]; then 252 iperf3_jitter 253 fi 254 255 if [ "$test_cpu" == "1" ]; then 256 cpu_metrics_iperf3 257 fi 258 } 259 260 main "$@"