Tips: Go Unit Testing Performance & How to Debug

Jirawan.C
KBTG Life
Published in
5 min readNov 11, 2021

In this article, we are going to develop a simple program, and then create a unit test on the code using Go’s testing package and show you how to measure the performance of your code. Lastly, we will learn how to debug the test in VS code.

Let’s Create a Program

We will begin by creating a small program of which objective is to find a prime number (a number greater than 1 with only two factors — themselves and 1).

Step 1: Create a Work Directory

$ mkdir ./isPrime

Step 2: Create a File

Select the work directory and create a new file named isPrime.go

Step 3: Add the Following Code

In this step, we will write some code in main called isPrime function. The isPrime will calculate the input and return true if it is a prime number or false if proven otherwise.

Step 4: Go Mod Init & Go Mod Tidy

The go mod init command creates a go.mod file to track your code’s dependencies.

$ go mod init tipsTest// then go mod tidy to add module requirements and sums$ go mod tidy

Step 5: Run the Program

$ go run isPrime.go
The result here shows true because 13 is indeed a prime number

Writing a Unit Test

A unit test is a way to test a specific piece of code from a program or package to check whether it works correctly.

Step 1: Creating a Test File

Create file of which the suffix of file name must include _test.go For example: isPrime_test.go

Step 2: Create a Test Function in Go

Includes this signature: func TestXxxx(t *testing.T). The prefix of the function name must be Test , followed by a suffix with the first character capitalized. The function must receive only one parameter with t *testing.T

func TestIsPrime(t *testing.T)  {
//body
}

Step 3: Add the Following Code for Calling isPrime Function

In this example, the TestIsPrime function will call isPrime function in package main which was already declared.

Step 4: Run the Test

Run this command…

$ go test

…and you will receive the following output.

// output
PASS
ok tipsTest 0.404s

Another option is to click “run test” and the result will show “ok” if it passes.

PASS means the code is working as expected. When a test fails, you will see FAIL in the output.

If you would like to see which tests are running and how long each one takes, add a -v flag.

$ go test -v

You will see the following output:

// output
=== RUN TestIsPrime
--- PASS: TestIsPrime (0.00s)
PASS
ok tipsTest 0.134s

Writing Benchmarks

A benchmark is a test that is used to compare processes or methods, measuring the performance when you run the tests on PC.

Step 1: Create a Benchmark Function
In Go, name the function in this format: func BenchmarkXxx(*testing.B). The prefix of the function name must be Benchmark , followed by a suffix with the first character capitalized. Then, add the following code in IsPrimeBenchmark_test.go

func BenchmarkIsPrime(b *testing.B) {
for i := 0; i < b.N; i++ {
isPrime(9897)
}
}

Step 2: Run the Benchmark

$ go test -bench=.// output
goos: darwin
goarch: amd64
pkg: tipsTest
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkIsPrime-8 192724854 6.221 ns/op
PASS
ok tipsTest 2.233s

The result output means that the loop ran 192,724,854 times at a speed of 6.221 nanosecond per loop.

The . will match every benchmark function in a file.

Step 3: Measure the Performance

Let’s enhance isPrime for a better performance by adding the following code into isPrime2.go

Add this following code into IsPrimeBenchmark_test.go

Now let’s use the go test again to run our benchmark.

$ go test -bench=.//output
goos: darwin
goarch: amd64
pkg: tipsTest
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkIsPrime-8 193049048 6.229 ns/op 0 B/op 0 allocs/op
BenchmarkIsPrime2-8 193192003 6.223 ns/op 0 B/op 0 allocs/op
PASS
ok tipsTest 3.781s

As you can see, the result of BenchmarkIsPrime2 shows that the loop ran 193,192,003 times at a speed of 6.223 nanosecond per loop, whereas BenchmarkIsPrime ran at a speed of 6.229 nanosecond per loop but the loop only ran 193,049,048 times.

How to Debug Your Code With Visual Studio Code

Now I will show you how to debug your code when an error occurs using breakpoints. The debugging will show the value and state of your application at specific points in its code execution.

Step 1: Install dlv-dap

Install 1 tool at go/bin in module mode.

$ brew install go-delve/delve/delve

Step 2: Debugging With Breakpoints

Click the gutter at the executable line of code that suspends program execution at a specific point. In this case, I will add a breakpoint on line 14. When the debugger session is running, it will stop at that line. if you don’t want exception breakpoints, just click on it in the gutter to remove.

Step 3: Starting a Debug Test

You will see a debug test option at the top of the test function. Click on it to start the test and see the state of the variables on the left side panel.

This example will add a breakpoint on line 14. When the debugger session is running, it will stop at that line. Then you can click forward in the red block to go to the next line or click on the left side of that block to continue.

You can find the code sample here.

I hope that this guide is useful for you. If you love this article, please help me by giving “claps” 👏 Thanks for reading!

References:

Want to read more stories like this? Or catch up with the latest trends in the technology world? Be sure to check out our website for more at www.kbtg.tech

--

--

Jirawan.C
KBTG Life

Hi, My name's JUGJIG. I working as a software engineer @Fintech company. I’m Go developer since 2020. I’m English learner. Nice to see you