Benchmark your API
Introduction
If you have ever created an API endpoint using plumbr or flask you may have wondered how to do some simple load testing…
Here is a simple way to test this using an R script.
Define a function that calls your api with some parameters
Depending on what your api does you may want to test different parameters altogether, for our case we will just make a simple call using fixed parameters:
library(rvest)
# ptm <- proc.time()
call_serv <- function(){
read_html("http://34.242.232.117:8000/predict?vore=carni&bodywt=85&brainwt=0.325&awake=17.8")
}
# proc.time() - ptm
Now we can leverage the microbenchmark package to measure the response time of the API.
To also test the load handling we will leverage the doParallel process to spin up multiple workers each sending requests to our API on the remote server:
library(microbenchmark)
library(doParallel)
cores <- 10
cl <- makeCluster(cores)
registerDoParallel(cl)
clusterEvalQ(cl, library(microbenchmark))
clusterEvalQ(cl, library(rvest))
# ptm <- proc.time()
api_res <- foreach(f =1:10, .combine = "rbind") %dopar% {
library(microbenchmark)
library(rvest)
x <- microbenchmark(
calls= call_serv() ,
times = 50L
)
print(x)
}
# proc.time() - ptm
stopCluster(cl)
Conclusion
By varying the parameters of the json request and the number of requests we can inspect the performance of our API in order to adjust the remote server specification