github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/queues/pathological/pathological_test.go (about)

     1  package pathological
     2  import (
     3  	"sync"
     4  	"testing"
     5  )
     6  
     7  const (
     8  	useFixedBench = true
     9  	benchIterations = 5000000
    10  )
    11  
    12  func runParallelBench(b *testing.B, numRoutines int, q Queue, test func(Queue, int)) {
    13  	if useFixedBench {
    14  		b.N = benchIterations
    15  	}
    16  	N := b.N / numRoutines
    17  	wg := &sync.WaitGroup{}
    18  	for i := 0; i < numRoutines; i += 1 {
    19  		wg.Add(1)
    20  		go func(){
    21  			test(q, N)
    22  			wg.Done()
    23  		}()
    24  	}
    25  	wg.Wait()
    26  }
    27  
    28  func benchANRN(q Queue, N int){
    29  	for i := 0; i < N; i += 1 {
    30  		q.Enqueue(i)
    31  	}
    32  	for i := 0; i < N; i += 1 {
    33  		q.Dequeue()
    34  	}
    35  }
    36  
    37  // ScLifo
    38  func BenchmarkScLifo_ANRNx2(b *testing.B) { runParallelBench(b, 2, NewScLifo(), benchANRN)}
    39  func BenchmarkScLifo_ANRNx3(b *testing.B) { runParallelBench(b, 3, NewScLifo(), benchANRN)}
    40  
    41  // SmLifo
    42  func BenchmarkSmLifo_ANRNx2(b *testing.B) { runParallelBench(b, 2, NewSmLifo(), benchANRN)}
    43  func BenchmarkSmLifo_ANRNx3(b *testing.B) { runParallelBench(b, 3, NewSmLifo(), benchANRN)}