Golang Help

golang help

benchmark

profiling

source go test -bench=. -benchmem -cpuprofile=cpu.out -memprofile=mem.out bench_test.go

go tool pprof -alloc_space mem.cputest mem.out (pprof) list ‘func name’

go build -gcflags “-m -m”

profiling example

source get data from pprof debug endpoint $ curl http://localhost:8080/debug/pprof/profile?seconds=29 > cpu.1000_reqs_sec_no_optimizations.prof

show in local webserver $ go tool pprof -http=:12345 cpu.1000_reqs_sec_no_optimizations.prof

benchcmp

go test -bench=. -benchmem bench_test.go > new.txt git stash go test -bench=. -benchmem bench_test.go > old.txt benchcmp old.txt new.txt

benchmark old ns/op new ns/op delta
BenchmarkToJSON 1579 495 -68.65%

benchmark old MB/s new MB/s speedup BenchmarkToJSON 12.66 46.41 3.67x

benchmark old allocs new allocs delta BenchmarkToJSON 2 2 +0.00%

benchmark old bytes new bytes delta BenchmarkToJSON 184 48 -73.91%

debuger

graphical delve?

dlv debug main.go # eq go run
dlv exec # debug compiled bin
dlv test # eq go test
dlv attach # debug running app
dlv trace #

dlv cli> break file:num # define break point
dlv cli> continue #run till break point alias c
dlv cli> locals #show vars
dlv cli> step # execute next line
dlv cli> next # Step over to next source line.
dlv cli> stepout # execute till return and stop
dlv cli> restart # restart app with debug and all breakpoints

dlv cli>source path # execute dlv commands from file defined in path separated by newline
dlv cli> frame 0 locals # show locals in 0 frame goroutine

dlv variables cmd:

dlv

dlv get app state

dlv manipulate app state

dlv app halt cmd

TODO delve