benches 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/Gemfile +3 -0
- data/Gemfile.lock +18 -0
- data/README.md +32 -0
- data/benches.gemspec +21 -0
- data/bin/benchpress +8 -0
- data/lib/benches/dsl.rb +32 -0
- data/lib/benches/routine.rb +20 -0
- data/lib/benches/version.rb +3 -0
- data/lib/benches.rb +3 -0
- data/license.md +9 -0
- data/spec/dsl_spec.rb +73 -0
- data/spec/routine_spec.rb +63 -0
- data/spec/spec_helper.rb +14 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0a31536816bbaf808af25fe99966c2d8968f6c6
|
4
|
+
data.tar.gz: d826ff23eebf397bf7c853ab12b226e67163a52e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b12382125632d996603f544e0f3ce547c5db085eaf28d5282b060ce6168271c3bbb7f131a657b5d37f9d29d6fc716416b4d8f0f48d39419e53b55bc094b91999
|
7
|
+
data.tar.gz: 7c9f41a9839e4a08081414f26b5b909f70bbbbdfa57c7a6114a16d1410db7ecbcebf1151066f88791daa84ec6f2984d6a269489ce9f6d721046eb660cd793341
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.2.5)
|
5
|
+
rspec (2.14.1)
|
6
|
+
rspec-core (~> 2.14.0)
|
7
|
+
rspec-expectations (~> 2.14.0)
|
8
|
+
rspec-mocks (~> 2.14.0)
|
9
|
+
rspec-core (2.14.8)
|
10
|
+
rspec-expectations (2.14.5)
|
11
|
+
diff-lcs (>= 1.1.3, < 2.0)
|
12
|
+
rspec-mocks (2.14.6)
|
13
|
+
|
14
|
+
PLATFORMS
|
15
|
+
ruby
|
16
|
+
|
17
|
+
DEPENDENCIES
|
18
|
+
rspec
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Benches
|
2
|
+
|
3
|
+
Benches defines a simple benchmarking DSL that allows you to create benchmark routines for your Ruby code.
|
4
|
+
|
5
|
+
## Defining a Routine
|
6
|
+
|
7
|
+
Create a folder called regimens. For each class that you want to benchmark,
|
8
|
+
create a file like integer.rb
|
9
|
+
|
10
|
+
integer_routine.rb
|
11
|
+
```ruby
|
12
|
+
routine 4, 'benches 400 reps of to_s' # no arguments
|
13
|
+
routine 30, 'benches 300 reps of * with (400)' # one argument
|
14
|
+
routine 'test_string', "benches 500 reps of gsub with (s, '')" # two arguments
|
15
|
+
```
|
16
|
+
|
17
|
+
This will return the benchmark results of running `4.to_s` 400 times,
|
18
|
+
`30 * 400` 300 times, and `'test_string'.gsub('s', '')` 500 times.
|
19
|
+
|
20
|
+
The command `benchpress` will run all of the regimens.
|
21
|
+
|
22
|
+
## Testing
|
23
|
+
|
24
|
+
Simply run `rspec` to run the test suite.
|
25
|
+
|
26
|
+
## Future Work
|
27
|
+
|
28
|
+
- Allow benchmark results to be tested in block passed to routine
|
29
|
+
|
30
|
+
## Credits
|
31
|
+
|
32
|
+
All code (c) Evan Hemsley 2014
|
data/benches.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "benches/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "benches"
|
7
|
+
s.version = Benches::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Evan Hemsley"]
|
10
|
+
s.email = ["evan.hemsley@gmail.com"]
|
11
|
+
s.homepage = 'http://github.com/ehemsley/benches'
|
12
|
+
s.summary = 'A simple DSL for benchmarking your Ruby code'
|
13
|
+
s.description = 'Benches defines a simple benchmarking DSL that allows you to create benchmark routines for your Ruby code.'
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.license = 'MIT'
|
21
|
+
end
|
data/bin/benchpress
ADDED
data/lib/benches/dsl.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#input: instance_name benches n reps of method_name (with *args)
|
2
|
+
#example: graph benches 50000 reps of dfs
|
3
|
+
def routine(instance, input)
|
4
|
+
params = input.split(" ")
|
5
|
+
if params.last == params[4]
|
6
|
+
args = nil
|
7
|
+
else
|
8
|
+
args = coerce_args(input[/\(.*?\)/].gsub(/[()]/, "").split(","))
|
9
|
+
end
|
10
|
+
|
11
|
+
Benches::Routine.new(instance, params[4], params[1].to_i, *args).call
|
12
|
+
end
|
13
|
+
|
14
|
+
def coerce_args(args)
|
15
|
+
args.map do |arg|
|
16
|
+
if integer?(arg)
|
17
|
+
arg.to_i
|
18
|
+
elsif float?(arg)
|
19
|
+
arg.to_f
|
20
|
+
else
|
21
|
+
arg
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def integer?(input)
|
27
|
+
!!(/^\d+$/ =~ input)
|
28
|
+
end
|
29
|
+
|
30
|
+
def float?(input)
|
31
|
+
!!(/^\d+(.\d+)$/ =~ input)
|
32
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Benches
|
2
|
+
class Routine
|
3
|
+
def initialize(instance, method, repetitions, *args)
|
4
|
+
@instance = instance
|
5
|
+
@method = method
|
6
|
+
@repetitions = repetitions
|
7
|
+
@args = args
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
Benchmark.bm(@method.length) do |x|
|
12
|
+
x.report("#{@method}:") do
|
13
|
+
@repetitions.times do
|
14
|
+
@instance.send(@method, *@args)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/benches.rb
ADDED
data/license.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) [2014] [Evan Hemsley]
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/spec/dsl_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'routine' do
|
4
|
+
context 'no method arguments' do
|
5
|
+
it 'returns benchmark result' do
|
6
|
+
expect((routine 'test_string', 'benches 20 reps of split').first).to be_a Benchmark::Tms
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'method has integer argument' do
|
11
|
+
it 'returns benchmark result' do
|
12
|
+
expect((routine 'test_string', "benches 20 reps of insert with (0,a)").first).to be_a Benchmark::Tms
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'method has float argument' do
|
17
|
+
it 'returns benchmark result' do
|
18
|
+
expect((routine 32.444, 'benches 20 reps of + with (4.43)').first).to be_a Benchmark::Tms
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'float?' do
|
24
|
+
context 'input string contains only a float' do
|
25
|
+
it 'returns true' do
|
26
|
+
expect(float?('32.555')).to eql true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'input string contains letters and numbers' do
|
31
|
+
it 'returns false' do
|
32
|
+
expect(float?('ab321')).to eql false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'input string contains an integer' do
|
37
|
+
it 'returns false' do
|
38
|
+
expect(float?('32')).to eql false
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'input string does not contain any numbers' do
|
43
|
+
it 'returns false' do
|
44
|
+
expect(float?('stuff')).to eql false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe 'integer?' do
|
50
|
+
context 'input string contains only an integer' do
|
51
|
+
it 'returns true' do
|
52
|
+
expect(integer?('32')).to eql true
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context 'input string contains a float' do
|
57
|
+
it 'returns false' do
|
58
|
+
expect(integer?('32.423')).to eql false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'input string contains letters and numbers' do
|
63
|
+
it 'returns false' do
|
64
|
+
expect(integer?('abc123')).to eql false
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'input string does not contain any numbers' do
|
69
|
+
it 'returns false' do
|
70
|
+
expect(integer?('abc')).to eql false
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Benches::Routine do
|
4
|
+
describe 'initialize' do
|
5
|
+
context 'no method arguments' do
|
6
|
+
let(:bench) { Benches::Routine.new("test_string", 'split', 50000) }
|
7
|
+
|
8
|
+
it 'assigns instance correctly' do
|
9
|
+
expect(bench.instance_variable_get('@instance')).to eql 'test_string'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'assigns method correctly' do
|
13
|
+
expect(bench.instance_variable_get('@method')).to eql 'split'
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'assigns repetitions correctly' do
|
17
|
+
expect(bench.instance_variable_get('@repetitions')).to eql 50000
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'sets args to empty array' do
|
21
|
+
expect(bench.instance_variable_get('@args')).to eql []
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'method has arguments' do
|
26
|
+
let(:bench) { Benches::Routine.new("test_string", 'insert', 50000, 1, 'a') }
|
27
|
+
|
28
|
+
it 'assigns instance correctly' do
|
29
|
+
expect(bench.instance_variable_get('@instance')).to eql 'test_string'
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'assigns method correctly' do
|
33
|
+
expect(bench.instance_variable_get('@method')).to eql 'insert'
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'assigns repetitions correctly' do
|
37
|
+
expect(bench.instance_variable_get('@repetitions')).to eql 50000
|
38
|
+
end
|
39
|
+
|
40
|
+
it' assigns args correctly' do
|
41
|
+
expect(bench.instance_variable_get('@args')).to eql [1, 'a']
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe 'call' do
|
47
|
+
context 'no method arguments' do
|
48
|
+
let(:bench) { Benches::Routine.new("test_string", 'split', 20) }
|
49
|
+
|
50
|
+
it 'performs benchmark' do
|
51
|
+
expect(bench.call.first).to be_a Benchmark::Tms
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'method has arguments' do
|
56
|
+
let(:bench) { Benches::Routine.new("test_string", 'insert', 20, 1, 'a') }
|
57
|
+
|
58
|
+
it 'performs benchmark' do
|
59
|
+
expect(bench.call.first).to be_a Benchmark::Tms
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
$:<< File.join(File.dirname(__FILE__), '..')
|
2
|
+
require 'lib/benches'
|
3
|
+
|
4
|
+
RSpec.configure do |config|
|
5
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
6
|
+
config.run_all_when_everything_filtered = true
|
7
|
+
config.filter_run :focus
|
8
|
+
|
9
|
+
config.order = 'random'
|
10
|
+
|
11
|
+
config.color_enabled = true
|
12
|
+
config.tty = true
|
13
|
+
config.formatter = :documentation
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benches
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Hemsley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Benches defines a simple benchmarking DSL that allows you to create benchmark
|
14
|
+
routines for your Ruby code.
|
15
|
+
email:
|
16
|
+
- evan.hemsley@gmail.com
|
17
|
+
executables:
|
18
|
+
- benchpress
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- Gemfile
|
23
|
+
- Gemfile.lock
|
24
|
+
- README.md
|
25
|
+
- benches.gemspec
|
26
|
+
- bin/benchpress
|
27
|
+
- lib/benches.rb
|
28
|
+
- lib/benches/dsl.rb
|
29
|
+
- lib/benches/routine.rb
|
30
|
+
- lib/benches/version.rb
|
31
|
+
- license.md
|
32
|
+
- spec/dsl_spec.rb
|
33
|
+
- spec/routine_spec.rb
|
34
|
+
- spec/spec_helper.rb
|
35
|
+
homepage: http://github.com/ehemsley/benches
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata: {}
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 2.2.2
|
56
|
+
signing_key:
|
57
|
+
specification_version: 4
|
58
|
+
summary: A simple DSL for benchmarking your Ruby code
|
59
|
+
test_files:
|
60
|
+
- spec/dsl_spec.rb
|
61
|
+
- spec/routine_spec.rb
|
62
|
+
- spec/spec_helper.rb
|