-
Notifications
You must be signed in to change notification settings - Fork 157
fix: use map lookup to find subscription instead of looping #1441
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
Closed
dkorittki
wants to merge
10
commits into
master
from
dominik/eng-9080-use-resolve-context-to-do-fast-lookup-of-subscriptions
+337
−16
Closed
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b345aee
fix: use direct map lookup to find subscription
dkorittki 54346fa
fix: update mock interface in tests
dkorittki 28f21ed
chore: revert all changes
dkorittki 7540333
fix: Use a map to lookup a resolve context
dkorittki baceae5
Merge branch 'master' into dominik/eng-9080-use-resolve-context-to-do…
dkorittki ec16d98
chore: add tests
dkorittki 9192199
Merge branch 'master' into dominik/eng-9080-use-resolve-context-to-do…
dkorittki 0f27265
chore: add benchmark
dkorittki 2e4d02d
feat: add fallback
dkorittki 97f3769
chore: use newContext to create test contexts
dkorittki 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
163 changes: 163 additions & 0 deletions
163
v2/pkg/engine/resolve/resolve_subscription_benchmark_test.go
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 |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package resolve | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func BenchmarkUpdateSubscription(b *testing.B) { | ||
| for _, n := range []int{1, 10, 100, 1000} { | ||
| b.Run(func() string { | ||
| switch n { | ||
| case 1: | ||
| return "1_subs" | ||
| case 10: | ||
| return "10_subs" | ||
| case 100: | ||
| return "100_subs" | ||
| default: | ||
| return "1000_subs" | ||
| } | ||
| }(), func(b *testing.B) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| b.Cleanup(cancel) | ||
|
|
||
| streamDone := make(chan struct{}) | ||
| b.Cleanup(func() { close(streamDone) }) | ||
|
|
||
| updaters := make([]func([]byte), n) | ||
| var setupWg sync.WaitGroup | ||
| setupWg.Add(n) | ||
|
|
||
| // Each subscription gets its own fakeStream so that its | ||
| // subscriptionOnStartFn captures the correct slot index i. | ||
| // executeStartupHooks uses add.resolve.Trigger.Source (the | ||
| // subscription's own plan source), so the hook fires on the | ||
| // right stream regardless of goroutine scheduling order. | ||
| // All subscriptions share the same static input, so they all | ||
| // land on a single trigger whose subscriptionIdentifiers map | ||
| // grows to N entries — the map we want to exercise. | ||
| makePlan := func(i int) *GraphQLSubscription { | ||
| stream := createFakeStream( | ||
| func(counter int) (message string, done bool) { | ||
| <-streamDone | ||
| return "", true | ||
| }, | ||
| 0, | ||
| nil, | ||
| func(hookCtx StartupHookContext, _ []byte) error { | ||
| updaters[i] = hookCtx.Updater | ||
| setupWg.Done() | ||
| return nil | ||
| }, | ||
| ) | ||
|
|
||
| fetches := Sequence() | ||
| fetches.Trigger = &FetchTreeNode{ | ||
| Kind: FetchTreeNodeKindTrigger, | ||
| Item: &FetchItem{ | ||
| Fetch: &SingleFetch{ | ||
| FetchDependencies: FetchDependencies{ | ||
| FetchID: 0, | ||
| }, | ||
| Info: &FetchInfo{ | ||
| DataSourceID: "0", | ||
| DataSourceName: "counter", | ||
| QueryPlan: &QueryPlan{ | ||
| Query: "subscription {\n counter\n}", | ||
| }, | ||
| }, | ||
| }, | ||
| ResponsePath: "counter", | ||
| }, | ||
| } | ||
|
|
||
| return &GraphQLSubscription{ | ||
| Trigger: GraphQLSubscriptionTrigger{ | ||
| Source: stream, | ||
| InputTemplate: InputTemplate{ | ||
| Segments: []TemplateSegment{ | ||
| { | ||
| SegmentType: StaticSegmentType, | ||
| Data: []byte(`{"method":"POST","url":"http://localhost:4000","body":{"query":"subscription { counter }"}}`), | ||
| }, | ||
| }, | ||
| }, | ||
| PostProcessing: PostProcessingConfiguration{ | ||
| SelectResponseDataPath: []string{"data"}, | ||
| SelectResponseErrorsPath: []string{"errors"}, | ||
| }, | ||
| }, | ||
| Response: &GraphQLResponse{ | ||
| Data: &Object{ | ||
| Fields: []*Field{ | ||
| { | ||
| Name: []byte("counter"), | ||
| Value: &Integer{ | ||
| Path: []string{"counter"}, | ||
| }, | ||
| Info: &FieldInfo{ | ||
| Name: "counter", | ||
| ExactParentTypeName: "Subscription", | ||
| Source: TypeFieldSource{ | ||
| IDs: []string{"0"}, | ||
| Names: []string{"counter"}, | ||
| }, | ||
| FetchID: 0, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| Fetches: fetches, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| resolver := newResolver(ctx) | ||
|
|
||
| recorders := make([]*SubscriptionRecorder, n) | ||
| for i := 0; i < n; i++ { | ||
| recorders[i] = &SubscriptionRecorder{ | ||
| buf: &bytes.Buffer{}, | ||
| messages: []string{}, | ||
| complete: atomic.Bool{}, | ||
| } | ||
| subCtx := &Context{ctx: context.Background()} | ||
| id := SubscriptionIdentifier{ | ||
| ConnectionID: 1, | ||
| SubscriptionID: int64(i + 1), | ||
| } | ||
| err := resolver.AsyncResolveGraphQLSubscription(subCtx, makePlan(i), recorders[i], id) | ||
| require.NoError(b, err) | ||
| } | ||
|
|
||
| // Block until all N startup hooks have fired, guaranteeing all | ||
| // entries are in subscriptionIdentifiers before timing starts. | ||
| setupWg.Wait() | ||
|
|
||
| b.ResetTimer() | ||
| b.ReportAllocs() | ||
|
|
||
| data := []byte(`{"data":{"counter":1}}`) | ||
| for i := 0; i < b.N; i++ { | ||
| // Update every subscription on the trigger sequentially. | ||
| // Each call does an O(1) map lookup in subscriptionIdentifiers | ||
| // then delivers to that subscription's worker. | ||
| for j := 0; j < n; j++ { | ||
| updaters[j](data) | ||
| } | ||
| } | ||
|
|
||
| // Every recorder must have received exactly b.N messages. | ||
| for i := 0; i < n; i++ { | ||
| recorders[i].AwaitMessages(b, b.N, 30*time.Second) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.