Tracing Your Tool

Powerful Command-Line Applications in Go — by Ricardo Gerardi (54 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Reducing Memory Allocation | TOC | Improving the colStats Tool to Process Files Concurrently 👉

The Go profiler is a great tool to help you understand how your program is spending CPU time. However, sometimes a program is spending time waiting for resources to be available. For example, it could be spending time waiting for a network connection or a file to be read. To help you understand those cases, Go provides another tool: the Tracer.

Similarly to the profiler, the tracer is also integrated with the testing and benchmarking features through the go test tool. Run the benchmarks again with the -trace option to create a trace:

​ ​$ ​​go​​ ​​test​​ ​​-bench​​ ​​.​​ ​​-benchtime=10x​​ ​​-run​​ ​​^$​​  ​​-trace​​ ​​trace01.out​
​ goos: linux
​ goarch: amd64
​ pkg: pragprog.com/rggo/performance/colStats
​ Benchmark_Run-4 10 685356800 ns/op
​ PASS
​ ok pragprog.com/rggo/performance/colStats 7.712s

Once the trace is created, view the results with the go tool trace command:

​ ​$ ​​go​​ ​​tool​​ ​​trace​​ ​​trace01.out​
​ 2019/04/14 14:55:26 Parsing trace...
​ 2019/04/14 14:55:27 Splitting trace...
​ 2019/04/14 14:55:28 Opening browser. Trace viewer is listening on
http://127.0.0.1:45561

The go tool trace command parses the contents of the trace file and makes the results…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.