Documentation
¶
Overview ¶
Package sizecheck is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package sizecheck measures a built package (a WebAssembly binary, an Android AAB or APK, or an iOS IPA) and checks it against a size budget, the checks behind `game size`. It also finds and measures the packages `nsgo build` writes under an output directory, which the editor's Build and Release screen reports.
Index ¶
- Constants
- func FormatBudgetReport(rep BudgetReport) string
- func FormatMobileReport(rep MobilePackageReport) string
- func FormatWasmMarkdownReport(rep BudgetReport) string
- func PackageDir(outDir, appName, appVersion, target string) string
- func PackageSlug(appName, appID string) string
- func ReleaseName(appName, appID, appVersion, target, suffix string) string
- type ArtifactMeasurement
- type BudgetExceededError
- type BudgetFile
- type BudgetReport
- type CategoryTotals
- type FileEntry
- type Measurement
- type MobileBudget
- type MobileDelta
- type MobilePackageReport
- func CheckMobilePackage(pkgPath string, kind PackageKind, budget MobileBudget) (MobilePackageReport, error)
- func CompareMobileReport(rep, baseline MobilePackageReport) MobilePackageReport
- func EvaluateMobileBudget(rep MobilePackageReport, budget MobileBudget) MobilePackageReport
- func InspectMobilePackage(pkgPath string, kind PackageKind) (MobilePackageReport, error)
- func InspectMobileReader(r io.ReaderAt, archiveSize int64, displayPath string, kind PackageKind) (MobilePackageReport, error)
- func LoadMobileReport(filePath string) (MobilePackageReport, error)
- type PackageKind
- type SizeBreakdown
- type WasmDelta
Examples ¶
Constants ¶
const ( // Shared categories. CategoryResources = "resources" CategoryOther = "other" // Android categories (AAB and APK). CategoryNativeLibs = "native_libs" CategoryDEX = "dex" CategoryAssets = "assets" CategoryManifestMeta = "manifest_meta" // iOS categories (IPA). CategoryExecutable = "executable" CategoryFrameworks = "frameworks" CategoryAssetsCar = "assets_car" CategoryLproj = "lproj" CategoryCodeSignature = "code_signature" )
Content category keys for Android AAB/APK and iOS IPA packages.
const DefaultTopLargestFiles = 10
DefaultTopLargestFiles is the default number of largest archive entries kept in MobilePackageReport.LargestFiles.
Variables ¶
This section is empty.
Functions ¶
func FormatBudgetReport ¶
func FormatBudgetReport(rep BudgetReport) string
FormatBudgetReport renders a human-readable summary of rep.
func FormatMobileReport ¶
func FormatMobileReport(rep MobilePackageReport) string
FormatMobileReport renders a GitHub-flavored Markdown summary suitable for terminal output and $GITHUB_STEP_SUMMARY.
func FormatWasmMarkdownReport ¶
func FormatWasmMarkdownReport(rep BudgetReport) string
FormatWasmMarkdownReport renders a Markdown summary for a WebAssembly BudgetReport.
func PackageDir ¶
PackageDir returns NSGo's per-target package directory under outDir: `<outDir>/<appName>-<appVersion>-<target>` (matching cmd/nsgo/build.go).
func PackageSlug ¶
PackageSlug returns NSGo's package file slug for an app (matching NSGo's cmd/nsgo/installer.go packageSlug): lowercase ASCII words from appName joined by hyphens, else the last dot-separated segment of appID, else "app".
func ReleaseName ¶
ReleaseName returns NSGo's release file name for target and suffix: `<slug>-<version>-<target-with-hyphens><suffix>` (matching cmd/nsgo/installer.go).
Types ¶
type ArtifactMeasurement ¶
type ArtifactMeasurement struct {
Found bool `json:"found"`
ArtifactPath string `json:"artifact_path,omitempty"`
ArtifactName string `json:"artifact_name,omitempty"`
Kind PackageKind `json:"kind,omitempty"`
SizeBytes int64 `json:"size_bytes"`
WasmMeasurement *Measurement `json:"wasm_measurement,omitempty"`
MobileReport *MobilePackageReport `json:"mobile_report,omitempty"`
Categories CategoryTotals `json:"categories"`
WebBuildDir string `json:"web_build_dir,omitempty"`
}
ArtifactMeasurement is the result of finding and measuring a real package written by `nsgo build` under a release output directory. When no package exists for the requested platform, Found is false and all byte counts are zero.
func MeasureBuiltArtifact ¶
func MeasureBuiltArtifact(outDir, appName, appID, appVersion, platform string, targets []string) (ArtifactMeasurement, error)
MeasureBuiltArtifact locates the package `nsgo build` wrote under outDir for platform (and its concrete targets) and measures its real bytes and categories. If no built artifact exists in outDir, it returns ArtifactMeasurement{Found: false}, nil.
type BudgetExceededError ¶
type BudgetExceededError struct {
Report MobilePackageReport
}
BudgetExceededError is returned by CheckMobilePackage when one or more limits in MobileBudget are exceeded. Its Error text includes all violations and the full Markdown space breakdown.
func (*BudgetExceededError) Error ¶
func (e *BudgetExceededError) Error() string
Error lists every budget violation followed by the Markdown space breakdown of the package.
type BudgetFile ¶
type BudgetFile struct {
Target string `json:"target"`
LDFlags string `json:"ldflags"`
MaxRawBytes int64 `json:"max_raw_bytes"`
MaxGzipBytes int64 `json:"max_gzip_bytes"`
Note string `json:"note,omitempty"`
}
BudgetFile is the JSON schema stored in benchmarks/web-budget.json.
func LoadBudget ¶
func LoadBudget(path string) (BudgetFile, error)
LoadBudget reads and decodes a BudgetFile from path. Its errors are *loaderr.Error values that name path, with the line of the problem and how to fix it.
type BudgetReport ¶
type BudgetReport struct {
Budget BudgetFile `json:"budget"`
Measured Measurement `json:"measured"`
Delta *WasmDelta `json:"delta,omitempty"`
Violations []string `json:"violations,omitempty"`
}
BudgetReport holds the result of comparing a Measurement against a BudgetFile.
func CompareWasm ¶
func CompareWasm(rep BudgetReport, baseline Measurement) BudgetReport
CompareWasm attaches a byte delta against baseline to rep.
func EvaluateBudget ¶
func EvaluateBudget(budget BudgetFile, got Measurement) BudgetReport
EvaluateBudget checks got against budget and returns a BudgetReport.
func (BudgetReport) Passed ¶
func (r BudgetReport) Passed() bool
Passed reports whether both raw and gzipped sizes are within budget.
type CategoryTotals ¶
type CategoryTotals struct {
CodeBytes int64 `json:"code_bytes"`
TexturesBytes int64 `json:"textures_bytes"`
AudioBytes int64 `json:"audio_bytes"`
OtherBytes int64 `json:"other_bytes"`
}
CategoryTotals aggregates real measured package bytes into four top-level buckets displayed by the editor's Build and Release screen and release reports.
func (CategoryTotals) Total ¶
func (c CategoryTotals) Total() int64
Total returns the sum of all four category totals.
type FileEntry ¶
type FileEntry struct {
Path string `json:"path"`
Category string `json:"category"`
ABI string `json:"abi,omitempty"`
CompressedBytes int64 `json:"compressed_bytes"`
UncompressedBytes int64 `json:"uncompressed_bytes"`
}
FileEntry describes one file inside a mobile package archive.
type Measurement ¶
Measurement stores measured raw and gzip-compressed WebAssembly sizes.
func LoadWasmBaseline ¶
func LoadWasmBaseline(path string) (Measurement, error)
LoadWasmBaseline reads a WebAssembly Measurement from a JSON report produced by `game size -kind wasm -json`. Its errors are *loaderr.Error values that name path.
func MeasureWasm ¶
func MeasureWasm(wasm []byte) (Measurement, error)
MeasureWasm returns the raw and gzipped (BestCompression) byte sizes of wasm.
Example ¶
MeasureWasm reports a WebAssembly binary's raw and gzipped sizes; EvaluateBudget checks them against a budget such as benchmarks/web-budget.json.
package main
import (
"bytes"
"fmt"
"github.com/nsgo-dev/game/pkg/tooling/sizecheck"
)
func main() {
wasm := bytes.Repeat([]byte("a"), 64)
m, err := sizecheck.MeasureWasm(wasm)
if err != nil {
fmt.Println(err)
return
}
report := sizecheck.EvaluateBudget(sizecheck.BudgetFile{
Target: "game.wasm",
MaxRawBytes: m.RawBytes,
MaxGzipBytes: m.GzipBytes,
}, m)
fmt.Println(report.Passed(), m.RawBytes)
}
Output: true 64
type MobileBudget ¶
type MobileBudget struct {
// MaxTotalBytes limits the total package archive size on disk.
MaxTotalBytes int64 `json:"max_total_bytes"`
// MaxPerABIBytes limits the compressed native library bytes for any single
// Android ABI (e.g. arm64-v8a, armeabi-v7a, x86_64).
MaxPerABIBytes int64 `json:"max_per_abi_bytes,omitempty"`
// MaxCategoryBytes limits compressed bytes per category key
// (e.g. "native_libs", "dex", "executable", "frameworks").
MaxCategoryBytes map[string]int64 `json:"max_category_bytes,omitempty"`
// Note documents the rationale behind the budget thresholds.
Note string `json:"note,omitempty"`
}
MobileBudget defines size budgets (in bytes) for Android AAB/APK and iOS IPA packages. Any limit <= 0 is treated as unbounded.
func DefaultAndroidAABBudget ¶
func DefaultAndroidAABBudget() MobileBudget
DefaultAndroidAABBudget returns the documented default size budget for an Android App Bundle (.aab).
Google Play caps per-device base-module downloads at 200 MiB. Keeping the combined AAB within 50 MiB total and 20 MiB per native ABI leaves headroom for game assets while preserving fast mobile install conversion.
func DefaultAndroidAPKBudget ¶
func DefaultAndroidAPKBudget() MobileBudget
DefaultAndroidAPKBudget returns the documented default size budget for an Android APK (.apk).
Multi-ABI APKs ship native libraries for every targeted ABI directly in the installable file. Keeping APKs within 40 MiB total and 20 MiB per ABI keeps direct downloads lean (well below the 100 MiB legacy APK ceiling).
func DefaultIOSIPABudget ¶
func DefaultIOSIPABudget() MobileBudget
DefaultIOSIPABudget returns the documented default size budget for an iOS App Store Package (.ipa).
Apple enforces a 200 MiB cellular download threshold. Keeping IPAs within 50 MiB total ensures fast cellular downloads even before App Thinning.
func DefaultMobileBudget ¶
func DefaultMobileBudget(kind PackageKind) (MobileBudget, error)
DefaultMobileBudget returns the default MobileBudget for kind.
func LoadMobileBudget ¶
func LoadMobileBudget(filePath string) (MobileBudget, error)
LoadMobileBudget reads and decodes a MobileBudget JSON file from filePath. Its errors are *loaderr.Error values that name filePath, with the line of the problem and how to fix it.
type MobileDelta ¶
type MobileDelta struct {
BaselineArchiveBytes int64 `json:"baseline_archive_bytes"`
ArchiveBytes int64 `json:"archive_bytes"`
CompressedBytes int64 `json:"compressed_bytes"`
UncompressedBytes int64 `json:"uncompressed_bytes"`
CategoryCompressedBytes map[string]int64 `json:"category_compressed_bytes,omitempty"`
ABICompressedBytes map[string]int64 `json:"abi_compressed_bytes,omitempty"`
}
MobileDelta stores byte differences between a current MobilePackageReport and a baseline MobilePackageReport.
type MobilePackageReport ¶
type MobilePackageReport struct {
Path string `json:"path"`
Kind PackageKind `json:"kind"`
ArchiveBytes int64 `json:"archive_bytes"`
CompressedBytes int64 `json:"compressed_bytes"`
UncompressedBytes int64 `json:"uncompressed_bytes"`
FileCount int `json:"file_count"`
Categories map[string]SizeBreakdown `json:"categories"`
ABIs map[string]SizeBreakdown `json:"abis,omitempty"`
LargestFiles []FileEntry `json:"largest_files"`
Budget MobileBudget `json:"budget"`
Delta *MobileDelta `json:"delta,omitempty"`
Violations []string `json:"violations,omitempty"`
Passed bool `json:"passed"`
}
MobilePackageReport holds the full size breakdown and budget evaluation for an Android AAB/APK or iOS IPA archive.
func CheckMobilePackage ¶
func CheckMobilePackage(pkgPath string, kind PackageKind, budget MobileBudget) (MobilePackageReport, error)
CheckMobilePackage inspects pkgPath as kind and enforces budget.
When the archive is readable, CheckMobilePackage always returns the populated MobilePackageReport. If any budget limit is exceeded, it also returns a *BudgetExceededError whose message contains every violation and the full Markdown space breakdown.
func CompareMobileReport ¶
func CompareMobileReport(rep, baseline MobilePackageReport) MobilePackageReport
CompareMobileReport attaches a byte delta against baseline to rep.
func EvaluateMobileBudget ¶
func EvaluateMobileBudget(rep MobilePackageReport, budget MobileBudget) MobilePackageReport
EvaluateMobileBudget evaluates an inspected MobilePackageReport against budget, populating Budget, Violations, and Passed.
func InspectMobilePackage ¶
func InspectMobilePackage(pkgPath string, kind PackageKind) (MobilePackageReport, error)
InspectMobilePackage opens the ZIP-based mobile archive at pkgPath and computes its category, ABI, and largest-file breakdowns.
func InspectMobileReader ¶
func InspectMobileReader(r io.ReaderAt, archiveSize int64, displayPath string, kind PackageKind) (MobilePackageReport, error)
InspectMobileReader inspects a ZIP-based mobile package from r with total archive byte length archiveSize.
func LoadMobileReport ¶
func LoadMobileReport(filePath string) (MobilePackageReport, error)
LoadMobileReport reads a MobilePackageReport JSON file produced by `game size -kind aab|apk|ipa -json`. Its errors are *loaderr.Error values that name filePath.
type PackageKind ¶
type PackageKind string
PackageKind identifies the package artifact format inspected by sizecheck.
const ( PackageKindWasm PackageKind = "wasm" PackageKindAAB PackageKind = "aab" PackageKindAPK PackageKind = "apk" PackageKindIPA PackageKind = "ipa" )
func DetectPackageKind ¶
func DetectPackageKind(filePath string) (PackageKind, bool)
DetectPackageKind infers a PackageKind from filePath's extension.
func ParsePackageKind ¶
func ParsePackageKind(raw string) (PackageKind, error)
ParsePackageKind parses a CLI -kind value into a PackageKind.
type SizeBreakdown ¶
type SizeBreakdown struct {
CompressedBytes int64 `json:"compressed_bytes"`
UncompressedBytes int64 `json:"uncompressed_bytes"`
FileCount int `json:"file_count"`
}
SizeBreakdown holds compressed and uncompressed byte totals plus entry count for a single category or ABI bucket.
type WasmDelta ¶
type WasmDelta struct {
Baseline Measurement `json:"baseline"`
RawBytes int64 `json:"raw_bytes"`
GzipBytes int64 `json:"gzip_bytes"`
}
WasmDelta stores byte differences between a current WebAssembly Measurement and a baseline Measurement.