blob: 6608b02bcefa3a7a43ea574c0da7ea527d567dc3 [file] [log] [blame]
sourabh_sourabhcb237052023-03-20 15:53:44 +00001#!/bin/bash
2#
3# Copyright 2023 Nordix Foundation.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18set -o errexit # Exit on most errors
19set -o nounset # Disallow expansion of unset variables
20set -o pipefail # Use last non-zero exit code in a pipeline
21#set -o xtrace # Uncomment for debugging
22
23GRAB_METRICS=true
24DOCKER_COMPOSE_FILE=../docker-compose/docker-compose.yml
25CREATE_REQUEST=/tmp/cmhandles-create-req.txt
26REMOVE_REQUEST=/tmp/cmhandles-remove-req.txt
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010027REPORT_FILE=./metrics-reports/deregister-summary-$(date --iso-8601=seconds).tsv
28SCRIPT_DIR=$(dirname -- "${BASH_SOURCE[0]}")
sourabh_sourabhcb237052023-03-20 15:53:44 +000029
30stop_docker() {
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010031 docker-compose -f $DOCKER_COMPOSE_FILE --profile dmi-stub --profile monitoring down >/dev/null
sourabh_sourabhcb237052023-03-20 15:53:44 +000032 docker container prune -f >/dev/null
33 docker volume prune -f >/dev/null
34}
35
36restart_docker() {
37 stop_docker
38 docker-compose -f $DOCKER_COMPOSE_FILE --profile dmi-stub --profile monitoring up -d >/dev/null
39}
40
41wait_for_cps_to_start() {
42 docker logs cps-and-ncmp -f | grep -m 1 'Started Application' >/dev/null || true
43}
44
45get_number_of_handles_ready() {
46 PGPASSWORD=cps psql -h localhost -p 5432 cpsdb cps -c \
47 "SELECT count(*) FROM public.fragment where attributes @> '{\"cm-handle-state\": \"READY\"}';" \
48 | sed '3!d' | sed 's/ *//'
49}
50
51wait_for_handles_to_be_ready() {
52 local TOTAL_HANDLES=$1
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010053 local HANDLES_READY=0
54 while [ "$HANDLES_READY" -lt "$TOTAL_HANDLES" ]; do
55 sleep 10
sourabh_sourabhcb237052023-03-20 15:53:44 +000056 HANDLES_READY=$(get_number_of_handles_ready)
57 echo "There are $HANDLES_READY CM handles in READY state."
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010058 done
sourabh_sourabhcb237052023-03-20 15:53:44 +000059}
60
61create_handles() {
62 curl --fail --silent --show-error \
63 --location 'http://localhost:8883/ncmpInventory/v1/ch' \
64 --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=' \
65 --header 'Content-Type: application/json' \
66 --data @$CREATE_REQUEST
67}
68
69remove_handles_and_record_time() {
70 curl --fail --silent --show-error --output /dev/null --write-out '%{time_total}\n' \
71 --location 'http://localhost:8883/ncmpInventory/v1/ch' \
72 --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=' \
73 --header 'Content-Type: application/json' \
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010074 --data @$REMOVE_REQUEST >> "$REPORT_FILE"
sourabh_sourabhcb237052023-03-20 15:53:44 +000075}
76
77create_request_bodies() {
78 local CREATE_SIZE=$1
79 local REMOVE_SIZE=$2
danielhanrahanaaf1a802023-03-13 18:39:12 +000080 echo -n '{"dmiPlugin": "http://ncmp-dmi-plugin-stub:8080","createdCmHandles":[' > $CREATE_REQUEST
81 echo -n '{"dmiPlugin": "http://ncmp-dmi-plugin-stub:8080","removedCmHandles":[' > $REMOVE_REQUEST
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010082 for i in $(seq 1 "$CREATE_SIZE"); do
83 local CMHANDLE
84 CMHANDLE=$(uuidgen | tr -d '-')
sourabh_sourabhcb237052023-03-20 15:53:44 +000085 echo -n "{\"cmHandle\": \"$CMHANDLE\",\"cmHandleProperties\":{\"neType\":\"RadioNode\"}}" \
86 >> $CREATE_REQUEST
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010087 if [ "$i" -lt "$CREATE_SIZE" ]; then
sourabh_sourabhcb237052023-03-20 15:53:44 +000088 echo -n "," >> $CREATE_REQUEST
89 fi
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010090 if [ "$i" -le "$REMOVE_SIZE" ]; then
sourabh_sourabhcb237052023-03-20 15:53:44 +000091 echo -n "\"$CMHANDLE\"" >> $REMOVE_REQUEST
92 fi
danielhanrahanbb0d4fb2023-06-02 11:44:09 +010093 if [ "$i" -lt "$REMOVE_SIZE" ]; then
sourabh_sourabhcb237052023-03-20 15:53:44 +000094 echo -n "," >> $REMOVE_REQUEST
95 fi
96 done
97 echo ']}' >> $CREATE_REQUEST
98 echo ']}' >> $REMOVE_REQUEST
99}
100
101test_deregistration() {
102 local REMOVE_SIZE=$1
103 local CREATE_SIZE=$2
104
105 echo "Testing deregistration of $REMOVE_SIZE out of $CREATE_SIZE CM handles"
106
107 echo "Restarting docker"
108 restart_docker
109 echo "Waiting for CPS to start"
110 wait_for_cps_to_start
111
112 echo "Creating request bodies"
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100113 create_request_bodies "$CREATE_SIZE" "$REMOVE_SIZE"
sourabh_sourabhcb237052023-03-20 15:53:44 +0000114
115 echo "[$(date --iso-8601=seconds)] Creating CM handles"
116 create_handles
117 echo "Waiting for CM handles to be in READY state"
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100118 wait_for_handles_to_be_ready "$CREATE_SIZE"
sourabh_sourabhcb237052023-03-20 15:53:44 +0000119
120 if [ "$GRAB_METRICS" = "true" ]; then
121 echo "Grabbing metrics before deregistration"
122 METRICS_BEFORE=$(./generate-metrics-report.sh)
123 fi
124
125 echo "[$(date --iso-8601=seconds)] Removing CM handles"
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100126 echo -e -n "$REMOVE_SIZE\t$CREATE_SIZE\t" >> "$REPORT_FILE"
sourabh_sourabhcb237052023-03-20 15:53:44 +0000127 remove_handles_and_record_time
128 echo "There are $(get_number_of_handles_ready) CM handles still in READY state."
129
130 if [ "$GRAB_METRICS" = "true" ]; then
131 echo "Grabbing metrics after deregistration"
132 METRICS_AFTER=$(./generate-metrics-report.sh)
133 echo "Generating metrics report"
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100134 ./subtract-metrics-reports.py -a "$METRICS_AFTER" -b "$METRICS_BEFORE" \
135 -o "metrics-reports/deregister-$(date --iso-8601=seconds)-$REMOVE_SIZE-$CREATE_SIZE.tsv"
136 rm "$METRICS_BEFORE" "$METRICS_AFTER"
sourabh_sourabhcb237052023-03-20 15:53:44 +0000137 fi
138
139 echo
140}
141
142cleanup() {
143 rm -f "$CREATE_REQUEST" "$REMOVE_REQUEST"
144 stop_docker
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100145 cat "$REPORT_FILE"
danielhanrahanaaf1a802023-03-13 18:39:12 +0000146 popd
sourabh_sourabhcb237052023-03-20 15:53:44 +0000147}
148trap cleanup EXIT
149
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100150pushd -- "$SCRIPT_DIR"
danielhanrahanaaf1a802023-03-13 18:39:12 +0000151
danielhanrahanbb0d4fb2023-06-02 11:44:09 +0100152mkdir -p "$(dirname "$REPORT_FILE")"
153echo -e "Removed\tTotal\tTime" > "$REPORT_FILE"
sourabh_sourabhcb237052023-03-20 15:53:44 +0000154
danielhanrahanaaf1a802023-03-13 18:39:12 +0000155for number_to_delete in 100 500 1000 5000 10000 20000; do
sourabh_sourabhcb237052023-03-20 15:53:44 +0000156 test_deregistration $number_to_delete $number_to_delete
157done