blob: 72ef8d2f1e5df4c7ad513e1fb67162b7b2c7d376 [file] [log] [blame]
// Copyright 2021 The Fuchsia Authors.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package diff
import (
"go.fuchsia.dev/infra/cmd/size_check/sizes"
)
// DiffBinarySizes diffs two BinarySizes objects.
func DiffBinarySizes(binarySizes, baselineBinarySizes sizes.BinarySizes) *Diff {
var cDiffs []*ComponentDiff
for component, metadata := range binarySizes {
// Skip any components which don't have a matching baseline component.
baselineMetadata, ok := baselineBinarySizes[component]
if !ok {
continue
}
cDiffs = append(cDiffs, NewComponentDiff(component, baselineMetadata.Size, metadata.Size, metadata.Budget, metadata.CreepBudget))
}
return NewDiff(cDiffs)
}
// Diff is a combined diff of per-component diffs.
type Diff struct {
// The per-component diffs.
ComponentDiffs []*ComponentDiff `json:"component_diffs"`
// Whether one or more budgets are exceeded.
BudgetExceeded bool `json:"budget_exceeded"`
// Whether one or more creep budgets are exceeded.
CreepBudgetExceeded bool `json:"creep_budget_exceeded"`
// The baseline build ID that was used, if applicable.
BaselineBuildID int64 `json:"baseline_build_id,omitempty"`
}
// NewDiff constructs a Diff from a slice of ComponentDiffs.
func NewDiff(componentDiffs []*ComponentDiff) *Diff {
var budgetExceeded bool
for _, c := range componentDiffs {
if c.BudgetExceeded {
budgetExceeded = true
break
}
}
var creepBudgetExceeded bool
for _, c := range componentDiffs {
if c.CreepBudgetExceeded {
creepBudgetExceeded = true
break
}
}
return &Diff{
ComponentDiffs: componentDiffs,
BudgetExceeded: budgetExceeded,
CreepBudgetExceeded: creepBudgetExceeded,
}
}
// ComponentDiff contains metadata of a per-component diff.
type ComponentDiff struct {
// Name of the component.
Name string `json:"name"`
// Baseline size of the component in bytes.
BaselineSize int64 `json:"baseline_size"`
// Size of the component in bytes.
Size int64 `json:"size"`
// Size diff in bytes.
SizeDiff int64 `json:"size_diff"`
// Budget of the component in bytes.
Budget int64 `json:"budget"`
// Creep budget of the component in bytes.
CreepBudget int64 `json:"creep_budget"`
// Whether the budget is exceeded.
BudgetExceeded bool `json:"budget_exceeded"`
// Whether the creep budget is exceeded.
CreepBudgetExceeded bool `json:"creep_budget_exceeded"`
}
// NewComponentDiff constructs a ComponentDiff.
func NewComponentDiff(name string, baselineSize, size, budget, creepBudget int64) *ComponentDiff {
return &ComponentDiff{
Name: name,
BaselineSize: baselineSize,
Size: size,
SizeDiff: size - baselineSize,
Budget: budget,
CreepBudget: creepBudget,
BudgetExceeded: size > budget,
CreepBudgetExceeded: size-baselineSize > creepBudget,
}
}