1#!/bin/bash
2
3rpcs=(1)
4conns=(1)
5warmup=10
6dur=10
7reqs=(1)
8resps=(1)
9rpc_types=(unary)
10
11# idx[0] = idx value for rpcs
12# idx[1] = idx value for conns
13# idx[2] = idx value for reqs
14# idx[3] = idx value for resps
15# idx[4] = idx value for rpc_types
16idx=(0 0 0 0 0)
17idx_max=(1 1 1 1 1)
18
19inc()
20{
21  for i in $(seq $((${#idx[@]}-1)) -1 0); do
22    idx[${i}]=$((${idx[${i}]}+1))
23    if [ ${idx[${i}]} == ${idx_max[${i}]} ]; then
24      idx[${i}]=0
25    else
26      break
27    fi
28  done
29  local fin
30  fin=1
31  # Check to see if we have looped back to the beginning.
32  for v in ${idx[@]}; do
33    if [ ${v} != 0 ]; then
34      fin=0
35      break
36    fi
37  done
38  if [ ${fin} == 1 ]; then
39    rm -Rf ${out_dir}
40    clean_and_die 0
41  fi
42}
43
44clean_and_die() {
45  rm -Rf ${out_dir}
46  exit $1
47}
48
49run(){
50  local nr
51  nr=${rpcs[${idx[0]}]}
52  local nc
53  nc=${conns[${idx[1]}]}
54  req_sz=${reqs[${idx[2]}]}
55  resp_sz=${resps[${idx[3]}]}
56  r_type=${rpc_types[${idx[4]}]}
57  # Following runs one benchmark
58  base_port=50051
59  delta=0
60  test_name="r_"${nr}"_c_"${nc}"_req_"${req_sz}"_resp_"${resp_sz}"_"${r_type}"_"$(date +%s)
61  echo "================================================================================"
62  echo ${test_name}
63  while :
64  do
65    port=$((${base_port}+${delta}))
66
67    # Launch the server in background
68    ${out_dir}/server --port=${port} --test_name="Server_"${test_name}&
69    server_pid=$(echo $!)
70
71    # Launch the client
72    ${out_dir}/client --port=${port} --d=${dur} --w=${warmup} --r=${nr} --c=${nc} --req=${req_sz} --resp=${resp_sz} --rpc_type=${r_type}  --test_name="client_"${test_name}
73    client_status=$(echo $?)
74
75    kill -INT ${server_pid}
76    wait ${server_pid}
77
78    if [ ${client_status} == 0 ]; then
79      break
80    fi
81
82    delta=$((${delta}+1))
83    if [ ${delta} == 10 ]; then
84      echo "Continuous 10 failed runs. Exiting now."
85      rm -Rf ${out_dir}
86      clean_and_die 1
87    fi
88  done
89
90}
91
92set_param(){
93  local argname=$1
94  shift
95  local idx=$1
96  shift
97  if [ $# -eq 0 ]; then
98    echo "${argname} not specified"
99    exit 1
100  fi
101  PARAM=($(echo $1 | sed 's/,/ /g'))
102  if [ ${idx} -lt 0 ]; then
103    return
104  fi
105  idx_max[${idx}]=${#PARAM[@]}
106}
107
108while [ $# -gt 0 ]; do
109  case "$1" in
110    -r)
111      shift
112      set_param "number of rpcs" 0 $1
113      rpcs=(${PARAM[@]})
114      shift
115      ;;
116    -c)
117      shift
118      set_param "number of connections" 1 $1
119      conns=(${PARAM[@]})
120      shift
121      ;;
122    -w)
123      shift
124      set_param "warm-up period" -1 $1
125      warmup=${PARAM}
126      shift
127      ;;
128    -d)
129      shift
130      set_param "duration" -1 $1
131      dur=${PARAM}
132      shift
133      ;;
134    -req)
135      shift
136      set_param "request size" 2 $1
137      reqs=(${PARAM[@]})
138      shift
139      ;;
140    -resp)
141      shift
142      set_param "response size" 3 $1
143      resps=(${PARAM[@]})
144      shift
145      ;;
146    -rpc_type)
147      shift
148      set_param "rpc type" 4 $1
149      rpc_types=(${PARAM[@]})
150      shift
151      ;;
152    -h|--help)
153      echo "Following are valid options:"
154      echo
155      echo "-h, --help        show brief help"
156      echo "-w                warm-up duration in seconds, default value is 10"
157      echo "-d                benchmark duration in seconds, default value is 60"
158      echo ""
159      echo "Each of the following can have multiple comma separated values."
160      echo ""
161      echo "-r                number of RPCs, default value is 1"
162      echo "-c                number of Connections, default value is 1"
163      echo "-req              req size in bytes, default value is 1"
164      echo "-resp             resp size in bytes, default value is 1"
165      echo "-rpc_type         valid values are unary|streaming, default is unary"
166      exit 0
167      ;;
168    *)
169      echo "Incorrect option $1"
170      exit 1
171      ;;
172  esac
173done
174
175# Build server and client
176out_dir=$(mktemp -d oss_benchXXX)
177
178go build -o ${out_dir}/server $GOPATH/src/google.golang.org/grpc/benchmark/server/main.go && go build -o ${out_dir}/client $GOPATH/src/google.golang.org/grpc/benchmark/client/main.go
179if [ $? != 0 ]; then
180  clean_and_die 1
181fi
182
183
184while :
185do
186  run
187  inc
188done
189