benchmeth 0.0.1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +41 -17
- data/lib/benchmeth.rb +31 -5
- data/lib/benchmeth/version.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -6,23 +6,32 @@ The super easy way to benchmark methods.
|
|
6
6
|
gem "benchmeth"
|
7
7
|
```
|
8
8
|
|
9
|
-
Just say which methods to benchmark
|
9
|
+
Just say which methods to benchmark.
|
10
10
|
|
11
11
|
```ruby
|
12
12
|
def compute
|
13
|
-
|
13
|
+
sleep(1)
|
14
14
|
end
|
15
15
|
|
16
|
-
|
17
|
-
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
18
|
-
# Or log them.
|
19
|
-
end
|
16
|
+
benchmark :compute
|
20
17
|
|
21
18
|
compute
|
19
|
+
```
|
20
|
+
|
21
|
+
By default, benchmark data is written to STDOUT
|
22
|
+
|
23
|
+
```
|
24
|
+
compute : 1000 ms
|
25
|
+
```
|
26
|
+
|
27
|
+
but you can easily do whatever you want with it.
|
22
28
|
|
23
|
-
|
24
|
-
|
25
|
-
#
|
29
|
+
```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]
|
34
|
+
end
|
26
35
|
```
|
27
36
|
|
28
37
|
To call a method without benchmarking, use:
|
@@ -47,11 +56,20 @@ class Person
|
|
47
56
|
end
|
48
57
|
|
49
58
|
# This must come after the methods are defined.
|
50
|
-
|
51
|
-
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
52
|
-
end
|
59
|
+
benchmark :work, :play
|
53
60
|
|
54
61
|
end
|
62
|
+
|
63
|
+
person = Person.new
|
64
|
+
person.work(1)
|
65
|
+
person.play
|
66
|
+
```
|
67
|
+
|
68
|
+
Like rdoc, instance methods are denoted with a pound sign (#).
|
69
|
+
|
70
|
+
```
|
71
|
+
Person#work : 1000 ms
|
72
|
+
Person#play : 500 ms
|
55
73
|
```
|
56
74
|
|
57
75
|
## Class Methods
|
@@ -59,15 +77,21 @@ end
|
|
59
77
|
```ruby
|
60
78
|
class Person
|
61
79
|
|
62
|
-
def self.
|
63
|
-
|
80
|
+
def self.find(id)
|
81
|
+
puts "Found person #{id}!"
|
64
82
|
end
|
65
83
|
|
66
84
|
class << self
|
67
|
-
|
68
|
-
puts "%s : %.2f ms" % [method_name, realtime * 1000]
|
69
|
-
end
|
85
|
+
benchmark :find
|
70
86
|
end
|
71
87
|
|
72
88
|
end
|
89
|
+
|
90
|
+
Person.find(1)
|
91
|
+
```
|
92
|
+
|
93
|
+
Like rdoc, class methods are denoted with a dot (.).
|
94
|
+
|
95
|
+
```
|
96
|
+
Person.find : 0 ms
|
73
97
|
```
|
data/lib/benchmeth.rb
CHANGED
@@ -1,23 +1,49 @@
|
|
1
1
|
require "benchmeth/version"
|
2
2
|
require "benchmark"
|
3
3
|
|
4
|
+
# :( Global
|
5
|
+
$benchmeth_main = self
|
6
|
+
|
4
7
|
module Benchmeth
|
8
|
+
DEFAULT_BLOCK = lambda do |method, realtime|
|
9
|
+
puts "%s : %d ms" % [method, realtime * 1000]
|
10
|
+
end
|
5
11
|
|
6
12
|
def self.included(base)
|
7
13
|
base.send :extend, ClassMethods
|
8
14
|
base.send :include, InstanceMethods
|
9
15
|
end
|
10
16
|
|
17
|
+
def self.on_benchmark(&block)
|
18
|
+
if block_given?
|
19
|
+
@on_benchmark_block = block
|
20
|
+
else
|
21
|
+
@on_benchmark_block || DEFAULT_BLOCK
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
11
25
|
module ClassMethods
|
12
26
|
|
13
|
-
def
|
27
|
+
def benchmark(*method_names)
|
14
28
|
method_names.each do |method_name|
|
15
29
|
method_name = method_name.to_sym
|
16
30
|
self.send :alias_method, :"#{method_name}_without_benchmark", method_name
|
17
31
|
self.send :define_method, method_name do |*args|
|
18
32
|
result = nil
|
19
|
-
|
20
|
-
|
33
|
+
method_prefix = nil
|
34
|
+
realtime = Benchmark.realtime do
|
35
|
+
result = self.send(:"#{method_name}_without_benchmark", *args)
|
36
|
+
method_prefix =
|
37
|
+
case self
|
38
|
+
when $benchmeth_main
|
39
|
+
""
|
40
|
+
when Class
|
41
|
+
"#{self.name}."
|
42
|
+
else
|
43
|
+
"#{self.class.name}#"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
Benchmeth.on_benchmark.call("#{method_prefix}#{method_name}", realtime)
|
21
47
|
result
|
22
48
|
end
|
23
49
|
end
|
@@ -27,8 +53,8 @@ module Benchmeth
|
|
27
53
|
|
28
54
|
module InstanceMethods
|
29
55
|
|
30
|
-
def
|
31
|
-
self.class.
|
56
|
+
def benchmark(*method_names, &block)
|
57
|
+
self.class.benchmark(*method_names, &block)
|
32
58
|
end
|
33
59
|
|
34
60
|
end
|
data/lib/benchmeth/version.rb
CHANGED