(t *testing.T)
| 24 | ) |
| 25 | |
| 26 | func TestMemoryTracking(t *testing.T) { |
| 27 | tests := []struct { |
| 28 | name string |
| 29 | expr string |
| 30 | decls []EnvOption |
| 31 | memOpts []types.MemoryTrackerOption |
| 32 | in any |
| 33 | wantPeak uint32 |
| 34 | }{ |
| 35 | { |
| 36 | name: "attribute_resolution", |
| 37 | expr: `a`, |
| 38 | decls: []EnvOption{Variable("a", ListType(IntType))}, |
| 39 | in: map[string]any{"a": []int64{1, 2, 3}}, |
| 40 | // 1 (list container) + 3 elements, then the call-free program peaks at the attribute. |
| 41 | wantPeak: 4, |
| 42 | }, |
| 43 | { |
| 44 | name: "call_output", |
| 45 | expr: `a + a`, |
| 46 | decls: []EnvOption{Variable("a", ListType(IntType))}, |
| 47 | in: map[string]any{"a": []int64{1, 2}}, |
| 48 | // The peak is the call output: a lazy concat list backed by both inputs, sizing |
| 49 | // as their sum (3 + 3 = 6), which exceeds either input observed on its own. |
| 50 | wantPeak: 6, |
| 51 | }, |
| 52 | { |
| 53 | name: "attribute_field_selection", |
| 54 | expr: `m.vals`, |
| 55 | decls: []EnvOption{Variable("m", MapType(StringType, ListType(IntType)))}, |
| 56 | in: map[string]any{"m": map[string][]int64{"vals": {1, 2, 3}}}, |
| 57 | // The resolved attribute value is the inner list: 1 (container) + 3 elements. |
| 58 | wantPeak: 4, |
| 59 | }, |
| 60 | } |
| 61 | |
| 62 | for _, tst := range tests { |
| 63 | tc := tst |
| 64 | t.Run(tc.name, func(t *testing.T) { |
| 65 | t.Parallel() |
| 66 | env := testEnv(t, tc.decls...) |
| 67 | ast, iss := env.Compile(tc.expr) |
| 68 | if iss.Err() != nil { |
| 69 | t.Fatalf("env.Compile(%v) failed: %v", tc.expr, iss.Err()) |
| 70 | } |
| 71 | program, err := env.Program(ast, MemoryTracking(tc.memOpts...)) |
| 72 | if err != nil { |
| 73 | t.Fatalf("env.Program() failed: %v", err) |
| 74 | } |
| 75 | _, details, err := program.Eval(tc.in) |
| 76 | if err != nil { |
| 77 | t.Fatalf("program.Eval() failed: %v", err) |
| 78 | } |
| 79 | peak := details.PeakMemory() |
| 80 | if peak == nil { |
| 81 | t.Fatalf("EvalDetails.PeakMemory() got nil, wanted %d", tc.wantPeak) |
| 82 | } |
| 83 | if *peak != tc.wantPeak { |
nothing calls this directly
no test coverage detected