1/* 2Copyright 2016 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package cache 18 19import ( 20 "sync" 21 "testing" 22 "time" 23 24 "k8s.io/apimachinery/pkg/util/wait" 25) 26 27const ( 28 concurrencyLevel = 5 29) 30 31func 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) 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} 59