time_up 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -4
- data/lib/time_up.rb +22 -17
- data/lib/time_up/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e94465307a2737c930d8dbd195c92fe315850abf8c7be4b1c6f88f1eec2d1bd
|
4
|
+
data.tar.gz: 82cac53d1b5c18acdeb7312da3cc929bed1590e707d8ac8edeb0480fff13af29
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0a1395d51df9233140f9f7b7dd49d6cdc3bc1c50eea76425da7300a6b165230c17452ecaf7b1721c50a9b87f788775913b93ada281906ab681a19376976a34c
|
7
|
+
data.tar.gz: 12856340affc8a8a298986c8b56c01198256346c5cd0caf8f7fac2726ea742d43fc05e113c7f93960c34cae08318b5c5ac912b5b5adb716e03748bef56818b1a
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -6,10 +6,10 @@ but don't necessarily want to reach for
|
|
6
6
|
Try `time_up`!
|
7
7
|
|
8
8
|
This gem is especially useful for long-running processes (like test suites) that
|
9
|
-
have several time-intensive operations that are repeated over
|
10
|
-
want to measure
|
11
|
-
test suite spends creating factories, truncating the database, or
|
12
|
-
critical code path.)
|
9
|
+
have several time-intensive operations that are repeated over the life of the
|
10
|
+
process and that you want to measure in aggregate. (For example, to see how
|
11
|
+
much time your test suite spends creating factories, truncating the database, or
|
12
|
+
invoking a critical code path.)
|
13
13
|
|
14
14
|
## Install
|
15
15
|
|
data/lib/time_up.rb
CHANGED
@@ -3,10 +3,11 @@ require_relative "time_up/version"
|
|
3
3
|
module TimeUp
|
4
4
|
class Error < StandardError; end
|
5
5
|
|
6
|
-
|
6
|
+
Thread.current[:time_up_timers] = {}
|
7
|
+
|
7
8
|
def self.start(name, &blk)
|
8
9
|
raise Error.new("Timer name must be a String or Symbol") unless name.is_a?(Symbol) || name.is_a?(String)
|
9
|
-
timer =
|
10
|
+
timer = __timers[name] ||= Timer.new(name)
|
10
11
|
timer.start
|
11
12
|
if blk
|
12
13
|
blk.call
|
@@ -17,42 +18,42 @@ module TimeUp
|
|
17
18
|
|
18
19
|
# Delegate methods
|
19
20
|
def self.timer(name)
|
20
|
-
|
21
|
+
__timers[name]
|
21
22
|
end
|
22
23
|
|
23
24
|
def self.stop(name)
|
24
25
|
__ensure_timer(name)
|
25
|
-
|
26
|
+
__timers[name].stop
|
26
27
|
end
|
27
28
|
|
28
29
|
def self.elapsed(name)
|
29
30
|
__ensure_timer(name)
|
30
|
-
|
31
|
+
__timers[name].elapsed
|
31
32
|
end
|
32
33
|
|
33
34
|
def self.reset(name)
|
34
35
|
__ensure_timer(name)
|
35
|
-
|
36
|
+
__timers[name].reset
|
36
37
|
end
|
37
38
|
|
38
39
|
# Interrogative methods
|
39
40
|
def self.total_elapsed
|
40
|
-
|
41
|
+
__timers.values.sum(&:elapsed)
|
41
42
|
end
|
42
43
|
|
43
44
|
def self.all_elapsed
|
44
|
-
|
45
|
+
__timers.values.map { |timer|
|
45
46
|
[timer.name, timer.elapsed]
|
46
47
|
}.to_h
|
47
48
|
end
|
48
49
|
|
49
50
|
def self.active_timers
|
50
|
-
|
51
|
+
__timers.values.select(&:active?)
|
51
52
|
end
|
52
53
|
|
53
54
|
def self.print_summary(io = $stdout)
|
54
|
-
longest_name_length =
|
55
|
-
summaries =
|
55
|
+
longest_name_length = __timers.values.map { |t| t.name.inspect.size }.max
|
56
|
+
summaries = __timers.values.map { |timer|
|
56
57
|
name = "#{timer.name.inspect}#{"*" if timer.active?}".ljust(longest_name_length + 1)
|
57
58
|
"#{name}\t#{"%.5f" % timer.elapsed}s"
|
58
59
|
}
|
@@ -62,27 +63,31 @@ module TimeUp
|
|
62
63
|
========================
|
63
64
|
#{summaries.join("\n")}
|
64
65
|
|
65
|
-
#{"* Denotes that the timer is still active\n" if
|
66
|
+
#{"* Denotes that the timer is still active\n" if __timers.values.any?(&:active?)}
|
66
67
|
SUMMARY
|
67
68
|
end
|
68
69
|
|
69
70
|
# Iterative methods
|
70
71
|
def self.stop_all
|
71
|
-
|
72
|
+
__timers.values.each(&:stop)
|
72
73
|
end
|
73
74
|
|
74
75
|
def self.reset_all
|
75
|
-
|
76
|
+
__timers.values.each(&:reset)
|
76
77
|
end
|
77
78
|
|
78
79
|
def self.delete_all
|
79
|
-
|
80
|
-
|
80
|
+
__timers.values.each { |t| t.reset(force: true) }
|
81
|
+
Thread.current[:time_up_timers] = {}
|
81
82
|
end
|
82
83
|
|
83
84
|
# Internal methods
|
85
|
+
def self.__timers
|
86
|
+
Thread.current[:time_up_timers]
|
87
|
+
end
|
88
|
+
|
84
89
|
def self.__ensure_timer(name)
|
85
|
-
raise Error.new("No timer named #{name.inspect}") unless
|
90
|
+
raise Error.new("No timer named #{name.inspect}") unless __timers[name]
|
86
91
|
end
|
87
92
|
|
88
93
|
class Timer
|
data/lib/time_up/version.rb
CHANGED