timify 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +4 -4
  3. data/LICENSE +21 -21
  4. data/README.md +187 -187
  5. data/lib/timify.rb +199 -199
  6. metadata +3 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 224c8980c101be7c0b632430f760cd4902cd0e7e80893c1971ee0755c45984d4
4
- data.tar.gz: e2f6e3e167cd555a6a9d11ecacf55a19895d26e7f5cd92aee0412ee41e8ff5cf
3
+ metadata.gz: 2c471402324f5322ef254103bd678d05f6097326f65c3ad91f58d8ccc0801934
4
+ data.tar.gz: 0e32ff0d342fbad59f3f5ae536777f6e34f6d29a88269ed02d83dc333f2f0478
5
5
  SHA512:
6
- metadata.gz: a96a8730523624686027f4678d4fe48420aa8a96a25203dd11f1445a09ca155957061f7801365f51c0a290ffe13b377f2763b3fe3988c1ae2d414571ce4cda6d
7
- data.tar.gz: decee0e39f1b680907094a5eafe11bd05bc3127cb22ca7e7c320e1818eed13362d8fa04a0aca00f39fbf8b9234135e8b629059509cefcbcb284e9a72e2774ccd
6
+ metadata.gz: c0e9c8732bb4c026fdea3be12f52a1c1249a8bafc3e19c4c999b01552c944264b7f6a463bbcce230e248e91f1954ac14564dc2a99fbcbffb9acea5a3517f3357
7
+ data.tar.gz: d789d1f6fa0c804fe5ad98ef53e6242b94cdc840522e1506d39581afcc56dd5de01685538e34e77008ae2c5c56cd4ca5dbec23f4fae3ad7c9f454dcc7b51a261
data/.yardopts CHANGED
@@ -1,5 +1,5 @@
1
- --readme README.md
2
- --title 'timify - Easily calculates the time running (elapsed time) from one location to another inside your code and reports statistics.'
3
- --charset utf-8
4
- --markup markdown
1
+ --readme README.md
2
+ --title 'timify - Easily calculates the time running (elapsed time) from one location to another inside your code and reports statistics.'
3
+ --charset utf-8
4
+ --markup markdown
5
5
  'lib/**/*.rb' - '*.md' - 'LICENSE'
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2017 Mario Ruiz
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Mario Ruiz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,187 +1,187 @@
1
- # Timify
2
-
3
- [![Gem Version](https://badge.fury.io/rb/timify.svg)](https://rubygems.org/gems/timify)
4
-
5
- Easily calculates the time running (elapsed time) from one location to another inside your code and reports statistics. It helps you improve your code and find out which part of your code is consuming more time.
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'timify'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install timify
22
-
23
- ## Usage
24
-
25
- ### initialize
26
- You need to supply a name for your Timify instance.
27
- You can have all the Timify instances you want at the same time.
28
- ```ruby
29
- t = Timify.new :create_user
30
- t.show = false
31
- t.min_time_to_show = 0.5
32
- ```
33
- min_time_to_show: minimum time to show the elapsed time when calling 'add' method
34
-
35
- show: print out results on screen
36
-
37
- you can supply those parameters also:
38
-
39
- ```ruby
40
- t = Timify.new :create_user, show: true, min_time_to_show: 0.3
41
- ```
42
-
43
- The scopes of the instances can be even global so you can measure the elapsed times between different classes, methods... on your code.
44
-
45
- You can disable the timify counter whenever you want using the status attritute that admits :on or :off, by default is set to :on
46
-
47
- ```ruby
48
- t.status = :off
49
- ```
50
-
51
- ### add
52
-
53
- Adds a new point to count the elapsed time. It will count from the last 'add' call or Timify creation in case of the first 'add'.
54
-
55
- You can supply a label that will summarize all the ones with the same label
56
-
57
- The output of this method will be the time elapsed in seconds (float).
58
-
59
- Examples:
60
-
61
- ```ruby
62
- t=Timify.new :example
63
- t.add; run_sqls; t.add :database
64
- t.add
65
- #some processes
66
- t.add
67
- #some processes
68
- send_email_alert(t.totals[:message]) if t.add > 0.2
69
- #some processes
70
- do_log(t.totals[:message]) if t.add > 0.5
71
- ```
72
-
73
- ### totals
74
-
75
- Returns all data for this instance
76
-
77
- In case json parameter supplied as true, the output will be in json format instead of a hash.
78
-
79
- The output hash contains:
80
-
81
- name: (String) name given for this instance'
82
-
83
- total_time: (float) total elapsed time from initialization to last 'add' call
84
-
85
- started: (Time)
86
-
87
- finished: (Time)
88
-
89
- message: (String) a printable friendly message giving all information
90
-
91
- locations, labels, ranges: (Hash) the resultant hash contains:
92
-
93
- secs: (float) number of seconds
94
-
95
- percent: (integer) percentage in reference to the total time
96
-
97
- count: (integer) number of times
98
-
99
- locations: (Hash) All summary data by location where was called
100
-
101
- labels: (Hash) All summary data by label given on 'add' method
102
-
103
- ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
104
-
105
-
106
- Example of output:
107
- ```ruby
108
- { :name=>:add_customer_wrong,
109
- :total_time=>4.461446,
110
- :started=>2017-11-06 16:10:53 +0000,
111
- :finished=>2017-11-06 16:10:57 +0000,
112
- :locations=>{
113
- "/add_customer.rb:509"=>{:secs=>0.0, :percent=>0, :count=>1},
114
- "/add_customer.rb:529"=>{:secs=>0.008001, :percent=>0, :count=>1},
115
- "/add_customer.rb:532"=>{:secs=>0.212021, :percent=>5, :count=>1},
116
- "/add_customer.rb:569"=>{:secs=>0.006001, :percent=>0, :count=>1},
117
- "/add_customer.rb:581"=>{:secs=>0.446045, :percent=>10, :count=>1},
118
- "/add_customer.rb:583"=>{:secs=>3.789378, :percent=>85, :count=>1},
119
- "/add_customer.rb:585"=>{:secs=>0.0, :percent=>0, :count=>1},
120
- "/add_customer.rb:587"=>{:secs=>0.0, :percent=>0, :count=>1},
121
- "/add_customer.rb:595"=>{:secs=>0.0, :percent=>0, :count=>1},
122
- "/add_customer.rb:603"=>{:secs=>0.0, :percent=>0, :count=>1},
123
- "/add_customer.rb:612"=>{:secs=>0.0, :percent=>0, :count=>1},
124
- "/add_customer.rb:617"=>{:secs=>0.0, :percent=>0, :count=>1}
125
- },
126
- :labels=>{
127
- :database_access=>{:secs=>4,447444, :percent=>99, :count=>3},
128
- :checkouts=>{:secs=>0.0, :percent=>0, :count=>2},
129
- },
130
- :ranges=>{
131
- "/add_customer.rb:509 - /add_customer.rb:529"=>{:secs=>0.008001, :percent=>0, :count=>1},
132
- "/add_customer.rb:529 - /add_customer.rb:532"=>{:secs=>0.212021, :percent=>5, :count=>1},
133
- "/add_customer.rb:532 - /add_customer.rb:569"=>{:secs=>0.006001, :percent=>0, :count=>1},
134
- "/add_customer.rb:569 - /add_customer.rb:581"=>{:secs=>0.446045, :percent=>10, :count=>1},
135
- "/add_customer.rb:581 - /add_customer.rb:583"=>{:secs=>3.789378, :percent=>85, :count=>1},
136
- "/add_customer.rb:583 - /add_customer.rb:585"=>{:secs=>0.0, :percent=>0, :count=>1},
137
- "/add_customer.rb:585 - /add_customer.rb:587"=>{:secs=>0.0, :percent=>0, :count=>1},
138
- "/add_customer.rb:587 - /add_customer.rb:595"=>{:secs=>0.0, :percent=>0, :count=>1},
139
- "/add_customer.rb:595 - /add_customer.rb:603"=>{:secs=>0.0, :percent=>0, :count=>1},
140
- "/add_customer.rb:603 - /add_customer.rb:612"=>{:secs=>0.0, :percent=>0, :count=>1},
141
- "/add_customer.rb:612 - /add_customer.rb:617"=>{:secs=>0.0, :percent=>0, :count=>1}
142
- },
143
- :message=>"
144
-
145
- Total time <add_customer_wrong>:4.46
146
- Total time by location:
147
- /add_customer.rb:509: 0.0 (0%) #1
148
- /add_customer.rb:529: 0.01 (0%) #1
149
- /add_customer.rb:532: 0.21 (5%) #1
150
- /add_customer.rb:569: 0.01 (0%) #1
151
- /add_customer.rb:581: 0.45 (10%) #1
152
- /add_customer.rb:583: 3.79 (85%) #1
153
- /add_customer.rb:585: 0.0 (0%) #1
154
- /add_customer.rb:587: 0.0 (0%) #1
155
- /add_customer.rb:595: 0.0 (0%) #1
156
- /add_customer.rb:603: 0.0 (0%) #1
157
- /add_customer.rb:612: 0.0 (0%) #1
158
- /add_customer.rb:617: 0.0 (0%) #1
159
- Total time by label:
160
- database_access: 4.45 (99%) #3
161
- checkouts: 0.0 (0%) #2
162
- Total time by range:
163
- /add_customer.rb:509 - /add_customer.rb:529: 0.01 (0%) #1
164
- /add_customer.rb:529 - /add_customer.rb:532: 0.21 (5%) #1
165
- /add_customer.rb:532 - /add_customer.rb:569: 0.01 (0%) #1
166
- /add_customer.rb:569 - /add_customer.rb:581: 0.45 (10%) #1
167
- /add_customer.rb:581 - /add_customer.rb:583: 3.79 (85%) #1
168
- /add_customer.rb:583 - /add_customer.rb:585: 0.0 (0%) #1
169
- /add_customer.rb:585 - /add_customer.rb:587: 0.0 (0%) #1
170
- /add_customer.rb:587 - /add_customer.rb:595: 0.0 (0%) #1
171
- /add_customer.rb:595 - /add_customer.rb:603: 0.0 (0%) #1
172
- /add_customer.rb:603 - /add_customer.rb:612: 0.0 (0%) #1
173
- /add_customer.rb:612 - /add_customer.rb:617: 0.0 (0%) #1
174
- "
175
- }
176
- ```
177
-
178
-
179
- ## Contributing
180
-
181
- Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/timify.
182
-
183
-
184
- ## License
185
-
186
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
187
-
1
+ # Timify
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/timify.svg)](https://rubygems.org/gems/timify)
4
+
5
+ Easily calculates the time running (elapsed time) from one location to another inside your code and reports statistics. It helps you improve your code and find out which part of your code is consuming more time.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'timify'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install timify
22
+
23
+ ## Usage
24
+
25
+ ### initialize
26
+ You need to supply a name for your Timify instance.
27
+ You can have all the Timify instances you want at the same time.
28
+ ```ruby
29
+ t = Timify.new :create_user
30
+ t.show = false
31
+ t.min_time_to_show = 0.5
32
+ ```
33
+ min_time_to_show: minimum time to show the elapsed time when calling 'add' method
34
+
35
+ show: print out results on screen
36
+
37
+ you can supply those parameters also:
38
+
39
+ ```ruby
40
+ t = Timify.new :create_user, show: true, min_time_to_show: 0.3
41
+ ```
42
+
43
+ The scopes of the instances can be even global so you can measure the elapsed times between different classes, methods... on your code.
44
+
45
+ You can disable the timify counter whenever you want using the status attritute that admits :on or :off, by default is set to :on
46
+
47
+ ```ruby
48
+ t.status = :off
49
+ ```
50
+
51
+ ### add
52
+
53
+ Adds a new point to count the elapsed time. It will count from the last 'add' call or Timify creation in case of the first 'add'.
54
+
55
+ You can supply a label that will summarize all the ones with the same label
56
+
57
+ The output of this method will be the time elapsed in seconds (float).
58
+
59
+ Examples:
60
+
61
+ ```ruby
62
+ t=Timify.new :example
63
+ t.add; run_sqls; t.add :database
64
+ t.add
65
+ #some processes
66
+ t.add
67
+ #some processes
68
+ send_email_alert(t.totals[:message]) if t.add > 0.2
69
+ #some processes
70
+ do_log(t.totals[:message]) if t.add > 0.5
71
+ ```
72
+
73
+ ### totals
74
+
75
+ Returns all data for this instance
76
+
77
+ In case json parameter supplied as true, the output will be in json format instead of a hash.
78
+
79
+ The output hash contains:
80
+
81
+ name: (String) name given for this instance'
82
+
83
+ total_time: (float) total elapsed time from initialization to last 'add' call
84
+
85
+ started: (Time)
86
+
87
+ finished: (Time)
88
+
89
+ message: (String) a printable friendly message giving all information
90
+
91
+ locations, labels, ranges: (Hash) the resultant hash contains:
92
+
93
+ secs: (float) number of seconds
94
+
95
+ percent: (integer) percentage in reference to the total time
96
+
97
+ count: (integer) number of times
98
+
99
+ locations: (Hash) All summary data by location where was called
100
+
101
+ labels: (Hash) All summary data by label given on 'add' method
102
+
103
+ ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
104
+
105
+
106
+ Example of output:
107
+ ```ruby
108
+ { :name=>:add_customer_wrong,
109
+ :total_time=>4.461446,
110
+ :started=>2017-11-06 16:10:53 +0000,
111
+ :finished=>2017-11-06 16:10:57 +0000,
112
+ :locations=>{
113
+ "/add_customer.rb:509"=>{:secs=>0.0, :percent=>0, :count=>1},
114
+ "/add_customer.rb:529"=>{:secs=>0.008001, :percent=>0, :count=>1},
115
+ "/add_customer.rb:532"=>{:secs=>0.212021, :percent=>5, :count=>1},
116
+ "/add_customer.rb:569"=>{:secs=>0.006001, :percent=>0, :count=>1},
117
+ "/add_customer.rb:581"=>{:secs=>0.446045, :percent=>10, :count=>1},
118
+ "/add_customer.rb:583"=>{:secs=>3.789378, :percent=>85, :count=>1},
119
+ "/add_customer.rb:585"=>{:secs=>0.0, :percent=>0, :count=>1},
120
+ "/add_customer.rb:587"=>{:secs=>0.0, :percent=>0, :count=>1},
121
+ "/add_customer.rb:595"=>{:secs=>0.0, :percent=>0, :count=>1},
122
+ "/add_customer.rb:603"=>{:secs=>0.0, :percent=>0, :count=>1},
123
+ "/add_customer.rb:612"=>{:secs=>0.0, :percent=>0, :count=>1},
124
+ "/add_customer.rb:617"=>{:secs=>0.0, :percent=>0, :count=>1}
125
+ },
126
+ :labels=>{
127
+ :database_access=>{:secs=>4,447444, :percent=>99, :count=>3},
128
+ :checkouts=>{:secs=>0.0, :percent=>0, :count=>2},
129
+ },
130
+ :ranges=>{
131
+ "/add_customer.rb:509 - /add_customer.rb:529"=>{:secs=>0.008001, :percent=>0, :count=>1},
132
+ "/add_customer.rb:529 - /add_customer.rb:532"=>{:secs=>0.212021, :percent=>5, :count=>1},
133
+ "/add_customer.rb:532 - /add_customer.rb:569"=>{:secs=>0.006001, :percent=>0, :count=>1},
134
+ "/add_customer.rb:569 - /add_customer.rb:581"=>{:secs=>0.446045, :percent=>10, :count=>1},
135
+ "/add_customer.rb:581 - /add_customer.rb:583"=>{:secs=>3.789378, :percent=>85, :count=>1},
136
+ "/add_customer.rb:583 - /add_customer.rb:585"=>{:secs=>0.0, :percent=>0, :count=>1},
137
+ "/add_customer.rb:585 - /add_customer.rb:587"=>{:secs=>0.0, :percent=>0, :count=>1},
138
+ "/add_customer.rb:587 - /add_customer.rb:595"=>{:secs=>0.0, :percent=>0, :count=>1},
139
+ "/add_customer.rb:595 - /add_customer.rb:603"=>{:secs=>0.0, :percent=>0, :count=>1},
140
+ "/add_customer.rb:603 - /add_customer.rb:612"=>{:secs=>0.0, :percent=>0, :count=>1},
141
+ "/add_customer.rb:612 - /add_customer.rb:617"=>{:secs=>0.0, :percent=>0, :count=>1}
142
+ },
143
+ :message=>"
144
+
145
+ Total time <add_customer_wrong>:4.46
146
+ Total time by location:
147
+ /add_customer.rb:509: 0.0 (0%) #1
148
+ /add_customer.rb:529: 0.01 (0%) #1
149
+ /add_customer.rb:532: 0.21 (5%) #1
150
+ /add_customer.rb:569: 0.01 (0%) #1
151
+ /add_customer.rb:581: 0.45 (10%) #1
152
+ /add_customer.rb:583: 3.79 (85%) #1
153
+ /add_customer.rb:585: 0.0 (0%) #1
154
+ /add_customer.rb:587: 0.0 (0%) #1
155
+ /add_customer.rb:595: 0.0 (0%) #1
156
+ /add_customer.rb:603: 0.0 (0%) #1
157
+ /add_customer.rb:612: 0.0 (0%) #1
158
+ /add_customer.rb:617: 0.0 (0%) #1
159
+ Total time by label:
160
+ database_access: 4.45 (99%) #3
161
+ checkouts: 0.0 (0%) #2
162
+ Total time by range:
163
+ /add_customer.rb:509 - /add_customer.rb:529: 0.01 (0%) #1
164
+ /add_customer.rb:529 - /add_customer.rb:532: 0.21 (5%) #1
165
+ /add_customer.rb:532 - /add_customer.rb:569: 0.01 (0%) #1
166
+ /add_customer.rb:569 - /add_customer.rb:581: 0.45 (10%) #1
167
+ /add_customer.rb:581 - /add_customer.rb:583: 3.79 (85%) #1
168
+ /add_customer.rb:583 - /add_customer.rb:585: 0.0 (0%) #1
169
+ /add_customer.rb:585 - /add_customer.rb:587: 0.0 (0%) #1
170
+ /add_customer.rb:587 - /add_customer.rb:595: 0.0 (0%) #1
171
+ /add_customer.rb:595 - /add_customer.rb:603: 0.0 (0%) #1
172
+ /add_customer.rb:603 - /add_customer.rb:612: 0.0 (0%) #1
173
+ /add_customer.rb:612 - /add_customer.rb:617: 0.0 (0%) #1
174
+ "
175
+ }
176
+ ```
177
+
178
+
179
+ ## Contributing
180
+
181
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marioruiz/timify.
182
+
183
+
184
+ ## License
185
+
186
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
187
+
data/lib/timify.rb CHANGED
@@ -1,199 +1,199 @@
1
- ###############################################################
2
- # Calculates the time running from one location to another inside your code. More info: https://github.com/MarioRuiz/timify/
3
- #
4
- # attr_accessor:
5
- # min_time_to_show: minimum time to show the elapsed time when calling 'add' method
6
- # show: print out results on screen
7
- # status: (default :on) You can set :on or :off the status so it will counting the time or not
8
- #
9
- # attr_reader:
10
- # name: name given for the Timify instance
11
- # initial_time: when the instance was created
12
- # total: total time elapsed
13
- # max_time_spent: maximum time measured for this instance
14
- ###############################################################
15
- class Timify
16
- attr_accessor :min_time_to_show, :show, :status
17
- attr_reader :name, :total, :initial_time, :max_time_spent
18
-
19
- ###############################################################
20
- # input:
21
- # name: name for the instance
22
- # min_time_to_show: minimum time to show the elapsed time when calling 'add' method
23
- # show: print out results on screen
24
- #
25
- # examples:
26
- # t = Timify.new :create_user
27
- # t.show = false
28
- # $tim = Timify.new :dbprocess, show:false, min_time_to_show: 0.5
29
- ###############################################################
30
- def initialize(name, min_time_to_show: 0, show: true)
31
- @name=name
32
- @min_time_to_show=min_time_to_show
33
- @show=show
34
- @initial_time=Time.new
35
- @max_time_spent=0
36
- @timify_prev=@initial_time
37
- @location_prev=nil
38
- @timify={}
39
- @timify_by_label={}
40
- @timify_by_range={}
41
- @count={}
42
- @total=0
43
- puts "<#{@name}> Timify init:<#{@initial_time}>. Location: #{caller[0].scan(/(.+):in\s/).join}"
44
- end
45
-
46
- ###############################################################
47
- # Adds a new point to count the time elapsed.
48
- # It will count from the last 'add' call or Timify creation in case of the first 'add'.
49
- #
50
- # input:
51
- # label: (optional) In case supplied it will summarize all the ones with the same label
52
- #
53
- # output: (float)
54
- # time elapsed in seconds
55
- #
56
- # examples:
57
- # t=Timify.new :example
58
- # t.add; run_sqls; t.add :database
59
- # t.add
60
- # #some processes
61
- # t.add
62
- # #some processes
63
- # send_email_alert if t.add > 0.2
64
- # #some processes
65
- # do_log(t.totals[:message]) if t.add > 0.5
66
- ###############################################################
67
- def add(*label)
68
- return 0 if @status==:off
69
- if !label.empty?
70
- label=label[0]
71
- else
72
- label=""
73
- end
74
-
75
- time_now=Time.new
76
- time_spent=(time_now-@timify_prev).to_f
77
- @total=(time_now-@initial_time).to_f
78
- new_max=false
79
- location=caller[0].scan(/(.+):in\s/).join
80
- if time_spent > @max_time_spent then
81
- new_max = true
82
- @max_time_spent = time_spent
83
- end
84
- @timify[location]=@timify[location].to_f+time_spent
85
- @count[location]=@count[location].to_i+1
86
- if !label.empty?
87
- @timify_by_label[label]=@timify_by_label[label].to_f+time_spent
88
- @count[label]=@count[label].to_i+1
89
- end
90
- if !@location_prev.nil?
91
- @timify_by_range["#{@location_prev} - #{location}"]=@timify_by_range["#{@location_prev} - #{location}"].to_f + time_spent
92
- @count["#{@location_prev} - #{location}"]=@count["#{@location_prev} - #{location}"].to_i+1
93
- end
94
-
95
-
96
- if @total > 0
97
- percent=((@timify[location]/@total)*100).round(0)
98
- else
99
- percent=0
100
- end
101
- if time_spent>=@min_time_to_show
102
- if @show
103
- puts "<#{@name}>#{"<#{label}>" if !label.empty?}#{"(New Max)" if new_max}: #{location} (#{percent}%): #{@total.round(2)}; #{time_spent.round(2)}"
104
- end
105
- end
106
- @timify_prev=time_now
107
- @location_prev=location
108
- return time_spent
109
- end
110
-
111
-
112
- ###############################################################
113
- # returns all data for this instance
114
- #
115
- # input:
116
- # json: (boolean) in case of true the output will be in json format instead of a hash
117
- #
118
- # output: (Hash or json string)
119
- # name: (String) name given for this instance
120
- # total_time: (float) total elapsed time from initialization to last 'add' call
121
- # started: (Time)
122
- # finished: (Time)
123
- # message: (String) a printable friendly message giving all information
124
- # locations, labels, ranges: (Hash) the resultant hash contains:
125
- # secs: (float) number of seconds
126
- # percent: (integer) percentage in reference to the total time
127
- # count: (integer) number of times
128
- # locations: (Hash) All summary data by location where was called
129
- # labels: (Hash) All summary data by label given on 'add' method
130
- # ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
131
- ###############################################################
132
- def totals(json: false)
133
- return {} if @status==:off
134
- require 'json' if json
135
- output={
136
- name: @name,
137
- total_time: @total.to_f,
138
- started: @initial_time,
139
- finished: @timify_prev,
140
- locations: {},
141
- labels: {},
142
- ranges: {}
143
- }
144
- message="\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}"
145
- message+="\nTotal time by location:\n"
146
- @timify.each {|location, secs|
147
- if @total==0 then
148
- percent=0
149
- else
150
- percent=(secs*100/(@total).to_f).round(0)
151
- end
152
- message+= "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}"
153
- output[:locations][location]={
154
- secs: secs,
155
- percent: percent,
156
- count: @count[location]
157
- }
158
- }
159
- if !@timify_by_label.empty?
160
- message+= "\nTotal time by label:\n"
161
- @timify_by_label.each {|label, secs|
162
- if @total==0 then
163
- percent=0
164
- else
165
- percent=(secs*100/(@total).to_f).round(0)
166
- end
167
- message+= "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}"
168
- output[:labels][label]={
169
- secs: secs,
170
- percent: percent,
171
- count: @count[label]
172
- }
173
- }
174
- end
175
-
176
- if !@timify_by_range.empty?
177
- message+= "\nTotal time by range:\n"
178
- @timify_by_range.each {|range, secs|
179
- if @total==0 then
180
- percent=0
181
- else
182
- percent=(secs*100/(@total).to_f).round(0)
183
- end
184
- message+= "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}"
185
- output[:ranges][range]={
186
- secs: secs,
187
- percent: percent,
188
- count: @count[range]
189
- }
190
- }
191
- end
192
-
193
- message+= "\n\n"
194
- output[:message]=message
195
- puts message if @show
196
- output=output.to_json if json
197
- return output
198
- end
199
- end
1
+ ###############################################################
2
+ # Calculates the time running from one location to another inside your code. More info: https://github.com/MarioRuiz/timify/
3
+ #
4
+ # attr_accessor:
5
+ # min_time_to_show: minimum time to show the elapsed time when calling 'add' method
6
+ # show: print out results on screen
7
+ # status: (default :on) You can set :on or :off the status so it will counting the time or not
8
+ #
9
+ # attr_reader:
10
+ # name: name given for the Timify instance
11
+ # initial_time: when the instance was created
12
+ # total: total time elapsed
13
+ # max_time_spent: maximum time measured for this instance
14
+ ###############################################################
15
+ class Timify
16
+ attr_accessor :min_time_to_show, :show, :status
17
+ attr_reader :name, :total, :initial_time, :max_time_spent
18
+
19
+ ###############################################################
20
+ # input:
21
+ # name: name for the instance
22
+ # min_time_to_show: minimum time to show the elapsed time when calling 'add' method
23
+ # show: print out results on screen
24
+ #
25
+ # examples:
26
+ # t = Timify.new :create_user
27
+ # t.show = false
28
+ # $tim = Timify.new :dbprocess, show:false, min_time_to_show: 0.5
29
+ ###############################################################
30
+ def initialize(name, min_time_to_show: 0, show: true)
31
+ @name=name
32
+ @min_time_to_show=min_time_to_show
33
+ @show=show
34
+ @initial_time=Time.new
35
+ @max_time_spent=0
36
+ @timify_prev=@initial_time
37
+ @location_prev=nil
38
+ @timify={}
39
+ @timify_by_label={}
40
+ @timify_by_range={}
41
+ @count={}
42
+ @total=0
43
+ puts "<#{@name}> Timify init:<#{@initial_time}>. Location: #{caller[0].scan(/(.+):in\s/).join}"
44
+ end
45
+
46
+ ###############################################################
47
+ # Adds a new point to count the time elapsed.
48
+ # It will count from the last 'add' call or Timify creation in case of the first 'add'.
49
+ #
50
+ # input:
51
+ # label: (optional) In case supplied it will summarize all the ones with the same label
52
+ #
53
+ # output: (float)
54
+ # time elapsed in seconds
55
+ #
56
+ # examples:
57
+ # t=Timify.new :example
58
+ # t.add; run_sqls; t.add :database
59
+ # t.add
60
+ # #some processes
61
+ # t.add
62
+ # #some processes
63
+ # send_email_alert if t.add > 0.2
64
+ # #some processes
65
+ # do_log(t.totals[:message]) if t.add > 0.5
66
+ ###############################################################
67
+ def add(*label)
68
+ return 0 if @status==:off
69
+ if !label.empty?
70
+ label=label[0]
71
+ else
72
+ label=""
73
+ end
74
+
75
+ time_now=Time.new
76
+ time_spent=(time_now-@timify_prev).to_f
77
+ @total=(time_now-@initial_time).to_f
78
+ new_max=false
79
+ location=caller[0].scan(/(.+):in\s/).join
80
+ if time_spent > @max_time_spent then
81
+ new_max = true
82
+ @max_time_spent = time_spent
83
+ end
84
+ @timify[location]=@timify[location].to_f+time_spent
85
+ @count[location]=@count[location].to_i+1
86
+ if !label.empty?
87
+ @timify_by_label[label]=@timify_by_label[label].to_f+time_spent
88
+ @count[label]=@count[label].to_i+1
89
+ end
90
+ if !@location_prev.nil?
91
+ @timify_by_range["#{@location_prev} - #{location}"]=@timify_by_range["#{@location_prev} - #{location}"].to_f + time_spent
92
+ @count["#{@location_prev} - #{location}"]=@count["#{@location_prev} - #{location}"].to_i+1
93
+ end
94
+
95
+
96
+ if @total > 0
97
+ percent=((@timify[location]/@total)*100).round(0)
98
+ else
99
+ percent=0
100
+ end
101
+ if time_spent>=@min_time_to_show
102
+ if @show
103
+ puts "<#{@name}>#{"<#{label}>" if !label.empty?}#{"(New Max)" if new_max}: #{location} (#{percent}%): #{@total.round(2)}; #{time_spent.round(2)}"
104
+ end
105
+ end
106
+ @timify_prev=time_now
107
+ @location_prev=location
108
+ return time_spent
109
+ end
110
+
111
+
112
+ ###############################################################
113
+ # returns all data for this instance
114
+ #
115
+ # input:
116
+ # json: (boolean) in case of true the output will be in json format instead of a hash
117
+ #
118
+ # output: (Hash or json string)
119
+ # name: (String) name given for this instance
120
+ # total_time: (float) total elapsed time from initialization to last 'add' call
121
+ # started: (Time)
122
+ # finished: (Time)
123
+ # message: (String) a printable friendly message giving all information
124
+ # locations, labels, ranges: (Hash) the resultant hash contains:
125
+ # secs: (float) number of seconds
126
+ # percent: (integer) percentage in reference to the total time
127
+ # count: (integer) number of times
128
+ # locations: (Hash) All summary data by location where was called
129
+ # labels: (Hash) All summary data by label given on 'add' method
130
+ # ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
131
+ ###############################################################
132
+ def totals(json: false)
133
+ return {} if @status==:off
134
+ require 'json' if json
135
+ output={
136
+ name: @name,
137
+ total_time: @total.to_f,
138
+ started: @initial_time,
139
+ finished: @timify_prev,
140
+ locations: {},
141
+ labels: {},
142
+ ranges: {}
143
+ }
144
+ message="\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}"
145
+ message+="\nTotal time by location:\n"
146
+ @timify.each {|location, secs|
147
+ if @total==0 then
148
+ percent=0
149
+ else
150
+ percent=(secs*100/(@total).to_f).round(0)
151
+ end
152
+ message+= "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}\n"
153
+ output[:locations][location]={
154
+ secs: secs,
155
+ percent: percent,
156
+ count: @count[location]
157
+ }
158
+ }
159
+ if !@timify_by_label.empty?
160
+ message+= "\nTotal time by label:\n"
161
+ @timify_by_label.each {|label, secs|
162
+ if @total==0 then
163
+ percent=0
164
+ else
165
+ percent=(secs*100/(@total).to_f).round(0)
166
+ end
167
+ message+= "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}\n"
168
+ output[:labels][label]={
169
+ secs: secs,
170
+ percent: percent,
171
+ count: @count[label]
172
+ }
173
+ }
174
+ end
175
+
176
+ if !@timify_by_range.empty?
177
+ message+= "\nTotal time by range:\n"
178
+ @timify_by_range.each {|range, secs|
179
+ if @total==0 then
180
+ percent=0
181
+ else
182
+ percent=(secs*100/(@total).to_f).round(0)
183
+ end
184
+ message+= "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}\n"
185
+ output[:ranges][range]={
186
+ secs: secs,
187
+ percent: percent,
188
+ count: @count[range]
189
+ }
190
+ }
191
+ end
192
+
193
+ message+= "\n\n"
194
+ output[:message]=message
195
+ puts message if @show
196
+ output=output.to_json if json
197
+ return output
198
+ end
199
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: timify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mario Ruiz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-30 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Easily calculate the time running (elapsed time) from one location to
14
14
  another inside your code and reports statistics. It helps you improve your code
@@ -43,8 +43,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
43
43
  - !ruby/object:Gem::Version
44
44
  version: '0'
45
45
  requirements: []
46
- rubyforge_project:
47
- rubygems_version: 2.7.6
46
+ rubygems_version: 3.0.3
48
47
  signing_key:
49
48
  specification_version: 4
50
49
  summary: Easily calculate the time running (elapsed time) from one location to another