...

Source file src/cloud.google.com/go/pubsub/internal/scheduler/publish_scheduler_benchmark_test.go

Documentation: cloud.google.com/go/pubsub/internal/scheduler

     1  // Copyright 2019 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  	"reflect"
    20  	"testing"
    21  
    22  	"cloud.google.com/go/pubsub/internal/scheduler"
    23  )
    24  
    25  const pubSchedulerWorkers = 100
    26  
    27  func BenchmarkPublisher_Unkeyed(b *testing.B) {
    28  	wait := make(chan struct{}, b.N)
    29  	ps := scheduler.NewPublishScheduler(pubSchedulerWorkers, func(bundle interface{}) {
    30  		nlen := reflect.ValueOf(bundle).Len()
    31  		for i := 0; i < nlen; i++ {
    32  			wait <- struct{}{}
    33  		}
    34  	})
    35  	go func() {
    36  		for i := 0; i < b.N; i++ {
    37  			if err := ps.Add("", fmt.Sprintf("item_%d", i), 1); err != nil {
    38  				b.Error(err)
    39  			}
    40  		}
    41  	}()
    42  	for j := 0; j < b.N; j++ {
    43  		<-wait
    44  	}
    45  }
    46  
    47  func BenchmarkPublisher_SingleKey(b *testing.B) {
    48  	wait := make(chan struct{}, b.N)
    49  	ps := scheduler.NewPublishScheduler(pubSchedulerWorkers, func(bundle interface{}) {
    50  		nlen := reflect.ValueOf(bundle).Len()
    51  		for i := 0; i < nlen; i++ {
    52  			wait <- struct{}{}
    53  		}
    54  	})
    55  	go func() {
    56  		for i := 0; i < b.N; i++ {
    57  			if err := ps.Add("some-key", fmt.Sprintf("item_%d", i), 1); err != nil {
    58  				b.Error(err)
    59  			}
    60  		}
    61  	}()
    62  	for j := 0; j < b.N; j++ {
    63  		<-wait
    64  	}
    65  }
    66  

View as plain text