-
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
8
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.
+30
−33
Open
Changes from 7 commits
Commits
Show all changes
8 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 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
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
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 |
---|---|---|
|
@@ -73,7 +73,6 @@ func (h *lockingHistogram) mergeInto(merged *stats.Histogram) { | |
|
||
type benchmarkClient struct { | ||
closeConns func() | ||
stop chan bool | ||
lastResetTime time.Time | ||
histogramOptions stats.HistogramOptions | ||
lockingHistograms []lockingHistogram | ||
|
@@ -168,7 +167,7 @@ func createConns(config *testpb.ClientConfig) ([]*grpc.ClientConn, func(), error | |
}, nil | ||
} | ||
|
||
func performRPCs(config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benchmarkClient) error { | ||
func performRPCs(ctx context.Context, config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benchmarkClient) error { | ||
// Read payload size and type from config. | ||
var ( | ||
payloadReqSize, payloadRespSize int | ||
|
@@ -212,17 +211,17 @@ func performRPCs(config *testpb.ClientConfig, conns []*grpc.ClientConn, bc *benc | |
|
||
switch config.RpcType { | ||
case testpb.RpcType_UNARY: | ||
bc.unaryLoop(conns, rpcCountPerConn, payloadReqSize, payloadRespSize, poissonLambda) | ||
bc.unaryLoop(ctx, conns, rpcCountPerConn, payloadReqSize, payloadRespSize, poissonLambda) | ||
case testpb.RpcType_STREAMING: | ||
bc.streamingLoop(conns, rpcCountPerConn, payloadReqSize, payloadRespSize, payloadType, poissonLambda) | ||
bc.streamingLoop(ctx, conns, rpcCountPerConn, payloadReqSize, payloadRespSize, payloadType, poissonLambda) | ||
default: | ||
return status.Errorf(codes.InvalidArgument, "unknown rpc type: %v", config.RpcType) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) { | ||
func startBenchmarkClient(ctx context.Context, config *testpb.ClientConfig) (*benchmarkClient, error) { | ||
printClientConfig(config) | ||
|
||
// Set running environment like how many cores to use. | ||
|
@@ -243,13 +242,12 @@ func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) | |
}, | ||
lockingHistograms: make([]lockingHistogram, rpcCountPerConn*len(conns)), | ||
|
||
stop: make(chan bool), | ||
lastResetTime: time.Now(), | ||
closeConns: closeConns, | ||
rusageLastReset: syscall.GetRusage(), | ||
} | ||
|
||
if err = performRPCs(config, conns, bc); err != nil { | ||
if err = performRPCs(ctx, config, conns, bc); err != nil { | ||
// Close all connections if performRPCs failed. | ||
closeConns() | ||
return nil, err | ||
|
@@ -258,14 +256,17 @@ func startBenchmarkClient(config *testpb.ClientConfig) (*benchmarkClient, error) | |
return bc, nil | ||
} | ||
|
||
func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn int, reqSize int, respSize int, poissonLambda *float64) { | ||
func (bc *benchmarkClient) unaryLoop(ctx context.Context, conns []*grpc.ClientConn, rpcCountPerConn int, reqSize int, respSize int, poissonLambda *float64) { | ||
for ic, conn := range conns { | ||
client := testgrpc.NewBenchmarkServiceClient(conn) | ||
// For each connection, create rpcCountPerConn goroutines to do rpc. | ||
for j := range rpcCountPerConn { | ||
// Create histogram for each goroutine. | ||
idx := ic*rpcCountPerConn + j | ||
bc.lockingHistograms[idx].histogram = stats.NewHistogram(bc.histogramOptions) | ||
if ctx.Err() != nil { | ||
return | ||
} | ||
// Start goroutine on the created mutex and histogram. | ||
go func(idx int) { | ||
// TODO: do warm up if necessary. | ||
|
@@ -274,31 +275,25 @@ func (bc *benchmarkClient) unaryLoop(conns []*grpc.ClientConn, rpcCountPerConn i | |
// before starting benchmark. | ||
if poissonLambda == nil { // Closed loop. | ||
for { | ||
select { | ||
case <-bc.stop: | ||
return | ||
default: | ||
} | ||
start := time.Now() | ||
if err := benchmark.DoUnaryCall(client, reqSize, respSize); err != nil { | ||
continue | ||
if err := benchmark.DoUnaryCall(ctx, client, reqSize, respSize); err != nil { | ||
return | ||
} | ||
elapse := time.Since(start) | ||
bc.lockingHistograms[idx].add(int64(elapse)) | ||
} | ||
} else { // Open loop. | ||
timeBetweenRPCs := time.Duration((rand.ExpFloat64() / *poissonLambda) * float64(time.Second)) | ||
time.AfterFunc(timeBetweenRPCs, func() { | ||
bc.poissonUnary(client, idx, reqSize, respSize, *poissonLambda) | ||
bc.poissonUnary(ctx, client, idx, reqSize, respSize, *poissonLambda) | ||
}) | ||
} | ||
|
||
}(idx) | ||
} | ||
} | ||
} | ||
|
||
func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerConn int, reqSize int, respSize int, payloadType string, poissonLambda *float64) { | ||
func (bc *benchmarkClient) streamingLoop(ctx context.Context, conns []*grpc.ClientConn, rpcCountPerConn int, reqSize int, respSize int, payloadType string, poissonLambda *float64) { | ||
var doRPC func(testgrpc.BenchmarkService_StreamingCallClient, int, int) error | ||
if payloadType == "bytebuf" { | ||
doRPC = benchmark.DoByteBufStreamingRoundTrip | ||
|
@@ -309,13 +304,16 @@ 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(ctx) | ||
if err != nil { | ||
logger.Fatalf("%v.StreamingCall(_) = _, %v", c, err) | ||
} | ||
idx := ic*rpcCountPerConn + j | ||
bc.lockingHistograms[idx].histogram = stats.NewHistogram(bc.histogramOptions) | ||
if poissonLambda == nil { // Closed loop. | ||
if stream.Context().Err() != nil { | ||
return | ||
} | ||
// Start goroutine on the created mutex and histogram. | ||
go func(idx int) { | ||
// TODO: do warm up if necessary. | ||
Comment on lines
312
to
319
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. You said
so I decided to do it like that. If it is not the appropriate way, please tell me |
||
|
@@ -329,11 +327,6 @@ func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerCo | |
} | ||
elapse := time.Since(start) | ||
bc.lockingHistograms[idx].add(int64(elapse)) | ||
select { | ||
case <-bc.stop: | ||
return | ||
default: | ||
} | ||
} | ||
}(idx) | ||
} else { // Open loop. | ||
|
@@ -346,24 +339,26 @@ func (bc *benchmarkClient) streamingLoop(conns []*grpc.ClientConn, rpcCountPerCo | |
} | ||
} | ||
|
||
func (bc *benchmarkClient) poissonUnary(client testgrpc.BenchmarkServiceClient, idx int, reqSize int, respSize int, lambda float64) { | ||
func (bc *benchmarkClient) poissonUnary(ctx context.Context, 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 err := benchmark.DoUnaryCall(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() { | ||
bc.poissonUnary(client, idx, reqSize, respSize, lambda) | ||
bc.poissonUnary(ctx, 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() | ||
|
||
if err := doRPC(stream, reqSize, respSize); err != nil { | ||
return | ||
} | ||
|
@@ -430,6 +425,5 @@ func (bc *benchmarkClient) getStats(reset bool) *testpb.ClientStats { | |
} | ||
|
||
func (bc *benchmarkClient) shutdown() { | ||
close(bc.stop) | ||
bc.closeConns() | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.