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 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
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
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
@@ -0,0 +1,7 @@
1
+ ## 0.1.1
2
+
3
+ - Added `use_notifications` option
4
+
5
+ ## 0.1.0
6
+
7
+ - First release
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'http://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in benchmeth.gemspec
4
4
  gemspec
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
- gem "benchmeth"
6
+ class Person
7
+ def compute
8
+ # boom
9
+ end
10
+ benchmark :compute
11
+ end
7
12
  ```
8
13
 
9
- Just say which methods to benchmark.
14
+ Works with class methods, too
10
15
 
11
16
  ```ruby
12
- def compute
13
- sleep(1)
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
- benchmark :compute
27
+ ## Installation
17
28
 
18
- compute
29
+ Add this line to your application’s Gemfile:
30
+
31
+ ```ruby
32
+ gem 'benchmeth'
19
33
  ```
20
34
 
21
- By default, benchmark data is written to STDOUT
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 |method, realtime|
31
- logger.info "#{method} took #{realtime} seconds!"
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, use:
51
+ To call a method without benchmarking, append `_without_benchmark` to the name.
38
52
 
39
- ```ruby
40
- compute_without_benchmark
41
- ```
53
+ ## ActiveSupport Notifications
42
54
 
43
- ## Instance Methods
55
+ You can switch to ActiveSupport notifications with:
44
56
 
45
57
  ```ruby
46
- class Person
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
- Like rdoc, instance methods are denoted with a pound sign (#).
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
- class Person
79
-
80
- def self.find(id)
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
- Like rdoc, class methods are denoted with a dot (.).
70
+ ## Contributing
94
71
 
95
- ```
96
- Person.find : 0 ms
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
@@ -1,2 +1,8 @@
1
- #!/usr/bin/env rake
2
1
  require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task default: :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
data/benchmeth.gemspec CHANGED
@@ -1,17 +1,25 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/benchmeth/version', __FILE__)
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 |gem|
5
- gem.authors = ["Andrew Kane"]
6
- gem.email = ["andrew@getformidable.com"]
7
- gem.description = %q{The super easy way to benchmark methods}
8
- gem.summary = %q{The super easy way to benchmark methods}
9
- gem.homepage = "https://github.com/ankane/benchmeth"
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
- gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
- gem.files = `git ls-files`.split("\n")
13
- gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
- gem.name = "benchmeth"
15
- gem.require_paths = ["lib"]
16
- gem.version = Benchmeth::VERSION
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
- DEFAULT_BLOCK = lambda do |method, realtime|
9
- puts "%s : %d ms" % [method, realtime * 1000]
7
+ class << self
8
+ attr_accessor :use_notifications
10
9
  end
10
+ self.use_notifications = false
11
11
 
12
- def self.included(base)
13
- base.send :extend, ClassMethods
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
- self.send :alias_method, :"#{method_name}_without_benchmark", method_name
31
- self.send :define_method, method_name do |*args|
32
- result = nil
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
- "#{self.name}."
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.send :include, Benchmeth
66
+ Object.extend Benchmeth::ClassMethods
67
+ Object.send :include, Benchmeth::InstanceMethods
@@ -1,3 +1,3 @@
1
1
  module Benchmeth
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -0,0 +1,12 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestBenchmeth < Minitest::Test
4
+ def test_instance_method
5
+ car = Car.new
6
+ car.boom(60)
7
+ end
8
+
9
+ def test_class_method
10
+ Car.boom2
11
+ end
12
+ end
@@ -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.0
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: 2011-10-14 00:00:00.000000000Z
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@getformidable.com
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: 1.8.10
107
+ rubygems_version: 2.6.8
49
108
  signing_key:
50
- specification_version: 3
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