I recently came across a great tool for benchmarking your APIs. It’s a nodejs tool written by Matteo Figus. Complete documentation on the tool could be found here

In this post I will provide simple tutorial for anyone to use this tool for their API’s

 

  • Create a folder and run navigate to the folder using your tool of choice for running node commands. I use Git Bash. Run the following command to install the api-benchmark package. This would require node to be installed beforehand.
$ npm install api-benchmark

 

  •  Now let’s add a new JavaScript file and name it as mybenchmark.js. We will require the benchmark tool
var apiBenchmark = require('api-benchmark');

 

  •  In this example we will use the Giphy API. Giphy is a GIF search engine. So let’s define a few variables that we will use.
var service = {
  server1: "http://api.giphy.com/"
};
var apiKey = 'dc6zaTOxFJmzC'; // public beta key

 

  •  Let’s add the routes which we want to test. In this example we will get the trending gifs.
var routes = {
		trending: {
			method: 'get',
			route: 'v1/gifs/trending?api_key=' + apiKey,
			headers: {
				'Accept': 'application/json'
			}
		}};

 

  •  And finally we run the benchmark
apiBenchmark.measure(service, routes, function(err, results){
	console.log(results);
});

 

  •  Here is the complete code of mybechmark.js for your convinence
var apiBenchmark = require('api-benchmark');

var service = {
  server1: "http://api.giphy.com/"
};
var apiKey = 'dc6zaTOxFJmzC'; // public beta key

var routes = {
		trending: {
			method: 'get',
			route: 'v1/gifs/trending?api_key=' + apiKey,
			headers: {
				'Accept': 'application/json'
			}
		}};

apiBenchmark.measure(service, routes, function(err, results){
	console.log(results);
});

 

  •  To see this in action we will run the benchmark by running the following command in your console.
$ node mybenchmark.js

 

  •  And you should see something like below.
Api-Benchmark 1

Api-Benchmark 1

 

 

  • This does show that our benchmark ran but we cannot interpret the results from here. To see that we will have to use the getHtml method available on api-benchmark.
apiBenchmark.measure(service, routes, function(err, results){
  apiBenchmark.getHtml(results, function(error, html){
    console.log(html);
  });
});

 

  •  However this will dump the entire html on the console which is nearly impossible to understand.
API Benchmark 2

API Benchmark 2

 

  • We will now save this html to a file so that we can view the results like the way it was intended. Let’s require another package to help us do that
var fs = require('fs');

 

  •  Change the apiBenchmark.measure to save the html in a file
apiBenchmark.measure(service, routes, function(err, results){
  apiBenchmark.getHtml(results, function(error, html){
    fs.writeFileSync('benchmarks.html', html);
  });
});

 

  •  To see this in action we will run the benchmark by running the following command in your console.
$ node mybenchmark.js

 

  • This would create a new html file (benchmarks.html) in your current folder with the results. It would look something like below. You see the details of your requests and your api is performing.
API Benchmark 3

API Benchmark Stats

 

  • It also has 2 more tabs which show Request Details and Response Details as well. All of this provides great insight into your APIs.
API Benchmark 4

API Benchmark Request Response

 

  • However I felt that if we could get the distribution of the api calls then it would provide deeper insight into my APIs. So I added a new tab to the report to showcase the distribtion of api calls overtime. The pull request is merged. So you would notice additional tab in the report i.e. distribution tab and you should see something like below

 

API Benchmark 5

API Benchmark Distribution

 

  • We could also specify the available options to benchmark the API’s deeply. Let’s try out a few
apiBenchmark.measure(service, routes, {
			debug: false,
			runMode: 'parallel',
			maxConcurrentRequests: 10,
			delay: 0,
			maxTime: 100000,
			minSamples: 100,
			stopOnError: false
		}, function(err, results){
	apiBenchmark.getHtml(results, function(error, html){
		fs.writeFileSync('benchmarks.html', html);
	});
});

 

API Benchmark Stats 100

API Benchmark Stats 100

 

API Benchmark Distribution 100

API Benchmark Distribution 100

 

Hope this helps you in getting started with api-benchmark. The entire source code for this post can be downloaded at googledrive or onedrive

 

Any questions, comments or feedback is always welcome.