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

     1  /*
     2  
     3  Copyright (c) 2024 - 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  // QueueFactory is a function that emits a Queue[time.Time]
    24  type QueueFactory func(capacity int) Queue[time.Time]
    25  
    26  func doQueueBenchmark(queueFactory QueueFactory, sampleSize int, b *testing.B) {
    27  	for iteration := 0; iteration < b.N; iteration++ {
    28  		q := queueFactory(sampleSize)
    29  		for x := 0; x < sampleSize; x++ {
    30  			q.Enqueue(time.Now().UTC())
    31  		}
    32  		for x := 0; x < sampleSize; x++ {
    33  			q.Dequeue()
    34  		}
    35  	}
    36  }
    37  
    38  func doFixedQueueBenchmark(queueFactory QueueFactory, sampleSize, stasisSize int, b *testing.B) {
    39  	for iteration := 0; iteration < b.N; iteration++ {
    40  		q := queueFactory(stasisSize)
    41  		for x := 0; x < sampleSize; x++ {
    42  			q.Enqueue(time.Now().UTC())
    43  			if q.Len() < stasisSize {
    44  				continue
    45  			}
    46  			q.Dequeue()
    47  		}
    48  	}
    49  }
    50  
    51  func makeLinkedList[T any](capacity int) Queue[T] {
    52  	return NewLinkedList[T]()
    53  }
    54  
    55  func makeChannelQueue[T any](capacity int) Queue[T] {
    56  	return NewChannelQueueWithCapacity[T](capacity)
    57  }
    58  
    59  func makeRingBuffer[T any](capacity int) Queue[T] {
    60  	return NewRingBufferWithCapacity[T](capacity)
    61  }
    62  
    63  func makeSyncedRingBuffer[T any](capacity int) Queue[T] {
    64  	rb := NewSyncRingBufferWithCapacity[T](capacity)
    65  	return rb
    66  }
    67  
    68  func BenchmarkLinkedList(b *testing.B) {
    69  	doQueueBenchmark(makeLinkedList[time.Time], DefaultSampleSize, b)
    70  }
    71  
    72  func BenchmarkChannelQueue(b *testing.B) {
    73  	doQueueBenchmark(makeChannelQueue[time.Time], DefaultSampleSize, b)
    74  }
    75  
    76  func BenchmarkRingBuffer(b *testing.B) {
    77  	doQueueBenchmark(makeRingBuffer[time.Time], DefaultSampleSize, b)
    78  }
    79  
    80  func BenchmarkRingBufferSynced(b *testing.B) {
    81  	doQueueBenchmark(makeSyncedRingBuffer[time.Time], DefaultSampleSize, b)
    82  }
    83  
    84  func BenchmarkFixedLinkedList(b *testing.B) {
    85  	doFixedQueueBenchmark(makeLinkedList[time.Time], DefaultSampleSize, DefaultStasisSize, b)
    86  }
    87  
    88  func BenchmarkFixedChannelQueue(b *testing.B) {
    89  	doFixedQueueBenchmark(makeChannelQueue[time.Time], DefaultSampleSize, DefaultStasisSize, b)
    90  }
    91  
    92  func BenchmarkFixedRingBuffer(b *testing.B) {
    93  	doFixedQueueBenchmark(makeRingBuffer[time.Time], DefaultSampleSize, DefaultStasisSize, b)
    94  }