Skip to content
master
Go to file
Code

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
bin
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Benchable

Write benchmarks without the hassle.

Installation

Add this line to your application's Gemfile:

gem 'benchable'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install benchable

Usage

Basic usage

Use the method Benchable.bench to declare a benchmark. Write each benchmark case with the bench method. The benchmark will run automatically.

Benchable.bench do
  bench 'sort' do
    (1..1000000).map { rand }.sort 
  end

  bench 'sort!' do
    (1..1000000).map { rand }.sort!
  end
end
# Output:
#                            user     system      total        real
# Sort                   0.483720   0.003975   0.487695 (  0.487695)
# Sort!                  0.477415   0.000009   0.477424 (  0.477409)

You can write a setup method to DRY up any logic.

Important: The setup method runs only once before all benchs, so be careful with mutation inside your benchs.

Benchable.bench do
  setup do
    @array = (1..1000000).map { rand }
  end

  bench 'sort' do
    @array.dup.sort 
  end

  bench 'sort!' do
    @array.dup.sort!
  end
end
# Output:
#                            user     system      total        real
# Sort                   0.400133   0.011995   0.412128 (  0.412339)
# Sort!                  0.388636   0.003980   0.392616 (  0.393054)

We've used Array#dup in the example above to prevent the benchmarks for modifying the original array

Benchmark types

There are 3 benchmark types available: bm, bmbm and ips. You can specify it by passing the type as a symbol on the Benchable.bench method. The default type is bm.

Benchable.bench(:bm) do
  # ...
end

Benchable.bench(:bmbm) do
  # ...
end

Benchable.bench(:ips) do
  # ...
end

Given an invalid benchmark type, Benchable will raise an exception.

Benchable.bench(:invalid) do
  # ...
end
# => Benchable::Error (Invalid benchmark type 'invalid')

Benchmark options

You can provide benchmark options by passing a hash to the Benchable.bench method.

Options for Benchmark.bm and Benchmark.bmbm

On bm and bmbm benchmarks the only available option is width, which specifies the leading spaces for labels on each line. The default width is 20.

Benchable.bench(width: 25) do
  # ...
end

Options for Benchmark::IPS

If you're using, you can pass any option accepted by Benchmark::IPS config method.

Benchable.bench(:ips, time: 5, warmup: 2) do
  # 
end
# Output:
# Warming up --------------------------------------
#                 Sort     1.000  i/100ms
#                Sort!     1.000  i/100ms
# Calculating -------------------------------------
#                 Sort      2.114  (± 0.0%) i/s -     11.000  in   5.205127s
#                Sort!      2.120  (± 0.0%) i/s -     11.000  in   5.189772s

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/MatheusRich/benchable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

Acknowledgments

Thanks @naomik for building the bease idea for this in his gist!

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Benchable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Write benchmarks without the hassle.

Topics

Resources

License

Packages

No packages published
You can’t perform that action at this time.