k8s.io/client-go@v0.31.1/tools/cache/processor_listener_test.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cache
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  )
    26  
    27  const (
    28  	concurrencyLevel = 5
    29  )
    30  
    31  func BenchmarkListener(b *testing.B) {
    32  	var notification addNotification
    33  
    34  	var swg sync.WaitGroup
    35  	swg.Add(b.N)
    36  	b.SetParallelism(concurrencyLevel)
    37  	// Preallocate enough space so that benchmark does not run out of it
    38  	pl := newProcessListener(&ResourceEventHandlerFuncs{
    39  		AddFunc: func(obj interface{}) {
    40  			swg.Done()
    41  		},
    42  	}, 0, 0, time.Now(), 1024*1024, func() bool { return true })
    43  	var wg wait.Group
    44  	defer wg.Wait()       // Wait for .run and .pop to stop
    45  	defer close(pl.addCh) // Tell .run and .pop to stop
    46  	wg.Start(pl.run)
    47  	wg.Start(pl.pop)
    48  
    49  	b.ReportAllocs()
    50  	b.ResetTimer()
    51  	b.RunParallel(func(pb *testing.PB) {
    52  		for pb.Next() {
    53  			pl.add(notification)
    54  		}
    55  	})
    56  	swg.Wait() // Block until all notifications have been received
    57  	b.StopTimer()
    58  }