[#4] limiting/semaphore: Add benchmark for semaphore
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
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>
This commit is contained in:
parent
59fb93fb23
commit
356851eed3
2 changed files with 121 additions and 0 deletions
26
limiting/semaphore/semaphore_bench.result
Normal file
26
limiting/semaphore/semaphore_bench.result
Normal file
|
@ -0,0 +1,26 @@
|
|||
goos: linux
|
||||
goarch: amd64
|
||||
pkg: git.frostfs.info/TrueCloudLab/frostfs-qos/limiting/semaphore
|
||||
cpu: 12th Gen Intel(R) Core(TM) i5-1235U
|
||||
BenchmarkSemaphore/semaphore_size=1/lock_duration=0s-12 6113605 1964 ns/op 383.1 acquire-ns/op 203.5 release-ns/op 0.6892 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1/lock_duration=1µs-12 5826655 2067 ns/op 382.0 acquire-ns/op 307.0 release-ns/op 0.1460 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1/lock_duration=10µs-12 5977272 2033 ns/op 370.4 acquire-ns/op 321.4 release-ns/op 0.05408 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1/lock_duration=100µs-12 5862900 2030 ns/op 365.6 acquire-ns/op 343.1 release-ns/op 0.01242 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10/lock_duration=0s-12 5637050 2173 ns/op 365.2 acquire-ns/op 261.7 release-ns/op 0.9765 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10/lock_duration=1µs-12 5470316 2225 ns/op 390.4 acquire-ns/op 357.2 release-ns/op 0.9249 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10/lock_duration=10µs-12 5584527 2134 ns/op 395.2 acquire-ns/op 339.0 release-ns/op 0.5409 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10/lock_duration=100µs-12 5841032 2036 ns/op 369.4 acquire-ns/op 330.7 release-ns/op 0.1182 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=100/lock_duration=0s-12 5600013 2159 ns/op 369.9 acquire-ns/op 271.1 release-ns/op 0.9976 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=100/lock_duration=1µs-12 5323606 2280 ns/op 394.0 acquire-ns/op 368.9 release-ns/op 0.9697 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=100/lock_duration=10µs-12 5133394 2353 ns/op 405.8 acquire-ns/op 374.5 release-ns/op 0.9498 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=100/lock_duration=100µs-12 5238136 2303 ns/op 387.2 acquire-ns/op 362.2 release-ns/op 0.8749 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1000/lock_duration=0s-12 5408720 2180 ns/op 367.6 acquire-ns/op 271.5 release-ns/op 0.9992 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1000/lock_duration=1µs-12 5114854 2366 ns/op 407.9 acquire-ns/op 376.4 release-ns/op 0.9966 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1000/lock_duration=10µs-12 4659454 2438 ns/op 412.2 acquire-ns/op 385.9 release-ns/op 0.9800 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=1000/lock_duration=100µs-12 4837894 2482 ns/op 401.7 acquire-ns/op 380.9 release-ns/op 0.9725 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10000/lock_duration=0s-12 5403058 2188 ns/op 367.5 acquire-ns/op 273.1 release-ns/op 1.000 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10000/lock_duration=1µs-12 5086929 2306 ns/op 390.6 acquire-ns/op 376.3 release-ns/op 1.000 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10000/lock_duration=10µs-12 5059968 2378 ns/op 410.2 acquire-ns/op 384.5 release-ns/op 1.000 success-rate
|
||||
BenchmarkSemaphore/semaphore_size=10000/lock_duration=100µs-12 4909206 2420 ns/op 408.4 acquire-ns/op 383.4 release-ns/op 1.000 success-rate
|
||||
PASS
|
||||
ok git.frostfs.info/TrueCloudLab/frostfs-qos/limiting/semaphore 284.895s
|
95
limiting/semaphore/semaphore_bench_test.go
Normal file
95
limiting/semaphore/semaphore_bench_test.go
Normal file
|
@ -0,0 +1,95 @@
|
|||
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")
|
||||
}
|
Loading…
Add table
Reference in a new issue