#!/bin/bash

HELLO=/opt/jobol/share/af-binder/bindings/samples/hello3.so

# run the service binder
v="strace -f -s 100 -o stress.strace"
v="valgrind --show-leak-kinds=definite --leak-check=yes"
v="valgrind"
v=
afb-daemon --no-ldpaths --trap-faults=no --binding $HELLO --ws-server unix:hello --no-http &
p_hello=$!
sleep 1

# run the client binder
v="strace -f -s 100 -o stress.strace"
v="valgrind --show-leak-kinds=definite --leak-check=yes"
v=
$v afb-daemon --no-ldpaths --trap-faults=no --ws-client unix:hello --ws-server unix:stress?as-api=hello --no-http &
p_stress=$!
sleep 1

NREV=100
NRCL=5

mkev() {
	local i
	for (( i = 1 ; i <= NREV ; i++))
	do
		echo 'eventadd {"tag":"'$i'","name":"event-'$i'"}'
	done
}

mksub() {
	local i
	for (( i = 1 ; i <= NREV ; i++))
	do
		echo 'eventsub {"tag":"'$i'"}'
	done
}

# create the events
mkev | afb-client-demo -p 8 -d unix:stress
echo sent
sleep 2

# stress the server with subscriptions
p=
for (( i = 1 ; i <= NRCL ; i++))
do
	echo launch client $i
	(
		s="strace -f -s 100 -o cli$i.strace"
		s=
		mksub |
		$s afb-client-demo -p 5 -d unix:stress |
		sed 's:^:['$i'] :'
	 ) &
	p="$p $!"
done

wait $p

kill $p_hello $p_stress

echo

