github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/queues/benchutils_test.go (about) 1 package goqueuestest 2 3 import ( 4 "sync" 5 "testing" 6 ) 7 8 const ( 9 useFixedBench = true 10 benchIterations = 5000000 11 ) 12 13 func runParallelBench(b *testing.B, numRoutines int, q Queue, test func(Queue, int)) { 14 if useFixedBench { 15 b.N = benchIterations 16 } 17 N := b.N / numRoutines 18 wg := &sync.WaitGroup{} 19 for i := 0; i < numRoutines; i += 1 { 20 wg.Add(1) 21 go func(){ 22 test(q, N) 23 wg.Done() 24 }() 25 } 26 wg.Wait() 27 } 28 29 func benchANRN(q Queue, N int){ 30 for i := 0; i < N; i += 1 { 31 q.Enqueue(i) 32 } 33 for i := 0; i < N; i += 1 { 34 q.Dequeue() 35 } 36 } 37 38 func benchA1R1(q Queue, N int){ 39 for i := 0; i < N; i += 1 { 40 q.Enqueue(i) 41 q.Dequeue() 42 } 43 } 44 45 func benchA1R2(q Queue, N int){ 46 N2 := N / 2 47 for i := 0; i < N2; i += 1 { 48 q.Enqueue(i) 49 } 50 for i := 0; i < N2; i += 1 { 51 q.Enqueue(i) 52 q.Dequeue() 53 q.Dequeue() 54 } 55 } 56 57 func benchA2R1(q Queue, N int){ 58 N2 := N / 2 59 for i := 0; i < N2; i += 1 { 60 q.Enqueue(i) 61 q.Enqueue(i) 62 q.Dequeue() 63 } 64 for i := 0; i < N2; i += 1 { 65 q.Dequeue() 66 } 67 } 68