API Tester With Goroutines

As a fun project and a useful tool to see the power of an API, I decided to build a Go project that overloads any API. The reason I chose to use Go instead of Java, JS, or even Python was because Goroutines are incredibly low cost and fast. With Java or Python I would have to spawn threads which are limited by the CPU, but Goroutines can be mass produced without much overhead.

Building Blocks

We need to build structs to represent all the possible objects that compose HTTP responses.

For the response we would get from any normal API, we have an error if an error occurred, the status code, and the response data.

The URL struct which holds the URL path, the json data to be inputed, and the number of calls we want— usually 1000.

Finally, the method struct has the method (‘GET’, etc.), the extension onto the base URL, the number of calls to this specific URL, and its respective json data.

Concurrency

With concurrency, programming task that do not run in parallel but switch between N task many times really really fast, programmers have to battle issues like deadlocks and starvation. These issues arise when channels do not release signals at critical times or have no idea when to stop listening.

Concurrency With GO

Go has built-in concurrent functionality with its Goroutines. The keyword ‘GO’ spawns a concurrent process that run along side the main Goroutine.

Starting most projects in GO that involve concurrency it is best to create a channel “done” that accepts and empty interface — accepting any data type. The purpose of the done channel is to signal to all other channels to stop receiving/sending data and close shop. Also its best practice to defer closing channels or anything that needs to be closed for the program to exit gracefully, as defer handles closing all open channels or anything else by the end of the method it is called in. I also created the response stream that is a channel of Response objects — this will be where we will read the response data of our mass api calls.

To see the Full repo —

Software Dev

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