Debian Codegraph @

pubsub/v2/internal/scheduler/receive_scheduler_benchmark_test.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package scheduler_test
16
17 import (
18 "fmt"
19 "testing"
20
21 "cloud.google.com/go/pubsub/v2/internal/scheduler"
22 )
23
24 const recSchedulerWorkers = 100
25
26 func BenchmarkReceive_SingleKey(b *testing.B) {
27 wait := make(chan struct{}, b.N)
28 ps := scheduler.NewReceiveScheduler(recSchedulerWorkers)
29 go func() {
30 for i := 0; i < b.N; i++ {
31 if err := ps.Add("some-key", fmt.Sprintf("item_%d", i), func(interface{}) {
32 wait <- struct{}{}
33 }); err != nil {
34 b.Error(err)
35 }
36 }
37 }()
38 for j := 0; j < b.N; j++ {
39 <-wait
40 }
41 }
42
43 func BenchmarkReceive_Unkeyed(b *testing.B) {
44 wait := make(chan struct{}, b.N)
45 ps := scheduler.NewReceiveScheduler(recSchedulerWorkers)
46 go func() {
47 for i := 0; i < b.N; i++ {
48 if err := ps.Add("", fmt.Sprintf("item_%d", i), func(interface{}) {
49 wait <- struct{}{}
50 }); err != nil {
51 b.Error(err)
52 }
53 }
54 }()
55 for j := 0; j < b.N; j++ {
56 <-wait
57 }
58 }