timify 0.0.1
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.
- checksums.yaml +7 -0
- data/lib/timify.rb +187 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: adc306de79305613ee050cea9d797ee57253ee39
|
4
|
+
data.tar.gz: 838de7697e2690ff71e630e9d45b8a73257b5d6b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75ffacabee4a87ec49dd97845ac15333541a10ef5d2d9c776cb9e8b43cc48caf84ec9ad019efe0337a98e15c2fecdcedd54d4378f81102912902104352ad4321
|
7
|
+
data.tar.gz: aa34ac09d253c2047e289b59b24fbbd786f4573eb1d4b4243b2fed6ba65b1bbc449c7b2353a0da01b9d631747349f1bd68a7e3682b71a1aaf385bf480af145a8
|
data/lib/timify.rb
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
###############################################################
|
2
|
+
# Calculates the time running from one location to another inside your code
|
3
|
+
# attr_accessor:
|
4
|
+
# min_time_to_show: minimum time to show the elapsed time when calling 'add' method
|
5
|
+
# show: print out results on screen
|
6
|
+
# attr_reader:
|
7
|
+
# name: name given for the Timify instance
|
8
|
+
# initial_time: when the instance was created
|
9
|
+
# total: total time elapsed
|
10
|
+
# max_time_spent: maximum time measured for this instance
|
11
|
+
###############################################################
|
12
|
+
class Timify
|
13
|
+
attr_accessor :min_time_to_show, :show
|
14
|
+
attr_reader :name, :total, :initial_time, :max_time_spent
|
15
|
+
|
16
|
+
###############################################################
|
17
|
+
# input:
|
18
|
+
# name: name for the instance
|
19
|
+
# min_time_to_show: minimum time to show the elapsed time when calling 'add' method
|
20
|
+
# show: print out results on screen
|
21
|
+
# examples:
|
22
|
+
# t = Timify.new :create_user
|
23
|
+
# t.show = false
|
24
|
+
# $tim = Timify.new :dbprocess, show:false, min_time_to_show: 0.5
|
25
|
+
###############################################################
|
26
|
+
def initialize(name, min_time_to_show: 0, show: true)
|
27
|
+
@name=name
|
28
|
+
@min_time_to_show=min_time_to_show
|
29
|
+
@show=show
|
30
|
+
@initial_time=Time.new
|
31
|
+
@max_time_spent=0
|
32
|
+
@timify_prev=@initial_time
|
33
|
+
@location_prev=nil
|
34
|
+
@timify={}
|
35
|
+
@timify_by_label={}
|
36
|
+
@timify_by_range={}
|
37
|
+
@count={}
|
38
|
+
@total=0
|
39
|
+
puts "<#{@name}> Timify init:<#{@initial_time}>. Location: #{caller[0].scan(/(.+):in\s/).join}"
|
40
|
+
end
|
41
|
+
|
42
|
+
###############################################################
|
43
|
+
# Adds a new point to count the time elapsed. It will count from the last 'add' call or Timify creation in case of the first 'add'.
|
44
|
+
# input:
|
45
|
+
# label: (optional) In case supplied it will summarize all the ones with the same label
|
46
|
+
# output: (float)
|
47
|
+
# time elapsed in seconds
|
48
|
+
# examples:
|
49
|
+
# t=Timify.new :example
|
50
|
+
# t.add; run_sqls; t.add :database
|
51
|
+
# t.add
|
52
|
+
# #some processes
|
53
|
+
# t.add
|
54
|
+
# #some processes
|
55
|
+
# send_email_alert if t.add > 0.2
|
56
|
+
# #some processes
|
57
|
+
# do_log(t.totals[:message]) if t.add > 0.5
|
58
|
+
###############################################################
|
59
|
+
def add(*label)
|
60
|
+
if !label.empty?
|
61
|
+
label=label[0]
|
62
|
+
else
|
63
|
+
label=""
|
64
|
+
end
|
65
|
+
|
66
|
+
time_now=Time.new
|
67
|
+
time_spent=(time_now-@timify_prev).to_f
|
68
|
+
@total=(time_now-@initial_time).to_f
|
69
|
+
new_max=false
|
70
|
+
location=caller[0].scan(/(.+):in\s/).join
|
71
|
+
if time_spent > @max_time_spent then
|
72
|
+
new_max = true
|
73
|
+
@max_time_spent = time_spent
|
74
|
+
end
|
75
|
+
@timify[location]=@timify[location].to_f+time_spent
|
76
|
+
@count[location]=@count[location].to_i+1
|
77
|
+
if !label.empty?
|
78
|
+
@timify_by_label[label]=@timify_by_label[label].to_f+time_spent
|
79
|
+
@count[label]=@count[label].to_i+1
|
80
|
+
end
|
81
|
+
if !@location_prev.nil?
|
82
|
+
@timify_by_range["#{@location_prev} - #{location}"]=@timify_by_range["#{@location_prev} - #{location}"].to_f + time_spent
|
83
|
+
@count["#{@location_prev} - #{location}"]=@count["#{@location_prev} - #{location}"].to_i+1
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
if @total > 0
|
88
|
+
percent=((@timify[location]/@total)*100).round(0)
|
89
|
+
else
|
90
|
+
percent=0
|
91
|
+
end
|
92
|
+
if time_spent>=@min_time_to_show
|
93
|
+
if @show
|
94
|
+
puts "<#{@name}>#{"<#{label}>" if !label.empty?}#{"(New Max)" if new_max}: #{location} (#{percent}%): #{@total.round(2)}; #{time_spent.round(2)}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
@timify_prev=time_now
|
98
|
+
@location_prev=location
|
99
|
+
return time_spent
|
100
|
+
end
|
101
|
+
|
102
|
+
|
103
|
+
###############################################################
|
104
|
+
# returns all data for this instance
|
105
|
+
# input:
|
106
|
+
# json: (boolean) in case of true the output will be in json format instead of a hash
|
107
|
+
# output: (Hash or json string)
|
108
|
+
# name: (String) name given for this instance'
|
109
|
+
# total_time: (float) total elapsed time from initialization to last 'add' call
|
110
|
+
# started: (Time)
|
111
|
+
# finished: (Time)
|
112
|
+
# message: (String) a printable friendly message giving all information
|
113
|
+
# locations, labels, ranges: (Hash) the resultant hash contains:
|
114
|
+
# secs: (float) number of seconds
|
115
|
+
# percent: (integer) percentage in reference to the total time
|
116
|
+
# count: (integer) number of times
|
117
|
+
# locations: (Hash) All summary data by location where was called
|
118
|
+
# labels: (Hash) All summary data by label given on 'add' method
|
119
|
+
# ranges: (Hash) All summary data by ranges where was called, from last 'add' call to current 'add' call
|
120
|
+
###############################################################
|
121
|
+
def totals(json: false)
|
122
|
+
require 'json' if json
|
123
|
+
output={
|
124
|
+
name: @name,
|
125
|
+
total_time: @total.to_f,
|
126
|
+
started: @initial_time,
|
127
|
+
finished: @timify_prev,
|
128
|
+
locations: {},
|
129
|
+
labels: {},
|
130
|
+
ranges: {}
|
131
|
+
}
|
132
|
+
message="\n\nTotal time <#{@name}>:#{@total.to_f.round(2)}"
|
133
|
+
message+="\nTotal time by location:\n"
|
134
|
+
@timify.each {|location, secs|
|
135
|
+
if @total==0 then
|
136
|
+
percent=0
|
137
|
+
else
|
138
|
+
percent=(secs*100/(@total).to_f).round(0)
|
139
|
+
end
|
140
|
+
message+= "\t#{location}: #{secs.round(2)} (#{percent}%) ##{@count[location]}"
|
141
|
+
output[:locations][location]={
|
142
|
+
secs: secs,
|
143
|
+
percent: percent,
|
144
|
+
count: @count[location]
|
145
|
+
}
|
146
|
+
}
|
147
|
+
if !@timify_by_label.empty?
|
148
|
+
message+= "\nTotal time by label:\n"
|
149
|
+
@timify_by_label.each {|label, secs|
|
150
|
+
if @total==0 then
|
151
|
+
percent=0
|
152
|
+
else
|
153
|
+
percent=(secs*100/(@total).to_f).round(0)
|
154
|
+
end
|
155
|
+
message+= "\t#{label}: #{secs.round(2)} (#{percent}%) ##{@count[label]}"
|
156
|
+
output[:labels][label]={
|
157
|
+
secs: secs,
|
158
|
+
percent: percent,
|
159
|
+
count: @count[label]
|
160
|
+
}
|
161
|
+
}
|
162
|
+
end
|
163
|
+
|
164
|
+
if !@timify_by_range.empty?
|
165
|
+
message+= "\nTotal time by range:\n"
|
166
|
+
@timify_by_range.each {|range, secs|
|
167
|
+
if @total==0 then
|
168
|
+
percent=0
|
169
|
+
else
|
170
|
+
percent=(secs*100/(@total).to_f).round(0)
|
171
|
+
end
|
172
|
+
message+= "\t#{range}: #{secs.round(2)} (#{percent}%) ##{@count[range]}"
|
173
|
+
output[:ranges][range]={
|
174
|
+
secs: secs,
|
175
|
+
percent: percent,
|
176
|
+
count: @count[range]
|
177
|
+
}
|
178
|
+
}
|
179
|
+
end
|
180
|
+
|
181
|
+
message+= "\n\n"
|
182
|
+
output[:message]=message
|
183
|
+
puts message if @show
|
184
|
+
output=output.to_json if json
|
185
|
+
return output
|
186
|
+
end
|
187
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: timify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mario Ruiz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Calculates the time running from one location to another inside your
|
14
|
+
code and reports summaries
|
15
|
+
email: marioruizs@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/timify.rb
|
21
|
+
homepage: https://github.com/MarioRuiz/timify
|
22
|
+
licenses:
|
23
|
+
- MIT
|
24
|
+
metadata: {}
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 2.6.11
|
42
|
+
signing_key:
|
43
|
+
specification_version: 4
|
44
|
+
summary: Calculates the time running and reports summaries
|
45
|
+
test_files: []
|