Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/MatheusRich/benchable
Write benchmarks without the hassle.
https://github.com/MatheusRich/benchable
benchmark benchmark-framework hacktoberfest ruby
Last synced: about 2 months ago
JSON representation
Write benchmarks without the hassle.
- Host: GitHub
- URL: https://github.com/MatheusRich/benchable
- Owner: MatheusRich
- License: mit
- Created: 2020-08-12T01:36:34.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2023-10-04T17:27:12.000Z (over 1 year ago)
- Last Synced: 2024-10-31T13:54:59.331Z (2 months ago)
- Topics: benchmark, benchmark-framework, hacktoberfest, ruby
- Language: Ruby
- Homepage:
- Size: 44.9 KB
- Stars: 76
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Benchable
Write benchmarks without the hassle.
## Installation
Add this line to your application's Gemfile:
```ruby
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.
```ruby
Benchable.bench do
bench 'sort' do
(1..1000000).map { rand }.sort
endbench '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.
```ruby
Benchable.bench do
setup do
@array = (1..1000000).map { rand }
endbench 'sort' do
@array.dup.sort
endbench '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
Four benchmark types are available: `bm`, `bmbm`, `ips` and `memory`. You can specify the type by passing it as a symbol on the `Benchable.bench` method. The default type is `bm`.
```ruby
Benchable.bench(:bm) do
# ...
endBenchable.bench(:bmbm) do
# ...
endBenchable.bench(:ips) do
# ...
endBenchable.bench(:memory) do
# ...
end
```You can also run multiple benchmarks at once:
```ruby
Benchable.bench(:ips, :memory) do
# ...
end
```Given an invalid benchmark type, Benchable will raise an exception.
```ruby
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`
The only available option is `width` on `bm` and `bmbm` benchmarks, which specifies the leading spaces for labels on each line. The default width is `20`.
```ruby
Benchable.bench(width: 25) do
# ...
end
```#### Options for `Benchmark::IPS`
If you're using `::IPS`, you can pass any option accepted by `Benchmark::IPS`'s `config` method.
```ruby
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
```#### Options for `Benchmark::Memory`
You can pass [any option accepted](https://github.com/michaelherold/benchmark-memory#options) by `Benchmark::Memory`.
```ruby
Benchable.bench(:memory, quiet: true) do
# ...
end
# Output:
# => #```
## 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](https://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](https://github.com/MatheusRich/benchable/blob/master/CODE_OF_CONDUCT.md).
## Acknowledgments
Thanks [@naomik](https://github.com/naomik) for building the base idea for this in [his gist](https://gist.github.com/naomik/6012505)!
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## 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](https://github.com/MatheusRich/benchable/blob/master/CODE_OF_CONDUCT.md).