-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0df5905
Showing
10 changed files
with
265 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
autobrew | ||
vendor/ | ||
dist/ | ||
goweight |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This is an example goreleaser.yaml file with some sane defaults. | ||
# Make sure to check the documentation at http://goreleaser.com | ||
builds: | ||
- env: | ||
- CGO_ENABLED=0 | ||
archive: | ||
replacements: | ||
darwin: Darwin | ||
linux: Linux | ||
windows: Windows | ||
386: i386 | ||
amd64: x86_64 | ||
checksum: | ||
name_template: 'checksums.txt' | ||
snapshot: | ||
name_template: "{{ .Tag }}-next" | ||
changelog: | ||
sort: asc | ||
filters: | ||
exclude: | ||
- '^docs:' | ||
- '^test:' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018-present, Dotan Nahum. | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
setup: | ||
-brew install goreleaser/tap/goreleaser | ||
# hack to workaround go 1.11 modules, see | ||
# https://github.com/vektra/mockery/issues/213 | ||
# https://github.com/golang/go/issues/24250 | ||
GO111MODULE=off go get gopkg.in/alecthomas/kingpin.v2 | ||
GO111MODULE=off go get github.com/vektra/mockery/.../ | ||
|
||
mocks: | ||
mockery -all -dir pkg | ||
|
||
test: | ||
go test ./pkg/... | ||
deps: | ||
go mod tidy && go mod vendor | ||
|
||
release: | ||
goreleaser --rm-dist | ||
|
||
.PHONY: deps setup release mocks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
![](media/cover.png) | ||
|
||
# goweight | ||
|
||
A tool to analyze and troubleshoot a Go binary size. | ||
|
||
For more, see [this blog post](https://medium.com/@jondot/a-story-of-a-fat-go-binary-20edc6549b97#.bzaq4nol0) | ||
|
||
✅ Get a breakdown of all modules inside a binary | ||
✅ Supports Go 1.11 modules | ||
✅ Output as JSON for tracking and/or monitoring as part of CI | ||
|
||
|
||
## Quick Start | ||
|
||
``` | ||
$ go get github.com/jondot/goweight | ||
$ cd current-project | ||
$ goweight | ||
``` | ||
|
||
As an example, here's what `goweight` has to say about itself: | ||
|
||
``` | ||
$ ./goweight | ||
3.0 MB runtime | ||
1.6 MB net | ||
1.4 MB reflect | ||
1.3 MB gopkg.in/alecthomas/kingpin.v2 | ||
870 kB math/big | ||
668 kB github.com/alecthomas/template | ||
628 kB syscall | ||
626 kB text/template | ||
550 kB go/ast | ||
546 kB encoding/json | ||
509 kB text/template/parse | ||
495 kB github.com/alecthomas/template/parse | ||
424 kB time | ||
402 kB regexp/syntax | ||
395 kB golang_org/x/net/dns/dnsmessage | ||
388 kB fmt | ||
``` | ||
|
||
### Thanks: | ||
|
||
To all [Contributors](https://github.com/jondot/goweight/graphs/contributors) - you make this happen, thanks! | ||
|
||
# Copyright | ||
|
||
Copyright (c) 2018 [@jondot](http://twitter.com/jondot). See [LICENSE](LICENSE.txt) for further details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module goweight | ||
|
||
require ( | ||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect | ||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d | ||
github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/spf13/pflag v1.0.2 | ||
github.com/stretchr/testify v1.2.2 // indirect | ||
github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 | ||
gopkg.in/alecthomas/kingpin.v2 v2.2.6 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= | ||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= | ||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= | ||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d h1:lDrio3iIdNb0Gw9CgH7cQF+iuB5mOOjdJ9ERNJCBgb4= | ||
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= | ||
github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53 h1:tGfIHhDghvEnneeRhODvGYOt305TPwingKt6p90F4MU= | ||
github.com/mattn/go-zglob v0.0.0-20180803001819-2ea3427bfa53/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/spf13/pflag v1.0.2 h1:Fy0orTDgHdbnzHcsOgfCN4LtHf0ec3wwtiwJqwvf3Gc= | ||
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= | ||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= | ||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= | ||
github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713 h1:knaxjm6QMbUMNvuaSnJZmw0gRX4V/79JVUQiziJGM84= | ||
github.com/thoas/go-funk v0.0.0-20180716193722-1060394a7713/go.mod h1:mlR+dHGb+4YgXkf13rkQTuzrneeHANxOm6+ZnEV9HsA= | ||
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc= | ||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"goweight/pkg" | ||
|
||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
var ( | ||
version = "dev" | ||
commit = "none" | ||
date = "unknown" | ||
) | ||
|
||
var ( | ||
jsonOutput = kingpin.Flag("json", "Output json").Short('j').Bool() | ||
) | ||
|
||
func main() { | ||
kingpin.Version(fmt.Sprintf("%s (%s)", version, commit)) | ||
kingpin.Parse() | ||
weight := pkg.NewGoWeight() | ||
work := weight.BuildCurrent() | ||
modules := weight.Process(work) | ||
|
||
if *jsonOutput { | ||
m, _ := json.Marshal(modules) | ||
fmt.Print(string(m)) | ||
} else { | ||
for _, module := range modules { | ||
fmt.Printf("%8s %s\n", module.SizeHuman, module.Name) | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package pkg | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"os/exec" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/dustin/go-humanize" | ||
"github.com/mattn/go-zglob" | ||
"github.com/thoas/go-funk" | ||
) | ||
|
||
var moduleRegex = regexp.MustCompile("packagefile (.*)=(.*)") | ||
|
||
func run(cmd string) string { | ||
out, err := exec.Command("sh", "-c", cmd).Output() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return string(out) | ||
} | ||
|
||
func processModule(line string) *ModuleEntry { | ||
captures := moduleRegex.FindAllStringSubmatch(line, -1) | ||
if captures == nil { | ||
return nil | ||
} | ||
path := captures[0][2] | ||
stat, _ := os.Stat(path) | ||
sz := uint64(stat.Size()) | ||
|
||
return &ModuleEntry{ | ||
Path: path, | ||
Name: captures[0][1], | ||
Size: sz, | ||
SizeHuman: humanize.Bytes(sz), | ||
} | ||
} | ||
|
||
type ModuleEntry struct { | ||
Path string `json:"path"` | ||
Name string `json:"name"` | ||
Size uint64 `json:"size"` | ||
SizeHuman string `json:"size_human"` | ||
} | ||
type GoWeight struct { | ||
} | ||
|
||
func NewGoWeight() *GoWeight { | ||
return &GoWeight{} | ||
} | ||
|
||
func (g *GoWeight) BuildCurrent() string { | ||
return strings.Split(strings.TrimSpace(run("go build -work -a 2>&1")), "=")[1] | ||
} | ||
func (g *GoWeight) Process(work string) []*ModuleEntry { | ||
|
||
files, err := zglob.Glob(work + "**/importcfg") | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
allLines := funk.Uniq(funk.FlattenDeep(funk.Map(files, func(file string) []string { | ||
f, err := ioutil.ReadFile(file) | ||
if err != nil { | ||
return []string{} | ||
} | ||
return strings.Split(string(f), "\n") | ||
}))) | ||
modules := funk.Compact(funk.Map(allLines, processModule)).([]*ModuleEntry) | ||
sort.Slice(modules, func(i, j int) bool { return modules[i].Size > modules[j].Size }) | ||
|
||
return modules | ||
} |