clockblock 0.0.2 → 0.0.3
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/.travis.yml +8 -0
- data/README.md +44 -44
- data/lib/clockblock/timer.rb +6 -5
- data/lib/clockblock/timing.rb +18 -16
- data/lib/clockblock/version.rb +1 -1
- data/spec/lib/clockblock/timer_spec.rb +1 -1
- data/spec/lib/clockblock/timing_spec.rb +43 -0
- metadata +27 -9
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,29 +1,35 @@
|
|
1
|
-
|
1
|
+
[](https://codeclimate.com/github/jackross/clockblock)
|
2
|
+
[](http://travis-ci.org/jackross/clockblock)
|
3
|
+
|
4
|
+
|
5
|
+
Clockblock
|
2
6
|
==========
|
3
7
|
|
4
|
-
|
8
|
+
Extend Clockblock::Timing in your class definition to add timers to your methods:
|
5
9
|
|
6
10
|
````ruby
|
7
11
|
require 'clockblock'
|
8
12
|
|
9
13
|
class Foo
|
10
14
|
extend Clockblock::Timing
|
15
|
+
add_timing_to :bar
|
11
16
|
|
12
17
|
def bar
|
13
18
|
sleep 2
|
14
19
|
end
|
15
|
-
add_timing_to :bar
|
16
20
|
|
17
21
|
end
|
18
22
|
|
19
23
|
f = Foo.new
|
20
24
|
f.bar
|
21
25
|
|
22
|
-
f.clockblock_timers
|
23
|
-
# => {:
|
26
|
+
f.clockblock_timers[:bar]
|
27
|
+
# => {:started_at=>2012-07-26 16:04:03 -0400, :finished_at=>2012-07-26 16:04:05 -0400, :duration=>2.001059, :stage=>:finished}
|
24
28
|
|
25
29
|
````
|
26
30
|
|
31
|
+
Extend your instances with Clockblock::Timing to add timers to your methods:
|
32
|
+
|
27
33
|
````ruby
|
28
34
|
require 'clockblock'
|
29
35
|
|
@@ -38,56 +44,50 @@ f.extend Clockblock::Timing
|
|
38
44
|
f.add_timing_to :bar
|
39
45
|
f.bar
|
40
46
|
|
41
|
-
f.clockblock_timers
|
42
|
-
# => {:
|
47
|
+
f.clockblock_timers[:bar]
|
48
|
+
# => {:started_at=>2012-07-26 16:04:03 -0400, :finished_at=>2012-07-26 16:04:05 -0400, :duration=>2.001059, :stage=>:finished}
|
43
49
|
|
44
50
|
````
|
45
51
|
|
52
|
+
Extend your classes with Clockblock::Timing to add timers to your methods:
|
53
|
+
|
46
54
|
````ruby
|
47
|
-
|
48
|
-
extend Clockblock::Timing
|
49
|
-
add_timing_to :baz
|
55
|
+
require 'clockblock'
|
50
56
|
|
57
|
+
class Foo
|
51
58
|
def bar
|
52
|
-
sleep
|
53
|
-
end
|
54
|
-
add_timing_to :bar
|
55
|
-
|
56
|
-
def baz
|
57
|
-
sleep 1
|
59
|
+
sleep 2
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
63
|
+
Foo.extend Clockblock::Timing
|
64
|
+
Foo.add_timing_to :bar, :baz
|
65
|
+
puts Foo.instance_variable_get(:@future_timer_methods)
|
66
|
+
class Foo; def baz; sleep 1; end; end
|
67
|
+
f = Foo.new
|
68
|
+
f.bar
|
69
|
+
f.baz
|
70
|
+
puts f.clockblock_timers
|
71
|
+
# => {:bar=>{:started_at=>2012-07-30 10:43:48 -0400, :finished_at=>2012-07-30 10:43:49 -0400, :duration=>1.0004, :stage=>:finished}, :baz=>{:started_at=>2012-07-30 10:43:49 -0400, :finished_at=>2012-07-30 10:43:50 -0400, :duration=>1.000906, :stage=>:finished}}
|
72
|
+
|
73
|
+
````
|
74
|
+
|
75
|
+
Wrap your code in a Clockblock clock block to measure execution duration.
|
76
|
+
|
77
|
+
````ruby
|
78
|
+
require 'clockblock'
|
79
|
+
|
80
|
+
class Foo
|
66
81
|
|
67
|
-
class Foo3
|
68
82
|
def bar
|
69
|
-
|
83
|
+
t = Clockblock::Timer.new
|
84
|
+
result = t.clock do
|
85
|
+
sleep 2 # replace 'sleep 2' with your code!
|
86
|
+
end
|
87
|
+
puts t.attributes
|
88
|
+
result
|
70
89
|
end
|
71
90
|
end
|
72
91
|
|
73
|
-
|
74
|
-
|
75
|
-
puts f1.clockblock_timers
|
76
|
-
puts Foo1.instance_variable_get(:@future_timer_methods)
|
77
|
-
|
78
|
-
f2 = Foo2.new
|
79
|
-
f2.extend Clockblock::Timing
|
80
|
-
f2.add_timing_to :bar, :baz
|
81
|
-
f2.bar
|
82
|
-
puts f2.clockblock_timers
|
83
|
-
puts f2.instance_variable_get(:@future_timer_methods)
|
84
|
-
|
85
|
-
Foo3.extend Clockblock::Timing
|
86
|
-
Foo3.add_timing_to :bar, :baz
|
87
|
-
puts Foo3.instance_variable_get(:@future_timer_methods)
|
88
|
-
class Foo3; def baz; sleep 1; end; end
|
89
|
-
f3 = Foo3.new
|
90
|
-
f3.bar
|
91
|
-
f3.baz
|
92
|
-
puts f3.clockblock_timers
|
93
|
-
````
|
92
|
+
````
|
93
|
+
|
data/lib/clockblock/timer.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
module Clockblock
|
2
2
|
class Timer
|
3
|
-
attr_reader :started_at, :finished_at, :stage
|
3
|
+
attr_reader :started_at, :finished_at, :stage, :clockblock_result
|
4
4
|
|
5
5
|
def initialize(opts = {})
|
6
6
|
@stage = :initialized
|
7
|
+
yield self if block_given?
|
7
8
|
end
|
8
9
|
|
9
10
|
def clock(name = "Anonymous Timer")
|
10
11
|
begin
|
11
12
|
start
|
12
13
|
@stage = :executing
|
13
|
-
|
14
|
+
@clockblock_result = yield
|
14
15
|
stop
|
15
16
|
rescue => ex
|
16
17
|
@stage = :error
|
17
18
|
raise TimerException.new("Error while executing '#{name}': '#{ex.message}'!")
|
18
19
|
end
|
19
|
-
|
20
|
+
@clockblock_result
|
20
21
|
end
|
21
22
|
|
22
23
|
def start
|
@@ -47,11 +48,11 @@ module Clockblock
|
|
47
48
|
end
|
48
49
|
|
49
50
|
def inspect
|
50
|
-
|
51
|
+
to_s
|
51
52
|
end
|
52
53
|
|
53
54
|
def to_s
|
54
|
-
attributes
|
55
|
+
attributes.to_s
|
55
56
|
end
|
56
57
|
|
57
58
|
class TimerException < Exception
|
data/lib/clockblock/timing.rb
CHANGED
@@ -2,11 +2,14 @@ module Clockblock
|
|
2
2
|
module Timing
|
3
3
|
|
4
4
|
module ClassMethods
|
5
|
+
def receiver
|
6
|
+
self.is_a?(Class) ? self : (class << self; self; end)
|
7
|
+
end
|
8
|
+
|
5
9
|
def add_timing_to *methods
|
6
10
|
methods.each do |method|
|
7
|
-
|
8
|
-
|
9
|
-
add_timing_to_method method, klass
|
11
|
+
if receiver.method_defined? method
|
12
|
+
add_timing_to_method method
|
10
13
|
else
|
11
14
|
@future_timer_methods ||= []
|
12
15
|
@future_timer_methods << method
|
@@ -14,9 +17,19 @@ module Clockblock
|
|
14
17
|
end
|
15
18
|
end
|
16
19
|
|
17
|
-
def
|
20
|
+
def method_added(method)
|
21
|
+
if @future_timer_methods && @future_timer_methods.include?(method)
|
22
|
+
unless @added_by_clockblock
|
23
|
+
@added_by_clockblock = true
|
24
|
+
add_timing_to_method method
|
25
|
+
end
|
26
|
+
@added_by_clockblock = false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def add_timing_to_method(method)
|
18
31
|
stashed_method = "#{method}_stashed_by_clockblock".to_sym
|
19
|
-
|
32
|
+
receiver.class_eval do
|
20
33
|
alias_method stashed_method, method
|
21
34
|
define_method method do |*args, &block|
|
22
35
|
@clockblock_timers ||= {}
|
@@ -28,17 +41,6 @@ module Clockblock
|
|
28
41
|
end
|
29
42
|
end
|
30
43
|
end
|
31
|
-
|
32
|
-
def method_added(method)
|
33
|
-
if @future_timer_methods && @future_timer_methods.include?(method)
|
34
|
-
unless @added_by_clockblock
|
35
|
-
@added_by_clockblock = true
|
36
|
-
klass = self.is_a?(Class) ? self : (class << self; self; end)
|
37
|
-
add_timing_to_method method, klass
|
38
|
-
end
|
39
|
-
@added_by_clockblock = false
|
40
|
-
end
|
41
|
-
end
|
42
44
|
end
|
43
45
|
|
44
46
|
def self.extended(base)
|
data/lib/clockblock/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "extending Clockblock::Timing in a class definition" do
|
4
|
+
it "wraps an already defined method in a clock block" do
|
5
|
+
|
6
|
+
foo_klass = Class.new do
|
7
|
+
extend Clockblock::Timing
|
8
|
+
|
9
|
+
def bar
|
10
|
+
sleep 1
|
11
|
+
end
|
12
|
+
add_timing_to :bar
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
f = foo_klass.new
|
17
|
+
f.bar.must_equal 1
|
18
|
+
f.clockblock_timers.must_be_kind_of Hash
|
19
|
+
f.clockblock_timers.keys.must_include :bar
|
20
|
+
f.clockblock_timers[:bar].must_be_instance_of Clockblock::Timer
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
it "wraps an undefined method in a clock block" do
|
25
|
+
|
26
|
+
foo_klass = Class.new do
|
27
|
+
extend Clockblock::Timing
|
28
|
+
add_timing_to :bar
|
29
|
+
|
30
|
+
def bar
|
31
|
+
sleep 1
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
f = foo_klass.new
|
37
|
+
f.bar.must_equal 1
|
38
|
+
f.clockblock_timers.must_be_kind_of Hash
|
39
|
+
f.clockblock_timers.keys.must_include :bar
|
40
|
+
f.clockblock_timers[:bar].must_be_instance_of Clockblock::Timer
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clockblock
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,15 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
- !ruby/object:Gem::Dependency
|
26
31
|
name: minitest
|
27
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
28
33
|
none: false
|
29
34
|
requirements:
|
30
35
|
- - ! '>='
|
@@ -32,10 +37,15 @@ dependencies:
|
|
32
37
|
version: '0'
|
33
38
|
type: :development
|
34
39
|
prerelease: false
|
35
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
- !ruby/object:Gem::Dependency
|
37
47
|
name: turn
|
38
|
-
requirement:
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
39
49
|
none: false
|
40
50
|
requirements:
|
41
51
|
- - ! '>='
|
@@ -43,7 +53,12 @@ dependencies:
|
|
43
53
|
version: '0'
|
44
54
|
type: :development
|
45
55
|
prerelease: false
|
46
|
-
version_requirements:
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
47
62
|
description: Enables easy and DRY code timing capabilities.
|
48
63
|
email:
|
49
64
|
- jack.ross@technekes.com
|
@@ -52,6 +67,7 @@ extensions: []
|
|
52
67
|
extra_rdoc_files: []
|
53
68
|
files:
|
54
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
55
71
|
- Gemfile
|
56
72
|
- LICENSE
|
57
73
|
- README.md
|
@@ -62,6 +78,7 @@ files:
|
|
62
78
|
- lib/clockblock/timing.rb
|
63
79
|
- lib/clockblock/version.rb
|
64
80
|
- spec/lib/clockblock/timer_spec.rb
|
81
|
+
- spec/lib/clockblock/timing_spec.rb
|
65
82
|
- spec/spec_helper.rb
|
66
83
|
homepage: ''
|
67
84
|
licenses: []
|
@@ -83,10 +100,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
83
100
|
version: '0'
|
84
101
|
requirements: []
|
85
102
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.8.
|
103
|
+
rubygems_version: 1.8.24
|
87
104
|
signing_key:
|
88
105
|
specification_version: 3
|
89
106
|
summary: Wrap your code in a Clock Block to measure execution duration.
|
90
107
|
test_files:
|
91
108
|
- spec/lib/clockblock/timer_spec.rb
|
109
|
+
- spec/lib/clockblock/timing_spec.rb
|
92
110
|
- spec/spec_helper.rb
|