Skip to content

Commit

Permalink
Add BenchmarkForEachJob
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
  • Loading branch information
colega committed Jan 11, 2022
1 parent 3f61fb4 commit 0f6aeb7
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions concurrency/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package concurrency
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -164,3 +165,37 @@ func TestForEachJob_ShouldReturnImmediatelyOnNoJobsProvided(t *testing.T) {
}))
require.Zero(t, processed.Load())
}

func BenchmarkForEachJob(b *testing.B) {
var ctx = context.Background()

for _, jobsCount := range []int{2, 16, 256, 1024} {
jobs := make([]string, jobsCount)
for j := 0; j < jobsCount; j++ {
jobs[j] = string(byte('a' + j%26))
}
concurrencies := []int{1, 2, 16}
if jobsCount/2 > concurrencies[len(concurrencies)-1] {
concurrencies = append(concurrencies, jobsCount/2)
}
if jobsCount > concurrencies[len(concurrencies)-1] {
concurrencies = append(concurrencies, jobsCount)
}

for _, concurrency := range concurrencies {
name := fmt.Sprintf("%d jobs / concurrency %d", jobsCount, concurrency)
{
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
processed := make([]string, len(jobs))
err := ForEachJob(ctx, len(jobs), concurrency, func(ctx context.Context, idx int) error {
processed[idx] = jobs[idx]
return nil
})
require.NoError(b, err)
}
})
}
}
}
}

0 comments on commit 0f6aeb7

Please sign in to comment.