github.com/kata-containers/tests@v0.0.0-20240307153542-772105b56064/metrics/network/nginx_kubernetes/nginx-network.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2023 Intel Corporation
     4  #
     5  # SPDX-License-Identifier: Apache-2.0
     6  
     7  set -e
     8  
     9  SCRIPT_PATH=$(dirname "$(readlink -f "$0")")
    10  
    11  source "${SCRIPT_PATH}/../../../.ci/lib.sh"
    12  source "${SCRIPT_PATH}/../../lib/common.bash"
    13  nginx_file=$(mktemp nginxresults.XXXXXXXXXX)
    14  TEST_NAME="${TEST_NAME:-nginx}"
    15  CI_JOB="${CI_JOB:-}"
    16  
    17  function remove_tmp_file() {
    18  	rm -rf "${nginx_file}"
    19  }
    20  
    21  trap remove_tmp_file EXIT
    22  
    23  function main() {
    24  	init_env
    25  	cmds=("bc" "jq" "ab")
    26  	check_cmds "${cmds[@]}"
    27  
    28  	# Check no processes are left behind
    29  	check_processes
    30  
    31  	if [ -z "${CI_JOB}" ]; then
    32  		# Start kubernetes
    33  		start_kubernetes
    34  	fi
    35  
    36  	export KUBECONFIG="$HOME/.kube/config"
    37  
    38   	wait_time=20
    39   	sleep_time=2
    40  	timeout="20s"
    41  
    42  	deployment="nginx-deployment"
    43  	kubectl create -f "${SCRIPT_PATH}/runtimeclass_workloads/nginx-networking.yaml"
    44  	kubectl wait --for=condition=Available --timeout="${timeout}" deployment/"${deployment}"
    45  	kubectl expose deployment/"${deployment}"
    46  	ip=$(kubectl get service/nginx-deployment -o jsonpath='{.spec.clusterIP}')
    47  
    48  	ab -n 100000 -c 100 http://"${ip}":80/ > "${nginx_file}"
    49  	metrics_json_init
    50   	rps=$(cat "${nginx_file}" | grep "Requests" | awk '{print $4}')
    51  	echo "Requests per second: ${rps}"
    52  
    53  	metrics_json_start_array
    54  
    55  	local json="$(cat << EOF
    56  	{
    57  		"requests": {
    58  			"Result" : ${rps},
    59  			"Units": "rps"
    60  		}
    61  	}
    62  EOF
    63  )"
    64  
    65  	metrics_json_add_array_element "$json"
    66  	metrics_json_end_array "Results"
    67  	metrics_json_save
    68  
    69  	nginx_cleanup
    70  }
    71  
    72  function nginx_cleanup() {
    73  	kubectl delete deployment "${deployment}"
    74  	kubectl delete service "${deployment}"
    75  	if [ -z "${CI_JOB}" ]; then
    76  		end_kubernetes
    77  		check_processes
    78  	fi
    79  }
    80  
    81  main "$@"