MCPcopy Index your code
hub / github.com/tinygo-org/tinygo / TestBinarySize

Function TestBinarySize

builder/sizes_test.go:34–74  ·  view source on GitHub ↗

Test whether code and data size is as expected for the given targets. This tests both the logic of loadProgramSize and checks that code size doesn't change unintentionally. If you find that code or data size is reduced, then great! You can reduce the number in this test. If you find that the code o

(t *testing.T)

Source from the content-addressed store, hash-verified

32// increase. If so, please consider whether this new feature is indeed worth the
33// size increase for all users.
34func TestBinarySize(t *testing.T) {
35 if runtime.GOOS == "linux" && !hasBuiltinTools {
36 // Debian LLVM packages are modified a bit and tend to produce
37 // different machine code. Ideally we'd fix this (with some attributes
38 // or something?), but for now skip it.
39 t.Skip("Skip: using external LLVM version so binary size might differ")
40 }
41
42 // This is a small number of very diverse targets that we want to test.
43 tests := []sizeTest{
44 // microcontrollers
45 {"hifive1b", "examples/echo", 3680, 280, 0, 2252},
46 {"microbit", "examples/serial", 2694, 342, 8, 2248},
47 {"wioterminal", "examples/pininterrupt", 7074, 1510, 120, 7248},
48
49 // TODO: also check wasm. Right now this is difficult, because
50 // wasm binaries are run through wasm-opt and therefore the
51 // output varies by binaryen version.
52 }
53 for _, tc := range tests {
54 tc := tc
55 t.Run(tc.target+"/"+tc.path, func(t *testing.T) {
56 t.Parallel()
57
58 // Build the binary.
59 result := buildBinary(t, tc.target, tc.path)
60
61 // Check whether the size of the binary matches the expected size.
62 sizes, err := loadProgramSize(result.Executable, nil)
63 if err != nil {
64 t.Fatal("could not read program size:", err)
65 }
66 if sizes.Code != tc.codeSize || sizes.ROData != tc.rodataSize || sizes.Data != tc.dataSize || sizes.BSS != tc.bssSize {
67 t.Errorf("Unexpected code size when compiling: -target=%s %s", tc.target, tc.path)
68 t.Errorf(" code rodata data bss")
69 t.Errorf("expected: %6d %6d %6d %6d", tc.codeSize, tc.rodataSize, tc.dataSize, tc.bssSize)
70 t.Errorf("actual: %6d %6d %6d %6d", sizes.Code, sizes.ROData, sizes.Data, sizes.BSS)
71 }
72 })
73 }
74}
75
76// Check that the -size=full flag attributes binary size to the correct package
77// without filesystem paths and things like that.

Callers

nothing calls this directly

Calls 7

buildBinaryFunction · 0.85
loadProgramSizeFunction · 0.85
ParallelMethod · 0.80
SkipMethod · 0.65
FatalMethod · 0.65
ErrorfMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected