golang分析编译后二进制文件依赖大小可视化插件go-size-analyzer的使用
Golang分析编译后二进制文件依赖大小可视化插件go-size-analyzer的使用
简介
go-size-analyzer是一个用于分析Go编译后二进制文件大小的工具,可以详细展示各依赖包和部分在二进制文件中所占的大小比例。
主要特性:
- 跨平台支持分析ELF、Mach-O、PE和WebAssembly(实验性)二进制格式
- 按包和部分详细展示大小分解
- 支持多种输出格式:text、json、html、svg
- 通过Web界面和终端UI交互式探索
- 支持二进制比较的diff模式(支持json和text输出)
安装
MacOS/Linux通过Homebrew安装
brew install go-size-analyzer
Windows通过scoop安装
scoop install go-size-analyzer
通过Go安装
go install github.com/Zxilly/go-size-analyzer/cmd/gsa@latest
使用示例
Web模式
$ gsa --web golang-compiled-binary
这将在8080端口启动一个Web服务器,可以在浏览器中查看结果。
Web界面截图:
终端UI模式
$ gsa --tui golang-compiled-binary
文本模式
$ gsa docker-compose-linux-x86_64
┌─────────────────────────────────────────────────────────────────────────────────┐
│ docker-compose-linux-x86_64 │
├─────────┬──────────────────────────────────────────────────┬────────┬───────────┤
│ PERCENT │ NAME │ SIZE │ TYPE │
├─────────┼──────────────────────────────────────────────────┼────────┼───────────┤
│ 17.37% │ k8s.io/api │ 11 MB │ vendor │
│ 15.52% │ .rodata │ 9.8 MB │ section │
│ 8.92% │ .gopclntab │ 5.6 MB │ section │
│ 7.51% │ .strtab │ 4.7 MB │ section │
│ 5.13% │ k8s.io/client-go │ 3.2 MB │ vendor │
│ 3.36% │ .symtab │ 2.1 MB │ section │
│ 3.29% │ github.com/moby/buildkit │ 2.1 MB │ vendor │
│ 2.02% │ google.golang.org/protobuf │ 1.3 MB │ vendor │
│ 1.96% │ github.com/google/gnostic-models │ 1.2 MB │ vendor │
│ 1.82% │ k8s.io/apimachinery │ 1.1 MB │ vendor │
│ 1.73% │ net │ 1.1 MB │ std │
│ 1.72% │ github.com/aws/aws-sdk-go-v2 │ 1.1 MB │ vendor │
│ 1.57% │ crypto │ 991 kB │ std │
│ 1.53% │ github.com/docker/compose/v2 │ 964 kB │ vendor │
│ 1.48% │ github.com/gogo/protobuf │ 931 kB │ vendor │
│ 1.40% │ runtime │ 884 kB │ std │
│ 1.32% │ go.opentelemetry.io/otel │ 833 kB │ vendor │
│ 1.28% │ .text │ 809 kB │ section │
│ 1.18% │ google.golang.org/grpc │ 742 kB │ vendor │
...[Collapsed]...
│ 0.00% │ github.com/google/shlex │ 0 B │ vendor │
│ 0.00% │ github.com/pmezard/go-difflib │ 0 B │ vendor │
│ 0.00% │ go.uber.org/mock │ 0 B │ vendor │
│ 0.00% │ github.com/kballard/go-shellquote │ 0 B │ vendor │
│ 0.00% │ tags.cncf.io/container-device-interface │ 0 B │ vendor │
│ 0.00% │ github.com/josharian/intern │ 0 B │ vendor │
│ 0.00% │ github.com/shibumi/go-pathspec │ 0 B │ vendor │
│ 0.00% │ dario.cat/mergo │ 0 B │ vendor │
│ 0.00% │ github.com/mattn/go-colorable │ 0 B │ vendor │
│ 0.00% │ github.com/secure-systems-lab/go-securesystemslib│ 0 B │ vendor │
├─────────┼──────────────────────────────────────────────────┼────────┼───────────┤
│ 100% │ KNOWN │ 63 MB │ │
│ 100% │ TOTAL │ 63 MB │ │
└─────────┴──────────────────────────────────────────────────┴────────┴───────────┘
Diff模式
$ gsa bin-linux-1.21-amd64 bin-linux-1.22-amd64
┌────────────────────────────────────────────────────────────────┐
│ Diff between bin-linux-1.21-amd64 and bin-linux-1.22-amd64 │
├─────────┬──────────────────────┬──────────┬──────────┬─────────┤
│ PERCENT │ NAME │ OLD SIZE │ NEW SIZE │ DIFF │
├─────────┼──────────────────────┼──────────┼──────────┼─────────┤
│ +28.69% │ runtime │ 801 kB │ 1.0 MB │ +230 kB │
│ +100% │ internal/chacha8rand │ │ 3.1 kB │ +3.1 kB │
│ +5.70% │ <autogenerated> │ 18 kB │ 19 kB │ +1.0 kB │
│ +8.59% │ internal/abi │ 6.1 kB │ 6.6 kB │ +525 B │
│ +10.52% │ internal/cpu │ 4.9 kB │ 5.4 kB │ +515 B │
│ +4.45% │ internal/reflectlite │ 3.9 kB │ 4.1 kB │ +173 B │
│ +2.64% │ internal/bytealg │ 1.5 kB │ 1.5 kB │ +39 B │
│ +0.80% │ strconv │ 4.0 kB │ 4.0 kB │ +32 B │
│ +0.19% │ syscall │ 13 kB │ 13 kB │ +24 B │
│ -0.37% │ embed │ 8.6 kB │ 8.6 kB │ -32 B │
│ -0.16% │ main │ 19 kB │ 19 kB │ -32 B │
│ -0.38% │ reflect │ 25 kB │ 25 kB │ -96 B │
│ -0.26% │ time │ 87 kB │ 87 kB │ -224 B │
│ -7.95% │ sync │ 9.5 kB │ 8.7 kB │ -755 B │
├─────────┼──────────────────────┼──────────┼──────────┼─────────┤
│ +8.47% │ .rodata │ 122 kB │ 132 kB │ +10 kB │
│ +5.04% │ .gopclntab │ 144 kB │ 152 kB │ +7.3 kB │
│ +3.61% │ .debug_info │ 168 kB │ 174 kB │ +6.1 kB │
│ +3.52% │ .debug_loc │ 81 kB │ 84 kB │ +2.9 kB │
│ +3.03% │ .debug_line │ 80 kB │ 82 kB │ +2.4 kB │
│ +3.41% │ .symtab │ 59 kB │ 61 kB │ +2.0 kB │
│ +4.29% │ .debug_frame │ 29 kB │ 30 kB │ +1.2 kB │
│ +1.25% │ .strtab │ 61 kB │ 62 kB │ +763 B │
│ +3.28% │ .debug_ranges │ 13 kB │ 13 kB │ +415 B │
│ +5.13% │ .data │ 5.0 kB │ 5.2 kB │ +256 B │
│ +7.32% │ .typelink │ 1.3 kB │ 1.3 kB │ +92 B │
│ +27.78% │ .go.buildinfo │ 288 B │ 368 B │ +80 B │
│ -1.56% │ .debug_gdb_scripts │ 64 B │ 63 B │ -1 B │
│ -0.63% │ .noptrdata │ 2.5 kB │ 2.5 kB │ -16 B │
├─────────┼──────────────────────┼──────────┼──────────┼─────────┤
│ +3.86% │ bin-linux-1.21-amd64 │ 1.6 MB │ 1.6 MB │ +61 kB │
│ │ bin-linux-1.22-amd64 │ │ │ │
└─────────┴──────────────────────┴──────────┴──────────┴─────────┘
Svg模式
$ gsa cockroach-darwin-amd64 -f svg -o data.svg --hide-sections
完整选项
A tool for determining the extent to which dependencies contribute to the
bloated size of compiled Go binaries.
Arguments:
<file> Binary file to analyze or result json file for diff
[<diff file>] New binary file or result json file to compare, optional
Flags:
-h, --help Show context-sensitive help.
--verbose Verbose output
-f, --format="text" Output format, possible values: text,json,html,svg
--no-disasm Skip disassembly pass
--no-symbol Skip symbol pass
--no-dwarf Skip dwarf pass
-o, --output=STRING Write to file
--version Show version
Text output options
--hide-sections Hide sections
--hide-main Hide main package
--hide-std Hide standard library
Json output options
--indent=INDENT Indentation for json output
--compact Hide function details, replacement with size
Svg output options
--width=1028 Width of the svg treemap
--height=640 Height of the svg treemap
--margin-box=4 Margin between boxes
--padding-box=4 Padding between box border and content
--padding-root=32 Padding around root content
Web interface options
--web use web interface to explore the details
--listen=":8080" listen address
--open Open browser
--update-cache Update the cache file for the web UI
Terminal interface options
--tui Use terminal interface to explore the details
Imports analysis options
--imports Try analyze package imports from source
注意事项
工具可以处理剥离的二进制文件,但可能导致结果不准确。
更多关于golang分析编译后二进制文件依赖大小可视化插件go-size-analyzer的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复