Skip to content

Commit

Permalink
[chore] remove time.Sleep from benchmarks (#8859)
Browse files Browse the repository at this point in the history
Refactor benchmarks to run as tests as well, remove use of time.Sleep in
benchmarks.
  • Loading branch information
atoulme authored Dec 19, 2023
1 parent b04b551 commit a7dc838
Showing 1 changed file with 43 additions and 23 deletions.
66 changes: 43 additions & 23 deletions exporter/exporterhelper/internal/bounded_memory_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package internal

import (
"context"
"errors"
"fmt"
"strconv"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -98,61 +99,80 @@ func TestShutdownWhileNotEmpty(t *testing.T) {
}

func Benchmark_QueueUsage_10000_1_50000(b *testing.B) {
queueUsage(b, 10000, 1, 50000)
benchmarkQueueUsage(b, 10000, 1, 50000)
}

func Benchmark_QueueUsage_10000_2_50000(b *testing.B) {
queueUsage(b, 10000, 2, 50000)
benchmarkQueueUsage(b, 10000, 2, 50000)
}
func Benchmark_QueueUsage_10000_5_50000(b *testing.B) {
queueUsage(b, 10000, 5, 50000)
benchmarkQueueUsage(b, 10000, 5, 50000)
}
func Benchmark_QueueUsage_10000_10_50000(b *testing.B) {
queueUsage(b, 10000, 10, 50000)
benchmarkQueueUsage(b, 10000, 10, 50000)
}

func Benchmark_QueueUsage_50000_1_50000(b *testing.B) {
queueUsage(b, 50000, 1, 50000)
benchmarkQueueUsage(b, 50000, 1, 50000)
}

func Benchmark_QueueUsage_50000_2_50000(b *testing.B) {
queueUsage(b, 50000, 2, 50000)
benchmarkQueueUsage(b, 50000, 2, 50000)
}
func Benchmark_QueueUsage_50000_5_50000(b *testing.B) {
queueUsage(b, 50000, 5, 50000)
benchmarkQueueUsage(b, 50000, 5, 50000)
}
func Benchmark_QueueUsage_50000_10_50000(b *testing.B) {
queueUsage(b, 50000, 10, 50000)
benchmarkQueueUsage(b, 50000, 10, 50000)
}

func Benchmark_QueueUsage_10000_1_250000(b *testing.B) {
queueUsage(b, 10000, 1, 250000)
benchmarkQueueUsage(b, 10000, 1, 250000)
}

func Benchmark_QueueUsage_10000_2_250000(b *testing.B) {
queueUsage(b, 10000, 2, 250000)
benchmarkQueueUsage(b, 10000, 2, 250000)
}
func Benchmark_QueueUsage_10000_5_250000(b *testing.B) {
queueUsage(b, 10000, 5, 250000)
benchmarkQueueUsage(b, 10000, 5, 250000)
}
func Benchmark_QueueUsage_10000_10_250000(b *testing.B) {
queueUsage(b, 10000, 10, 250000)
benchmarkQueueUsage(b, 10000, 10, 250000)
}

func queueUsage(b *testing.B, capacity int, numConsumers int, numberOfItems int) {
func TestQueueUsage(t *testing.T) {
t.Run("with enough workers", func(t *testing.T) {
queueUsage(t, 10000, 5, 1000)
})
t.Run("past capacity", func(t *testing.T) {
queueUsage(t, 10000, 2, 50000)
})
}

func benchmarkQueueUsage(b *testing.B, capacity int, numConsumers int, numberOfItems int) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
q := NewBoundedMemoryQueue[string](capacity)
consumers := NewQueueConsumers(q, numConsumers, func(context.Context, string) error {
time.Sleep(1 * time.Millisecond)
return nil
})
require.NoError(b, consumers.Start(context.Background(), componenttest.NewNopHost()))
for j := 0; j < numberOfItems; j++ {
_ = q.Offer(context.Background(), fmt.Sprintf("%d", j))
queueUsage(b, capacity, numConsumers, numberOfItems)
}
}

func queueUsage(tb testing.TB, capacity int, numConsumers int, numberOfItems int) {
var wg sync.WaitGroup
wg.Add(numberOfItems)
q := NewBoundedMemoryQueue[string](capacity)
consumers := NewQueueConsumers(q, numConsumers, func(context.Context, string) error {
wg.Done()
return nil
})
require.NoError(tb, consumers.Start(context.Background(), componenttest.NewNopHost()))
for j := 0; j < numberOfItems; j++ {
if err := q.Offer(context.Background(), fmt.Sprintf("%d", j)); errors.Is(err, ErrQueueIsFull) {
wg.Done()
}
assert.NoError(b, consumers.Shutdown(context.Background()))
}
assert.NoError(tb, consumers.Shutdown(context.Background()))

wg.Wait()
}

func TestZeroSizeNoConsumers(t *testing.T) {
Expand Down

0 comments on commit a7dc838

Please sign in to comment.