-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathtest_profiling.py
More file actions
293 lines (213 loc) · 7.99 KB
/
Copy pathtest_profiling.py
File metadata and controls
293 lines (213 loc) · 7.99 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import weakref
import pytest
from statemachine import HistoryState
from statemachine import State
from statemachine import StateChart
# ---------------------------------------------------------------------------
# Machines under test
# ---------------------------------------------------------------------------
# 1. Flat machine with model, guards, and listener callbacks (v1-style)
class OrderControl(StateChart):
allow_event_without_transition = False
catch_errors_as_events = False
waiting_for_payment = State(initial=True)
processing = State()
shipping = State()
completed = State(final=True)
add_to_order = waiting_for_payment.to(waiting_for_payment)
receive_payment = waiting_for_payment.to(
processing, cond="payments_enough"
) | waiting_for_payment.to(waiting_for_payment, unless="payments_enough")
process_order = processing.to(shipping, cond="payment_received")
ship_order = shipping.to(completed)
class Order:
def __init__(self):
self.order_total = 0
self.payments = []
self.payment_received = False
self.state_machine = OrderControl(model=weakref.proxy(self))
def payments_enough(self, amount):
return sum(self.payments) + amount >= self.order_total
def before_add_to_order(self, amount):
self.order_total += amount
return self.order_total
def on_receive_payment(self, amount):
self.payments.append(amount)
return self.payments
def after_receive_payment(self):
self.payment_received = True
# 2. Compound (nested) states
class CompoundSC(StateChart):
class active(State.Compound, name="Active"):
idle = State(initial=True)
working = State()
begin = idle.to(working)
off = State(initial=True)
done = State(final=True)
turn_on = off.to(active)
turn_off = active.to(done)
# 3. Parallel regions
class ParallelSC(StateChart):
class both(State.Parallel, name="Both"):
class left(State.Compound, name="Left"):
l1 = State(initial=True)
l2 = State()
go_l = l1.to(l2)
back_l = l2.to(l1)
class right(State.Compound, name="Right"):
r1 = State(initial=True)
r2 = State()
go_r = r1.to(r2)
back_r = r2.to(r1)
start = State(initial=True)
enter = start.to(both)
# 4. Guards with boolean expressions
class GuardedSC(StateChart):
s1 = State(initial=True)
s2 = State()
s3 = State(final=True)
def check_a(self):
return True
def check_b(self):
return False
go = s1.to(s2, cond="check_a") | s1.to(s3, cond="check_b")
back = s2.to(s1)
# 5. History states (shallow)
class HistoryShallowSC(StateChart):
class process(State.Compound, name="Process"):
step1 = State(initial=True)
step2 = State()
advance = step1.to(step2)
h = HistoryState()
paused = State(initial=True)
pause = process.to(paused)
resume = paused.to(process.h)
begin = paused.to(process)
# 6. Deep history with nested compound states
class DeepHistorySC(StateChart):
class outer(State.Compound, name="Outer"):
class inner(State.Compound, name="Inner"):
a = State(initial=True)
b = State()
go = a.to(b)
back = b.to(a)
start = State(initial=True)
enter_inner = start.to(inner)
h = HistoryState(type="deep")
away = State(initial=True)
dive = away.to(outer)
leave = outer.to(away)
restore = away.to(outer.h)
# 7. Many-transition stress machine (wide, not deep)
class ManyTransitionsSC(StateChart):
s1 = State(initial=True)
s2 = State()
s3 = State()
s4 = State()
s5 = State()
go_12 = s1.to(s2)
go_23 = s2.to(s3)
go_34 = s3.to(s4)
go_45 = s4.to(s5)
go_51 = s5.to(s1)
reset = s2.to(s1) | s3.to(s1) | s4.to(s1) | s5.to(s1)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def create_order():
order = Order()
assert order.state_machine.waiting_for_payment.is_active
def add_to_order(sm, amount):
sm.add_to_order(amount)
# ---------------------------------------------------------------------------
# Benchmark: instance creation
# ---------------------------------------------------------------------------
@pytest.mark.slow()
class TestSetupPerformance:
"""Benchmark the cost of creating and activating state machine instances."""
def test_flat_machine(self, benchmark):
benchmark.pedantic(create_order, rounds=10, iterations=1000)
def test_compound_machine(self, benchmark):
benchmark.pedantic(lambda: CompoundSC(), rounds=10, iterations=1000)
def test_parallel_machine(self, benchmark):
benchmark.pedantic(lambda: ParallelSC(), rounds=10, iterations=1000)
def test_guarded_machine(self, benchmark):
benchmark.pedantic(lambda: GuardedSC(), rounds=10, iterations=1000)
def test_history_machine(self, benchmark):
benchmark.pedantic(lambda: HistoryShallowSC(), rounds=10, iterations=1000)
def test_deep_history_machine(self, benchmark):
benchmark.pedantic(lambda: DeepHistorySC(), rounds=10, iterations=1000)
# ---------------------------------------------------------------------------
# Benchmark: event throughput
# ---------------------------------------------------------------------------
@pytest.mark.slow()
class TestEventPerformance:
"""Benchmark event processing (self-transitions and state changes)."""
def test_flat_self_transition(self, benchmark):
"""Self-transition on a flat machine with model/listener."""
order = Order()
sm = order.state_machine
benchmark.pedantic(add_to_order, args=(sm, 1), rounds=10, iterations=1000)
def test_compound_enter_exit(self, benchmark):
"""Enter and exit a compound state repeatedly."""
def cycle():
sm = CompoundSC()
sm.turn_on()
sm.begin()
sm.turn_off()
benchmark.pedantic(cycle, rounds=10, iterations=500)
def test_parallel_region_events(self, benchmark):
"""Send events within parallel regions."""
sm = ParallelSC()
sm.enter()
def cycle():
sm.go_l()
sm.go_r()
sm.back_l()
sm.back_r()
benchmark.pedantic(cycle, rounds=10, iterations=500)
def test_guarded_transitions(self, benchmark):
"""Guard evaluation + transition selection."""
sm = GuardedSC()
def cycle():
sm.go()
sm.back()
benchmark.pedantic(cycle, rounds=10, iterations=1000)
def test_history_pause_resume(self, benchmark):
"""Shallow history: pause and resume compound state."""
sm = HistoryShallowSC()
sm.begin()
sm.advance()
def cycle():
sm.pause()
sm.resume()
benchmark.pedantic(cycle, rounds=10, iterations=500)
def test_deep_history_cycle(self, benchmark):
"""Deep history: leave and restore nested compound state."""
sm = DeepHistorySC()
sm.dive()
sm.enter_inner()
sm.go()
def cycle():
sm.leave()
sm.restore()
benchmark.pedantic(cycle, rounds=10, iterations=500)
def test_many_transitions_full_cycle(self, benchmark):
"""Traverse a 5-state ring (s1→s2→s3→s4→s5→s1)."""
sm = ManyTransitionsSC()
def cycle():
sm.go_12()
sm.go_23()
sm.go_34()
sm.go_45()
sm.go_51()
benchmark.pedantic(cycle, rounds=10, iterations=500)
def test_many_transitions_reset(self, benchmark):
"""Composite event (|) selecting among multiple source states."""
sm = ManyTransitionsSC()
def cycle():
sm.go_12()
sm.go_23()
sm.go_34()
sm.reset()
benchmark.pedantic(cycle, rounds=10, iterations=500)