82287e6f1c0eb51300510cc700d98d0bb3d47cd6
[dcaegen2/collectors/hv-ves.git] / tools / performance / local / local-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 CERT_FILE=${CERT_FILE:-/ssl/client.p12}
22 CERT_PASS_FILE=${CERT_PASS_FILE:-/ssl/client.pass}
23 HV_VES_NETWORK=${HV_VES_NETWORK:-local_default}
24 VOLUME_MAPPING=${VOLUME_MAPPING:-$SCRIPT_DIRECTORY/../../ssl/:/ssl}
25 PRODUCER_IMAGE_NAME=${PRODUCER_IMAGE_NAME:-the-a-team-registry-local.esisoj70.emea.nsn-net.net/onap/org.onap.dcaegen2.collectors.hv-ves.hv-collector-go-client:latest}
26
27 PRODUCER_APP_NAME=hv-ves-producer
28 HV_VES_ADDRESS=ves-hv-collector:6061
29 CONTAINERS_COUNT=1
30 CLIENTS_PER_CONTAINER=1
31 MSG_SIZE=16384
32 MSG_COUNT=1000
33 INTERVAL_MS=0
34
35 function usage() {
36     echo ""
37     echo "Run HV-VES performance test locally"
38     echo "Usage $0 setup|start|clean|help"
39     echo "  setup    : generate certs and set up docker components"
40     echo "  start    : run the performance test"
41     echo "    Optional parameters:"
42     echo "      --address    : HV-VES address in host:port format (ves-hv-collector:6061)"
43     echo "      --containers : number of docker containers to create (1)"
44     echo "      --clients    : number of clients in a single container (1)"
45     echo "      --msg-size   : size in bytes of a single message (16384)"
46     echo "      --msg-count  : amount of messages to sent by one client in single container (1000)"
47     echo "      --interval   : interval between messages (0)"
48     echo "  clean    : remove generated certs, HV-VES components and producers"
49     echo "  help     : print usage"
50     echo "Example invocations:"
51     echo "./local-performance-test.sh setup"
52     echo "./local-performance-test.sh start --containers 10 --clients 100 --msg-count 10000"
53     echo "./local-performance-test.sh clean"
54     exit 1
55 }
56
57 function setup_environment(){
58     echo "Setting up"
59     cd ../../ssl
60     ./gen-certs.sh
61     cd "$SCRIPT_DIRECTORY"
62     docker-compose up -d
63
64     echo "Waiting for components to be healthy.."
65     while [[ $(docker-compose ps | grep -c "unhealthy\|starting") -ne 0 ]] ; do
66         sleep 1
67     done
68
69     echo "All components ready"
70     exit 0
71 }
72
73 function start_performance_test(){
74
75     TEST_ID=$(date +%s)
76     create_containers ${CONTAINERS_COUNT} ${TEST_ID} &
77
78     while :; do
79         ACTIVE_PRODUCERS=$(docker ps  --format "table {{.ID}}\t{{.Status}}" -f "label=id=$TEST_ID")
80         ACTIVE_PRODUCERS_COUNT=$(echo "$ACTIVE_PRODUCERS" | grep -c "Up")
81
82         clear
83         print_test_configuration
84         echo "Active producers ($ACTIVE_PRODUCERS_COUNT/$CONTAINERS_COUNT):"
85         echo "$ACTIVE_PRODUCERS"
86
87         EXITED_CONTAINERS=$(docker ps -aq -f "label=id=$TEST_ID" -f status=exited | wc -l)
88         [[ ${EXITED_CONTAINERS} -eq ${CONTAINERS_COUNT} ]] && break
89
90         sleep 1
91     done
92
93     clear
94     print_test_configuration
95     echo "Test finished"
96     # TODO put test result here
97     exit 0
98 }
99
100 function print_test_configuration(){
101         echo "PERFORMANCE TEST IN PROGRESS"
102         echo ""
103         echo "Test configuration:"
104         echo "Containers count: $CONTAINERS_COUNT"
105         echo "Clients per container: $CLIENTS_PER_CONTAINER"
106         echo "Message size: $MSG_SIZE"
107         echo "Messages per client: $MSG_COUNT"
108         echo "Interval: $INTERVAL_MS"
109         echo ""
110 }
111
112 function create_containers(){
113
114     for i in $(seq 1 ${1}); do
115         docker run -d -l id="$2" -l app="$PRODUCER_APP_NAME" -v "$VOLUME_MAPPING" --network="$HV_VES_NETWORK" "$PRODUCER_IMAGE_NAME" \
116         --address "$HV_VES_ADDRESS" \
117         --certfile "$CERT_FILE" \
118         --certpass "$CERT_PASS_FILE" \
119         --clients "$CLIENTS_PER_CONTAINER" \
120         --msgsize "$MSG_SIZE" \
121         --msgcount "$MSG_COUNT" \
122         --intervalms "$INTERVAL_MS" > /dev/null
123     done
124 }
125
126 function clean(){
127     echo "Cleaning up"
128
129     echo "Removing active producers"
130     docker rm --force $(docker ps -aqf "label=app=$PRODUCER_APP_NAME")
131
132     echo "Clearing generated certs"
133     cd ../../ssl
134     ./gen-certs.sh clean
135
136     cd "$SCRIPT_DIRECTORY"
137
138     echo "Removing HV-VES components"
139     docker-compose down
140     exit 0
141 }
142
143 cd "$SCRIPT_DIRECTORY"
144
145 if [[ $# -eq 0 ]]; then
146     usage
147 else
148     for arg in ${@}
149     do
150         case ${arg} in
151             setup)
152             setup_environment
153             ;;
154             start)
155             shift 1
156             while [[ $(($#)) -gt 0 ]]; do
157                 case "${1}" in
158                     --address)
159                         HV_VES_ADDRESS=${2}
160                         ;;
161                      --containers)
162                         CONTAINERS_COUNT=${2}
163                         ;;
164                     --clients)
165                         CLIENTS_PER_CONTAINER=${2}
166                         ;;
167                     --msg-size)
168                         MSG_SIZE=${2}
169                         ;;
170                     --msg-count)
171                         MSG_COUNT=${2}
172                         ;;
173                     --interval)
174                         INTERVAL_MS=${2}
175                         ;;
176                 esac
177                 shift 2
178             done
179             start_performance_test
180             ;;
181             clean)
182             clean
183             ;;
184             help)
185             usage
186             ;;
187             *)
188             echo "Unknown action: ${arg}" >&2
189             usage
190             ;;
191         esac
192     done
193 fi