Skip to content

Golang experimental modules and utilities such as lru1 cache, site crawler, hot to cold trap function, memosort and more.

License

Notifications You must be signed in to change notification settings

krasoffski/gomill

Repository files navigation

Golang experimental modules and utilities

GoDoc Go Report Card Code Climate Say Thanks!

Notes about useful tools

To lint code and tools

  • binstale - verifies the binaries in your GOPATH/bin are stale or up to date
  • go-torch - flame graph profiler for Go programs
  • errcheck - checks for unchecked errors in Go programs
  • interfacer - linter that suggests interface types

Checking coverage

$ go test -coverprofile cover.report
$ go tool cover -html=cover.report -o cover.html

Inspecting package

$ go list -f '{{ .Name }}: {{ .Doc }}'
unique: Package unique provides a simple function for removing...
$ go list -f '{{ .Imports }}'
[flag fmt sync]
$ go list -f '{{ .Imports }}' fmt
[errors io math os reflect strconv sync unicode/utf8]
$ go list -f '{{ join .Imports "\n" }}' fmt
errors
io
math
os
reflect
strconv
sync
unicode/utf8

Getting documentation

$ go doc 'github.com/krasoffski/gomill/unique' Strings
func Strings(s []string) []string
    Strings removes duplicated strings from a slice of strings. It returns a new
    slice of strings without duplicates.

Performing CPU profiling

Enable profiling in your code.

package main

import (
	"os"
	"runtime/pprof"
)

func main() {
	f, err := os.Create("multiplier.cpuprofile")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	pprof.StartCPUProfile(f)
	defer pprof.StopCPUProfile()

	// Your program here
}

Than you can analyze report.

$ go tool pprof multiplier multiplier.cpuprofile
Entering interactive mode (type "help" for commands)
(pprof) top
3150ms of 5640ms total (55.85%)
Dropped 18 nodes (cum <= 28.20ms)
Showing top 10 nodes out of 95 (cum >= 3180ms)
      flat  flat%   sum%        cum   cum%
    1700ms 30.14% 30.14%     1920ms 34.04%  syscall.Syscall
     440ms  7.80% 37.94%     1290ms 22.87%  runtime.selectgoImpl
     250ms  4.43% 42.38%      250ms  4.43%  runtime/internal/atomic.Xchg
     150ms  2.66% 45.04%      150ms  2.66%  runtime.futex
     130ms  2.30% 47.34%      130ms  2.30%  runtime/internal/atomic.Cas
     120ms  2.13% 49.47%      120ms  2.13%  runtime.usleep
     110ms  1.95% 51.42%      160ms  2.84%  fmt.(*fmt).integer
     100ms  1.77% 53.19%     2670ms 47.34%  fmt.Fprintln
      80ms  1.42% 54.61%       80ms  1.42%  runtime/internal/atomic.Load
      70ms  1.24% 55.85%     3180ms 56.38%  main.main
(pprof) web
(pprof) list multiplier

What is more, you are able to create a Flame Graph.

$ go-torch --file=output.svg multiplier multiplier.cpuprofile
INFO[14:49:02] Run pprof command: go tool pprof -raw -seconds 30 multiplier multiplier.cpuprofile
INFO[14:49:02] Writing svg to output.svg
$ open torch.svg

Note: without application load CPU profile might be empty.

Tracing with go tool trace

Enable tracing in your code.

package main

import (
	"os"
	"runtime/trace"
)

func main() {
	f, err := os.Create("trace.out")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	err = trace.Start(f)
	if err != nil {
		panic(err)
	}
	defer trace.Stop()

	// Your program here
}

Than you can open web brouser for investigation

$ go tool trace trace.out

Setting version

Version defined in the source code of data race example.

// Version of race example.
var Version = "0.1.0"

Inject new version using -ldflags.

$ go build -ldflags="-X main.Version=0.2.1"
$ ./race -version
Version: 0.2.1

Creating small binary file

Remove the debugging information included in the executable binary file using -ldflags. Also re-pack binary using upx.

$ go build
$ du -h crawler
5.4M	crawler
$ go build -ldflags="-s -w"
$ du -h crawler
3.6M	crawler
$ upx crawler
$ du -h crawler
1.4M	crawler

Note: in some cases upx might produce corrupted binary file. E.g.:

$ go version
go version go1.9.1 linux/amd64
$ go build -ldflags="-s -w"
$ upx --version
upx 3.91
UCL data compression library 1.03
LZMA SDK version 9.22 beta
$ upx ./pipes
$ ./pipes -pipes 100
Segmentation fault (core dumped)

Running simple benchmark test

Create simple benchmark test like bellow

package popcount

import "testing"

func BenchmarkPopcount1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		PopCount1(275032803564053945)
	}
}

Run test with options like go test -bench=. or go test -bench=BenchmarkPopcount1:

$ go test -bench=.
PASS
BenchmarkPopcount1-2	200000000	         9.36 ns/op
ok  	github.com/krasoffski/gomill/gopl/ch02/popcount	2.824s

About

Golang experimental modules and utilities such as lru1 cache, site crawler, hot to cold trap function, memosort and more.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published