Add first set of SI95 unit tests and health check
[ric-plt/lib/rmr.git] / test / app_test / run_lcall_test.ksh
1 #!/usr/bin/env ksh
2 # vim: ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019-2020 Nokia
5 #    Copyright (c) 2018-2020 AT&T Intellectual Property.
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 #==================================================================================
19 #
20
21 # ---------------------------------------------------------------------------------
22 #       Mnemonic:       run_lcall_test.ksh
23 #       Abstract:       This is a simple script to set up and run the basic send/receive
24 #                               processes for some library validation on top of nng. This
25 #                               particular test starts the latency caller and latency receiver
26 #                               processes such that they exchange messages and track the latency
27 #                               from the caller's perepective (both outbound to receiver, and then
28 #                               back.  Stats are presented at the end.   This test is NOT intended
29 #                               to be used as a CI validation test.
30 #
31 #                               The sender and receiver processes all have a 20s timeout (+/-)
32 #                               which means that all messages must be sent, and acked within that
33 #                               time or the processes will give up and report failure.  Keep in mind
34 #                               that n messages with a delay value (-d) set will affect whether or
35 #                               not the messages can be sent in the 20s timeout period.  There is
36 #                               currently no provision to adjust the timeout other than by changing
37 #                               the C source.  The default (100 msgs with 500 micro-sec delay) works
38 #                               just fine for base testing.
39 #
40 #                               Example command line:
41 #                                       # run with 10 caller threads sending 10,000 meessages each, 
42 #                                       # 5 receivers, and a 10 mu-s delay between each caller send
43 #                                       ksh ./run_lcall_test.ksh -d 10 -n 10000 -r 5 -c 10
44 #
45 #       Date:           28 May 2019
46 #       Author:         E. Scott Daniels
47 # ---------------------------------------------------------------------------------
48
49
50 # The sender and receivers are run asynch. Their exit statuses are captured in a
51 # file in order for the 'main' to pick them up easily.
52 #
53 function run_sender {
54         ./lcaller${si} ${nmsg:-10} ${delay:-500} ${cthreads:-3} 
55         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
56 }
57
58 # $1 is the instance so we can keep logs separate
59 function run_rcvr {
60         typeset port
61
62         port=$(( 4460 + ${1:-0} ))
63         export RMR_RTG_SVC=$(( 9990 + $1 ))
64         ./lreceiver${si} $(( ((nmsg * cthreads)/nrcvrs) + 10 )) $port
65         echo $? >/tmp/PID$$.$1.rrc
66 }
67
68 #       Drop a contrived route table in such that the sender sends each message to n
69 #       receivers.
70 #
71 function set_rt {
72         typeset port=4460
73         typeset groups="localhost:4460"
74         for (( i=1; i < ${1:-3}; i++ ))
75         do
76                 groups="$groups,localhost:$((port+i))"
77         done
78
79         cat <<endKat >lcall.rt
80                 newrt | start
81                 mse |0 | 0 | $groups
82                 mse |1 | 10 | $groups
83                 mse |2 | 20 | $groups
84                 rte |3 | $groups
85                 rte |4 | $groups
86                 rte |5 | $groups
87                 rte |6 | $groups
88                 rte |7 | $groups
89                 rte |8 | $groups
90                 rte |9 | $groups
91                 rte |10 | $groups
92                 rte |11 | $groups
93                 newrt | end
94 endKat
95 }
96
97 # ---------------------------------------------------------
98
99 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
100 then
101         hn=$(hostname)
102         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
103 fi
104
105 export RMR_ASYNC_CONN=0         # ensure we don't lose first msg as drops waiting for conn look like errors
106 cthreads=3                                      # number of caller threads
107 nmsg=100                                        # total number of messages to be exchanged (-n value changes)
108 delay=500                                       # microsec sleep between msg 1,000,000 == 1s
109 wait=1
110 rebuild=0
111 verbose=0
112 nrcvrs=3                                        # this is sane, but -r allows it to be set up
113 use_installed=0
114 force_make=0
115 si=""
116
117 while [[ $1 == -* ]]
118 do
119         case $1 in
120                 -c)     cthreads=$2; shift;;
121                 -B)     rebuild=1;;
122                 -d)     delay=$2; shift;;
123                 -i)     use_installed=1;;
124                 -n)     nmsg=$2; shift;;
125                 -M)     force_make=1;;
126                 -N)     si="";;                                 # build NNG binaries (turn off si)
127                 -r)     nrcvrs=$2; shift;;
128                 -S)     si="_si";;                              # build SI95 binaries
129                 -v)     verbose=1;;
130
131                 *)      echo "unrecognised option: $1"
132                         echo "usage: $0 [-B] [-c caller-threads] [-d micor-sec-delay] [-i] [-M] [-n num-msgs] [-r num-receivers] [-S]"
133                         echo "  -B forces an RMR rebuild which will use .build"
134                         echo "  -i will use installed libraries (/usr/local) and cause -B to be ignored if supplied)"
135                         echo "  -M force test applictions to be remade"
136                         echo "  -S build/test SI95 based binaries"
137                         exit 1
138                         ;;
139         esac
140
141         shift
142 done
143
144 if (( verbose ))
145 then
146         echo "2" >.verbose
147         export RMR_VCTL_FILE=".verbose"
148 fi
149
150 if (( use_installed ))                  # point at installed library
151 then
152         export LD_LIBRARY_PATH=${LD_LIBRARY_PATH-:/usr/local/lib}       # use caller's or set sane default
153 else
154         if (( rebuild ))
155         then
156                 build_path=../../.build         # if we rebuild we can insist that it is in .build :)
157                 set -e
158                 $SHELL ./rebuild.ksh
159                 set +e
160         else
161                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
162
163                 if [[ ! -d $build_path ]]
164                 then
165                         echo "cannot find build in: $build_path"
166                         echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
167                         exit 1
168                 fi
169         fi
170
171         if [[ -d $build_path/lib64 ]]
172         then
173                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64:$LD_LIBRARY_PATH
174         else
175                 export LD_LIBRARY_PATH=$build_path:$build_path/lib:$LD_LIBRARY_PATH
176         fi
177 fi
178
179 export LIBRARY_PATH=$LD_LIBRARY_PATH
180 export RMR_SEED_RT=./lcall.rt
181
182 set_rt $nrcvrs                                          # set up the rt for n receivers
183
184 if (( force_make || rebuild )) || [[ ! -f ./lcaller{$si} || ! -f ./lreceiver${si} ]]
185 then
186         if ! make -B lcaller${si} lreceiver${si} >/dev/null 2>&1
187         then
188                 echo "[FAIL] cannot find lcaller${si} and/or lreceiver${si} binary, and cannot make them.... humm?"
189                 exit 1
190         fi
191 fi
192
193 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
194 do
195         run_rcvr $i &
196 done
197
198 sleep 2                         # let receivers init so we don't shoot at an empty target
199 run_sender &
200
201 wait
202
203
204 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
205 do
206         head -1 /tmp/PID$$.$i.rrc | read x
207         (( rrc += x ))
208 done
209
210 head -1 /tmp/PID$$.src | read src
211
212 if (( !! (src + rrc) ))
213 then
214         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
215 else
216         echo "[PASS] sender rc=$src  receiver rc=$rrc"
217         rm -f lcall.rt
218 fi
219
220 rm /tmp/PID$$.*
221 rm -f .verbose
222
223 exit $(( !! (src + rrc) ))
224