Skip to content

Commit 1384ff8

Browse files
committed
Refactor run_benchmark script.
1 parent 3fd8364 commit 1384ff8

File tree

2 files changed

+73
-93
lines changed

2 files changed

+73
-93
lines changed

benchmark/client/main.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ import (
3737
)
3838

3939
var (
40-
port = flag.String("port", "50051", "Localhost port to connect to.")
41-
r = flag.Int("r", 1, "The number of concurrent RPCs on each connection.")
42-
c = flag.Int("c", 1, "The number of parallel connections.")
43-
w = flag.Int("w", 10, "Warm-up duration in seconds")
44-
d = flag.Int("d", 60, "Benchmark duration in seconds")
45-
rqSize = flag.Int("req", 1, "Request message size in bytes.")
46-
rspSize = flag.Int("resp", 1, "Response message size in bytes.")
47-
rpcType = flag.String("rpc_type", "unary",
40+
port = flag.String("port", "50051", "Localhost port to connect to.")
41+
numRPC = flag.Int("r", 1, "The number of concurrent RPCs on each connection.")
42+
numConn = flag.Int("c", 1, "The number of parallel connections.")
43+
warmupDur = flag.Int("w", 10, "Warm-up duration in seconds")
44+
duration = flag.Int("d", 60, "Benchmark duration in seconds")
45+
rqSize = flag.Int("req", 1, "Request message size in bytes.")
46+
rspSize = flag.Int("resp", 1, "Response message size in bytes.")
47+
rpcType = flag.String("rpc_type", "unary",
4848
`Configure different client rpc type. Valid options are:
4949
unary;
5050
streaming.`)
@@ -74,8 +74,8 @@ func main() {
7474
connectCtx, connectCancel := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
7575
defer connectCancel()
7676
ccs := buildConnections(connectCtx)
77-
warmDeadline := time.Now().Add(time.Duration(*w) * time.Second)
78-
endDeadline := warmDeadline.Add(time.Duration(*d) * time.Second)
77+
warmDeadline := time.Now().Add(time.Duration(*warmupDur) * time.Second)
78+
endDeadline := warmDeadline.Add(time.Duration(*duration) * time.Second)
7979
cf, err := os.Create("/tmp/" + *testName + ".cpu")
8080
if err != nil {
8181
grpclog.Fatalf("Error creating file: %v", err)
@@ -109,15 +109,15 @@ func main() {
109109
}
110110

111111
func buildConnections(ctx context.Context) []*grpc.ClientConn {
112-
ccs := make([]*grpc.ClientConn, *c)
112+
ccs := make([]*grpc.ClientConn, *numConn)
113113
for i := range ccs {
114114
ccs[i] = benchmark.NewClientConnWithContext(ctx, "localhost:"+*port, grpc.WithInsecure(), grpc.WithBlock())
115115
}
116116
return ccs
117117
}
118118

119119
func runWithConn(cc *grpc.ClientConn, req *testpb.SimpleRequest, warmDeadline, endDeadline time.Time) {
120-
for i := 0; i < *r; i++ {
120+
for i := 0; i < *numRPC; i++ {
121121
wg.Add(1)
122122
go func() {
123123
defer wg.Done()
@@ -165,7 +165,7 @@ func makeCaller(cc *grpc.ClientConn, req *testpb.SimpleRequest) func() {
165165
}
166166

167167
func parseHist(hist *stats.Histogram) {
168-
fmt.Println("qps:", float64(hist.Count)/float64(*d))
168+
fmt.Println("qps:", float64(hist.Count)/float64(*duration))
169169
fmt.Printf("Latency: (50/90/99 %%ile): %v/%v/%v\n",
170170
time.Duration(median(.5, hist)),
171171
time.Duration(median(.9, hist)),

benchmark/run_bench.sh

+60-80
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/bin/bash
22

3-
rs=(1)
4-
cs=(1)
5-
w=10
6-
d=10
3+
rpcs=(1)
4+
conns=(1)
5+
warmup=10
6+
dur=10
77
reqs=(1)
88
resps=(1)
99
rpc_types=(unary)
1010

11-
# idx[0] = idx value for rs
12-
# idx[1] = idx value for cs
11+
# idx[0] = idx value for rpcs
12+
# idx[1] = idx value for conns
1313
# idx[2] = idx value for reqs
1414
# idx[3] = idx value for resps
1515
# idx[4] = idx value for rpc_types
@@ -18,33 +18,39 @@ idx_max=(1 1 1 1 1)
1818

1919
inc()
2020
{
21-
for i in $(seq $((${#idx[@]}-1)) -1 0)
22-
do
21+
for i in $(seq $((${#idx[@]}-1)) -1 0); do
2322
idx[${i}]=$((${idx[${i}]}+1))
2423
if [ ${idx[${i}]} == ${idx_max[${i}]} ]; then
2524
idx[${i}]=0
2625
else
2726
break
2827
fi
2928
done
30-
return=1
29+
local fin
30+
fin=1
3131
# Check to see if we have looped back to the beginning.
3232
for v in ${idx[@]}; do
33-
if [ ${v} == 0 ]; then
34-
return=$((${return}*1))
35-
else
36-
return=$((${return}*0))
33+
if [ ${v} != 0 ]; then
34+
fin=0
35+
break
3736
fi
3837
done
39-
if [ ${return} == 1 ]; then
40-
rm -Rf ${out_dir}
41-
exit 0
38+
if [ ${fin} == 1 ]; then
39+
rm -Rf ${out_dir}
40+
clean_and_die 0
4241
fi
4342
}
4443

44+
clean_and_die() {
45+
rm -Rf ${out_dir}
46+
exit $1
47+
}
48+
4549
run(){
46-
nr=${rs[${idx[0]}]}
47-
nc=${cs[${idx[1]}]}
50+
local nr
51+
nr=${rpcs[${idx[0]}]}
52+
local nc
53+
nc=${conns[${idx[1]}]}
4854
req_sz=${reqs[${idx[2]}]}
4955
resp_sz=${resps[${idx[3]}]}
5056
r_type=${rpc_types[${idx[4]}]}
@@ -63,7 +69,7 @@ run(){
6369
server_pid=$(echo $!)
6470

6571
# Launch the client
66-
${out_dir}/client --port=${port} --d=${d} --w=${w} --r=${nr} --c=${nc} --req=${req_sz} --resp=${resp_sz} --rpc_type=${r_type} --test_name="client_"${test_name}
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}
6773
client_status=$(echo $?)
6874

6975
kill ${server_pid}
@@ -77,101 +83,75 @@ run(){
7783
if [ ${delta} == 10 ]; then
7884
echo "Continuous 10 failed runs. Exiting now."
7985
rm -Rf ${out_dir}
80-
exit 1
86+
clean_and_die 1
8187
fi
8288
done
8389

8490
}
8591

86-
while test $# -gt 0; do
92+
set_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+
108+
while [ $# -gt 0 ]; do
87109
case "$1" in
88110
-r)
89111
shift
90-
if test $# -gt 0; then
91-
rs=($(echo $1 | sed 's/,/ /g'))
92-
idx_max[0]=${#rs[@]}
93-
else
94-
echo "number of rpcs not specified"
95-
exit 1
96-
fi
112+
set_param "number of rpcs" 0 $1
113+
rpcs=(${PARAM[@]})
97114
shift
98115
;;
99116
-c)
100117
shift
101-
if test $# -gt 0; then
102-
cs=($(echo $1 | sed 's/,/ /g'))
103-
idx_max[1]=${#cs[@]}
104-
else
105-
echo "number of connections not specified"
106-
exit 1
107-
fi
118+
set_param "number of connections" 1 $1
119+
conns=(${PARAM[@]})
108120
shift
109121
;;
110122
-w)
111123
shift
112-
if test $# -gt 0; then
113-
w=$1
114-
else
115-
echo "warm-up period not specified"
116-
exit 1
117-
fi
124+
set_param "warm-up period" -1 $1
125+
warmup=${PARAM}
118126
shift
119127
;;
120128
-d)
121129
shift
122-
if test $# -gt 0; then
123-
d=$1
124-
else
125-
echo "duration not specified"
126-
exit 1
127-
fi
130+
set_param "duration" -1 $1
131+
dur=${PARAM}
128132
shift
129133
;;
130134
-req)
131135
shift
132-
if test $# -gt 0; then
133-
reqs=($(echo $1 | sed 's/,/ /g'))
134-
idx_max[2]=${#reqs[@]}
135-
else
136-
echo "request size not specified"
137-
exit 1
138-
fi
136+
set_param "request size" 2 $1
137+
reqs=(${PARAM[@]})
139138
shift
140139
;;
141140
-resp)
142141
shift
143-
if test $# -gt 0; then
144-
resps=($(echo $1 | sed 's/,/ /g'))
145-
idx_max[3]=${#resps[@]}
146-
else
147-
echo "response size not specified"
148-
exit 1
149-
fi
142+
set_param "response size" 3 $1
143+
resps=(${PARAM[@]})
150144
shift
151145
;;
152146
-rpc_type)
153147
shift
154-
if test $# -gt 0; then
155-
rpc_types=($(echo $1 | sed 's/,/ /g'))
156-
idx_max[4]=${#rpc_types[@]}
157-
for val in ${rpc_types[@]}; do
158-
case "${val}" in
159-
"unary"|"streaming");;
160-
*) echo "Incorrect value for rpc_type"
161-
exit 1
162-
;;
163-
esac
164-
165-
done
166-
else
167-
echo "rpc type not specified not specified"
168-
exit 1
169-
fi
148+
set_param "rpc type" 4 $1
149+
rpc_types=(${PARAM[@]})
170150
shift
171151
;;
172152
-h|--help)
173153
echo "Following are valid options:"
174-
echo ""
154+
echo
175155
echo "-h, --help show brief help"
176156
echo "-w warm-up duration in seconds, default value is 10"
177157
echo "-d benchmark duration in seconds, default value is 60"
@@ -185,7 +165,7 @@ while test $# -gt 0; do
185165
echo "-rpc_type valid values are unary|streaming, default is unary"
186166
;;
187167
*)
188-
echo "Incorrect option"
168+
echo "Incorrect option $1"
189169
exit 1
190170
;;
191171
esac
@@ -196,7 +176,7 @@ out_dir=$(mktemp -d oss_benchXXX)
196176

197177
go 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
198178
if [ $? != 0 ]; then
199-
exit 1
179+
clean_and_die 1
200180
fi
201181

202182

0 commit comments

Comments
 (0)