tick 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.sw*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm ree@tick
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tick.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tick (0.0.1)
5
+ rainbow
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ ZenTest (4.4.2)
11
+ autotest (4.4.6)
12
+ ZenTest (>= 4.4.1)
13
+ columnize (0.3.2)
14
+ diff-lcs (1.1.2)
15
+ linecache (0.43)
16
+ rainbow (1.1)
17
+ rr (1.0.2)
18
+ rspec (2.3.0)
19
+ rspec-core (~> 2.3.0)
20
+ rspec-expectations (~> 2.3.0)
21
+ rspec-mocks (~> 2.3.0)
22
+ rspec-core (2.3.1)
23
+ rspec-expectations (2.3.0)
24
+ diff-lcs (~> 1.1.2)
25
+ rspec-mocks (2.3.0)
26
+ ruby-debug (0.10.4)
27
+ columnize (>= 0.1)
28
+ ruby-debug-base (~> 0.10.4.0)
29
+ ruby-debug-base (0.10.4)
30
+ linecache (>= 0.3)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ autotest
37
+ rainbow
38
+ rr
39
+ rspec
40
+ ruby-debug
41
+ tick!
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ Tick
2
+ =====
3
+
4
+ About
5
+ ------
6
+
7
+ Tick benchmark your method and print benchmark in color
8
+
9
+
10
+ Installation
11
+ -------
12
+
13
+ gem install tick
14
+
15
+
16
+ Usge
17
+ --------
18
+
19
+ def foo
20
+ end
21
+ tick :foo
22
+
23
+
24
+ Configuration
25
+ -------------
26
+
27
+ By default you don't need any configuration.
28
+
29
+
30
+ Enable tick:
31
+
32
+ Tick.enabled = true
33
+
34
+ default: true
35
+
36
+ Whether print benchmark in color
37
+
38
+ Tick.color = true
39
+
40
+ default: true
41
+
42
+ Logger:
43
+
44
+ Tick.logger = Logger.new(STDOUT)
45
+
46
+ default: Rails.logger if in Rails environment otherwise Logger.new(STDOUT)
47
+
48
+ Customize messages:
49
+
50
+ Tick.desc_message = lambda { |class_name, method_name| "TIME c:#{class_name} m:#{method_name}" }
51
+ Tick.time_message = lambda { |sec| "COST (#{sec})" }
52
+
53
+ Default:
54
+ * desc_message: "TICK: method '#{method_name.to_s}' in class '#{self.class.name}'"
55
+ * time_message: "(#{sec.to_s} ms)"
56
+
57
+
58
+ Set 256 color:
59
+
60
+ Tick.desc_color = "#FFC482"
61
+ Tick.time_color = "#FFC482"
62
+
63
+ Default:
64
+ * desc_color: yellow
65
+ * time_color: cyan
66
+
67
+
68
+ Special Thanks To
69
+ -----------------
70
+
71
+ * sickill's rainbow gem
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,2 @@
1
+ Autotest.add_discovery { "rspec2" }
2
+
@@ -0,0 +1,3 @@
1
+ module Tick
2
+ VERSION = "0.1.0"
3
+ end
data/lib/tick.rb ADDED
@@ -0,0 +1,132 @@
1
+ require 'logger'
2
+ require 'benchmark'
3
+ require 'rainbow'
4
+
5
+ module Tick
6
+
7
+ Sickill::Rainbow.enabled = true
8
+
9
+ def self.included(base)
10
+ base.extend(ClassMethods)
11
+ base.send(:include, InstanceMethods)
12
+ end
13
+
14
+ def self.logger
15
+ return @logger if @logger
16
+
17
+ if defined?(Rails)
18
+ Rails.logger
19
+ else
20
+ Logger.new(STDOUT)
21
+ end
22
+ end
23
+
24
+ def self.logger=(logger)
25
+ @logger = logger
26
+ end
27
+
28
+ def self.color=(is_turn_on)
29
+ return unless is_turn_on.kind_of?(TrueClass) || is_turn_on.kind_of?(FalseClass)
30
+ Sickill::Rainbow.enabled = is_turn_on
31
+ end
32
+
33
+ def self.enabled
34
+ @enabled = true if @enabled.nil?
35
+ @enabled
36
+ end
37
+
38
+ def self.enabled=(is_turn_on)
39
+ @enabled = is_turn_on
40
+ end
41
+
42
+ def self.desc_message
43
+ @desc_message
44
+ end
45
+
46
+ def self.desc_message=(block)
47
+ return @desc_message = nil if block.nil?
48
+ raise ArgumentError.new("wrong number of arguments (#{block.arity} for 2)") if block.arity != 2
49
+ @desc_message = block
50
+ end
51
+
52
+ def self.desc_color
53
+ @desc_color
54
+ end
55
+
56
+ def self.desc_color=(color)
57
+ @desc_color = color
58
+ end
59
+
60
+ def self.time_message
61
+ @time_message
62
+ end
63
+
64
+ def self.time_message=(block)
65
+ return @time_message = nil if block.nil?
66
+ raise ArgumentError.new("wrong number of arguments (#{block.arity} for 1)") if block.arity != 1
67
+ @time_message = block
68
+ end
69
+
70
+ def self.time_color
71
+ @time_color
72
+ end
73
+
74
+ def self.time_color=(color)
75
+ @time_color = color
76
+ end
77
+
78
+ def self.reset
79
+ self.enabled = true
80
+ self.color = true
81
+ @desc_message = nil
82
+ @time_message = nil
83
+ @desc_color = nil
84
+ @time_color = nil
85
+ end
86
+
87
+
88
+ module ClassMethods
89
+ def tick(method_name)
90
+ alias_method "#{method_name}_without_tick", method_name
91
+ define_method method_name do
92
+ result = nil
93
+ if Tick.enabled
94
+ sec = Benchmark.realtime { result = self.send("#{method_name}_without_tick") }
95
+ _log_benchmark(method_name, sec)
96
+ else
97
+ result = self.send("#{method_name}_without_tick")
98
+ end
99
+ result
100
+ end
101
+ end
102
+ end
103
+
104
+ module InstanceMethods
105
+ def _log_benchmark(method_name, sec)
106
+ desc = Tick.desc_message.nil? ? "TICK: method '#{method_name.to_s}' in class '#{self.class.name}'" : Tick.desc_message.call(self.class.name, method_name)
107
+ time = Tick.time_message.nil? ? "(#{sec.to_s} ms)" : Tick.time_message.call(sec)
108
+ message = self._colorize_desc(desc)
109
+ message << " "
110
+ message << self._colorize_time(time)
111
+ Tick.logger.debug(message)
112
+ end
113
+
114
+ def _colorize_desc(str)
115
+ if Tick.desc_color
116
+ str.color(Tick.desc_color)
117
+ else
118
+ str.bright.foreground(:yellow)
119
+ end
120
+ end
121
+
122
+ def _colorize_time(str)
123
+ if Tick.time_color
124
+ str.color(Tick.time_color)
125
+ else
126
+ str.underline.foreground(:cyan).bright
127
+ end
128
+ end
129
+
130
+ end
131
+
132
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'rspec'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__),'..','lib'))
6
+
7
+ require 'tick'
8
+
9
+ RSpec.configure do |config|
10
+ config.mock_with :rr
11
+ config.after(:each) do
12
+ Tick.reset
13
+ end
14
+ end
15
+
16
+
data/spec/tick_spec.rb ADDED
@@ -0,0 +1,180 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe Tick do
5
+ it "will use Rails logger if Rails defined" do
6
+ module Rails; end
7
+ default_logger = Logger.new(STDOUT)
8
+ mock(Rails).logger { default_logger }
9
+ Tick.logger.should == default_logger
10
+ Object.send(:remove_const, :Rails)
11
+ end
12
+
13
+ it "will create new logger if no Rails defined" do
14
+ Tick.logger.should be_kind_of(Logger)
15
+ end
16
+
17
+ it "can set logger" do
18
+ logger = Logger.new(STDOUT)
19
+ Tick.logger = logger
20
+ Tick.logger.should == logger
21
+ end
22
+
23
+ it "turn on color by default" do
24
+ Sickill::Rainbow.enabled.should be true
25
+ end
26
+
27
+ it "can turn off color" do
28
+ Tick.color = false
29
+ Sickill::Rainbow.enabled.should be false
30
+ end
31
+
32
+ it "can not set color to value which is not boolean" do
33
+ old_value = Sickill::Rainbow.enabled
34
+ Tick.color = "asdasdas"
35
+ Sickill::Rainbow.enabled.should be old_value
36
+ end
37
+
38
+ it "is turn on by default" do
39
+ Tick.enabled.should be true
40
+ end
41
+
42
+ it "can be turn off" do
43
+ Tick.enabled = false
44
+ Tick.enabled.should be false
45
+ end
46
+
47
+ it "can set custom desc color" do
48
+ Tick.desc_color = "#FFC482"
49
+ Tick.desc_color.should == "#FFC482"
50
+ end
51
+
52
+ it "can set custom time color" do
53
+ Tick.time_color = "#FFC482"
54
+ Tick.time_color.should == "#FFC482"
55
+ end
56
+
57
+ it "can set custom desc message" do
58
+ a_proc = lambda { |class_name, method_name| "TIME c:#{class_name} m:#{method_name}" }
59
+ Tick.desc_message = a_proc
60
+ Tick.desc_message.should be a_proc
61
+ end
62
+
63
+ it "will raise exception if desc_message lambda arg number dones't match" do
64
+ a_proc = lambda { |class_name| "TIME c:#{class_name}" }
65
+ lambda {Tick.desc_message = a_proc}.should raise_error(ArgumentError, "wrong number of arguments (1 for 2)")
66
+ end
67
+
68
+ it "will raise exception if desc_message lambda arg number dones't match" do
69
+ a_proc = lambda { |a, b|"COST (0.1)" }
70
+ lambda {Tick.time_message = a_proc}.should raise_error(ArgumentError, "wrong number of arguments (2 for 1)")
71
+ end
72
+
73
+ it "can set custom time message" do
74
+ a_proc = lambda { |sec| "COST (#{sec})" }
75
+ Tick.time_message = a_proc
76
+ Tick.time_message.should be a_proc
77
+ end
78
+ end
79
+
80
+ describe "A class include Tick" do
81
+ before do
82
+ @klass = Class.new
83
+ @klass.send(:include, Tick)
84
+ end
85
+
86
+ it "should have class method tick" do
87
+ @klass.should respond_to :tick
88
+ end
89
+
90
+ it "should not have logger method" do
91
+ @klass.should_not respond_to :logger
92
+ @klass.new.should_not respond_to :logger
93
+ end
94
+
95
+ it "should raise exception if passing wrong method name" do
96
+ lambda {@klass.send(:tick, :xxx)}.should raise_error
97
+ end
98
+
99
+ describe "tick on :default method" do
100
+ before do
101
+ @klass.class_eval do
102
+ def default
103
+ sleep 0.5
104
+ end
105
+ end
106
+
107
+ @klass.send(:tick, :default)
108
+ @instance = @klass.new
109
+ @instance.should respond_to :default
110
+ end
111
+
112
+ it "should log time when default method be called" do
113
+ mock(@instance)._log_benchmark(:default, numeric)
114
+ @instance.default
115
+ end
116
+
117
+ it "should not do benchmark if Tick is turn off" do
118
+ dont_allow(Benchmark).realtime(anything)
119
+ old_value = Tick.enabled
120
+ Tick.enabled = false
121
+ @instance.default
122
+ Tick.enabled = old_value
123
+ end
124
+ end
125
+
126
+ describe "#_log_benchmark" do
127
+ before do
128
+ @instance = @klass.new
129
+ mock(@instance)._colorize_desc(anything) do |desc|
130
+ desc
131
+ end
132
+ mock(@instance)._colorize_time(anything) do |time|
133
+ time
134
+ end
135
+ end
136
+
137
+ it "log sec in color" do
138
+ desc = "TICK: method 'default' in class ''"
139
+ time = "(0.1 ms)"
140
+ message = ""
141
+ message << desc
142
+ message << " "
143
+ message << time
144
+ mock(Tick.logger).debug(message)
145
+ @instance._log_benchmark(:default, 0.1)
146
+ end
147
+
148
+ it "should print custom message if set custom message" do
149
+ Tick.desc_message = lambda {|class_name, method_name| "c:#{class_name} m:#{method_name}" }
150
+ Tick.time_message = lambda {|sec| "cost #{sec}" }
151
+ desc = "c: m:default"
152
+ time = "cost 0.1"
153
+ message = ""
154
+ message << desc
155
+ message << " "
156
+ message << time
157
+ mock(Tick.logger).debug(message)
158
+ @instance._log_benchmark(:default, 0.1)
159
+ end
160
+ end
161
+
162
+ describe "#colorize" do
163
+ it "should set desc color if desc color is set" do
164
+ str = "a"
165
+ color = "#FFC482"
166
+ Tick.desc_color = color
167
+ @klass.new._colorize_desc(str).should == str.color(color)
168
+ end
169
+
170
+ it "should set time color if time color is set" do
171
+ str = "a"
172
+ color = "#FFC482"
173
+ Tick.time_color = color
174
+ @klass.new._colorize_time(str).should == str.color(color)
175
+ end
176
+
177
+ end
178
+
179
+
180
+ end
data/spec.opts ADDED
@@ -0,0 +1,11 @@
1
+ --colour
2
+ --format
3
+ document
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --timeout
8
+ 20
9
+ --diff
10
+ --backtrace
11
+
data/tick.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tick/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tick"
7
+ s.version = Tick::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["allenwei"]
10
+ s.email = ["digruby@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/tick"
12
+ s.summary = %q{Tick benchmark your method and print it in color}
13
+
14
+ s.rubyforge_project = "tick"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('rainbow')
22
+
23
+ s.add_development_dependency('rspec')
24
+ s.add_development_dependency('rr')
25
+ s.add_development_dependency('autotest')
26
+ s.add_development_dependency('ruby-debug')
27
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tick
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - allenwei
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-01 00:00:00 +08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rainbow
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rr
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: autotest
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: ruby-debug
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :development
90
+ version_requirements: *id005
91
+ description:
92
+ email:
93
+ - digruby@gmail.com
94
+ executables: []
95
+
96
+ extensions: []
97
+
98
+ extra_rdoc_files: []
99
+
100
+ files:
101
+ - .gitignore
102
+ - .rvmrc
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - README.md
106
+ - Rakefile
107
+ - autotest/discover.rb
108
+ - lib/tick.rb
109
+ - lib/tick/version.rb
110
+ - spec.opts
111
+ - spec/spec_helper.rb
112
+ - spec/tick_spec.rb
113
+ - tick.gemspec
114
+ has_rdoc: true
115
+ homepage: http://rubygems.org/gems/tick
116
+ licenses: []
117
+
118
+ post_install_message:
119
+ rdoc_options: []
120
+
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ hash: 3
129
+ segments:
130
+ - 0
131
+ version: "0"
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ none: false
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ hash: 3
138
+ segments:
139
+ - 0
140
+ version: "0"
141
+ requirements: []
142
+
143
+ rubyforge_project: tick
144
+ rubygems_version: 1.3.7
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Tick benchmark your method and print it in color
148
+ test_files:
149
+ - spec/spec_helper.rb
150
+ - spec/tick_spec.rb