All checks were successful
DCO action / DCO (pull_request) Successful in 35s
Tests and linters / Run gofumpt (pull_request) Successful in 47s
Vulncheck / Vulncheck (pull_request) Successful in 48s
Tests and linters / Staticcheck (pull_request) Successful in 1m7s
Tests and linters / Lint (pull_request) Successful in 1m29s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m37s
Tests and linters / Tests (pull_request) Successful in 1m38s
Tests and linters / Tests with -race (pull_request) Successful in 1m36s
Tests and linters / gopls check (pull_request) Successful in 1m46s
Vulncheck / Vulncheck (push) Successful in 46s
Tests and linters / Run gofumpt (push) Successful in 45s
Tests and linters / Staticcheck (push) Successful in 1m8s
Tests and linters / Tests (push) Successful in 1m28s
Tests and linters / Lint (push) Successful in 1m36s
Pre-commit hooks / Pre-commit (push) Successful in 1m44s
Tests and linters / Tests with -race (push) Successful in 1m37s
Tests and linters / gopls check (push) Successful in 1m47s
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
95 lines
2.3 KiB
Go
95 lines
2.3 KiB
Go
package semaphore_test
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
semaphores "git.frostfs.info/TrueCloudLab/frostfs-qos/limiting/semaphore"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/sync/errgroup"
|
|
)
|
|
|
|
const maxWorkers = 10_000_000
|
|
|
|
type benchmarkSemaphoreMetrics struct {
|
|
mu sync.Mutex
|
|
|
|
acquireDuration,
|
|
releaseDuration time.Duration
|
|
|
|
acquireCount,
|
|
releaseCount uint64
|
|
}
|
|
|
|
func (c *benchmarkSemaphoreMetrics) reportAcquire(duration time.Duration) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.acquireDuration += duration
|
|
c.acquireCount += 1
|
|
}
|
|
|
|
func (c *benchmarkSemaphoreMetrics) reportRelease(duration time.Duration) {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.releaseDuration += duration
|
|
c.releaseCount += 1
|
|
}
|
|
|
|
func (c *benchmarkSemaphoreMetrics) getResults() (timePerAcquire, timePerRelease, successRate float64) {
|
|
timePerAcquire = float64(c.acquireDuration) / float64(c.acquireCount)
|
|
timePerRelease = float64(c.releaseDuration) / float64(c.releaseCount)
|
|
successRate = float64(c.releaseCount) / float64(c.acquireCount)
|
|
return
|
|
}
|
|
|
|
func BenchmarkSemaphore(b *testing.B) {
|
|
sizes := []int64{1, 10, 100, 1000, 10000}
|
|
lockDurations := []time.Duration{0, time.Microsecond, 10 * time.Microsecond, 100 * time.Microsecond}
|
|
|
|
for _, size := range sizes {
|
|
for _, lockDuration := range lockDurations {
|
|
name := fmt.Sprintf("semaphore_size=%d/lock_duration=%v", size, lockDuration)
|
|
b.Run(name, func(b *testing.B) {
|
|
benchmarkSemaphore(b, semaphores.NewSemaphore(size), lockDuration)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func benchmarkSemaphore(b *testing.B, sem *semaphores.Semaphore, lockDuration time.Duration) {
|
|
var m benchmarkSemaphoreMetrics
|
|
var g errgroup.Group
|
|
g.SetLimit(maxWorkers)
|
|
|
|
for range b.N {
|
|
g.Go(func() error {
|
|
now := time.Now()
|
|
ok := sem.Acquire()
|
|
m.reportAcquire(time.Since(now))
|
|
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
time.Sleep(lockDuration)
|
|
|
|
now = time.Now()
|
|
sem.Release()
|
|
m.reportRelease(time.Since(now))
|
|
|
|
return nil
|
|
})
|
|
}
|
|
require.NoError(b, g.Wait())
|
|
|
|
require.Equal(b, uint64(b.N), m.acquireCount)
|
|
require.LessOrEqual(b, m.releaseCount, m.acquireCount)
|
|
|
|
timePerAcquire, timePerRelease, successRate := m.getResults()
|
|
|
|
b.ReportMetric(timePerAcquire, "acquire-ns/op")
|
|
b.ReportMetric(timePerRelease, "release-ns/op")
|
|
b.ReportMetric(successRate, "success-rate")
|
|
}
|