-
Notifications
You must be signed in to change notification settings - Fork 4.6k
benchmark/client: add context for cancellation #8614
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
wdsjk
wants to merge
12
commits into
grpc:master
Choose a base branch
from
wdsjk:benchmark-client-context-cancellation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+14
−19
Open
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c92ee83
benchmark/client: add context for cancellation
wdsjk b1db5d5
add comment to an exported function in benchmark.go
wdsjk cf5b7ef
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk 123e0d4
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk 87d80e9
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk ea7453f
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk 798974a
refactor: add passing the context through function params
wdsjk 7161064
refactor: change context.TODO() to context.Background() in benchmain/…
wdsjk f3c53df
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk 08c45bc
refactor: minor changes
wdsjk 13f3600
Merge branch 'grpc:master' into benchmark-client-context-cancellation
wdsjk e09a7f5
fix performance drop
wdsjk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ package main | |
|
||
import ( | ||
"context" | ||
"errors" | ||
"flag" | ||
"math" | ||
rand "math/rand/v2" | ||
|
@@ -73,7 +74,8 @@ func (h *lockingHistogram) mergeInto(merged *stats.Histogram) { | |
|
||
type benchmarkClient struct { | ||
closeConns func() | ||
stop chan bool | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
lastResetTime time.Time | ||
histogramOptions stats.HistogramOptions | ||
lockingHistograms []lockingHistogram | ||
|
@@ -223,6 +225,10 @@ func performRPCs(config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benc | |
} | ||
|
||
func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) { | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return startBenchmarkClientWithContext(context.Background(), config) | ||
} | ||
|
||
func startBenchmarkClientWithContext(ctx context.Context, config *testpb.ClientConfig) (*benchmarkClient, error) { | ||
printClientConfig(config) | ||
|
||
// Set running environment like how many cores to use. | ||
|
@@ -233,6 +239,7 @@ func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) | |
return nil, err | ||
} | ||
|
||
ctx, cancel := context.WithCancel(ctx) | ||
rpcCountPerConn := int(config.OutstandingRpcsPerChannel) | ||
bc := &benchmarkClient{ | ||
histogramOptions: stats.HistogramOptions{ | ||
|
@@ -243,7 +250,8 @@ func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) | |
}, | ||
lockingHistograms: make([]lockingHistogram, rpcCountPerConn*len(conns)), | ||
|
||
stop: make(chan bool), | ||
ctx: ctx, | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
cancel: cancel, | ||
lastResetTime: time.Now(), | ||
closeConns: closeConns, | ||
rusageLastReset: syscall.GetRusage(), | ||
|
@@ -274,13 +282,14 @@ func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn i | |
// before starting benchmark. | ||
if poissonLambda == nil { // Closed loop. | ||
for { | ||
select { | ||
case <-bc.stop: | ||
if bc.ctx.Err() != nil { | ||
return | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
default: | ||
} | ||
start := time.Now() | ||
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil { | ||
if err := benchmark.DoUnaryCallWithContext(bc.ctx, client, reqSize, respSize); err != nil { | ||
if status.Code(err) == codes.Canceled || errors.Is(err, context.Canceled) { | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
continue | ||
} | ||
elapse := time.Since(start) | ||
|
@@ -289,10 +298,12 @@ func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn i | |
} else { // Open loop. | ||
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / *poissonLambda) * float64(time.Second)) | ||
time.AfterFunc(timeBetweenRPCs, func() { | ||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
bc.poissonUnary(client, idx, reqSize, respSize, *poissonLambda) | ||
}) | ||
} | ||
|
||
}(idx) | ||
} | ||
} | ||
|
@@ -309,8 +320,11 @@ func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerCo | |
// For each connection, create rpcCountPerConn goroutines to do rpc. | ||
for j := 0; j < rpcCountPerConn; j++ { | ||
c := testgrpc.NewBenchmarkServiceClient(conn) | ||
stream, err := c.StreamingCall(context.Background()) | ||
stream, err := c.StreamingCall(bc.ctx) | ||
if err != nil { | ||
if status.Code(err) == codes.Canceled || errors.Is(err, context.Canceled) { | ||
return | ||
} | ||
logger.Fatalf("%v.StreamingCall(_) = _, %v", c, err) | ||
} | ||
idx := ic*rpcCountPerConn + j | ||
|
@@ -323,22 +337,26 @@ func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerCo | |
// The worker client needs to wait for some time after client is created, | ||
// before starting benchmark. | ||
for { | ||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
start := time.Now() | ||
if err := doRPC(stream, reqSize, respSize); err != nil { | ||
return | ||
if status.Code(err) == codes.Canceled || errors.Is(err, context.Canceled) { | ||
return | ||
} | ||
continue | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
elapse := time.Since(start) | ||
bc.lockingHistograms[idx].add(int64(elapse)) | ||
select { | ||
case <-bc.stop: | ||
return | ||
default: | ||
} | ||
Comment on lines
332
to
336
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should replace this with a check for |
||
} | ||
}(idx) | ||
} else { // Open loop. | ||
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / *poissonLambda) * float64(time.Second)) | ||
time.AfterFunc(timeBetweenRPCs, func() { | ||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
bc.poissonStreaming(stream, idx, reqSize, respSize, *poissonLambda, doRPC) | ||
}) | ||
} | ||
|
@@ -349,21 +367,34 @@ func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerCo | |
func (bc *benchmarkClient) poissonUnary(client testgrpc.BenchmarkServiceClient, idx int, reqSize int, respSize int, lambda float64) { | ||
go func() { | ||
start := time.Now() | ||
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil { | ||
|
||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if err := benchmark.DoUnaryCallWithContext(bc.ctx, client, reqSize, respSize); err != nil { | ||
return | ||
} | ||
elapse := time.Since(start) | ||
bc.lockingHistograms[idx].add(int64(elapse)) | ||
}() | ||
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / lambda) * float64(time.Second)) | ||
time.AfterFunc(timeBetweenRPCs, func() { | ||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
bc.poissonUnary(client, idx, reqSize, respSize, lambda) | ||
}) | ||
} | ||
|
||
func (bc *benchmarkClient) poissonStreaming(stream testgrpc.BenchmarkService_StreamingCallClient, idx int, reqSize int, respSize int, lambda float64, doRPC func(testgrpc.BenchmarkService_StreamingCallClient, int, int) error) { | ||
go func() { | ||
start := time.Now() | ||
|
||
wdsjk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if err := doRPC(stream, reqSize, respSize); err != nil { | ||
return | ||
} | ||
|
@@ -372,6 +403,9 @@ func (bc *benchmarkClient) poissonStreaming(stream testgrpc.BenchmarkService_Str | |
}() | ||
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / lambda) * float64(time.Second)) | ||
time.AfterFunc(timeBetweenRPCs, func() { | ||
if bc.ctx.Err() != nil { | ||
return | ||
} | ||
arjan-bal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
bc.poissonStreaming(stream, idx, reqSize, respSize, lambda, doRPC) | ||
}) | ||
} | ||
|
@@ -430,6 +464,8 @@ func (bc *benchmarkClient) getStats(reset bool) *testpb.ClientStats { | |
} | ||
|
||
func (bc *benchmarkClient) shutdown() { | ||
close(bc.stop) | ||
if bc.cancel != nil { | ||
bc.cancel() | ||
} | ||
bc.closeConns() | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please revert the change to pass a context to
DoUnaryCall
? Using an empty context (like the ones returned Background() or TODO()) seems to be important to avoid contention in concurrent RPCs.See #8614 (comment).