github.com/blend/go-sdk@v1.20220411.3/collections/queue_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package collections
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  const (
    16  	// DefaultSampleSize is the default number of steps to run per test.
    17  	DefaultSampleSize = 10000
    18  
    19  	// DefaultStasisSize is the stasis size for the fixed length test.
    20  	DefaultStasisSize = 512
    21  )
    22  
    23  type QueueFactory func(capacity int) Queue
    24  
    25  func doQueueBenchmark(queueFactory QueueFactory, sampleSize int, b *testing.B) {
    26  	for iteration := 0; iteration < b.N; iteration++ {
    27  		q := queueFactory(sampleSize)
    28  		for x := 0; x < sampleSize; x++ {
    29  			q.Enqueue(time.Now().UTC())
    30  		}
    31  		for x := 0; x < sampleSize; x++ {
    32  			q.Dequeue()
    33  		}
    34  	}
    35  }
    36  
    37  func doFixedQueueBenchmark(queueFactory QueueFactory, sampleSize, stasisSize int, b *testing.B) {
    38  	for iteration := 0; iteration < b.N; iteration++ {
    39  		q := queueFactory(stasisSize)
    40  		for x := 0; x < sampleSize; x++ {
    41  			q.Enqueue(time.Now().UTC())
    42  			if q.Len() < stasisSize {
    43  				continue
    44  			}
    45  			q.Dequeue()
    46  		}
    47  	}
    48  }
    49  
    50  func makeLinkedList(capacity int) Queue {
    51  	return NewLinkedList()
    52  }
    53  
    54  func makeChannelQueue(capacity int) Queue {
    55  	return NewChannelQueueWithCapacity(capacity)
    56  }
    57  
    58  func makeRingBuffer(capacity int) Queue {
    59  	return NewRingBufferWithCapacity(capacity)
    60  }
    61  
    62  func makeSyncedRingBuffer(capacity int) Queue {
    63  	rb := NewSyncRingBufferWithCapacity(capacity)
    64  	return rb
    65  }
    66  
    67  func BenchmarkLinkedList(b *testing.B) {
    68  	doQueueBenchmark(makeLinkedList, DefaultSampleSize, b)
    69  }
    70  
    71  func BenchmarkChannelQueue(b *testing.B) {
    72  	doQueueBenchmark(makeChannelQueue, DefaultSampleSize, b)
    73  }
    74  
    75  func BenchmarkRingBuffer(b *testing.B) {
    76  	doQueueBenchmark(makeRingBuffer, DefaultSampleSize, b)
    77  }
    78  
    79  func BenchmarkRingBufferSynced(b *testing.B) {
    80  	doQueueBenchmark(makeSyncedRingBuffer, DefaultSampleSize, b)
    81  }
    82  
    83  func BenchmarkFixedLinkedList(b *testing.B) {
    84  	doFixedQueueBenchmark(makeLinkedList, DefaultSampleSize, DefaultStasisSize, b)
    85  }
    86  
    87  func BenchmarkFixedChannelQueue(b *testing.B) {
    88  	doFixedQueueBenchmark(makeChannelQueue, DefaultSampleSize, DefaultStasisSize, b)
    89  }
    90  
    91  func BenchmarkFixedRingBuffer(b *testing.B) {
    92  	doFixedQueueBenchmark(makeRingBuffer, DefaultSampleSize, DefaultStasisSize, b)
    93  }