MCPcopy Create free account
hub / github.com/cel-expr/cel-go / TestCostSizingStrategyComparison

Function TestCostSizingStrategyComparison

cel/cost_test.go:151–426  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

149}
150
151func TestCostSizingStrategyComparison(t *testing.T) {
152 tests := []struct {
153 name string
154 expr string
155 envOpts []EnvOption
156 hints map[string]uint64
157 in any
158 wantDefaultEst cost.CostEstimate
159 wantDefaultCost uint64
160 wantAggregateEst cost.CostEstimate
161 wantAggregateCost uint64
162 }{
163 {
164 name: "list_of_strings_in_list",
165 expr: `"b" in list`,
166 envOpts: []EnvOption{
167 Variable("list", ListType(StringType)),
168 CostModel(
169 cost.Overload(overloads.InList,
170 cost.EvalCost(cost.Arg(1)),
171 ),
172 ),
173 },
174 hints: map[string]uint64{"list": 2, "list.@items": 5},
175 in: map[string]any{"list": []string{"hello", "world"}},
176 // Default sizing: list item count = 2 -> actual: 1 (ident) + 2 = 3
177 wantDefaultEst: cost.RangedCostEstimate(2, 3),
178 wantDefaultCost: 3,
179 // Aggregate sizing: container(1) + "hello"(5) + "world"(5) = 11 -> actual: 1 (ident) + 11 = 12
180 wantAggregateEst: cost.RangedCostEstimate(2, 12),
181 wantAggregateCost: 12,
182 },
183 {
184 name: "map_string_to_string",
185 expr: `custom_func(map_val)`,
186 envOpts: []EnvOption{
187 Variable("map_val", MapType(StringType, StringType)),
188 Function("custom_func",
189 Overload("custom_func_map", []*Type{MapType(StringType, StringType)}, IntType,
190 UnaryBinding(func(val ref.Val) ref.Val {
191 return types.Int(1)
192 }),
193 ),
194 ),
195 CostModel(
196 cost.Overload("custom_func_map",
197 cost.EvalCost(cost.Arg(0)),
198 ),
199 ),
200 },
201 hints: map[string]uint64{"map_val": 2, "map_val.@keys": 2, "map_val.@values": 2},
202 in: map[string]any{"map_val": map[string]string{"k1": "v1", "k2": "v2"}},
203 // Default sizing: map entries count = 2 -> actual: 1 (ident) + 2 = 3
204 wantDefaultEst: cost.RangedCostEstimate(2, 3),
205 wantDefaultCost: 3,
206 // Aggregate sizing: container(1) + 2 keys(2+2) + 2 values(2+2) = 9 -> actual: 1 + 9 = 10
207 wantAggregateEst: cost.RangedCostEstimate(2, 10),
208 wantAggregateCost: 10,

Callers

nothing calls this directly

Calls 15

OverloadFunction · 0.92
EvalCostFunction · 0.92
ArgFunction · 0.92
RangedCostEstimateFunction · 0.92
IntTypeAlias · 0.92
MemberOverloadFunction · 0.92
TargetFunction · 0.92
DefaultSizingStrategyFunction · 0.92
AggregateSizingStrategyFunction · 0.92
CostModelFunction · 0.85
CostSizingStrategyFunction · 0.85
CostTrackingFunction · 0.85

Tested by

no test coverage detected