...

/

Investigating Performance

Investigating Performance

This lesson explains the evaluation criteria of the performance of a Go program using Go tools, which includes time and memory utilization.

Time and memory consumption

A handy script to measure memory and consumption on a Unix-like systems is xtime:

#!/bin/sh
/usr/bin/time -f '%Uu %Ss %er %MkB %C' "$@"

Used on the command-line as xtime progexec, where prog is a Go executable program, it shows an output like:

56.63u 0.26s 56.92r 1642640kB progexec

giving the user time, system time, real time and maximum memory usage, respectively.

Tuning with go test

If the code uses the Go testing package’s benchmarking support, we could use the go test standard -cpuprofile and -memprofile flags, causing a CPU or memory usage profile to be written to the file specified.

go test -cpuprofile cpu.prof -memprofile mem.prof -bench 

This compiles and executes the tests and benchmarks in the current folder. It also writes a CPU profile to cpu.prof and a memory profile to mem.prof.

Tuning

...