March 2021
Over a year ago, my team and I set out to develop a new version of our application; a distributed set of Dockerized microservices to provide customers with a high availability and sleek web application for high speed data transfer. Throughout this process, I’ve learned quite a few lessons. I want to start sharing some of these lessons here to not only reflect on these lessons in the future, but share any tidbits I picked up for others.
Today, I want to talk about Ruby benchmarking. This stuff is so useful! And I honestly wish this was one of the first things I learned how to do in Ruby. So here’s Ruby benchmarking in a few short lines.
First, Ruby has a benchmarking library built into its standard library, which ya gotta love. No need to import some random library. You can start by throwing this on the top of the file you want to test:
require 'benchmark'
Easy as pie, right? Alright, now lets say we wanna test this snippet of code:
Array(1..100).map{ |num| num ** 2 }
Lets slap a Benchmark.measure around it:
Benchmark.measure {
num_of_tests.times do {
Array(1..100).map{ |num| num ** 2 }
}
}
This Benchmark.measure call will return four values in seconds:
Benchmark.measure.user)Benchmark.measure.system)Benchmark.measure.total)Benchmark.measure.real)To print these values you could do:
puts Benchmark.measure {
num_of_tests.times do {
Array(1..100).map{ |num| num ** 2 }
}
}
or you could access a specific value with:
stats = Benchmark.measure {
num_of_tests.times do {
Array(1..100).map{ |num| num ** 2 }
}
}
puts stats.real
This would output something like: 87.810000. You can come up with all sorts of statistics on individual runs if you’d like given you have the num_of_tests and a few different measurements for total time.
And that’s a quick rundown on Ruby’s benchmark library! Efficient, easy, and a game changer for developers. I only wish I knew about it sooner!