Merge "Subscription model with status and predicates"
[cps.git] / test-tools / test-deregistration.sh
1 #!/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
18 set -o errexit  # Exit on most errors
19 set -o nounset  # Disallow expansion of unset variables
20 set -o pipefail # Use last non-zero exit code in a pipeline
21 #set -o xtrace   # Uncomment for debugging
22
23 GRAB_METRICS=true
24 DOCKER_COMPOSE_FILE=../docker-compose/docker-compose.yml
25 CREATE_REQUEST=/tmp/cmhandles-create-req.txt
26 REMOVE_REQUEST=/tmp/cmhandles-remove-req.txt
27 REPORT_FILE=metrics-reports/deregister-summary-$(date --iso-8601=seconds).tsv
28
29 stop_docker() {
30     docker-compose -f $DOCKER_COMPOSE_FILE down >/dev/null
31     docker container prune -f >/dev/null
32     docker volume prune -f >/dev/null
33 }
34
35 restart_docker() {
36     stop_docker
37     docker-compose -f $DOCKER_COMPOSE_FILE --profile dmi-stub --profile monitoring up -d >/dev/null
38 }
39
40 wait_for_cps_to_start() {
41     docker logs cps-and-ncmp -f | grep -m 1 'Started Application' >/dev/null || true
42 }
43
44 get_number_of_handles_ready() {
45     PGPASSWORD=cps psql -h localhost -p 5432 cpsdb cps -c \
46         "SELECT count(*) FROM public.fragment where attributes @> '{\"cm-handle-state\": \"READY\"}';" \
47         | sed '3!d' | sed 's/ *//'
48 }
49
50 wait_for_handles_to_be_ready() {
51     local TOTAL_HANDLES=$1
52     while
53         sleep 30
54         HANDLES_READY=$(get_number_of_handles_ready)
55         echo "There are $HANDLES_READY CM handles in READY state."
56         [ $HANDLES_READY -ne $TOTAL_HANDLES ]
57     do true; done
58 }
59
60 create_handles() {
61     curl --fail --silent --show-error \
62         --location 'http://localhost:8883/ncmpInventory/v1/ch' \
63         --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=' \
64         --header 'Content-Type: application/json' \
65         --data @$CREATE_REQUEST
66 }
67
68 remove_handles_and_record_time() {
69     curl --fail --silent --show-error --output /dev/null --write-out '%{time_total}\n' \
70         --location 'http://localhost:8883/ncmpInventory/v1/ch' \
71         --header 'Authorization: Basic Y3BzdXNlcjpjcHNyMGNrcyE=' \
72         --header 'Content-Type: application/json' \
73         --header 'Cookie: JSESSIONID=node018g80wfn6qfk9yihx8pne7bc31.node0' \
74         --data @$REMOVE_REQUEST >> $REPORT_FILE
75 }
76
77 create_request_bodies() {
78     local CREATE_SIZE=$1
79     local REMOVE_SIZE=$2
80     echo -n '{"dmiPlugin": "http://ncmp-dmi-plugin-stub:8783","createdCmHandles":[' > $CREATE_REQUEST
81     echo -n '{"dmiPlugin": "http://ncmp-dmi-plugin-stub:8783","removedCmHandles":[' > $REMOVE_REQUEST
82     for i in $(seq 1 $CREATE_SIZE); do
83         local CMHANDLE=$(uuidgen | tr -d '-')
84         echo -n "{\"cmHandle\": \"$CMHANDLE\",\"cmHandleProperties\":{\"neType\":\"RadioNode\"}}" \
85             >> $CREATE_REQUEST
86         if [ $i -lt $CREATE_SIZE ]; then
87             echo -n "," >> $CREATE_REQUEST
88         fi
89         if [ $i -le $REMOVE_SIZE ]; then
90             echo -n "\"$CMHANDLE\"" >> $REMOVE_REQUEST
91         fi
92         if [ $i -lt $REMOVE_SIZE ]; then
93             echo -n "," >> $REMOVE_REQUEST
94         fi
95     done
96     echo ']}' >> $CREATE_REQUEST
97     echo ']}' >> $REMOVE_REQUEST
98 }
99
100 test_deregistration() {
101     local REMOVE_SIZE=$1
102     local CREATE_SIZE=$2
103
104     echo "Testing deregistration of $REMOVE_SIZE out of $CREATE_SIZE CM handles"
105
106     echo "Restarting docker"
107     restart_docker
108     echo "Waiting for CPS to start"
109     wait_for_cps_to_start
110
111     echo "Creating request bodies"
112     create_request_bodies $CREATE_SIZE $REMOVE_SIZE
113
114     echo "[$(date --iso-8601=seconds)] Creating CM handles"
115     create_handles
116     echo "Waiting for CM handles to be in READY state"
117     wait_for_handles_to_be_ready $CREATE_SIZE
118
119     if [ "$GRAB_METRICS" = "true" ]; then
120         echo "Grabbing metrics before deregistration"
121         METRICS_BEFORE=$(./generate-metrics-report.sh)
122     fi
123
124     echo "[$(date --iso-8601=seconds)] Removing CM handles"
125     echo -e -n "$REMOVE_SIZE\t$CREATE_SIZE\t" >> $REPORT_FILE
126     remove_handles_and_record_time
127     echo "There are $(get_number_of_handles_ready) CM handles still in READY state."
128
129     if [ "$GRAB_METRICS" = "true" ]; then
130         echo "Grabbing metrics after deregistration"
131         METRICS_AFTER=$(./generate-metrics-report.sh)
132         echo "Generating metrics report"
133         ./subtract-metrics-reports.py -a $METRICS_AFTER -b $METRICS_BEFORE \
134             -o metrics-reports/deregister-$(date --iso-8601=seconds)-$REMOVE_SIZE-$CREATE_SIZE.tsv
135         rm $METRICS_BEFORE $METRICS_AFTER
136     fi
137
138     echo
139 }
140
141 cleanup() {
142     rm -f "$CREATE_REQUEST" "$REMOVE_REQUEST"
143     stop_docker
144 }
145 trap cleanup EXIT
146
147 mkdir -p $(dirname $REPORT_FILE)
148 echo -e "Removed\tTotal\tTime" > $REPORT_FILE
149
150 # Delete N/N: 100/100, 200/200... 20000/20000
151 for number_to_delete in 100 200 300 400 500 600 700 800 900 1000 2000 3000 4000 5000 10000 15000 20000; do
152     test_deregistration $number_to_delete $number_to_delete
153 done
154 # Delete N/C: 1000/5000, 2000/5000... 5000/5000
155 for number_to_delete in 1000 2000 3000 4000 5000; do
156     test_deregistration $number_to_delete 5000
157 done
158 # Delete C/N: 1000/1000, 1000/2000... 1000/5000
159 for total in 1000 2000 3000 4000 5000; do
160     test_deregistration 1000 $total
161 done
162