hi there serdar gagalop here for infoworld at idg in this episode of smartgo we're going to
look at how you can write benchmarks to gauge the performance of your go code
as with testing which we covered in a previous video go standard library includes tools for
creating and running benchmarks typically done side by side with your test suite to demonstrate
this i'll pull out a new version of the code that i created in the last video this is my two ways of
computing a fibonacci sequence one uses machine native integers and the other uses goes math
big package we have a test suite for this file which should also look familiar but in the test
suite we now have some benchmark functions for the same code benchmarks and go work roughly the same
way tests do we create a function with a name that starts with benchmark notice the capital b
and then we follow that with another name that also starts with a capital letter the benchmark
function takes a single parameter of typetesting.b and contains a loop which iterates b dot n times
this is basically a mechanism by which go's testing framework can run a function a number of
times until its benchmark statistics converge on a consistent enough range of results so that the
outliers can be discarded the contents of the loop are just the functions that you want to benchmark
now to run these benchmarks we use the go test dash bench command along with
whatever files we want to run tests for a dot in this case means we'll run everything
when we run the benchmarks the benchmarking system first provides us with some stats about the system
and the package that we're benchmarking and then return stats about the performance of each
function that we've benchmarked as you can see the math big version of our fibonacci function is a
lot less performant than the plain integer version each op in the integer version takes far less time
to run and so we can run many more rounds of the function in the allotted time on the other hand
the big integer version doesn't cap out at the maximum size of a 64-bit editor so there is that
now when you run benchmarks you can also gather statistics about the performance of the
program's cpu and memory usage and save those to files for later analysis to do this we add
some options to the go test command including where to save our cpu and memory profile data
and once the benchmarking is done we can analyze the data with the go tool prof command and have
a look at the details and one small issue with the details i've just gathered here is that we
have statistics for these two different function benchmarks all wadded up together but we can run
only specific benchmarks by telling the dash bench switch which names of benchmarks to match
and so when we rerun our profile and to look only at the integer version of our function
we can see in the resulting profile a much cleaner breakdown and as you can see from the breakdown
our program spends the vast majority of its time in the actual fibonacci function the remaining
time is taken up with the overhead from the go runtime or the overhead from the testing framework
so this is a reasonably efficient function now if we run the benchmarks for the big integer
version of this program and then inspect those results we get a very different picture you can
see that the functions in the math.big package are taking up the vast majority of the time here
we are also spending a lot of time in the memory allocation and garbage collection
parts of the runtime so there may be some ways that we could perform further optimizations on
this incarnation of the function for instance to find a way to do less thrashing with memory
those are things that are going to be worth exploring in a future video though from here you
should get a basic idea of how benchmarking can work and go with just the tools that are available
in the base library that's it for this episode if you liked it please leave a comment below
don't forget to subscribe to the idg tech talk channel on youtube and for more smart go and
more dev with serdar be sure to follow me on facebook youtube and infoworld.com