benchmeth 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/benchmeth.gemspec +17 -0
- data/lib/benchmeth.rb +38 -0
- data/lib/benchmeth/version.rb +3 -0
- metadata +52 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Benchmeth
|
2
|
+
|
3
|
+
The super easy way to benchmark methods.
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
gem "benchmeth"
|
7
|
+
```
|
8
|
+
|
9
|
+
Just say which methods to benchmark and what you'd like to do with the results.
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
def compute
|
13
|
+
puts "CRUNCH!!"
|
14
|
+
end
|
15
|
+
|
16
|
+
benchmeth :compute do |method_name, realtime|
|
17
|
+
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
18
|
+
# Or log them.
|
19
|
+
end
|
20
|
+
|
21
|
+
compute
|
22
|
+
|
23
|
+
# Output:
|
24
|
+
# CRUNCH!!
|
25
|
+
# compute : 0.09 ms
|
26
|
+
```
|
27
|
+
|
28
|
+
To call a method without benchmarking, use:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
compute_without_benchmark
|
32
|
+
```
|
33
|
+
|
34
|
+
## Instance Methods
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class Person
|
38
|
+
|
39
|
+
def work(seconds)
|
40
|
+
puts "Working for #{seconds} seconds"
|
41
|
+
sleep(seconds)
|
42
|
+
end
|
43
|
+
|
44
|
+
def play
|
45
|
+
puts "Time to play!"
|
46
|
+
sleep(rand * 4)
|
47
|
+
end
|
48
|
+
|
49
|
+
# This must come after the methods are defined.
|
50
|
+
benchmeth :work, :play do |method_name, realtime|
|
51
|
+
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Class Methods
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class Person
|
61
|
+
|
62
|
+
def self.count
|
63
|
+
2
|
64
|
+
end
|
65
|
+
|
66
|
+
class << self
|
67
|
+
benchmeth :count do |method_name, realtime|
|
68
|
+
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
```
|
data/Rakefile
ADDED
data/benchmeth.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/benchmeth/version', __FILE__)
|
3
|
+
|
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"
|
10
|
+
|
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
|
17
|
+
end
|
data/lib/benchmeth.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "benchmeth/version"
|
2
|
+
require "benchmark"
|
3
|
+
|
4
|
+
module Benchmeth
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.send :extend, ClassMethods
|
8
|
+
base.send :include, InstanceMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def benchmeth(*method_names, &block)
|
14
|
+
method_names.each do |method_name|
|
15
|
+
method_name = method_name.to_sym
|
16
|
+
self.send :alias_method, :"#{method_name}_without_benchmark", method_name
|
17
|
+
self.send :define_method, method_name do |*args|
|
18
|
+
result = nil
|
19
|
+
realtime = Benchmark.realtime { result = self.send(:"#{method_name}_without_benchmark", *args) }
|
20
|
+
block.call(method_name, realtime)
|
21
|
+
result
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
|
30
|
+
def benchmeth(*method_names, &block)
|
31
|
+
self.class.benchmeth(*method_names, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
Object.send :include, Benchmeth
|
metadata
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: benchmeth
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Kane
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-14 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: The super easy way to benchmark methods
|
15
|
+
email:
|
16
|
+
- andrew@getformidable.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- benchmeth.gemspec
|
26
|
+
- lib/benchmeth.rb
|
27
|
+
- lib/benchmeth/version.rb
|
28
|
+
homepage: https://github.com/ankane/benchmeth
|
29
|
+
licenses: []
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 1.8.10
|
49
|
+
signing_key:
|
50
|
+
specification_version: 3
|
51
|
+
summary: The super easy way to benchmark methods
|
52
|
+
test_files: []
|