-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_benchmark_test.go
79 lines (70 loc) · 1.52 KB
/
command_benchmark_test.go
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
package circuit
import (
"context"
"testing"
"github.com/bunnier/circuit/breaker"
)
// run 原始的功能函数。
var run = func() string {
return "ok"
}
// wrapRun 是包装后用于 command 用的功能函数。
var wrapRun = func(ctx context.Context, param interface{}) (interface{}, error) {
return run(), nil
}
func BenchmarkDirectly(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
run()
}
b.StopTimer()
}
func BenchmarkParallelDirectly(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
run()
}
})
}
func BenchmarkCutCommand(b *testing.B) {
command := NewCommand("test", wrapRun)
defer command.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
command.Execute(nil)
}
b.StopTimer()
}
func BenchmarkParallelCutCommand(b *testing.B) {
command := NewCommand("test", wrapRun)
defer command.Close()
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
command.Execute(nil)
}
})
b.StopTimer()
}
func BenchmarkSreCommand(b *testing.B) {
sreBreaker := breaker.NewSreBreaker("test")
command := NewCommand("test", wrapRun, WithCommandBreaker(sreBreaker))
defer command.Close()
b.ResetTimer()
for i := 0; i < b.N; i++ {
command.Execute(nil)
}
b.StopTimer()
}
func BenchmarkParallelSreCommand(b *testing.B) {
sreBreaker := breaker.NewSreBreaker("test")
command := NewCommand("test", wrapRun, WithCommandBreaker(sreBreaker))
defer command.Close()
b.ResetTimer()
b.RunParallel(func(p *testing.PB) {
for p.Next() {
command.Execute(nil)
}
})
b.StopTimer()
}