-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatechart_breaking_test.go
More file actions
411 lines (334 loc) · 9.49 KB
/
statechart_breaking_test.go
File metadata and controls
411 lines (334 loc) · 9.49 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package statechartx
import (
"context"
"runtime"
"testing"
"time"
)
// TestMaxStates finds the maximum number of states before failure
func TestMaxStates(t *testing.T) {
if testing.Short() {
t.Skip("Skipping breaking point test in short mode")
}
t.Log("Starting TestMaxStates - finding breaking point...")
// Start with 100K states and double until failure
startSize := 100000
maxAttempts := 10
for attempt := 0; attempt < maxAttempts; attempt++ {
numStates := startSize * (1 << attempt) // 100K, 200K, 400K, 800K, ...
t.Logf("Attempting %d states...", numStates)
// Record memory before
var m1 runtime.MemStats
runtime.ReadMemStats(&m1)
root := &State{
ID: 1,
Children: make(map[StateID]*State),
}
start := time.Now()
success := true
// Create states in batches to avoid timeout
const batchSize = 10000
stateID := StateID(2)
for i := 0; i < numStates; i += batchSize {
for j := 0; j < batchSize && i+j < numStates; j++ {
state := &State{
ID: stateID,
Parent: root,
}
root.Children[stateID] = state
stateID++
}
// Check if we're running out of memory
if i%100000 == 0 && i > 0 {
var m runtime.MemStats
runtime.ReadMemStats(&m)
if m.Alloc > 8*1024*1024*1024 { // 8GB limit
t.Logf("Memory limit reached at %d states", i)
success = false
break
}
}
}
if !success {
t.Logf("Failed at %d states due to memory constraints", numStates)
break
}
duration := time.Since(start)
// Try to start it
root.Initial = StateID(2) // First child
machine, err := NewMachine(root)
if err != nil {
t.Logf("Failed to create machine with %d states: %v", numStates, err)
break
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
err = rt.Start(ctx)
if err != nil {
t.Logf("Failed to start with %d states: %v", numStates, err)
rt.Stop()
break
}
// Measure memory while still running
var m2 runtime.MemStats
runtime.ReadMemStats(&m2)
// Handle case where GC reduced memory below initial allocation
var memoryUsed float64
if m2.Alloc > m1.Alloc {
memoryUsed = float64(m2.Alloc-m1.Alloc) / (1024 * 1024) // MB
} else {
memoryUsed = -float64(m1.Alloc-m2.Alloc) / (1024 * 1024) // MB (negative indicates GC freed memory)
}
rt.Stop()
t.Logf("✓ Successfully created and started %d states in %v (%.2f MB)", numStates, duration, memoryUsed)
// If this took too long, stop
if duration > 30*time.Second {
t.Logf("Stopping test - creation time exceeds 30s")
break
}
}
}
// TestMaxEventThroughput finds maximum sustainable event throughput
func TestMaxEventThroughput(t *testing.T) {
if testing.Short() {
t.Skip("Skipping breaking point test in short mode")
}
t.Log("Starting TestMaxEventThroughput...")
const (
STATE1 StateID = 1
STATE2 StateID = 2
EVENT_TOGGLE EventID = 1
)
state1 := &State{ID: STATE1, Transitions: []*Transition{}}
state2 := &State{ID: STATE2, Transitions: []*Transition{}}
state1.Transitions = append(state1.Transitions, &Transition{Event: EVENT_TOGGLE, Source: state1, Target: STATE2})
state2.Transitions = append(state2.Transitions, &Transition{Event: EVENT_TOGGLE, Source: state2, Target: STATE1})
root := &State{
ID: 100,
Initial: STATE1,
Children: map[StateID]*State{STATE1: state1, STATE2: state2},
}
state1.Parent = root
state2.Parent = root
machine, err := NewMachine(root)
if err != nil {
t.Fatalf("Failed to create machine: %v", err)
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Fatalf("Failed to start: %v", err)
}
defer rt.Stop()
// Test increasing event loads
eventCounts := []int{10000, 100000, 1000000, 5000000, 10000000}
for _, numEvents := range eventCounts {
t.Logf("Testing %d events...", numEvents)
start := time.Now()
for i := 0; i < numEvents; i++ {
rt.SendEvent(ctx, Event{ID: EVENT_TOGGLE})
}
duration := time.Since(start)
throughput := float64(numEvents) / duration.Seconds()
avgTime := duration / time.Duration(numEvents)
t.Logf(" Throughput: %.0f events/sec, Avg time: %v", throughput, avgTime)
// If throughput drops significantly, we've found the limit
if avgTime > 10*time.Microsecond {
t.Logf("Throughput degradation detected at %d events", numEvents)
break
}
}
}
// TestMaxParallelRegions finds maximum number of parallel regions
func TestMaxParallelRegions(t *testing.T) {
if testing.Short() {
t.Skip("Skipping breaking point test in short mode")
}
t.Log("Starting TestMaxParallelRegions...")
// Test increasing numbers of parallel regions
regionCounts := []int{100, 500, 1000, 2000, 5000, 10000}
for _, numRegions := range regionCounts {
t.Logf("Testing %d parallel regions...", numRegions)
root := &State{
ID: 1,
IsParallel: true,
Children: make(map[StateID]*State),
}
// Create parallel regions
createStart := time.Now()
stateID := StateID(2)
for i := 0; i < numRegions; i++ {
region := &State{
ID: stateID,
Parent: root,
Children: make(map[StateID]*State),
}
stateID++
state := &State{
ID: stateID,
Parent: region,
}
stateID++
region.Children[state.ID] = state
region.Initial = state.ID
root.Children[region.ID] = region
}
createTime := time.Since(createStart)
// Try to start
startTime := time.Now()
machine, err := NewMachine(root)
if err != nil {
t.Logf("Failed to create machine with %d regions: %v", numRegions, err)
break
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
err = rt.Start(ctx)
if err != nil {
t.Logf("Failed to start with %d regions: %v", numRegions, err)
rt.Stop()
break
}
startDuration := time.Since(startTime)
t.Logf(" Created in %v, Started in %v", createTime, startDuration)
// Test event processing
eventStart := time.Now()
rt.SendEvent(ctx, Event{ID: 1})
eventTime := time.Since(eventStart)
t.Logf(" Event processing: %v", eventTime)
rt.Stop()
// If startup takes too long, we've found the practical limit
if startDuration > 10*time.Second {
t.Logf("Startup time exceeds 10s at %d regions", numRegions)
break
}
}
}
// TestMaxHierarchyDepth finds maximum hierarchy depth
func TestMaxHierarchyDepth(t *testing.T) {
if testing.Short() {
t.Skip("Skipping breaking point test in short mode")
}
t.Log("Starting TestMaxHierarchyDepth...")
// Test increasing depths
depths := []int{100, 500, 1000, 2000, 5000, 10000}
for _, depth := range depths {
t.Logf("Testing depth %d...", depth)
root := &State{
ID: 1,
Children: make(map[StateID]*State),
}
// Create deep hierarchy
createStart := time.Now()
current := root
stateID := StateID(2)
for i := 0; i < depth; i++ {
child := &State{
ID: stateID,
Parent: current,
Children: make(map[StateID]*State),
}
current.Children[stateID] = child
current.Initial = stateID
current = child
stateID++
}
createTime := time.Since(createStart)
// Try to start (enters all nested states)
startTime := time.Now()
machine, err := NewMachine(root)
if err != nil {
t.Logf("Failed to create machine with depth %d: %v", depth, err)
break
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
err = rt.Start(ctx)
if err != nil {
t.Logf("Failed to start with depth %d: %v", depth, err)
rt.Stop()
break
}
startDuration := time.Since(startTime)
t.Logf(" Created in %v, Started in %v", createTime, startDuration)
// Test LCA computation
lcaStart := time.Now()
lca := computeLCA(current, root)
lcaTime := time.Since(lcaStart)
if lca == nil {
t.Logf("LCA computation failed at depth %d", depth)
rt.Stop()
break
}
t.Logf(" LCA computation: %v", lcaTime)
rt.Stop()
// If operations take too long, we've found the practical limit
if startDuration > 5*time.Second || lcaTime > 1*time.Second {
t.Logf("Performance degradation at depth %d", depth)
break
}
}
}
// TestMemoryPressure tests behavior under memory pressure
func TestMemoryPressure(t *testing.T) {
if testing.Short() {
t.Skip("Skipping breaking point test in short mode")
}
t.Log("Starting TestMemoryPressure...")
// Create many statecharts to apply memory pressure
var runtimes []*Runtime
var m runtime.MemStats
const (
STATE1 StateID = 1
STATE2 StateID = 2
ROOT_ID StateID = 1000
)
for i := 0; ; i++ {
root := &State{
ID: ROOT_ID,
Initial: STATE1,
Children: make(map[StateID]*State),
}
// Create a moderately complex statechart
for j := 0; j < 100; j++ {
state := &State{
ID: StateID(j + 1),
Parent: root,
}
root.Children[state.ID] = state
}
machine, err := NewMachine(root)
if err != nil {
t.Logf("Failed to create machine %d: %v", i, err)
break
}
rt := NewRuntime(machine, nil)
ctx := context.Background()
if err := rt.Start(ctx); err != nil {
t.Logf("Failed to start machine %d: %v", i, err)
break
}
runtimes = append(runtimes, rt)
// Check memory every 100 machines
if i%100 == 0 {
runtime.ReadMemStats(&m)
memoryGB := float64(m.Alloc) / (1024 * 1024 * 1024)
t.Logf("Created %d machines, Memory: %.2f GB", i, memoryGB)
// Stop at 4GB to be safe
if m.Alloc > 4*1024*1024*1024 {
t.Logf("Stopping at 4GB memory usage with %d machines", i)
break
}
}
// Stop after 10,000 machines regardless
if i >= 10000 {
t.Logf("Reached 10,000 machines limit")
break
}
}
t.Logf("Total machines created: %d", len(runtimes))
// Cleanup
for _, rt := range runtimes {
rt.Stop()
}
}