benchable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/tests-and-linter.yml +28 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +21 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +128 -0
- data/Guardfile +72 -0
- data/LICENSE.txt +21 -0
- data/README.md +139 -0
- data/Rakefile +8 -0
- data/benchable.gemspec +31 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/benchable.rb +18 -0
- data/lib/benchable/benchmark.rb +71 -0
- data/lib/benchable/version.rb +5 -0
- metadata +84 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7c0476be4799e0045af57ce9be72401caf7acc7a0de12f0288d02b5c0dac1be5
|
4
|
+
data.tar.gz: edfd89e36cbb1e2d63d16d20f2b40ae8c441580d97bee9bdd5fc4710afd62bd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46ce119b1f4d4991e1e022b3b79449b857d60cdc635afa435983b3cab2a1719a5cf6586faa6f2685627592fae6badff8e0110e254a99067183e89721788dd899
|
7
|
+
data.tar.gz: dbf498e5e3fc4aa94846f0cb4e701bf97f456e68c4bd68af5bb577338228efa56328ef1676cc1f55369874026c71d70e0f2b7c0c75026c87973761fae10180cc
|
@@ -0,0 +1,28 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [master]
|
6
|
+
pull_request:
|
7
|
+
branches: [master]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
build:
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
strategy:
|
13
|
+
matrix:
|
14
|
+
ruby: [2.4, 2.5, 2.6, 2.7]
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@v2
|
18
|
+
- name: Set up Ruby ${{ matrix.ruby }}
|
19
|
+
uses: actions/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{ matrix.ruby }}
|
22
|
+
- name: Build and test with Rake
|
23
|
+
run: |
|
24
|
+
gem install bundler
|
25
|
+
bundle install --jobs 4 --retry 3
|
26
|
+
bundle exec rake
|
27
|
+
- name: Rubocop Linter Action
|
28
|
+
run: bundle exec rubocop --parallel
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-rspec
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
NewCops: enable
|
6
|
+
|
7
|
+
Layout/LineLength:
|
8
|
+
Max: 120
|
9
|
+
|
10
|
+
Metrics/BlockLength:
|
11
|
+
Exclude:
|
12
|
+
- "spec/**/*"
|
13
|
+
- "Guardfile"
|
14
|
+
|
15
|
+
Style/Documentation:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Style/DocumentationMethod:
|
19
|
+
Enabled: false
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
All notable changes to this project will be documented in this file.
|
4
|
+
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
7
|
+
|
8
|
+
## [Unreleased]
|
9
|
+
|
10
|
+
<!-- ### Added -->
|
11
|
+
<!-- ### Changed -->
|
12
|
+
<!-- ### Removed -->
|
13
|
+
---
|
14
|
+
|
15
|
+
## [0.1.0] - 2020-08-16
|
16
|
+
|
17
|
+
### Added
|
18
|
+
- Support for `Benchmark.bm`, `Benchmark.bmbm` and `Benchmark.ips`.
|
19
|
+
|
20
|
+
[unreleased]: https://github.com/MatheusRich/benchable/compare/v0.1.0...HEAD
|
21
|
+
[0.1.0]: https://github.com/MatheusRich/benchable/releases/tag/v0.1.0
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at matheusrichardt@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in benchable.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem 'guard-rspec', require: false
|
9
|
+
gem 'rake', '~> 12.0'
|
10
|
+
gem 'rspec', '~> 3.0'
|
11
|
+
gem 'rubocop', '~> 0.89.1'
|
12
|
+
gem 'rubocop-rspec', '~> 1.42', require: false
|
13
|
+
gem 'simplecov', '~> 0.18.5', require: false
|
14
|
+
gem 'solargraph', '~> 0.39.14'
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
benchable (0.1.0)
|
5
|
+
benchmark-ips (~> 2.8, >= 2.8.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
ast (2.4.1)
|
11
|
+
backport (1.1.2)
|
12
|
+
benchmark (0.1.0)
|
13
|
+
benchmark-ips (2.8.2)
|
14
|
+
coderay (1.1.3)
|
15
|
+
diff-lcs (1.4.4)
|
16
|
+
docile (1.3.2)
|
17
|
+
e2mmap (0.1.0)
|
18
|
+
ffi (1.13.1)
|
19
|
+
formatador (0.2.5)
|
20
|
+
guard (2.16.2)
|
21
|
+
formatador (>= 0.2.4)
|
22
|
+
listen (>= 2.7, < 4.0)
|
23
|
+
lumberjack (>= 1.0.12, < 2.0)
|
24
|
+
nenv (~> 0.1)
|
25
|
+
notiffany (~> 0.0)
|
26
|
+
pry (>= 0.9.12)
|
27
|
+
shellany (~> 0.0)
|
28
|
+
thor (>= 0.18.1)
|
29
|
+
guard-compat (1.2.1)
|
30
|
+
guard-rspec (4.7.3)
|
31
|
+
guard (~> 2.1)
|
32
|
+
guard-compat (~> 1.1)
|
33
|
+
rspec (>= 2.99.0, < 4.0)
|
34
|
+
jaro_winkler (1.5.4)
|
35
|
+
listen (3.2.1)
|
36
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
37
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
38
|
+
lumberjack (1.2.7)
|
39
|
+
maruku (0.7.3)
|
40
|
+
method_source (1.0.0)
|
41
|
+
mini_portile2 (2.4.0)
|
42
|
+
nenv (0.3.0)
|
43
|
+
nokogiri (1.10.10)
|
44
|
+
mini_portile2 (~> 2.4.0)
|
45
|
+
notiffany (0.1.3)
|
46
|
+
nenv (~> 0.1)
|
47
|
+
shellany (~> 0.0)
|
48
|
+
parallel (1.19.2)
|
49
|
+
parser (2.7.1.4)
|
50
|
+
ast (~> 2.4.1)
|
51
|
+
pry (0.13.1)
|
52
|
+
coderay (~> 1.1)
|
53
|
+
method_source (~> 1.0)
|
54
|
+
rainbow (3.0.0)
|
55
|
+
rake (12.3.3)
|
56
|
+
rb-fsevent (0.10.4)
|
57
|
+
rb-inotify (0.10.1)
|
58
|
+
ffi (~> 1.0)
|
59
|
+
regexp_parser (1.7.1)
|
60
|
+
reverse_markdown (2.0.0)
|
61
|
+
nokogiri
|
62
|
+
rexml (3.2.4)
|
63
|
+
rspec (3.9.0)
|
64
|
+
rspec-core (~> 3.9.0)
|
65
|
+
rspec-expectations (~> 3.9.0)
|
66
|
+
rspec-mocks (~> 3.9.0)
|
67
|
+
rspec-core (3.9.2)
|
68
|
+
rspec-support (~> 3.9.3)
|
69
|
+
rspec-expectations (3.9.2)
|
70
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
71
|
+
rspec-support (~> 3.9.0)
|
72
|
+
rspec-mocks (3.9.1)
|
73
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
+
rspec-support (~> 3.9.0)
|
75
|
+
rspec-support (3.9.3)
|
76
|
+
rubocop (0.89.1)
|
77
|
+
parallel (~> 1.10)
|
78
|
+
parser (>= 2.7.1.1)
|
79
|
+
rainbow (>= 2.2.2, < 4.0)
|
80
|
+
regexp_parser (>= 1.7)
|
81
|
+
rexml
|
82
|
+
rubocop-ast (>= 0.3.0, < 1.0)
|
83
|
+
ruby-progressbar (~> 1.7)
|
84
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
85
|
+
rubocop-ast (0.3.0)
|
86
|
+
parser (>= 2.7.1.4)
|
87
|
+
rubocop-rspec (1.42.0)
|
88
|
+
rubocop (>= 0.87.0)
|
89
|
+
ruby-progressbar (1.10.1)
|
90
|
+
shellany (0.0.1)
|
91
|
+
simplecov (0.18.5)
|
92
|
+
docile (~> 1.1)
|
93
|
+
simplecov-html (~> 0.11)
|
94
|
+
simplecov-html (0.12.2)
|
95
|
+
solargraph (0.39.14)
|
96
|
+
backport (~> 1.1)
|
97
|
+
benchmark
|
98
|
+
bundler (>= 1.17.2)
|
99
|
+
e2mmap
|
100
|
+
jaro_winkler (~> 1.5)
|
101
|
+
maruku (~> 0.7, >= 0.7.3)
|
102
|
+
nokogiri (~> 1.9, >= 1.9.1)
|
103
|
+
parser (~> 2.3)
|
104
|
+
reverse_markdown (>= 1.0.5, < 3)
|
105
|
+
rubocop (~> 0.52)
|
106
|
+
thor (~> 1.0)
|
107
|
+
tilt (~> 2.0)
|
108
|
+
yard (~> 0.9, >= 0.9.24)
|
109
|
+
thor (1.0.1)
|
110
|
+
tilt (2.0.10)
|
111
|
+
unicode-display_width (1.7.0)
|
112
|
+
yard (0.9.25)
|
113
|
+
|
114
|
+
PLATFORMS
|
115
|
+
ruby
|
116
|
+
|
117
|
+
DEPENDENCIES
|
118
|
+
benchable!
|
119
|
+
guard-rspec
|
120
|
+
rake (~> 12.0)
|
121
|
+
rspec (~> 3.0)
|
122
|
+
rubocop (~> 0.89.1)
|
123
|
+
rubocop-rspec (~> 1.42)
|
124
|
+
simplecov (~> 0.18.5)
|
125
|
+
solargraph (~> 0.39.14)
|
126
|
+
|
127
|
+
BUNDLED WITH
|
128
|
+
2.1.4
|
data/Guardfile
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# A sample Guardfile
|
4
|
+
# More info at https://github.com/guard/guard#readme
|
5
|
+
|
6
|
+
## Uncomment and set this to only include directories you want to watch
|
7
|
+
# directories %w(app lib config test spec features) \
|
8
|
+
# .select{|d| Dir.exist?(d) ? d : UI.warning("Directory #{d} does not exist")}
|
9
|
+
|
10
|
+
## Note: if you are using the `directories` clause above and you are not
|
11
|
+
## watching the project directory ('.'), then you will want to move
|
12
|
+
## the Guardfile to a watched dir and symlink it back, e.g.
|
13
|
+
#
|
14
|
+
# $ mkdir config
|
15
|
+
# $ mv Guardfile config/
|
16
|
+
# $ ln -s config/Guardfile .
|
17
|
+
#
|
18
|
+
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"
|
19
|
+
|
20
|
+
# Note: The cmd option is now required due to the increasing number of ways
|
21
|
+
# rspec may be run, below are examples of the most common uses.
|
22
|
+
# * bundler: 'bundle exec rspec'
|
23
|
+
# * bundler binstubs: 'bin/rspec'
|
24
|
+
# * spring: 'bin/rspec' (This will use spring if running and you have
|
25
|
+
# installed the spring binstubs per the docs)
|
26
|
+
# * zeus: 'zeus rspec' (requires the server to be started separately)
|
27
|
+
# * 'just' rspec: 'rspec'
|
28
|
+
|
29
|
+
guard :rspec, cmd: 'bundle exec rspec' do
|
30
|
+
require 'guard/rspec/dsl'
|
31
|
+
dsl = Guard::RSpec::Dsl.new(self)
|
32
|
+
|
33
|
+
# Feel free to open issues for suggestions and improvements
|
34
|
+
|
35
|
+
# RSpec files
|
36
|
+
rspec = dsl.rspec
|
37
|
+
watch(rspec.spec_helper) { rspec.spec_dir }
|
38
|
+
watch(rspec.spec_support) { rspec.spec_dir }
|
39
|
+
watch(rspec.spec_files)
|
40
|
+
|
41
|
+
# Ruby files
|
42
|
+
ruby = dsl.ruby
|
43
|
+
dsl.watch_spec_files_for(ruby.lib_files)
|
44
|
+
|
45
|
+
# Rails files
|
46
|
+
rails = dsl.rails(view_extensions: %w[erb haml slim])
|
47
|
+
dsl.watch_spec_files_for(rails.app_files)
|
48
|
+
dsl.watch_spec_files_for(rails.views)
|
49
|
+
|
50
|
+
watch(rails.controllers) do |m|
|
51
|
+
[
|
52
|
+
rspec.spec.call("routing/#{m[1]}_routing"),
|
53
|
+
rspec.spec.call("controllers/#{m[1]}_controller"),
|
54
|
+
rspec.spec.call("acceptance/#{m[1]}")
|
55
|
+
]
|
56
|
+
end
|
57
|
+
|
58
|
+
# Rails config changes
|
59
|
+
watch(rails.spec_helper) { rspec.spec_dir }
|
60
|
+
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
|
61
|
+
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }
|
62
|
+
|
63
|
+
# Capybara features specs
|
64
|
+
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
|
65
|
+
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }
|
66
|
+
|
67
|
+
# Turnip features and steps
|
68
|
+
watch(%r{^spec/acceptance/(.+)\.feature$})
|
69
|
+
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
|
70
|
+
Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance'
|
71
|
+
end
|
72
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Matheus Richard
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# Benchable
|
2
|
+
|
3
|
+
Write benchmarks without the hassle.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'benchable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install benchable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
### Basic usage
|
23
|
+
Use the method `Benchable.bench` to declare a benchmark. Write each benchmark case with the `bench` method. The benchmark will run automatically.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Benchable.bench do
|
27
|
+
bench 'sort' do
|
28
|
+
(1..1000000).map { rand }.sort
|
29
|
+
end
|
30
|
+
|
31
|
+
bench 'sort!' do
|
32
|
+
(1..1000000).map { rand }.sort!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
# Output:
|
36
|
+
# user system total real
|
37
|
+
# Sort 0.483720 0.003975 0.487695 ( 0.487695)
|
38
|
+
# Sort! 0.477415 0.000009 0.477424 ( 0.477409)
|
39
|
+
```
|
40
|
+
|
41
|
+
You can write a setup method to DRY up any logic.
|
42
|
+
|
43
|
+
**Important:** The setup method runs **only once** before **all** benchs, so be careful with mutation inside your benchs.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
Benchable.bench do
|
47
|
+
setup do
|
48
|
+
@array = (1..1000000).map { rand }
|
49
|
+
end
|
50
|
+
|
51
|
+
bench 'sort' do
|
52
|
+
@array.dup.sort
|
53
|
+
end
|
54
|
+
|
55
|
+
bench 'sort!' do
|
56
|
+
@array.dup.sort!
|
57
|
+
end
|
58
|
+
end
|
59
|
+
# Output:
|
60
|
+
# user system total real
|
61
|
+
# Sort 0.400133 0.011995 0.412128 ( 0.412339)
|
62
|
+
# Sort! 0.388636 0.003980 0.392616 ( 0.393054)
|
63
|
+
```
|
64
|
+
> We've used `Array#dup` in the example above to prevent the benchmarks for modifying the original array
|
65
|
+
|
66
|
+
### Benchmark types
|
67
|
+
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`.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
Benchable.bench(:bm) do
|
71
|
+
# ...
|
72
|
+
end
|
73
|
+
|
74
|
+
Benchable.bench(:bmbm) do
|
75
|
+
# ...
|
76
|
+
end
|
77
|
+
|
78
|
+
Benchable.bench(:ips) do
|
79
|
+
# ...
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
Given an invalid benchmark type, Benchable will raise an exception.
|
84
|
+
```ruby
|
85
|
+
Benchable.bench(:invalid) do
|
86
|
+
# ...
|
87
|
+
end
|
88
|
+
# => Benchable::Error (Invalid benchmark type 'invalid')
|
89
|
+
```
|
90
|
+
|
91
|
+
### Benchmark options
|
92
|
+
You can provide benchmark options by passing a hash to the `Benchable.bench` method.
|
93
|
+
|
94
|
+
### Options for `Benchmark.bm` and `Benchmark.bmbm`
|
95
|
+
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`.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
Benchable.bench(width: 25) do
|
99
|
+
# ...
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
### Options for `Benchmark::IPS`
|
104
|
+
If you're using, you can pass any option accepted by `Benchmark::IPS` `config` method.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
Benchable.bench(:ips, time: 5, warmup: 2) do
|
108
|
+
#
|
109
|
+
end
|
110
|
+
# Output:
|
111
|
+
# Warming up --------------------------------------
|
112
|
+
# Sort 1.000 i/100ms
|
113
|
+
# Sort! 1.000 i/100ms
|
114
|
+
# Calculating -------------------------------------
|
115
|
+
# Sort 2.114 (± 0.0%) i/s - 11.000 in 5.205127s
|
116
|
+
# Sort! 2.120 (± 0.0%) i/s - 11.000 in 5.189772s
|
117
|
+
```
|
118
|
+
|
119
|
+
## Development
|
120
|
+
|
121
|
+
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.
|
122
|
+
|
123
|
+
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).
|
124
|
+
|
125
|
+
## Contributing
|
126
|
+
|
127
|
+
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).
|
128
|
+
|
129
|
+
|
130
|
+
## Acknowledgments
|
131
|
+
Thanks [@naomik](https://github.com/naomik) for building the bease idea for this in [his gist](https://gist.github.com/naomik/6012505)!
|
132
|
+
|
133
|
+
## License
|
134
|
+
|
135
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
136
|
+
|
137
|
+
## Code of Conduct
|
138
|
+
|
139
|
+
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).
|
data/Rakefile
ADDED
data/benchable.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/benchable/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'benchable'
|
7
|
+
spec.version = Benchable::VERSION
|
8
|
+
spec.authors = ['Matheus Richard']
|
9
|
+
spec.email = ['matheusrichardt@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Write benchmarks without the hassle'
|
12
|
+
# spec.description = %q{TODO: Write a longer description or delete this line.}
|
13
|
+
spec.homepage = 'https://github.com/MatheusRich/benchable'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
|
16
|
+
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/MatheusRich/benchable'
|
19
|
+
spec.metadata['changelog_uri'] = 'https://github.com/MatheusRich/benchable/blob/master/CHANGELOG.md'
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'benchmark-ips', '~> 2.8', '>= 2.8.2'
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'benchable'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/benchable.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'benchable/benchmark'
|
4
|
+
require_relative 'benchable/version'
|
5
|
+
|
6
|
+
module Benchable
|
7
|
+
class Error < StandardError; end
|
8
|
+
|
9
|
+
def self.build(type = :bm, **options, &block)
|
10
|
+
bench_class = Class.new(Benchmark, &block)
|
11
|
+
|
12
|
+
bench_class.new(type, **options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.bench(type = :bm, **options, &block)
|
16
|
+
build(type, **options, &block).run
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
require 'benchmark/ips'
|
5
|
+
|
6
|
+
module Benchable
|
7
|
+
class Benchmark
|
8
|
+
DEFAULT_WIDTH = 20
|
9
|
+
BENCHMARK_TYPES = %i[bm bmbm ips].freeze
|
10
|
+
|
11
|
+
def initialize(benchmark_type, options = {})
|
12
|
+
@benchmark_type = benchmark_type
|
13
|
+
@options = options
|
14
|
+
|
15
|
+
raise Error, "Invalid benchmark type '#{benchmark_type}'" unless valid_benchmark_type?
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.setup(&block)
|
19
|
+
define_method(:setup, &block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.bench(name, &block)
|
23
|
+
define_method(method_name_for(name), &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
def setup; end
|
27
|
+
|
28
|
+
def run
|
29
|
+
setup
|
30
|
+
run_benchmark
|
31
|
+
end
|
32
|
+
|
33
|
+
def cases
|
34
|
+
public_methods.grep(/\Abench_/)
|
35
|
+
end
|
36
|
+
|
37
|
+
private_class_method def self.method_name_for(name)
|
38
|
+
"bench_#{name.to_s.gsub(' ', '_').downcase}"
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
attr_reader :benchmark_type, :options
|
44
|
+
|
45
|
+
def valid_benchmark_type?
|
46
|
+
BENCHMARK_TYPES.include? benchmark_type
|
47
|
+
end
|
48
|
+
|
49
|
+
def run_benchmark
|
50
|
+
benchmark do |with|
|
51
|
+
with.config(**options) if benchmark_type == :ips
|
52
|
+
|
53
|
+
cases.each do |benchmark_case|
|
54
|
+
with.report(name_for(benchmark_case)) do
|
55
|
+
method(benchmark_case).call
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def name_for(benchmark_case)
|
62
|
+
benchmark_case.to_s.gsub('bench_', '').gsub('_', ' ').capitalize
|
63
|
+
end
|
64
|
+
|
65
|
+
def benchmark(&block)
|
66
|
+
width = options[:width] || DEFAULT_WIDTH
|
67
|
+
|
68
|
+
::Benchmark.public_send(benchmark_type, width, &block)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benchable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matheus Richard
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: benchmark-ips
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.8'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.8.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.8'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.8.2
|
33
|
+
description:
|
34
|
+
email:
|
35
|
+
- matheusrichardt@gmail.com
|
36
|
+
executables: []
|
37
|
+
extensions: []
|
38
|
+
extra_rdoc_files: []
|
39
|
+
files:
|
40
|
+
- ".github/workflows/tests-and-linter.yml"
|
41
|
+
- ".gitignore"
|
42
|
+
- ".rspec"
|
43
|
+
- ".rubocop.yml"
|
44
|
+
- CHANGELOG.md
|
45
|
+
- CODE_OF_CONDUCT.md
|
46
|
+
- Gemfile
|
47
|
+
- Gemfile.lock
|
48
|
+
- Guardfile
|
49
|
+
- LICENSE.txt
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- benchable.gemspec
|
53
|
+
- bin/console
|
54
|
+
- bin/setup
|
55
|
+
- lib/benchable.rb
|
56
|
+
- lib/benchable/benchmark.rb
|
57
|
+
- lib/benchable/version.rb
|
58
|
+
homepage: https://github.com/MatheusRich/benchable
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata:
|
62
|
+
homepage_uri: https://github.com/MatheusRich/benchable
|
63
|
+
source_code_uri: https://github.com/MatheusRich/benchable
|
64
|
+
changelog_uri: https://github.com/MatheusRich/benchable/blob/master/CHANGELOG.md
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
require_paths:
|
68
|
+
- lib
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: 2.4.0
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
79
|
+
requirements: []
|
80
|
+
rubygems_version: 3.1.2
|
81
|
+
signing_key:
|
82
|
+
specification_version: 4
|
83
|
+
summary: Write benchmarks without the hassle
|
84
|
+
test_files: []
|