-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_test.go
More file actions
4592 lines (4325 loc) · 139 KB
/
script_test.go
File metadata and controls
4592 lines (4325 loc) · 139 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
const testTimeoutUnit = time.Millisecond
const testTimeout = 5 * time.Second
func scriptFixture(name string) string {
return filepath.Join("testdata", name)
}
// ---------------------------------------------------------------------------
// Increment 1 — Script execution skeleton
// ---------------------------------------------------------------------------
func TestScriptInlinePrint(t *testing.T) {
out := captureOutput(func() {
code := runScript(`print("hello")`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "hello" {
t.Fatalf("expected 'hello', got %q", out)
}
}
func TestScriptInlineFail(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(`fail("boom")`, "", nil, testTimeout)
if code != 1 {
t.Fatalf("expected exit 1, got %d", code)
}
})
if !strings.Contains(stderr, "boom") {
t.Fatalf("expected stderr to contain 'boom', got %q", stderr)
}
}
func TestScriptInlineWarn(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(`warn("note")`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if !strings.Contains(stderr, "note") {
t.Fatalf("expected stderr to contain 'note', got %q", stderr)
}
}
func TestScriptArgs(t *testing.T) {
out := captureOutput(func() {
code := runScript(`print(ARGS)`, "", []string{"a", "b", "c"}, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if !strings.Contains(out, `"a"`) || !strings.Contains(out, `"b"`) || !strings.Contains(out, `"c"`) {
t.Fatalf("expected ARGS to contain a, b, c, got %q", out)
}
}
func TestScriptArgsEmpty(t *testing.T) {
out := captureOutput(func() {
code := runScript(`print(len(ARGS))`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "0" {
t.Fatalf("expected empty ARGS, got %q", out)
}
}
func TestScriptSyntaxError(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(`def(`, "", nil, testTimeout)
if code != 2 {
t.Fatalf("expected exit 2, got %d", code)
}
})
if !strings.Contains(stderr, "error") {
t.Fatalf("expected error in stderr, got %q", stderr)
}
}
func TestScriptFile(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "test.star")
os.WriteFile(path, []byte(`print("from file")`), 0644)
out := captureOutput(func() {
code := runScript("", path, nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if !strings.Contains(out, "from file") {
t.Fatalf("expected 'from file', got %q", out)
}
}
func TestScriptTimeout(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(`
x = 0
while True:
x += 1
`, "", nil, 50*testTimeoutUnit)
if code == 0 {
t.Fatalf("expected non-zero exit code")
}
})
if !strings.Contains(stderr, "timed out") {
t.Fatalf("expected timeout error, got %q", stderr)
}
}
func TestScriptTopLevelControl(t *testing.T) {
out := captureOutput(func() {
code := runScript(`
if True:
print("ok")
`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "ok" {
t.Fatalf("expected 'ok', got %q", out)
}
}
func TestScriptSets(t *testing.T) {
out := captureOutput(func() {
code := runScript(`s = set([1, 2, 3]); print(len(s))`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "3" {
t.Fatalf("expected '3', got %q", out)
}
}
func TestScriptWhile(t *testing.T) {
out := captureOutput(func() {
code := runScript(`
x = 0
while x < 5:
x += 1
print(x)
`, "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "5" {
t.Fatalf("expected '5', got %q", out)
}
}
// ---------------------------------------------------------------------------
// Increment 2 — open() + Profile + Stack + Frame
// ---------------------------------------------------------------------------
func TestOpenJFR(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
print(p.samples)
print(p.event)
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 2 {
t.Fatalf("expected 2 lines, got %q", out)
}
samples := lines[0]
if samples == "0" {
t.Fatalf("expected samples > 0, got %s", samples)
}
if strings.TrimSpace(lines[1]) != "cpu" {
t.Fatalf("expected event 'cpu', got %q", lines[1])
}
}
func TestOpenJFRWall(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q, event="wall")
print(p.event)
print(p.samples)
`, scriptFixture("wall.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 2 {
t.Fatalf("expected 2 lines, got %q", out)
}
if strings.TrimSpace(lines[0]) != "wall" {
t.Fatalf("expected event 'wall', got %q", lines[0])
}
if strings.TrimSpace(lines[1]) == "0" {
t.Fatalf("expected wall samples > 0")
}
}
func TestOpenJFRMultiEvents(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
print(len(p.events))
for e in p.events:
print(e)
`, scriptFixture("multi.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 2 {
t.Fatalf("expected multiple events, got %q", out)
}
}
func TestOpenCollapsed(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
print(p.samples)
print(p.duration)
`, scriptFixture("perf.collapsed")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 2 {
t.Fatalf("expected 2 lines, got %q", out)
}
if strings.TrimSpace(lines[0]) == "0" {
t.Fatalf("expected samples > 0")
}
if strings.TrimSpace(lines[1]) != "0.0" && strings.TrimSpace(lines[1]) != "0" {
t.Fatalf("expected duration 0 for collapsed, got %q", lines[1])
}
}
func TestOpenStartEnd(t *testing.T) {
// Open the full profile first.
var fullSamples string
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`p = open(%q); print(p.samples)`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
fullSamples = strings.TrimSpace(out)
// Open with a time window.
out = captureOutput(func() {
code := runScript(fmt.Sprintf(`p = open(%q, start="1s", end="5s"); print(p.samples)`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
windowSamples := strings.TrimSpace(out)
if windowSamples == fullSamples {
t.Logf("warning: time window did not reduce samples (recording may be too short)")
}
}
func TestOpenStartEndTimelineScope(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q, start="1s", end="5s")
print(p.start)
print(p.end)
buckets = p.timeline(resolution="1s")
print(len(buckets))
print(buckets[0].label)
print(buckets[len(buckets)-1].label)
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 5 {
t.Fatalf("expected 5 lines, got %d: %q", len(lines), out)
}
if strings.TrimSpace(lines[0]) != "1.0" {
t.Fatalf("expected profile.start 1.0, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "5.0" {
t.Fatalf("expected profile.end 5.0, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "4" {
t.Fatalf("expected 4 buckets, got %q", lines[2])
}
if strings.TrimSpace(lines[3]) != "1.0s-2.0s" {
t.Fatalf("expected first bucket label 1.0s-2.0s, got %q", lines[3])
}
if strings.TrimSpace(lines[4]) != "4.0s-5.0s" {
t.Fatalf("expected last bucket label 4.0s-5.0s, got %q", lines[4])
}
}
func TestOpenStartOnlyTimelineScope(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q, start="2s")
print(p.start)
buckets = p.timeline(resolution="1s")
print(len(buckets))
print(buckets[0].label)
print(buckets[len(buckets)-1].label)
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 4 {
t.Fatalf("expected 4 lines, got %d: %q", len(lines), out)
}
if strings.TrimSpace(lines[0]) != "2.0" {
t.Fatalf("expected profile.start 2.0, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "4" {
t.Fatalf("expected 4 buckets, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "2.0s-3.0s" {
t.Fatalf("expected first bucket label 2.0s-3.0s, got %q", lines[2])
}
if strings.TrimSpace(lines[3]) != "5.0s-6.0s" {
t.Fatalf("expected last bucket label 5.0s-6.0s, got %q", lines[3])
}
}
func TestOpenEndOnlyTimelineScope(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q, end="3s")
print(p.end)
buckets = p.timeline(resolution="1s")
print(len(buckets))
print(buckets[0].label)
print(buckets[len(buckets)-1].label)
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) != 4 {
t.Fatalf("expected 4 lines, got %d: %q", len(lines), out)
}
if strings.TrimSpace(lines[0]) != "3.0" {
t.Fatalf("expected profile.end 3.0, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "3" {
t.Fatalf("expected 3 buckets, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "0.0s-1.0s" {
t.Fatalf("expected first bucket label 0.0s-1.0s, got %q", lines[2])
}
if strings.TrimSpace(lines[3]) != "2.0s-3.0s" {
t.Fatalf("expected last bucket label 2.0s-3.0s, got %q", lines[3])
}
}
func TestOpenStartAfterEnd(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(fmt.Sprintf(`p = open(%q, start="5s", end="1s")`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code == 0 {
t.Fatalf("expected error for start > end")
}
})
if !strings.Contains(stderr, "end must be >= start") {
t.Fatalf("expected 'end must be >= start' in stderr, got %q", stderr)
}
}
func TestOpenNotFound(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(`p = open("nonexistent.jfr")`, "", nil, testTimeout)
if code == 0 {
t.Fatalf("expected error for nonexistent file")
}
})
if !strings.Contains(stderr, "error") {
t.Fatalf("expected error in stderr, got %q", stderr)
}
}
func TestOpenBadEvent(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(fmt.Sprintf(`p = open(%q, event="bogus")`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code == 0 {
t.Fatalf("expected error for bad event type")
}
})
if !strings.Contains(stderr, "bogus") || !strings.Contains(stderr, "error") {
t.Fatalf("expected error mentioning bogus, got %q", stderr)
}
}
func TestProfileFields(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
print(type(p.stacks))
print(type(p.samples))
print(type(p.duration))
print(type(p.event))
print(type(p.events))
print(type(p.path))
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
expected := []string{"list", "int", "float", "string", "list", "string"}
for i, exp := range expected {
if i >= len(lines) {
t.Fatalf("expected %d lines, got %d", len(expected), len(lines))
}
if strings.TrimSpace(lines[i]) != exp {
t.Fatalf("field %d: expected type %q, got %q", i, exp, lines[i])
}
}
}
func TestStackFields(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
s = p.stacks[0]
print(type(s.frames))
print(type(s.thread))
print(type(s.samples))
print(s.depth)
print(type(s.leaf))
print(type(s.root))
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 6 {
t.Fatalf("expected 6 lines, got %q", out)
}
if strings.TrimSpace(lines[0]) != "list" {
t.Fatalf("frames should be list, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "string" {
t.Fatalf("thread should be string, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "int" {
t.Fatalf("samples should be int, got %q", lines[2])
}
if strings.TrimSpace(lines[4]) != "Frame" {
t.Fatalf("leaf should be Frame, got %q", lines[4])
}
}
func TestFrameFieldsJava(t *testing.T) {
sf := makeStackFile([]stack{
{frames: []string{"com/example/App.process"}, lines: []uint32{42}, count: 10},
})
p := newStarlarkProfile(sf, nil, "cpu", "test")
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
f = s.frames[0]
print(f.name)
print(f.fqn)
print(f.pkg)
print(f.cls)
print(f.method)
print(f.line)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
expects := []string{"App.process", "com.example.App.process", "com.example", "App", "process", "42"}
for i, exp := range expects {
if i >= len(lines) {
t.Fatalf("expected %d lines, got %d", len(expects), len(lines))
}
if strings.TrimSpace(lines[i]) != exp {
t.Fatalf("field %d: expected %q, got %q", i, exp, lines[i])
}
}
}
func TestFrameFieldsNative(t *testing.T) {
sf := makeStackFile([]stack{
{frames: []string{"libc.so.6.__sched_yield"}, lines: []uint32{0}, count: 5},
})
p := newStarlarkProfile(sf, nil, "cpu", "test")
out := captureOutput(func() {
code := runScript(`
f = p.stacks[0].frames[0]
print("name=" + f.name)
print("pkg=" + f.pkg)
print("cls=" + f.cls)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 3 {
t.Fatalf("expected 3 lines, got %d: %q", len(lines), out)
}
if strings.TrimSpace(lines[0]) != "name=__sched_yield" {
t.Fatalf("expected name '__sched_yield', got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "pkg=" {
t.Fatalf("expected empty pkg, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "cls=" {
t.Fatalf("expected empty cls, got %q", lines[2])
}
}
func TestFrameFieldsKernel(t *testing.T) {
sf := makeStackFile([]stack{
{frames: []string{"__do_softirq"}, lines: []uint32{0}, count: 5},
})
p := newStarlarkProfile(sf, nil, "cpu", "test")
out := captureOutput(func() {
code := runScript(`
f = p.stacks[0].frames[0]
print("name=" + f.name)
print("pkg=" + f.pkg)
print("cls=" + f.cls)
print("method=" + f.method)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if len(lines) < 4 {
t.Fatalf("expected 4 lines, got %d: %q", len(lines), out)
}
if strings.TrimSpace(lines[0]) != "name=__do_softirq" {
t.Fatalf("expected '__do_softirq', got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "pkg=" {
t.Fatalf("expected empty pkg, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "cls=" {
t.Fatalf("expected empty cls, got %q", lines[2])
}
if strings.TrimSpace(lines[3]) != "method=__do_softirq" {
t.Fatalf("expected method '__do_softirq', got %q", lines[3])
}
}
func TestFrameLine(t *testing.T) {
out := captureOutput(func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
found = False
for s in p.stacks:
if found:
break
for f in s.frames:
if f.line > 0:
print(f.name, f.line)
found = True
break
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
out = strings.TrimSpace(out)
if out == "" {
t.Log("no frames with line numbers in cpu.jfr (this is OK if profiler didn't capture lines)")
}
}
func TestProfileStacksImmutable(t *testing.T) {
stderr := captureStream(&os.Stderr, func() {
code := runScript(fmt.Sprintf(`
p = open(%q)
p.stacks.append("bad")
`, scriptFixture("cpu.jfr")), "", nil, testTimeout)
if code == 0 {
t.Fatalf("expected error when modifying frozen stacks")
}
})
_ = stderr // error is expected
}
// ---------------------------------------------------------------------------
// Increment 3 — Stack methods
// ---------------------------------------------------------------------------
func testProfile() *starlarkProfile {
sf := makeStackFile([]stack{
{frames: []string{"java/lang/Thread.run", "com/example/Server.handle", "com/example/Service.process", "java/util/HashMap.put"}, lines: []uint32{0, 10, 20, 30}, count: 10, thread: "worker-1"},
{frames: []string{"java/lang/Thread.run", "com/example/Server.handle", "com/example/Encoder.encode"}, lines: []uint32{0, 10, 40}, count: 5, thread: "worker-2"},
{frames: []string{"__do_softirq"}, lines: []uint32{0}, count: 3, thread: ""},
})
return newStarlarkProfile(sf, nil, "cpu", "test")
}
func TestStackHas(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
print(s.has("HashMap.put"))
print(s.has("NonExistent"))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "True" {
t.Fatalf("expected True for HashMap.put, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "False" {
t.Fatalf("expected False for NonExistent, got %q", lines[1])
}
}
func TestStackHasShortName(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`print(p.stacks[0].has("HashMap"))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "True" {
t.Fatalf("expected True, got %q", out)
}
}
func TestStackHasSeq(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
print(s.has_seq("Server.handle", "HashMap.put"))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "True" {
t.Fatalf("expected True, got %q", out)
}
}
func TestStackHasSeqNonAdjacent(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
print(s.has_seq("Thread.run", "HashMap.put"))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "True" {
t.Fatalf("expected True, got %q", out)
}
}
func TestStackHasSeqWrongOrder(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
print(s.has_seq("HashMap.put", "Server.handle"))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "False" {
t.Fatalf("expected False, got %q", out)
}
}
func TestStackHasSeqSingle(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`print(p.stacks[0].has_seq("HashMap.put"))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "True" {
t.Fatalf("expected True, got %q", out)
}
}
func TestStackAbove(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
above = s.above("Server.handle")
print(len(above))
for f in above:
print(f.name)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "2" {
t.Fatalf("expected 2 frames above, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "Service.process" {
t.Fatalf("expected Service.process, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "HashMap.put" {
t.Fatalf("expected HashMap.put, got %q", lines[2])
}
}
func TestStackAboveNoMatch(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`print(len(p.stacks[0].above("NonExistent")))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "0" {
t.Fatalf("expected 0, got %q", out)
}
}
func TestStackBelow(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
below = s.below("HashMap.put")
print(len(below))
for f in below:
print(f.name)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "3" {
t.Fatalf("expected 3 frames below, got %q", lines[0])
}
}
func TestStackBelowNoMatch(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`print(len(p.stacks[0].below("NonExistent")))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "0" {
t.Fatalf("expected 0, got %q", out)
}
}
func TestStackAboveBelowEdge(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
# Match at leaf — above is empty
print(len(s.above("HashMap.put")))
# Match at root — below is empty
print(len(s.below("Thread.run")))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "0" {
t.Fatalf("expected 0 frames above leaf, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "0" {
t.Fatalf("expected 0 frames below root, got %q", lines[1])
}
}
func TestStackMethodsEmptyStack(t *testing.T) {
sf := makeStackFile([]stack{
{frames: []string{}, lines: []uint32{}, count: 1},
})
p := newStarlarkProfile(sf, nil, "cpu", "test")
out := captureOutput(func() {
code := runScript(`
s = p.stacks[0]
print(s.has("anything"))
print(len(s.above("anything")))
print(len(s.below("anything")))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "False" {
t.Fatalf("expected False for has() on empty stack, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "0" {
t.Fatalf("expected 0 for above on empty stack, got %q", lines[1])
}
if strings.TrimSpace(lines[2]) != "0" {
t.Fatalf("expected 0 for below on empty stack, got %q", lines[2])
}
}
// ---------------------------------------------------------------------------
// Increment 4 — Profile methods (hot, threads, filter, group_by)
// ---------------------------------------------------------------------------
func TestProfileHot(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
methods = p.hot()
print(len(methods))
print(type(methods[0]))
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[1]) != "Method" {
t.Fatalf("expected Method type, got %q", lines[1])
}
}
func TestProfileHotLimit(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`print(len(p.hot(2)))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "2" {
t.Fatalf("expected 2, got %q", out)
}
}
func TestProfileHotFQN(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
m = p.hot(fqn=True)[0]
print(m.name)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
name := strings.TrimSpace(out)
// FQN should contain dots (package separator).
if !strings.Contains(name, ".") {
t.Fatalf("expected FQN with dots, got %q", name)
}
}
func TestProfileHotEmpty(t *testing.T) {
sf := makeStackFile(nil)
p := newStarlarkProfile(sf, nil, "cpu", "test")
out := captureOutput(func() {
code := runScript(`print(len(p.hot()))`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
if strings.TrimSpace(out) != "0" {
t.Fatalf("expected 0, got %q", out)
}
}
func TestMethodFields(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
m = p.hot()[0]
print(type(m.name))
print(type(m.fqn))
print(type(m.self))
print(type(m.self_pct))
print(type(m.total))
print(type(m.total_pct))
print(m.self > 0)
print(m.self_pct > 0.0)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
expected := []string{"string", "string", "int", "float", "int", "float", "True", "True"}
for i, exp := range expected {
if i >= len(lines) {
t.Fatalf("expected %d lines, got %d", len(expected), len(lines))
}
if strings.TrimSpace(lines[i]) != exp {
t.Fatalf("line %d: expected %q, got %q", i, exp, lines[i])
}
}
}
func TestProfileHotSortTotal(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
m = p.hot(sort="total")[0]
print(m.name)
print(m.total)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
// Top by total should be Thread.run or Server.handle (both total=15).
name := strings.TrimSpace(lines[0])
if name != "Thread.run" && name != "Server.handle" {
t.Fatalf("expected Thread.run or Server.handle at top by total, got %q", name)
}
if strings.TrimSpace(lines[1]) != "15" {
t.Fatalf("expected total=15, got %q", lines[1])
}
}
func TestProfileHotSortSelfExplicit(t *testing.T) {
p := testProfile()
out := captureOutput(func() {
code := runScript(`
m = p.hot(sort="self")[0]
print(m.name)
print(m.self)
`, "", nil, testTimeout, withPredeclared("p", p))
if code != 0 {
t.Fatalf("expected exit 0, got %d", code)
}
})
lines := strings.Split(strings.TrimSpace(out), "\n")
if strings.TrimSpace(lines[0]) != "HashMap.put" {
t.Fatalf("expected HashMap.put at top by self, got %q", lines[0])
}
if strings.TrimSpace(lines[1]) != "10" {
t.Fatalf("expected self=10, got %q", lines[1])
}
}
func TestProfileHotSortInvalid(t *testing.T) {
p := testProfile()
stderr := captureStream(&os.Stderr, func() {
code := runScript(`p.hot(sort="bogus")`, "", nil, testTimeout, withPredeclared("p", p))
if code == 0 {