-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfsm_test.go
239 lines (196 loc) · 5.33 KB
/
fsm_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package fsm
import (
"fmt"
"sync"
"testing"
)
// Define test states, events and payload
type testState string
type testEvent string
type testPayload struct {
Value string
}
const (
StateA testState = "A"
StateB testState = "B"
StateC testState = "C"
StateD testState = "D"
)
const (
Event1 testEvent = "Event1"
Event2 testEvent = "Event2"
Event3 testEvent = "Event3"
)
// Simple condition that always returns true
type alwaysTrueCondition struct{}
func (c *alwaysTrueCondition) IsSatisfied(payload testPayload) bool {
return true
}
// Simple action that does nothing
type noopAction struct{}
func (a *noopAction) Execute(from, to testState, event testEvent, payload testPayload) error {
return nil
}
// Create a test state machine
func createTestStateMachine(tb testing.TB) StateMachine[testState, testEvent, testPayload] {
builder := NewStateMachineBuilder[testState, testEvent, testPayload]()
// Define state transitions
builder.ExternalTransition().
From(StateA).
To(StateB).
On(Event1).
When(&alwaysTrueCondition{}).
Perform(&noopAction{})
builder.ExternalTransition().
From(StateB).
To(StateC).
On(Event2).
When(&alwaysTrueCondition{}).
Perform(&noopAction{})
builder.ExternalTransition().
From(StateC).
To(StateA).
On(Event3).
When(&alwaysTrueCondition{}).
Perform(&noopAction{})
// Build the state machine
sm, err := builder.Build("TestStateMachine")
if err != nil {
tb.Fatalf("Failed to build state machine: %v", err)
}
return sm
}
// Benchmark: Single-thread state transition
func BenchmarkSingleThreadTransition(b *testing.B) {
sm := createTestStateMachine(b)
payload := testPayload{Value: "test"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
currentState := StateA
newState, err := sm.FireEvent(currentState, Event1, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
currentState = newState
newState, err = sm.FireEvent(currentState, Event2, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
currentState = newState
newState, err = sm.FireEvent(currentState, Event3, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
}
}
// Benchmark: Multi-thread state transition
func BenchmarkMultiThreadTransition(b *testing.B) {
sm := createTestStateMachine(b)
payload := testPayload{Value: "test"}
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
currentState := StateA
newState, err := sm.FireEvent(currentState, Event1, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
currentState = newState
newState, err = sm.FireEvent(currentState, Event2, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
currentState = newState
newState, err = sm.FireEvent(currentState, Event3, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
}
})
}
// Benchmark: State machine creation
func BenchmarkStateMachineCreation(b *testing.B) {
for i := 0; i < b.N; i++ {
builder := NewStateMachineBuilder[testState, testEvent, testPayload]()
builder.ExternalTransition().
From(StateA).
To(StateB).
On(Event1).
When(&alwaysTrueCondition{}).
Perform(&noopAction{})
machineId := fmt.Sprintf("TestMachine-%d", i)
_, err := builder.Build(machineId)
if err != nil {
b.Fatalf("Failed to build state machine: %v", err)
}
// Clean up to avoid memory leaks
RemoveStateMachine(machineId)
}
}
// Benchmark: Large state machine with many states and transitions
func BenchmarkLargeStateMachine(b *testing.B) {
// Create a state machine with 100 states and transitions
builder := NewStateMachineBuilder[testState, testEvent, testPayload]()
const numStates = 100
states := make([]testState, numStates)
for i := 0; i < numStates; i++ {
states[i] = testState(fmt.Sprintf("State%d", i))
}
// Create chain transitions: State0 -> State1 -> ... -> State99 -> State0
for i := 0; i < numStates; i++ {
from := states[i]
to := states[(i+1)%numStates]
builder.ExternalTransition().
From(from).
To(to).
On(Event1).
When(&alwaysTrueCondition{}).
Perform(&noopAction{})
}
sm, err := builder.Build("LargeStateMachine")
if err != nil {
b.Fatalf("Failed to build large state machine: %v", err)
}
payload := testPayload{Value: "test"}
b.ResetTimer()
for i := 0; i < b.N; i++ {
currentState := states[i%numStates]
newState, err := sm.FireEvent(currentState, Event1, payload)
if err != nil {
b.Fatalf("Failed to fire event: %v", err)
}
_ = newState
}
}
// Benchmark: Concurrent state machine access
func BenchmarkConcurrentStateMachineAccess(b *testing.B) {
sm := createTestStateMachine(b)
payload := testPayload{Value: "test"}
var wg sync.WaitGroup
numGoroutines := 100
b.ResetTimer()
for i := 0; i < b.N; i++ {
wg.Add(numGoroutines)
for j := 0; j < numGoroutines; j++ {
go func(j int) {
defer wg.Done()
// Each goroutine executes a different state transition
currentState := StateA
event := Event1
if j%3 == 1 {
currentState = StateB
event = Event2
} else if j%3 == 2 {
currentState = StateC
event = Event3
}
_, err := sm.FireEvent(currentState, event, payload)
if err != nil {
// Ignore errors, as some are expected in concurrent testing
// For example, when state doesn't match the event
}
}(j)
}
wg.Wait()
}
}