File: ratelimit_bench_test.go

package info (click to toggle)
golang-uber-ratelimit 0.3.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 228 kB
  • sloc: makefile: 44
file content (91 lines) | stat: -rw-r--r-- 2,115 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ratelimit

import (
	"fmt"
	"runtime"
	"sync"
	"testing"

	"go.uber.org/atomic"
)

func BenchmarkRateLimiter(b *testing.B) {
	count := atomic.NewInt64(0)
	for _, procs := range []int{1, 4, 8, 16} {
		runtime.GOMAXPROCS(procs)
		for name, limiter := range map[string]Limiter{
			"atomic":       newAtomicBased(int64(b.N) * int64(1000000000000)),
			"atomic_int64": newAtomicInt64Based(int64(b.N) * int64(1000000000000)),
			"mutex":        newMutexBased(int64(b.N) * int64(1000000000000)),
		} {
			for ng := 1; ng < 16; ng++ {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 16; ng < 128; ng += 8 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 128; ng < 512; ng += 16 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 512; ng < 1024; ng += 32 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 1024; ng < 2048; ng += 64 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 2048; ng < 4096; ng += 128 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 4096; ng < 16384; ng += 512 {
				runner(b, name, procs, ng, limiter, count)
			}
			for ng := 16384; ng < 65536; ng += 2048 {
				runner(b, name, procs, ng, limiter, count)
			}
		}
	}
	fmt.Printf("\nmark%d\n", count.Load())
}

func runner(b *testing.B, name string, procs int, ng int, limiter Limiter, count *atomic.Int64) bool {
	return b.Run(fmt.Sprintf("type:%s;max_procs:%d;goroutines:%d", name, procs, ng), func(b *testing.B) {
		b.ReportAllocs()

		var wg sync.WaitGroup
		trigger := atomic.NewBool(true)
		n := b.N
		batchSize := n / ng
		if batchSize == 0 {
			batchSize = n
		}
		for n > 0 {
			wg.Add(1)
			batch := min(n, batchSize)
			n -= batch
			go func(quota int) {
				for trigger.Load() {
					runtime.Gosched()
				}
				localCnt := 0
				for i := 0; i < quota; i++ {
					res := limiter.Take()
					localCnt += res.Nanosecond()
				}
				count.Add(int64(localCnt))
				wg.Done()
			}(batch)
		}

		b.StartTimer()
		trigger.Store(false)
		wg.Wait()
		b.StopTimer()
	})
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}