Skip to content
Merged
2 changes: 1 addition & 1 deletion execution/engine/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
return err
}
if resp != nil {
operation.ComputeActualCost(costCalculator, varsView, execContext.resolveContext.ActualListSizes)
operation.ComputeActualCost(costCalculator, varsView, execContext.resolveContext.ArrayStats)
}
return nil
case *plan.SubscriptionResponsePlan:
Expand Down
929 changes: 884 additions & 45 deletions execution/engine/execution_engine_cost_test.go

Large diffs are not rendered by default.

22 changes: 6 additions & 16 deletions execution/engine/execution_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4912,22 +4912,12 @@ func TestExecutionEngine_Execute(t *testing.T) {
},
dataSources: makeDataSource(t, makeDataSourceOpts{includeCostConfig: true}),
expectedResponse: `{"data":{"accounts":[{"some":{"title":"User1"}},{"some":{"__typename":"User","id":"2"}},{"some":{"title":"User3"}}]}}`,
// Cost breakdown with federation:
// Query.accounts: fieldCost=5, multiplier=3 (listSize)
// accounts returns interface [Node!]! with implementing types [User, Admin]
//
// Children (per interface member type):
// User.some: User: fieldCost=3 (DS1:2 + DS2:1 summed)
// User.title: 4 (DS2, resolved via _entities federation)
// cost = 3 + 4 = 7
//
// Admin.some: User: fieldCost=3 (DS1 only)
// cost = 3
//
// Children total = 7 + 3 = 10
// (is it possible to improve accuracy here by using the largest fragment instead of the sum?)
// Total = (5 + 10) * 3 = 45
expectedEstimatedCost: intPtr(45),
// 3 * (5 + max(7, 3))
expectedEstimatedCost: intPtr(36),
// total __ 2 Users ________ 1 Admin
// 3 * (5 + 0.67*(3 + 4*1) + 0.33*3)
// 3 * (5 + 4.69 + 1)
expectedActualCost: intPtr(32),
},
computeCosts(),
))
Expand Down
6 changes: 3 additions & 3 deletions execution/graphql/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ func (r *Request) EstimatedCost() int {
return r.estimatedCost
}

func (r *Request) ComputeActualCost(calc *plan.CostCalculator, vars resolve.VariablesView, actualListSizes map[string]int) {
func (r *Request) ComputeActualCost(calc *plan.CostCalculator, vars resolve.VariablesView, arrayStats map[string]resolve.ArrayStats) {
if calc != nil {
r.actualCost = calc.ActualCost(vars, actualListSizes)
r.actualCost = calc.ActualCost(vars, arrayStats)
// Debugging of cost trees. Uncomment to debug.
// fmt.Println(calc.DebugPrint(vars, actualListSizes))
// fmt.Println(calc.DebugPrint(vars, arrayStats))
} else {
r.actualCost = 0
}
Expand Down
Loading
Loading