A quick and lightweight benchmarking tool for Ruby
There are several tools for benchmarking Ruby code. Unfortunately, it's hard
to remember the way to set up the benchmarks, what format to use to represent
what to test, and how to perform the testing. One of the most popular tools
used for benchmarking Ruby code is benchmark-driver
, which was used heavily
by the Ruby 3x3 team. It uses three levels of abstraction and is powerful,
but there is a lot of inconsistency in naming conventions and using the tool.
The idea of winr
is to offer an easy to configure benchmarking tool that is
easy to use. There are three levels of abstraction:
Within an environment, context, or task, the following can be defined:
Configuration information is stored in a Ruby file, like the following:
$ cat test/sum.rb
{
environments: [
{
name: "Shiny new Ruby",
command: "/opt/ruby-3.2.0/bin/ruby",
},
{
name: "Old trusted Ruby",
command: "/opt/ruby-2.7.6/bin/ruby",
},
],
contexts: [
{
begin: <<~"|",
require "digest/md5"
max = 1e5.to_i
|
},
],
tasks: [
{
name: "array splat",
script: <<~"|",
ary = [*1..max]
sum = ary.sum
|
},
{
name: "times",
script: <<~"|",
sum = 0
max.to_i.times {|n| sum += n }
|
},
],
warmup: 3,
}
The benchmark is run as follows:
$ winr test/sum.rb
┌──────────────────┬───────────────────────────────────────┐
│ Shiny new Ruby │ Results │
├──────────────────┼───────────┬─────────────┬─────────────┤
│ array splat │ 2.99 s │ 768.19 i/s │ 1.30 ms/i │
│ times │ 7.86 s │ 292.33 i/s │ 3.42 ms/i │
└──────────────────┴───────────┴─────────────┴─────────────┘
┌──────────────────┬───────────────────────────────────────┐
│ Old trusted Ruby │ Results │
├──────────────────┼───────────┬─────────────┬─────────────┤
│ array splat │ 2.96 s │ 777.46 i/s │ 1.29 ms/i │
│ times │ 7.73 s │ 297.19 i/s │ 3.36 ms/i │
└──────────────────┴───────────┴─────────────┴─────────────┘
┌──────────────────┬─────────────────────────────────────┐
│ Rank │ Performance │
├──────────────────┼─────────────┬──────────────┬────────┤
│ array splat │ 777.46 i/s │ fastest │ 2/1/1 │
│ array splat │ 768.19 i/s │ 1.01x slower │ 1/1/1 │
│ times │ 297.19 i/s │ 2.62x slower │ 2/1/2 │
│ times │ 292.33 i/s │ 2.66x slower │ 1/1/2 │
└──────────────────┴─────────────┴──────────────┴────────┘
The following screenshot shows the output (notice the units and color), when the value of max
is changed to 1e3
.
Install via rubygems
with:
gem install winr
$ winr -h
usage: winr [options] <dir ...>
-c, --[no-]color Enable color output (default is true)
-d, --debug Enable debug mode
-h, --help Show help and command usage
-i, --iterations <count> Force the number of iterations for each task
-r, --reverse Show contexts vertically and tasks horizontally
-s, --stats Comma-separated list of stats (loops, time, ips, spi)
-v, --verbose Show command, version details, and markdown backticks
Available statistics:
ips iterations per second
loops number of iterations
spi seconds per iteration
time time to run all iterations
This software is licensed under terms of the MIT License.