enhance(API): Add multi-threaded call
[ric-plt/lib/rmr.git] / test / app_test / run_call_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_call_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.
25 #                               It should be possible to clone the repo, switch to this directory
26 #                               and execute  'ksh run -B'  which will build RMr, make the sender and
27 #                               recevier then  run the basic test.
28 #
29 #                               The call test drives three RMr based processes:
30 #                                       caller which invokes rmr_mt_call() to send n messages
31 #
32 #                                       receiver which executes rmr_rts_msg() on each received
33 #                                               message with type == 5.
34 #       
35 #                                       sender which sends n "pings" to the caller process
36 #
37 #                               The script assumes that all three proessess are running on the same
38 #                               host/container such that setting up the route table with localhost:port
39 #                               will work.
40 #
41 #                               The number of message, and the delay between each message may be given
42 #                               on the command line. The number of threads may also be given. The
43 #                               number of messages defines the number _each_ caller will sent (5000 
44 #                               messages and three threads == 15,000 total messages.
45 #
46 #                               Example command line:
47 #                                       ksh ./run_call_test.ksh         # default 10 messages at 1 msg/sec 3 threads
48 #                                       ksh ./run_call_test.ksh -n 5000  -t 6 -d 400    # 5k messages, 6 threads, delay 400 mus
49 #
50 #       Date:           20 May 2019
51 #       Author:         E. Scott Daniels
52 # ---------------------------------------------------------------------------------
53
54
55 # The sender and receiver are run asynch. Their exit statuses are captured in a
56 # file in order for the 'main' to pick them up easily.
57 #
58 function run_sender {
59         if (( nano_sender ))
60         then
61                 #       ./sender_nano $nmsg $delay
62                 echo "nano is not supported"
63                 exit 1
64         else
65                 ./caller $nmsg $delay $nthreads
66         fi
67         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
68 }
69
70 # start receiver listening for nmsgs from each thread
71 function run_rcvr {
72         if (( nano_receiver ))
73         then
74                 #./receiver_nano $(( nmsg * nthreads ))
75                 echo "nano is not supported"
76                 exit 1
77         else
78                 ./mt_receiver $(( nmsg * nthreads ))            # we'll test with the RMr multi threaded receive function
79         fi
80         echo $? >/tmp/PID$$.rrc
81 }
82
83 # This will send 100 messages to the caller process. This is a test to verify that
84 # threaded calling is not affected by normal messages and that normal messages can
85 # be received concurrently.
86 #
87 function run_pinger {
88     RMR_RTG_SVC=9999 ./sender 100 100 4 3333 >/dev/NULL 2>&1  # send pings
89 }
90
91
92 # Generate a route table that is tailored to our needs of sender sending messages to
93 # the caller, and caller sending mtype == 5 to the receiver.
94 #
95 function mk_rt {
96
97 cat <<endKat >caller.rt
98 # this is a specialised rt for caller testing. mtype 5 go to the
99 # receiver, and all others go to the caller.
100
101 newrt | start
102 mse | 0 |  0 | localhost:43086
103 mse | 1 | 10 | localhost:43086
104 mse | 2 | 20 | localhost:43086
105 rte | 3 | localhost:43086
106 mse | 3 | 100 | localhost:43086 # special test to ensure that this does not affect previous entry
107 rte | 4 | localhost:43086
108 rte | 5 | localhost:4560
109 rte | 6 | localhost:43086
110 rte | 7 | localhost:43086
111 rte | 8 | localhost:43086
112 rte | 9 | localhost:43086
113 rte | 10 | localhost:43086
114 rte | 11 | localhost:43086
115 rte | 12 | localhost:43086
116 rte | 13 | localhost:43086
117
118 newrt | end | 16
119 endKat
120 }
121
122 # ---------------------------------------------------------
123
124 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
125 delay=1000000                           # microsec sleep between msg 1,000,000 == 1s
126 nano_sender=0                           # start nano version if set (-N)
127 nano_receiver=0
128 wait=1
129 rebuild=0
130 verbose=0
131 nthreads=3
132 dev_base=1                                      # -D turns off to allow this to run on installed libs
133
134 nano_sender=0                           # mt-call is not supported in nano
135 nano_receiver=0
136
137
138 while [[ $1 == -* ]]
139 do
140         case $1 in
141                 -B)     rebuild=1;;
142                 -d)     delay=$2; shift;;
143                 -D)     dev_base=0;;
144                 -n)     nmsg=$2; shift;;
145                 -t)     nthreads=$2; shift;;
146                 -v)     verbose=1;;
147
148                 *)      echo "unrecognised option: $1"
149                         echo "usage: $0 [-B] [-d micro-sec-delay] [-n num-msgs] [-t num-threads]"
150                         echo "  -B forces a rebuild which will use .build"
151                         exit 1
152                         ;;
153         esac
154
155         shift
156 done
157
158 if (( verbose ))
159 then
160         echo "2" >.verbose
161         export RMR_VCTL_FILE=".verbose"
162 fi
163
164 if (( rebuild ))
165 then
166         build_path=../../.build
167         set -e
168         ksh ./rebuild.ksh
169         set +e
170 else
171         build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
172
173         if [[ ! -d $build_path ]]
174         then
175                 echo "cannot find build in: $build_path"
176                 echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
177                 exit 1
178         fi
179 fi
180
181 if (( dev_base ))                                                       # assume we are testing against what we've built, not what is installed
182 then
183         if [[ -d $build_path/lib64 ]]
184         then
185                 export LD_LIBRARY_PATH=$build_path:$build_path/lib64
186         else
187                 export LD_LIBRARY_PATH=$build_path:$build_path/lib
188         fi
189 else                                                                            # -D option gets us here to test an installed library
190         export LD_LIBRARY_PATH=/usr/local/lib
191         export LIBRARY_PATH=$LD_LIBRARY_PATH
192 fi
193
194 export RMR_SEED_RT=${RMR_SEED_RT:-./caller.rt}          # allow easy testing with different rt
195
196 if [[ ! -f $RMR_SEED_RT ]]                                                      # special caller rt for three process setup
197 then
198         mk_rt
199 fi
200
201 if [[ ! -f ./sender ]]
202 then
203         if ! make >/dev/null 2>&1
204         then
205                 echo "[FAIL] cannot find sender binary, and cannot make it.... humm?"
206                 exit 1
207         fi
208 fi
209
210 run_rcvr &
211 sleep 2                         # if caller starts faster than rcvr we can drop, so pause a bit
212 run_sender &
213 run_pinger &
214
215 wait
216 head -1 /tmp/PID$$.rrc | read rrc               # get pass/fail state from each
217 head -1 /tmp/PID$$.src | read src
218
219 if (( !! (src + rrc) ))
220 then
221         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
222 else
223         echo "[PASS] sender rc=$src  receiver rc=$rrc"
224 fi
225
226 rm /tmp/PID$$.*
227 rm -f .verbose
228
229 exit $(( !! (src + rrc) ))
230