benchmark_wrapper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +89 -0
- data/Rakefile +6 -0
- data/benchmark_wrapper.gemspec +25 -0
- data/lib/benchmark_wrapper.rb +45 -0
- data/lib/benchmark_wrapper/version.rb +3 -0
- data/spec/lib/benchmark_wrapper_spec.rb +90 -0
- data/spec/spec_helper.rb +23 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d3453361e87d0cb94b05c407523ce5055d15823d
|
4
|
+
data.tar.gz: cdfdd4407b524906893f6244fe445b4d3026fd4b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 257a85f8bcbe3daa014734b2baa2cc35b932a837a9eb4b207243280ee82105f8d7e2a70e6f0b50b20fb790ae922b7e9c4ebeee69b2832892f11484a6af5af505
|
7
|
+
data.tar.gz: 022299bff2dad99b7bb17e841ccd27826a808a98ebed55a359709d729f86bc958e79666b40cc0394301ad15974cf65bb36da6ff25a8439373770c65f1cfaa235
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 LFDM
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# BenchmarkWrapper
|
2
|
+
[![Build Status](https://travis-ci.org/LFDM/benchmark_wrapper.png)](https://travis-ci.org/LFDM/benchmark_wrapper)
|
3
|
+
[![Coverage Status](https://coveralls.io/repos/LFDM/benchmark_wrapper/badge.png)](https://coveralls.io/r/LFDM/benchmark_wrapper)
|
4
|
+
[![Dependency Status](https://gemnasium.com/LFDM/benchmark_wrapper.png)](https://gemnasium.com/LFDM/benchmark_wrapper)
|
5
|
+
|
6
|
+
Extend your class to wrap a benchmark around whatever method you want.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'benchmark_wrapper'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install benchmark_wrapper
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'benchmark_wrapper'
|
26
|
+
|
27
|
+
class A
|
28
|
+
extend BenchmarkWrapper
|
29
|
+
|
30
|
+
def method1; 1; end
|
31
|
+
|
32
|
+
def method2(arg); arg + yield; end
|
33
|
+
|
34
|
+
wrap_with_benchmark :method1, :method2
|
35
|
+
end
|
36
|
+
|
37
|
+
a = A.new
|
38
|
+
|
39
|
+
a.method1
|
40
|
+
# 0.000000 0.000000 0.000000 ( 0.000010)
|
41
|
+
# => 1
|
42
|
+
|
43
|
+
a.method2(1) { 1 }
|
44
|
+
# 0.000000 0.000000 0.000000 ( 0.000008)
|
45
|
+
# => 2
|
46
|
+
```
|
47
|
+
|
48
|
+
It's important to define the methods you want to be benchmarked before
|
49
|
+
you wrap them. The following will not work:
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class A
|
53
|
+
extend BenchmarkWrapper
|
54
|
+
|
55
|
+
wrap_with_benchmark :method1
|
56
|
+
|
57
|
+
def method1; 1; end
|
58
|
+
end
|
59
|
+
|
60
|
+
# NameError: undefined method `method1' for class `A'
|
61
|
+
```
|
62
|
+
|
63
|
+
Output defaults to $stdout but can be redirected:
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
RESULTS = []
|
67
|
+
|
68
|
+
class B
|
69
|
+
extend BenchmarkWrapper
|
70
|
+
|
71
|
+
def method1; 1; end
|
72
|
+
|
73
|
+
wrap_with_benchmark :method1, out: RESULTS, out_method: :<<
|
74
|
+
end
|
75
|
+
|
76
|
+
B.new.method1
|
77
|
+
# => 1
|
78
|
+
|
79
|
+
RESULTS
|
80
|
+
# => [#<Benchmark::Tms ...>]
|
81
|
+
```
|
82
|
+
|
83
|
+
## Contributing
|
84
|
+
|
85
|
+
1. Fork it
|
86
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
87
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
88
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
89
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'benchmark_wrapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "benchmark_wrapper"
|
8
|
+
spec.version = BenchmarkWrapper::VERSION
|
9
|
+
spec.authors = ["LFDM"]
|
10
|
+
spec.email = ["1986gh@gmail.com"]
|
11
|
+
spec.description = %q{Benchmarking module}
|
12
|
+
spec.summary = %q{Module to extend classes to wrap certain method calls with benchmarking}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov", "~> 0.7"
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "benchmark_wrapper/version"
|
2
|
+
require "benchmark"
|
3
|
+
|
4
|
+
module BenchmarkWrapper
|
5
|
+
def wrap_with_benchmark(*meths)
|
6
|
+
opts = extract_benchmark_receiver_options(meths)
|
7
|
+
out = opts[:out]
|
8
|
+
out_method = opts[:out_method]
|
9
|
+
|
10
|
+
meths.each do |meth|
|
11
|
+
without_bm, with_bm = wrapper_methods(meth)
|
12
|
+
|
13
|
+
define_method(with_bm) do |*args, &blk|
|
14
|
+
# obscenely ugly, but Benchmark class seems
|
15
|
+
# to have nothing to avoid this
|
16
|
+
ret_val = nil
|
17
|
+
bm = Benchmark.measure { ret_val = send(without_bm, *args, &blk) }
|
18
|
+
out.send(out_method, bm)
|
19
|
+
ret_val
|
20
|
+
end
|
21
|
+
|
22
|
+
alias_method without_bm, meth
|
23
|
+
alias_method meth, with_bm
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def extract_benchmark_receiver_options(arr)
|
30
|
+
opts = arr.last.kind_of?(Hash) ? arr.pop : {}
|
31
|
+
default_bm_receiver.merge(opts)
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_bm_receiver
|
35
|
+
# would love to have this inside a constant,
|
36
|
+
# but it would get untestable this way!
|
37
|
+
{ out: $stdout, out_method: :puts}
|
38
|
+
end
|
39
|
+
|
40
|
+
def wrapper_methods(meth)
|
41
|
+
['', 'out'].map do |str|
|
42
|
+
"#{meth}_with#{str}_benchmark".to_sym
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'stringio'
|
3
|
+
|
4
|
+
describe BenchmarkWrapper do
|
5
|
+
before :all do
|
6
|
+
string_io = StringIO.new
|
7
|
+
@stdout = $stdout
|
8
|
+
$stdout = string_io
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:dummy) do
|
12
|
+
Class.new do
|
13
|
+
extend BenchmarkWrapper
|
14
|
+
|
15
|
+
def test1; 1; end
|
16
|
+
def test2; 2; end
|
17
|
+
def test_with_arg(x); 1 + x; end
|
18
|
+
def test_with_blk; yield; end
|
19
|
+
def test_with_arg_and_blk(x); x + yield; end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should have a version number' do
|
24
|
+
BenchmarkWrapper::VERSION.should_not be_nil
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#wrap_with_benchmark" do
|
28
|
+
# Behavior tests
|
29
|
+
context 'with the default receiver $stdout' do
|
30
|
+
it "wraps a given method with a benchmark" do
|
31
|
+
dummy.wrap_with_benchmark(:test1)
|
32
|
+
obj = dummy.new
|
33
|
+
$stdout.should receive(:puts)
|
34
|
+
obj.test1
|
35
|
+
end
|
36
|
+
|
37
|
+
it "wrapped methods return their original return value" do
|
38
|
+
dummy.wrap_with_benchmark(:test1)
|
39
|
+
dummy.new.test1.should == 1
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can wrap multiple methods at once" do
|
43
|
+
dummy.wrap_with_benchmark(:test1, :test2)
|
44
|
+
obj = dummy.new
|
45
|
+
$stdout.should receive(:puts).twice
|
46
|
+
obj.test1.should == 1
|
47
|
+
obj.test2.should == 2
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'with a given receiver' do
|
52
|
+
it "defers the benchmark result to the given receiver" do
|
53
|
+
container = []
|
54
|
+
dummy.wrap_with_benchmark(:test1, out: container, out_method: :<<)
|
55
|
+
dummy.new.test1
|
56
|
+
container.should have(1).item
|
57
|
+
container.first.should be_an_instance_of Benchmark::Tms
|
58
|
+
end
|
59
|
+
|
60
|
+
it "can wrap multiple methods at once" do
|
61
|
+
container = []
|
62
|
+
dummy.wrap_with_benchmark(:test1, :test2, out: container, out_method: :<<)
|
63
|
+
obj = dummy.new
|
64
|
+
obj.test1.should == 1
|
65
|
+
obj.test2.should == 2
|
66
|
+
container.should have(2).items
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "doesn't fail when method take arguments" do
|
71
|
+
dummy.wrap_with_benchmark(:test_with_arg)
|
72
|
+
dummy.new.test_with_arg(0).should == 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it "doesn't fail when method takes a block" do
|
76
|
+
dummy.wrap_with_benchmark(:test_with_blk)
|
77
|
+
dummy.new.test_with_blk { 3 }.should == 3
|
78
|
+
end
|
79
|
+
|
80
|
+
it "doesn't fail when method takes argument and block" do
|
81
|
+
dummy.wrap_with_benchmark(:test_with_arg_and_blk)
|
82
|
+
dummy.new.test_with_arg_and_blk(1) { 4 }.should == 5
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
after :all do
|
88
|
+
$stdout = @stdout
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
require 'coveralls'
|
3
|
+
|
4
|
+
Coveralls.wear!
|
5
|
+
|
6
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
7
|
+
SimpleCov::Formatter::HTMLFormatter,
|
8
|
+
Coveralls::SimpleCov::Formatter
|
9
|
+
]
|
10
|
+
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter '/spec/'
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
17
|
+
require 'benchmark_wrapper'
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
21
|
+
config.run_all_when_everything_filtered = true
|
22
|
+
config.filter_run :focus
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benchmark_wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- LFDM
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.7'
|
69
|
+
description: Benchmarking module
|
70
|
+
email:
|
71
|
+
- 1986gh@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- benchmark_wrapper.gemspec
|
84
|
+
- lib/benchmark_wrapper.rb
|
85
|
+
- lib/benchmark_wrapper/version.rb
|
86
|
+
- spec/lib/benchmark_wrapper_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
homepage: ''
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.1.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Module to extend classes to wrap certain method calls with benchmarking
|
112
|
+
test_files:
|
113
|
+
- spec/lib/benchmark_wrapper_spec.rb
|
114
|
+
- spec/spec_helper.rb
|