fix(rtable): Prevent direct send hairpins
[ric-plt/lib/rmr.git] / test / app_test / run_lcall_test.ksh
1 #!/usr/bin/env ksh
2 # :vi ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019 Nokia
5 #    Copyright (c) 2018-2019 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 nano/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         if (( $nano_sender ))
55         then
56                 echo "nanomsg not supportded"
57                 exit 1
58         else
59                 ./lcaller ${nmsg:-10} ${delay:-500} ${cthreads:-3} 
60         fi
61         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
62 }
63
64 # $1 is the instance so we can keep logs separate
65 function run_rcvr {
66         typeset port
67
68         port=$(( 4460 + ${1:-0} ))
69         export RMR_RTG_SVC=$(( 9990 + $1 ))
70         if (( $nano_receiver ))
71         then
72                 echo "nanomsg not supported"
73                 exit 1
74         else
75                 ./lreceiver $(( ((nmsg * cthreads)/nrcvrs) + 10 )) $port
76         fi
77         echo $? >/tmp/PID$$.$1.rrc
78 }
79
80 #       Drop a contrived route table in such that the sender sends each message to n
81 #       receivers.
82 #
83 function set_rt {
84         typeset port=4460
85         typeset groups="localhost:4460"
86         for (( i=1; i < ${1:-3}; i++ ))
87         do
88                 groups="$groups,localhost:$((port+i))"
89         done
90
91         cat <<endKat >lcall.rt
92                 newrt | start
93                 mse |0 | 0 | $groups
94                 mse |1 | 10 | $groups
95                 mse |2 | 20 | $groups
96                 rte |3 | $groups
97                 rte |4 | $groups
98                 rte |5 | $groups
99                 rte |6 | $groups
100                 rte |7 | $groups
101                 rte |8 | $groups
102                 rte |9 | $groups
103                 rte |10 | $groups
104                 rte |11 | $groups
105                 newrt | end
106 endKat
107 }
108
109 # ---------------------------------------------------------
110
111 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
112 then
113         hn=$(hostname)
114         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
115 fi
116
117 cthreads=3                                      # number of caller threads
118 nmsg=100                                        # total number of messages to be exchanged (-n value changes)
119 delay=500                                       # microsec sleep between msg 1,000,000 == 1s
120 nano_sender=0                           # start nano version if set (-N)
121 nano_receiver=0
122 wait=1
123 rebuild=0
124 verbose=0
125 nrcvrs=3                                        # this is sane, but -r allows it to be set up
126 use_installed=0
127
128 while [[ $1 == -* ]]
129 do
130         case $1 in
131                 -c)     cthreads=$2; shift;;
132                 -B)     rebuild=1;;
133                 -d)     delay=$2; shift;;
134                 -i)     use_installed=1;;
135                 -N)     echo "abort: nanomsg does not support epoll and thus cannot be used for mt-caller"
136                         echo ""
137                         exit 1;
138                         ;;
139                 -n)     nmsg=$2; shift;;
140                 -r)     nrcvrs=$2; shift;;
141                 -v)     verbose=1;;
142
143                 *)      echo "unrecognised option: $1"
144                         echo "usage: $0 [-B] [-c caller-threads] [-d micor-sec-delay] [-i] [-n num-msgs] [-r num-receivers]"
145                         echo "  -B forces a rebuild which will use .build"
146                         echo "  -i will use installed libraries (/usr/local) and cause -B to be ignored if supplied)"
147                         exit 1
148                         ;;
149         esac
150
151         shift
152 done
153
154 if (( verbose ))
155 then
156         echo "2" >.verbose
157         export RMR_VCTL_FILE=".verbose"
158 fi
159
160 if (( use_installed ))                  # point at installed library
161 then
162         export LD_LIBRARY_PATH=/usr/local/lib
163         export LIBRARY_PATH=$LD_LIBRARY_PATH
164 else
165         if (( rebuild ))
166         then
167                 build_path=../../.build         # if we rebuild we can insist that it is in .build :)
168                 set -e
169                 ksh ./rebuild.ksh
170                 set +e
171         else
172                 build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
173
174                 if [[ ! -d $build_path ]]
175                 then
176                         echo "cannot find build in: $build_path"
177                         echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
178                         exit 1
179                 fi
180         fi
181
182         if [[ -d $build_path/lib64 ]]
183         then
184                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64
185         else
186                 export LD_LIBRARY_PATH=$build_path:$build_path/lib
187         fi
188 fi
189
190 export LIBRARY_PATH=$LD_LIBRARY_PATH
191 export RMR_SEED_RT=./lcall.rt
192
193 set_rt $nrcvrs                                          # set up the rt for n receivers
194
195 if (( rebuild )) || [[ ! -f ./lcaller ]]
196 then
197         if ! make -B lcaller lreceiver >/dev/null 2>&1
198         then
199                 echo "[FAIL] cannot find lcaller binary, and cannot make it.... humm?"
200                 exit 1
201         fi
202 fi
203
204 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
205 do
206         run_rcvr $i &
207 done
208
209 sleep 2                         # let receivers init so we don't shoot at an empty target
210 run_sender &
211
212 wait
213
214
215 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
216 do
217         head -1 /tmp/PID$$.$i.rrc | read x
218         (( rrc += x ))
219 done
220
221 head -1 /tmp/PID$$.src | read src
222
223 if (( !! (src + rrc) ))
224 then
225         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
226 else
227         echo "[PASS] sender rc=$src  receiver rc=$rrc"
228         rm -f lcall.rt
229 fi
230
231 rm /tmp/PID$$.*
232 rm -f .verbose
233
234 exit $(( !! (src + rrc) ))
235