benchmeth 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -17
- data/CHANGELOG.md +7 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +22 -0
- data/README.md +41 -61
- data/Rakefile +7 -1
- data/benchmeth.gemspec +22 -14
- data/lib/benchmeth.rb +25 -22
- data/lib/benchmeth/version.rb +1 -1
- data/test/benchmeth_test.rb +12 -0
- data/test/test_helper.rb +31 -0
- metadata +75 -14
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 06caf16f70d677e519ca32bb7c1074fd691435c0
|
4
|
+
data.tar.gz: d2008e32f84b5027d3cccc740928aee6e1fdc333
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a2f7f3bf7f8d966ccb45814e77b8576fe308e96ff5e7b27e2e9de95ab17976a14b8b02631e6ed23673bd51a3a8d80225d33ea08f1ee5e196011f13ec38a26f3b
|
7
|
+
data.tar.gz: 5f686662e3d458cf66e46d4345e2f1f8939755484086611544922c6e66d48373f2039e04b67ea4badb3a019da5616b1ddc8c3c54c1efd74d1a5d7d8bc73e7393
|
data/.gitignore
CHANGED
@@ -1,17 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.bundle
|
11
|
+
*.so
|
12
|
+
*.o
|
13
|
+
*.a
|
14
|
+
mkmf.log
|
data/CHANGELOG.md
ADDED
data/Gemfile
CHANGED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Andrew Kane
|
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
CHANGED
@@ -1,24 +1,40 @@
|
|
1
1
|
# Benchmeth
|
2
2
|
|
3
|
-
The super easy way to benchmark methods
|
3
|
+
The super easy way to benchmark methods in a live application
|
4
4
|
|
5
5
|
```ruby
|
6
|
-
|
6
|
+
class Person
|
7
|
+
def compute
|
8
|
+
# boom
|
9
|
+
end
|
10
|
+
benchmark :compute
|
11
|
+
end
|
7
12
|
```
|
8
13
|
|
9
|
-
|
14
|
+
Works with class methods, too
|
10
15
|
|
11
16
|
```ruby
|
12
|
-
|
13
|
-
|
17
|
+
class Person
|
18
|
+
def self.compute
|
19
|
+
# yolo
|
20
|
+
end
|
21
|
+
class << self
|
22
|
+
benchmark :compute
|
23
|
+
end
|
14
24
|
end
|
25
|
+
```
|
15
26
|
|
16
|
-
|
27
|
+
## Installation
|
17
28
|
|
18
|
-
|
29
|
+
Add this line to your application’s Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'benchmeth'
|
19
33
|
```
|
20
34
|
|
21
|
-
|
35
|
+
## How to Use
|
36
|
+
|
37
|
+
By default, benchmark data is written to `stdout` in the following format:
|
22
38
|
|
23
39
|
```
|
24
40
|
compute : 1000 ms
|
@@ -27,71 +43,35 @@ compute : 1000 ms
|
|
27
43
|
but you can easily do whatever you want with it.
|
28
44
|
|
29
45
|
```ruby
|
30
|
-
Benchmeth.on_benchmark do |
|
31
|
-
|
32
|
-
# The default is:
|
33
|
-
# puts "%s : %d ms" % [method, realtime * 1000]
|
46
|
+
Benchmeth.on_benchmark do |method_name, seconds|
|
47
|
+
puts "#{method_name} took #{seconds} seconds!"
|
34
48
|
end
|
35
49
|
```
|
36
50
|
|
37
|
-
To call a method without benchmarking,
|
51
|
+
To call a method without benchmarking, append `_without_benchmark` to the name.
|
38
52
|
|
39
|
-
|
40
|
-
compute_without_benchmark
|
41
|
-
```
|
53
|
+
## ActiveSupport Notifications
|
42
54
|
|
43
|
-
|
55
|
+
You can switch to ActiveSupport notifications with:
|
44
56
|
|
45
57
|
```ruby
|
46
|
-
|
47
|
-
|
48
|
-
def work(seconds)
|
49
|
-
puts "Working for #{seconds} seconds"
|
50
|
-
sleep(seconds)
|
51
|
-
end
|
52
|
-
|
53
|
-
def play
|
54
|
-
puts "Time to play!"
|
55
|
-
sleep(rand * 4)
|
56
|
-
end
|
57
|
-
|
58
|
-
# This must come after the methods are defined.
|
59
|
-
benchmark :work, :play
|
60
|
-
|
61
|
-
end
|
62
|
-
|
63
|
-
person = Person.new
|
64
|
-
person.work(1)
|
65
|
-
person.play
|
58
|
+
Benchmeth.use_notifications = true
|
66
59
|
```
|
67
60
|
|
68
|
-
|
69
|
-
|
70
|
-
```
|
71
|
-
Person#work : 1000 ms
|
72
|
-
Person#play : 500 ms
|
73
|
-
```
|
74
|
-
|
75
|
-
## Class Methods
|
61
|
+
And subscribe with:
|
76
62
|
|
77
63
|
```ruby
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
puts "Found person #{id}!"
|
82
|
-
end
|
83
|
-
|
84
|
-
class << self
|
85
|
-
benchmark :find
|
86
|
-
end
|
87
|
-
|
64
|
+
ActiveSupport::Notifications.subscribe "benchmark.benchmeth" do |*args|
|
65
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
66
|
+
puts "%s : %d ms" % [event.payload[:name], event.duration]
|
88
67
|
end
|
89
|
-
|
90
|
-
Person.find(1)
|
91
68
|
```
|
92
69
|
|
93
|
-
|
70
|
+
## Contributing
|
94
71
|
|
95
|
-
|
96
|
-
|
97
|
-
|
72
|
+
Everyone is encouraged to help improve this project. Here are a few ways you can help:
|
73
|
+
|
74
|
+
- [Report bugs](https://github.com/ankane/benchmeth/issues)
|
75
|
+
- Fix bugs and [submit pull requests](https://github.com/ankane/benchmeth/pulls)
|
76
|
+
- Write, clarify, or fix documentation
|
77
|
+
- Suggest or add new features
|
data/Rakefile
CHANGED
data/benchmeth.gemspec
CHANGED
@@ -1,17 +1,25 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "benchmeth/version"
|
3
5
|
|
4
|
-
Gem::Specification.new do |
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "benchmeth"
|
8
|
+
spec.version = Benchmeth::VERSION
|
9
|
+
spec.authors = ["Andrew Kane"]
|
10
|
+
spec.email = ["andrew@chartkick.com"]
|
11
|
+
spec.summary = "The super easy way to benchmark methods"
|
12
|
+
spec.description = "The super easy way to benchmark methods"
|
13
|
+
spec.homepage = "https://github.com/ankane/benchmeth"
|
14
|
+
spec.license = "MIT"
|
10
15
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |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"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "minitest"
|
24
|
+
spec.add_development_dependency "activesupport"
|
17
25
|
end
|
data/lib/benchmeth.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
1
|
require "benchmeth/version"
|
2
|
-
require "benchmark"
|
3
2
|
|
4
3
|
# :( Global
|
5
4
|
$benchmeth_main = self
|
6
5
|
|
7
6
|
module Benchmeth
|
8
|
-
|
9
|
-
|
7
|
+
class << self
|
8
|
+
attr_accessor :use_notifications
|
10
9
|
end
|
10
|
+
self.use_notifications = false
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
base.send :include, InstanceMethods
|
12
|
+
DEFAULT_BLOCK = lambda do |method, realtime|
|
13
|
+
puts "%s : %d ms" % [method, realtime * 1000]
|
15
14
|
end
|
16
15
|
|
17
16
|
def self.on_benchmark(&block)
|
@@ -23,42 +22,46 @@ module Benchmeth
|
|
23
22
|
end
|
24
23
|
|
25
24
|
module ClassMethods
|
26
|
-
|
27
25
|
def benchmark(*method_names)
|
28
26
|
method_names.each do |method_name|
|
29
27
|
method_name = method_name.to_sym
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
method_prefix = nil
|
34
|
-
realtime = Benchmark.realtime do
|
35
|
-
result = self.send(:"#{method_name}_without_benchmark", *args)
|
36
|
-
method_prefix =
|
28
|
+
send :alias_method, :"#{method_name}_without_benchmark", method_name
|
29
|
+
send :define_method, method_name do |*args, &block|
|
30
|
+
method_prefix =
|
37
31
|
case self
|
38
32
|
when $benchmeth_main
|
39
33
|
""
|
40
34
|
when Class
|
41
|
-
"#{
|
35
|
+
"#{name}."
|
42
36
|
else
|
43
37
|
"#{self.class.name}#"
|
44
38
|
end
|
39
|
+
|
40
|
+
if Benchmeth.use_notifications
|
41
|
+
payload = {
|
42
|
+
name: "#{method_prefix}#{method_name}"
|
43
|
+
}
|
44
|
+
ActiveSupport::Notifications.instrument "benchmark.benchmeth", payload do
|
45
|
+
send(:"#{method_name}_without_benchmark", *args, &block)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
start_time = Time.now
|
49
|
+
result = send(:"#{method_name}_without_benchmark", *args, &block)
|
50
|
+
realtime = Time.now - start_time
|
51
|
+
Benchmeth.on_benchmark.call("#{method_prefix}#{method_name}", realtime)
|
52
|
+
result
|
45
53
|
end
|
46
|
-
Benchmeth.on_benchmark.call("#{method_prefix}#{method_name}", realtime)
|
47
|
-
result
|
48
54
|
end
|
49
55
|
end
|
50
56
|
end
|
51
|
-
|
52
57
|
end
|
53
58
|
|
54
59
|
module InstanceMethods
|
55
|
-
|
56
60
|
def benchmark(*method_names, &block)
|
57
61
|
self.class.benchmark(*method_names, &block)
|
58
62
|
end
|
59
|
-
|
60
63
|
end
|
61
|
-
|
62
64
|
end
|
63
65
|
|
64
|
-
Object.
|
66
|
+
Object.extend Benchmeth::ClassMethods
|
67
|
+
Object.send :include, Benchmeth::InstanceMethods
|
data/lib/benchmeth/version.rb
CHANGED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require "bundler/setup"
|
2
|
+
Bundler.require(:default)
|
3
|
+
require "minitest/autorun"
|
4
|
+
require "minitest/pride"
|
5
|
+
require "active_support"
|
6
|
+
|
7
|
+
class Car
|
8
|
+
def start(speed)
|
9
|
+
sleep(1)
|
10
|
+
end
|
11
|
+
benchmark :start
|
12
|
+
|
13
|
+
def boom(speed)
|
14
|
+
sleep(1)
|
15
|
+
end
|
16
|
+
benchmark :boom #, payload: -> (speed) { speed * 10000000 }
|
17
|
+
|
18
|
+
def self.boom2
|
19
|
+
sleep(1)
|
20
|
+
end
|
21
|
+
class << self
|
22
|
+
benchmark :boom2
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
ActiveSupport::Notifications.subscribe "benchmark.benchmeth" do |*args|
|
27
|
+
event = ActiveSupport::Notifications::Event.new(*args)
|
28
|
+
puts "[AS] %s : %d ms" % [event.payload[:name], event.duration]
|
29
|
+
end
|
30
|
+
|
31
|
+
# Benchmeth.use_notifications = true
|
metadata
CHANGED
@@ -1,52 +1,113 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: benchmeth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Andrew Kane
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
11
|
+
date: 2017-03-31 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: minitest
|
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: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
14
69
|
description: The super easy way to benchmark methods
|
15
70
|
email:
|
16
|
-
- andrew@
|
71
|
+
- andrew@chartkick.com
|
17
72
|
executables: []
|
18
73
|
extensions: []
|
19
74
|
extra_rdoc_files: []
|
20
75
|
files:
|
21
|
-
- .gitignore
|
76
|
+
- ".gitignore"
|
77
|
+
- CHANGELOG.md
|
22
78
|
- Gemfile
|
79
|
+
- LICENSE.txt
|
23
80
|
- README.md
|
24
81
|
- Rakefile
|
25
82
|
- benchmeth.gemspec
|
26
83
|
- lib/benchmeth.rb
|
27
84
|
- lib/benchmeth/version.rb
|
85
|
+
- test/benchmeth_test.rb
|
86
|
+
- test/test_helper.rb
|
28
87
|
homepage: https://github.com/ankane/benchmeth
|
29
|
-
licenses:
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
30
91
|
post_install_message:
|
31
92
|
rdoc_options: []
|
32
93
|
require_paths:
|
33
94
|
- lib
|
34
95
|
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
-
none: false
|
36
96
|
requirements:
|
37
|
-
- -
|
97
|
+
- - ">="
|
38
98
|
- !ruby/object:Gem::Version
|
39
99
|
version: '0'
|
40
100
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
101
|
requirements:
|
43
|
-
- -
|
102
|
+
- - ">="
|
44
103
|
- !ruby/object:Gem::Version
|
45
104
|
version: '0'
|
46
105
|
requirements: []
|
47
106
|
rubyforge_project:
|
48
|
-
rubygems_version:
|
107
|
+
rubygems_version: 2.6.8
|
49
108
|
signing_key:
|
50
|
-
specification_version:
|
109
|
+
specification_version: 4
|
51
110
|
summary: The super easy way to benchmark methods
|
52
|
-
test_files:
|
111
|
+
test_files:
|
112
|
+
- test/benchmeth_test.rb
|
113
|
+
- test/test_helper.rb
|