Benchmark with Golang

WBOY
Release: 2024-09-05 21:31:03
Original
843 people have browsed it

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 < 2) 
   F(0) = 1
   F(2) = 2
else 
   F(x) = F(x-1) + F(x-2)

In practice, the sequence is:
1, 1, 2, 3, 5, 8, 13, etc.
Copy after login

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 <= 2 {
        return 1
    }
    return fibRecursive(n-1) + fibRecursive(n-2)
}
Copy after login

Iterative Method

// main.go

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

    if position <= 2 {
        return 1
    }

    var result, i uint
    for i = 2; i < position; i++ {
        result = slc[i-1] + slc[i-2]
        slc[i] = result
    }

    return result
}
Copy after login

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 < b.N; i++ { 
        fibIterative(uint(100))
    }
}

// Same as above
func BenchmarkFibRecursive(b *testing.B) {
    for i := 0; i < b.N; i++ {
        fibRecursive(100)
    }
}
Copy after login

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
Copy after login

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 ====
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

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.

The above is the detailed content of Benchmark with Golang. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!