Skip to content

Commit e350804

Browse files
authored
benchmark: Avoid spawning a goroutine per unary call (#8591)
The benchmark client is presently spawning a new goroutine per unary call and blocking on its completion. Since the spawning goroutine is blocked, it is more efficient to do the work in the spawning goroutine itself. This change has the following effect on the [benchmark performance](https://grafana-dot-grpc-testing.appspot.com/): 1. Unary 8-core: 184k QPS to 233k QPS (+26%) 2. Unary 30-core: 403k QPS to 624k QPS (+54%) ## Tested * Ran the benchmark on the same GKE cluster to repro the results from the dashboard. * Created a docker image with the changes in this PR. Re-ran the benchmark with the new image. RELEASE NOTES: N/A
1 parent 68caa7c commit e350804

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

benchmark/worker/benchmark_client.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn i
262262
for ic, conn := range conns {
263263
client := testgrpc.NewBenchmarkServiceClient(conn)
264264
// For each connection, create rpcCountPerConn goroutines to do rpc.
265-
for j := 0; j < rpcCountPerConn; j++ {
265+
for j := range rpcCountPerConn {
266266
// Create histogram for each goroutine.
267267
idx := ic*rpcCountPerConn + j
268268
bc.lockingHistograms[idx].histogram = stats.NewHistogram(bc.histogramOptions)
@@ -273,29 +273,18 @@ func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn i
273273
// The worker client needs to wait for some time after client is created,
274274
// before starting benchmark.
275275
if poissonLambda == nil { // Closed loop.
276-
done := make(chan bool)
277276
for {
278-
go func() {
279-
start := time.Now()
280-
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil {
281-
select {
282-
case <-bc.stop:
283-
case done <- false:
284-
}
285-
return
286-
}
287-
elapse := time.Since(start)
288-
bc.lockingHistograms[idx].add(int64(elapse))
289-
select {
290-
case <-bc.stop:
291-
case done <- true:
292-
}
293-
}()
294277
select {
295278
case <-bc.stop:
296279
return
297-
case <-done:
280+
default:
281+
}
282+
start := time.Now()
283+
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil {
284+
continue
298285
}
286+
elapse := time.Since(start)
287+
bc.lockingHistograms[idx].add(int64(elapse))
299288
}
300289
} else { // Open loop.
301290
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / *poissonLambda) * float64(time.Second))

0 commit comments

Comments
 (0)