"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Benchmark with Golang

Benchmark with Golang

Published on 2024-11-04
Browse:436

Hello Gophers ?

In this blog post, I will show you how to use an awesome tool built into the #golang testing package. How would you test the performance of a piece of code or a function? Use benchmark tests.

Let’s go.

For this test, I'll be using the classic Fibonacci Number or Fibonacci Sequence, which is determined by:

if (x 



This sequence is important because it appears in several parts of mathematics and nature as well, as shown below:

Benchmark with Golang

There are several ways to implement this code, and I'll be picking two for our benchmark testing: the recursive and iterative methods of calculating it. The main objective of the functions is to provide a position and return the Fibonacci number at that position.

Recursive Method

// main.go

func fibRecursive(n int) int {
    if n 



Iterative Method

// main.go

func fibIterative(position uint) uint {
    slc := make([]uint, position)
    slc[0] = 1
    slc[1] = 1

    if position 



These methods are not optimized, but the results of the tests are significantly different even for a small number. You'll see this in the tests. To follow along with the code, you can click here.

Now, for the benchmark tests, let’s write some tests in the _main_test.go file. Using Golang's documentation on benchmark, you can create the functions to be tested as follows:

// main_test.go

// The key is to start every function you want to benchmark with the keyword Benchmark and use b *testing.B instead of t *testing.T as input 
func BenchmarkFibIterative(b *testing.B) {
    // Use this for-loop to ensure the code will behave correctly. 
    // Now, you can put the function or piece of code you want to test
    for i := 0; i 



Question, before you go on: which one is faster?

Let's run the test for a small number (10) and for a slightly bigger number (80). To run the benchmark tests, you simply run the command:

go test -bench=NameoftheFunction

If you want to know more about this command, check here.

First test: position=10

//(fibIterative)
Results:
cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         24491042                42.50 ns/op
PASS
ok      playground      1.651s

Let’s analyze with the help of this image:

Benchmark with Golang

According to the image, we have 8 cores for the tests, no time limit (it will run until completion). It took 1.651s to complete the task.

==== Extra ====
We got 24,491,042 iterations (computations), and each iteration (op) took 42.50 ns.

Doing some math, we can calculate how much time one op took:

42.50 ns/op with 1 ns = 1/1,000,000,000 s
op ≈ 2.35270590588e-12 s
==== Extra ====

That’s a good result. Let’s check the recursive function for position 10:

// Results
BenchmarkFibRecursive-8          6035011               187.8 ns/op
PASS
ok      playground      1.882s

We can see that it took 1.882s to complete the task.

The iterative function won by a few deciseconds. Let’s try one more test with:

Position 50

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8         27896118                45.37 ns/op
PASS
ok      playground      2.876s

// Results for the Recursive Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8          6365198               186.3 ns/op
PASS
ok      playground      1.918s

Wow! Now the recursive function is faster?

Let’s finish with a slightly larger number.

Position 80

// Results for the Iterative Function

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibIterative-8          5102344               229.5 ns/op
PASS
ok      playground      1.933s

// Results for the Recursive Function
// My poor PC couldn’t handle it, so I had to reduce the position to 50 just to get some results printed.

cpu: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
BenchmarkFibRecursive-8                1        44319299474 ns/op
PASS
ok      playground      44.951s

The difference is huge. For position 80, the iterative approach took approximately 2 seconds. For position 50, the recursive function took around 45 seconds. This demonstrates the importance of benchmarking your code when your Golang project starts to slow down.

Conclusion

If your production code is running slowly or is unpredictably slower, you can use this technique, combined with pprof or other tools from the built-in testing package, to identify and test where your code is performing poorly and how to optimize it.

Side note: not all code that is beautiful to the eyes is more performant.

Extra Exercise

Can you find a better way to improve the recursive function? (Tip: use Dynamic Programming). This article explains why for some small numbers, the recursive strategy is better.

Release Statement This article is reproduced at: https://dev.to/pedrobertao/benchmark-with-golang-md4?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3