-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfsm_bench_test.go
138 lines (124 loc) · 3.41 KB
/
fsm_bench_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package fsm_test
import (
"testing"
"github.com/zerjioang/go-fsm"
)
/*
* Benchmark functions start with Benchmark not Test.
* Benchmark functions are run several times by the testing package.
The value of b.N will increase each time until the benchmark runner
is satisfied with the stability of the benchmark. This has some important
ramifications which we’ll investigate later in this article.
* Each benchmark is run for a minimum of 1 second by default.
If the second has not elapsed when the Benchmark function returns,
the value of b.N is increased in the sequence 1, 2, 5, 10, 20, 50, …
and the function run again.
* the for loop is crucial to the operation of the benchmark driver
it must be: for n := 0; n < b.N; n++
* Add b.ReportAllocs() at the beginning of each benchmark to know allocations
* Add b.SetBytes(1) InitConstants()to measure byte transfers
Optimization info: https://stackimpact.com/blog/practical-golang-benchmarks/
*/
func BenchmarkFsm(b *testing.B) {
b.Run("instantiation", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
for n := 0; n < b.N; n++ {
_ = fsm.New()
}
})
b.Run("instantiation-ptr", func(b *testing.B) {
b.ReportAllocs()
b.SetBytes(1)
for n := 0; n < b.N; n++ {
_ = fsm.NewPtr()
}
})
b.Run("add-state", func(b *testing.B) {
m := fsm.NewPtr()
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.AddState("start", fsm.NoStateEvents)
}
})
b.Run("add-state-no-name", func(b *testing.B) {
m := fsm.NewPtr()
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.AddState("", fsm.NoStateEvents)
}
})
b.Run("add-state-no-event", func(b *testing.B) {
m := fsm.NewPtr()
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.AddState("start", fsm.NoStateEvents)
}
})
b.Run("change-state-empty", func(b *testing.B) {
m := fsm.NewPtr()
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.ChangeStateTo("")
}
})
b.Run("change-state", func(b *testing.B) {
m := fsm.NewPtr()
m.AddState("start", fsm.NoStateEvents)
m.AddState("finish", fsm.NoStateEvents)
m.AddState("a", fsm.NoStateEvents)
m.AddState("b", fsm.NoStateEvents)
m.AddState("c", fsm.NoStateEvents)
m.AddTransaction("toA", "start", "a")
m.AddTransaction("toFinish", "a", "finish")
m.SetInitialState("start")
m.SetFinalState("finish")
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.ChangeStateTo("a")
}
})
b.Run("has-state-false", func(b *testing.B) {
m := fsm.NewPtr()
b.ReportAllocs()
b.SetBytes(1)
for n := 0; n < b.N; n++ {
m.HasState("start")
}
})
b.Run("has-state-true", func(b *testing.B) {
m := fsm.NewPtr()
m.AddState("start", fsm.NoStateEvents)
m.AddState("finish", fsm.NoStateEvents)
m.AddState("a", fsm.NoStateEvents)
m.AddState("b", fsm.NoStateEvents)
m.AddState("c", fsm.NoStateEvents)
b.ReportAllocs()
b.SetBytes(1)
for n := 0; n < b.N; n++ {
m.HasState("start")
}
})
b.Run("to-dot", func(b *testing.B) {
m := fsm.NewPtr()
m.AddState("start", fsm.NoStateEvents)
m.AddState("finish", fsm.NoStateEvents)
m.AddState("a", fsm.NoStateEvents)
m.AddState("b", fsm.NoStateEvents)
m.AddState("c", fsm.NoStateEvents)
m.AddTransaction("toA", "start", "a")
m.AddTransaction("toFinish", "a", "finish")
m.SetInitialState("start")
m.SetFinalState("finish")
b.SetBytes(1)
b.ReportAllocs()
for n := 0; n < b.N; n++ {
m.DotGraph()
}
})
}