Add partition offset metric to each topic partition
[dcaegen2/collectors/hv-ves.git] / tools / performance / cloud / cloud-based-performance-test.sh
1 #!/usr/bin/env bash
2 # ============LICENSE_START=======================================================
3 # dcaegen2-collectors-veshv
4 # ================================================================================
5 # Copyright (C) 2019 NOKIA
6 # ================================================================================
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 #      http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 # ============LICENSE_END=========================================================
19
20 SCRIPT_DIRECTORY="$(pwd "$0")"
21 CONTAINERS_COUNT=1
22 PROPERTIES_FILE=${SCRIPT_DIRECTORY}/test.properties
23 CONFIG_MAP_NAME=performance-test-config
24 PRODUCER_APPS_LABEL=hv-collector-producer
25 CONSUMER_APPS_LABEL=hv-collector-kafka-consumer
26 PROMETHEUS_CONF_LABEL=prometheus-server-conf
27 PROMETHEUS_APPS_LABEL=hv-collector-prometheus
28 ONAP_NAMESPACE=onap
29 MAXIMUM_BACK_OFF_CHECK_ITERATIONS=30
30 CHECK_NUMBER=0
31 NAME_REASON_PATTERN="custom-columns=NAME:.metadata.name,REASON:.status.containerStatuses[].state.waiting.reason"
32
33 function clean() {
34     echo "Cleaning up environment"
35
36     echo "Attempting to delete test parameters ConfigMap"
37     kubectl delete configmap ${CONFIG_MAP_NAME} -n ${ONAP_NAMESPACE}
38
39     echo "Attempting to delete prometheus ConfigMap"
40     kubectl delete configmap -l name=${PROMETHEUS_CONF_LABEL} -n ${ONAP_NAMESPACE}
41
42     echo "Attempting to delete prometheus deployment and service"
43     kubectl delete service,deployments -l app=${PROMETHEUS_APPS_LABEL} -n ${ONAP_NAMESPACE}
44
45     echo "Attempting to delete consumer deployments"
46     kubectl delete deployments -l app=${CONSUMER_APPS_LABEL} -n ${ONAP_NAMESPACE}
47
48     echo "Attempting to delete producer pods"
49     kubectl delete pods -l app=${PRODUCER_APPS_LABEL} -n ${ONAP_NAMESPACE}
50
51     echo "Environment clean up finished!"
52 }
53
54 function create_producers() {
55     set -e
56     for i in $(seq 1 ${CONTAINERS_COUNT});
57     do
58         echo "Creating ${i}/${CONTAINERS_COUNT} producer"
59         kubectl create -f producer-pod.yaml -n ${ONAP_NAMESPACE}
60     done
61     echo "Producers created"
62     set +e
63 }
64
65 function usage() {
66     echo ""
67     echo "Run cloud based HV-VES performance test"
68     echo "Usage $0 setup|start|clean|help"
69     echo "  setup    : set up ConfigMap and consumers"
70     echo "  start    : create producers - start the performance test"
71     echo "    Optional parameters:"
72     echo "      --containers      : number of producer containers to create (1)"
73     echo "      --properties-file : path to file with benchmark properties (./test.properties)"
74     echo "  clean    : remove ConfigMap, HV-VES consumers and producers"
75     echo "  help     : print usage"
76     echo "Example invocations:"
77     echo "./cloud-based-performance-test.sh setup"
78     echo "./cloud-based-performance-test.sh start"
79     echo "./cloud-based-performance-test.sh start --containers 10"
80     echo "./cloud-based-performance-test.sh start --properties-file ~/other_test.properties"
81     echo "./cloud-based-performance-test.sh clean"
82     exit 1
83 }
84
85 function setup_environment() {
86     echo "Setting up environment"
87     echo "Creating test properties ConfigMap from: $PROPERTIES_FILE"
88     kubectl create configmap ${CONFIG_MAP_NAME} --from-env-file=${PROPERTIES_FILE} -n ${ONAP_NAMESPACE}
89
90     echo "Creating consumer deployment"
91     kubectl apply -f consumer-deployment.yaml
92
93     echo "Creating ConfigMap for prometheus deployment"
94     kubectl apply -f prometheus-config-map.yaml
95
96     echo "Creating prometheus deployment"
97     kubectl apply -f prometheus-deployment.yaml
98
99     echo "Waiting for consumers to be running."
100     while [[ $(kubectl get pods -l app=${CONSUMER_APPS_LABEL} -n ${ONAP_NAMESPACE} | grep -c "unhealthy\|starting") -ne 0 ]] ; do
101         sleep 1
102     done
103     echo "Setting up environment finished!"
104 }
105
106 function start_performance_test() {
107     echo "Starting cloud based performance tests"
108     echo "________________________________________"
109     echo "Test configuration:"
110     echo "Producer containers count: ${CONTAINERS_COUNT}"
111     echo "Properties file path: ${PROPERTIES_FILE}"
112     echo "________________________________________"
113
114     create_producers
115
116     echo "Waiting for producers completion"
117     while :; do
118         COMPLETED_PRODUCERS=$(kubectl get pods -l app=${PRODUCER_APPS_LABEL} -n ${ONAP_NAMESPACE} | grep -c "Completed")
119         IMAGE_PULL_BACK_OFFS=$(kubectl get pods -l app=${PRODUCER_APPS_LABEL} -n ${ONAP_NAMESPACE} -o ${NAME_REASON_PATTERN} | grep -c "ImagePullBackOff \| ErrImagePull")
120
121         if [[ ${IMAGE_PULL_BACK_OFFS} -gt 0 ]]; then
122             CHECK_NUMBER=$((CHECK_NUMBER + 1))
123             if [[ ${CHECK_NUMBER} -gt ${MAXIMUM_BACK_OFF_CHECK_ITERATIONS} ]]; then
124                 echo "Error: Image pull problem"
125                 exit 1
126             fi
127         fi
128         
129         [[ ${COMPLETED_PRODUCERS} -eq ${CONTAINERS_COUNT} || ${CHECK_NUMBER} -gt ${MAXIMUM_BACK_OFF_CHECK_ITERATIONS} ]] && break
130         sleep 1
131     done
132
133     echo "Performance test finished"
134     exit 0
135 }
136
137 cd ${SCRIPT_DIRECTORY}
138
139 if [[ $# -eq 0 ]]; then
140     usage
141 else
142     for arg in ${@}
143     do
144         case ${arg} in
145             setup)
146             setup_environment
147             ;;
148             start)
149             shift 1
150             while [[ $(($#)) -gt 0 ]]; do
151                 case "${1}" in
152                     --containers)
153                         CONTAINERS_COUNT=${2}
154                         ;;
155                     --properties-file)
156                         PROPERTIES_FILE=${2}
157                         ;;
158                     *)
159                         echo "Unknown option: ${1}"
160                         usage
161                         ;;
162                 esac
163                 shift 2
164             done
165             start_performance_test
166             ;;
167             clean)
168             clean
169             ;;
170             help)
171             usage
172             ;;
173             *)
174             echo "Unknown action: ${arg}" >&2
175             usage
176             ;;
177         esac
178     done
179 fi