github.com/dmaizel/tests@v0.0.0-20210728163746-cae6a2d9cee8/metrics/network/network-metrics-nuttcp.sh (about) 1 #!/bin/bash 2 # 3 # Copyright (c) 2018 Intel Corporation 4 # 5 # SPDX-License-Identifier: Apache-2.0 6 # 7 # Description: 8 # This metrics test measures the UDP network bandwidth using nuttcp 9 # in a interconnection container-client <----> container-server. 10 11 set -e 12 13 SCRIPT_PATH=$(dirname "$(readlink -f "$0")") 14 source "${SCRIPT_PATH}/lib/network-common.bash" 15 source "${SCRIPT_PATH}/../lib/common.bash" 16 17 # Measurement time (seconds) 18 transmit_timeout="${transmit_timeout:-30}" 19 # Image name 20 image="${IMAGE:-local-nuttcp}" 21 # Dockerfile 22 dockerfile="${SCRIPT_PATH}/nuttcp_dockerfile/Dockerfile" 23 # Name of the server container 24 server_name="${server_name:-network-server}" 25 # Nuttcp version 26 nuttcp_version="${nuttcp_version:-7.3.2}" 27 28 function udp_default_buffer_size { 29 # Test UDP Jumbo (9000 byte MTU) packets 30 # Packet header (ICMP+IP) is 28bytes, so maximum payload is 8972 bytes 31 # See the nuttcp documentation for more hints: 32 # https://fasterdata.es.net/performance-testing/network-troubleshooting-tools/nuttcp/ 33 local bl=8972 34 local TEST_NAME="${TEST_NAME:-nuttcp test with ${bl} buffer size}" 35 local result="$(udp_bandwidth)" 36 save_results "$TEST_NAME" "$result" 37 } 38 39 function udp_specific_buffer_size { 40 # Test UDP standard (1500 byte MTU) packets 41 # Even though the packet header (ICMP+IP) is 28 bytes, which would 42 # result in 1472 byte frames, the nuttcp documentation recommends 43 # use of 1200 byte payloads. 44 # See the nuttcp examples.txt for more information: 45 # http://nuttcp.net/nuttcp/5.1.3/examples.txt 46 local bl=1200 47 local TEST_NAME="${TEST_NAME:-nuttcp test with ${bl} buffer size}" 48 local result="$(udp_bandwidth)" 49 save_results "$TEST_NAME" "$result" 50 } 51 52 function udp_bandwidth { 53 # Arguments to run the client/server 54 local server_extra_args="--name=$server_name" 55 local client_extra_args="--rm" 56 57 local server_command="tail -f /dev/null" 58 local server_address=$(start_server "$image" "$server_command" "$server_extra_args") 59 60 local client_command="/root/nuttcp -T${transmit_timeout} -u -Ru -i1 -l${bl} ${server_address}" 61 local server_command="/root/nuttcp -u -S" 62 63 docker exec ${server_name} sh -c "${server_command}" 64 output=$(start_client "$image" "$client_command" "$client_extra_args") 65 66 clean_env 67 echo "$output" 68 69 # Check no processes are left behind 70 check_processes 71 } 72 73 function save_results { 74 local TEST_NAME="$1" 75 local result="$2" 76 77 if [ -z "$result" ]; then 78 die "no result output" 79 fi 80 81 metrics_json_init 82 save_config 83 84 metrics_json_start_array 85 86 local result_line=$(echo "$result" | tail -1) 87 local -a results 88 read -a results <<< ${result_line%$'\r'} 89 90 local total_bandwidth=${results[6]} 91 local total_bandwidth_units=${results[7]} 92 local total_loss=${results[16]} 93 local total_loss_units="%" 94 95 local json="$(cat << EOF 96 { 97 "bandwidth": { 98 "Result" : $total_bandwidth, 99 "Units" : "$total_bandwidth_units" 100 }, 101 "packet loss": { 102 "Result" : $total_loss, 103 "Units" : "$total_loss_units" 104 } 105 } 106 EOF 107 )" 108 109 metrics_json_add_array_element "$json" 110 metrics_json_end_array "Results" 111 metrics_json_save 112 } 113 114 function help { 115 echo "$(cat << EOF 116 Usage: $0 "[options]" 117 Description: 118 This script measures the UDP network bandwidth 119 using different buffer sizes. 120 121 Options: 122 -a Run all nuttcp tests 123 -b Run with default buffer size (8972) 124 -c Run with specific buffer size (1200) 125 -h Shows help 126 EOF 127 )" 128 } 129 130 function save_config { 131 metrics_json_start_array 132 133 local json="$(cat << EOF 134 { 135 "image": "$image", 136 "transmit timeout": "$transmit_timeout", 137 "nuttcp version": "$nuttcp_version" 138 } 139 EOF 140 )" 141 metrics_json_add_array_element "$json" 142 metrics_json_end_array "Config" 143 } 144 145 function main() { 146 local OPTIND 147 while getopts ":abch:" opt 148 do 149 case "$opt" in 150 a) # Run all nuttcp tests 151 test_bandwidth="1" 152 ;; 153 b) # UDP bandwidth with default buffer size 154 test_bandwidth_default="1" 155 ;; 156 c) # UDP bandwidth with specific buffer size 157 test_bandwidth_specific="1" 158 ;; 159 h) 160 help 161 exit 0; 162 ;; 163 \?) 164 echo "An invalid option has been entered: -$OPTARG"; 165 help 166 exit 1; 167 ;; 168 :) 169 echo "Missing argument for -$OPTARG"; 170 help 171 exit 1; 172 ;; 173 esac 174 done 175 shift $((OPTIND-1)) 176 177 [[ -z "$test_bandwidth" ]] && \ 178 [[ -z "$test_bandwidth_default" ]] && \ 179 [[ -z "$test_bandwidth_specific" ]] && \ 180 help && die "Must choose at least one test" 181 182 # Check tools/commands dependencies 183 cmds=("docker") 184 check_cmds "${cmds[@]}" 185 check_dockerfiles_images "$image" "$dockerfile" 186 187 # Check no processes are left behind 188 check_processes 189 190 # Initialize/clean environment 191 init_env 192 193 if [ "$test_bandwidth" == "1" ]; then 194 udp_default_buffer_size 195 udp_specific_buffer_size 196 fi 197 198 if [ "$test_bandwidth_default" == "1" ]; then 199 udp_default_buffer_size 200 fi 201 202 if [ "$test_bandwidth_specific" == "1" ]; then 203 udp_specific_buffer_size 204 fi 205 } 206 207 main "$@"