TDD & Benchmarking in GO

In this blog, we shall be looking at following concepts :-

  • Testing the functions written in GO.

Question:- How do we Test the functions written in Go ?

Step #1.) Let’s first create a file called as “sqrt.go” and define the name of package. We also import some errors package into the same :-

Step #2.) In this file, we shall declare two variables, to which we shall be using throughout our codebase :-

Step #3.) We also write a function to find the absolute value of the given number :-

Step #4.) We also write a write a function to compute the square-root of the given number :- We shall be testing this particular method going forward :-

Step #5.) Next, we create yet another file under same package and create following method into it :-

Step #6.) Now, we write a simple method named “TestSimple” and test it out as shown below :-

  • At line #11, we have pointer to “testing.T” being present into it.
go test <FILE_CONTAINING_TESTCASE> <FILE_CONTAINING_METHOD> -v

Question:- How do we Test a given function with multiple values in one method itself ?

  • Let’s write a method “TestMany” to test the function with multiple values.

Question:- How do we execute a particular TestCase in GO ?

go test -run <TEST_METHOD_NAME> <FILE_CONTAINING_TESTCASE> <FILE_CONTAINING_METHOD> -v

Question:- Demonstrate the process of reading the test-scenarios from the Excel file and then executing them for the afore-shown Square-Root Function.

Step #1.) Here is the CSV file, to which we are planning to read for the tes-cases :-

Step #2.) Since, we shall be reading the CSV file, we go ahead and import the package @ line #4, as shown in below snapshot :-

Step #3.) Let’s start writing our TestMethod to test-out various use-cases, as shown in the excel file :-

Step #4.) We write the Infinite Loop to read through all records in the Excel file. Below code-piece is self-explanatory :-

Step #5.) Now, let’s run the above test-cases, by running below command :-

go test sqrt_test.go sqrt.go -v

Question:- Whats the need of Benchmarking the code in GO ?

Answer:- When you write code, you sometimes want to benchmark it and make sure that you’re not making it slower when adding features or fixing bugs.

Question :- Is there a way to do benchmarking / profiling of functions written in GO ?

Answer:- We create a benchmark in a test file ending with _test.go. And we call it Benchmark and then the name of the benchmark. It gets one parameter, which is a pointer to testing.B.

  • So what we do in the benchmark is we iterate b.N times calling our function.

Question :- Whats the command to do Benchmarking in GO ?

Answer:- By default, go test, doesn’t executes benchmarks. So, we have to specify explicitly that, we wanted to run the tests :-

go test sqrt_test.go sqrt.go -v -bench . -run TTT
  • dot means all the benchmarks.

And we are running ~88 nanoseconds per operation.

Question :- Whats the main advantages of Profiling like this ?

Answer:-

  • Before optimizing a program, you need to profile it to see where it spends its time.

Question :- Can we know @ line level granularity that, how much time is being taken by each line to be executed ?

Answer:- Yes, that’s possible :-

  • We’ll do the benchmark and we say — Cpuprofile=prof.out.

That’s all in this section. If you liked reading this blog, kindly do press on clap button multiple times, to indicate your appreciation. We would see you in next series.

References :-

--

--

Software Engineer for Big Data distributed systems

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store