time_up 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3e94465307a2737c930d8dbd195c92fe315850abf8c7be4b1c6f88f1eec2d1bd
4
- data.tar.gz: 82cac53d1b5c18acdeb7312da3cc929bed1590e707d8ac8edeb0480fff13af29
3
+ metadata.gz: 5b040cfa422a59cc1c7f2157e8f7261ed1bdc7c470da76fead75b026159f02c8
4
+ data.tar.gz: 4b817578713714392dd0ab8f2694bce78c6ddf93e9b5eea16f302a5d4549c559
5
5
  SHA512:
6
- metadata.gz: b0a1395d51df9233140f9f7b7dd49d6cdc3bc1c50eea76425da7300a6b165230c17452ecaf7b1721c50a9b87f788775913b93ada281906ab681a19376976a34c
7
- data.tar.gz: 12856340affc8a8a298986c8b56c01198256346c5cd0caf8f7fac2726ea742d43fc05e113c7f93960c34cae08318b5c5ac912b5b5adb716e03748bef56818b1a
6
+ metadata.gz: 3b4fe6bff25861a0b9b76b95684e25eed9056886091556f22b70820251ea3f41b08879b2628dd8b8bee74e03720b0f39d7af87af5664c0bcaa152f7e8b88d329
7
+ data.tar.gz: e0b0953129fc39ed079d3b6addebf579d8defbebcda484649a9826ff0a3e3b2383360d7d8269572746b5f5469cb076d4e6fa770ed36735f1c2fe55f319c91c5b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ # 0.0.3
2
+
3
+ - Change the return value of TimeUp.start when passed a block to be the
4
+ evaluated value of the block (for easier insertion into existing code without
5
+ adding a bunch of new assignment and returns)
6
+ - Allow timer instances' `start` method to be called with a block
7
+ - Add `timings`, `count`, `min`, `max`, and `mean` methods for basic stats
8
+ tracking
9
+ - Add `TimeUp.all_stats` to roll up all these
10
+
1
11
  # 0.0.2
2
12
 
3
13
  - Switch from a module method to Thread.current variable
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- time_up (0.0.2)
4
+ time_up (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -65,10 +65,10 @@ sleep 5
65
65
  puts TimeUp.stop :eggs # => ~5.0
66
66
  ```
67
67
 
68
- `TimeUp.start` also returns an instance of the named timer, which has its own
69
- `start`, `stop`, `elaped`, and `reset` methods. If you want to find that
70
- instance later, you can also call `TimeUp.timer(:some_name)`. So the above
71
- example could be rewritten as:
68
+ When passes without a block, `TimeUp.start` returns an instance of the timer,
69
+ which has its own `start`, `stop`, `elaped`, and `reset` methods. If you want to
70
+ find that instance later, you can also call `TimeUp.timer(:some_name)`. So the
71
+ above example could be rewritten as:
72
72
 
73
73
  ```ruby
74
74
  egg_timer = TimeUp.start :eggs
@@ -124,18 +124,35 @@ straightforward, so I'd encourage you to [read the code](/lib/time_up.rb).
124
124
 
125
125
  ### `TimeUp` module
126
126
 
127
- `TimeUp.start(name, [&blk])` - Starts (or restarts) and returns a named `Timer`
127
+ `TimeUp.timer(name)` - Returns the `Timer` instance named `name` (creating it,
128
+ if it doesn't exist)
128
129
 
129
- `TimeUp.timer(name)` - Returns any a `Timer` instance named `name` or `nil`
130
+ `TimeUp.start(name, [&blk])` - Starts (or restarts) a named
131
+ [Timer](#timeuptimer-class). If passed with a block, will return whatever the
132
+ block evaluates to. If passed without a block, it will return the timer object
130
133
 
131
134
  `TimeUp.stop(name)` - Stops the named timer or raises if it's not defined
132
135
 
136
+ `TimeUp.reset(name)` - Resets the named timer's elapsed time to 0, effectively
137
+ restarting it if it's currently running. Raises if the timer isn't defined.
138
+
133
139
  `TimeUp.elapsed(name)` - Returns a `Float` of the total elapsed seconds that the
134
140
  named timer has been running (and raises if no timer is defined with the given
135
141
  `name`)
136
142
 
137
- `TimeUp.reset(name)` - Resets the named timer's elapsed time to 0, effectively
138
- restarting it if it's currently running. Raises if the timer isn't defined.
143
+ `TimeUp.timings(name)` - Returns an array of each recorded start-to-stop
144
+ duration of the timer (including the current one, if active)
145
+
146
+ `TimeUp.count(name)` - The number of times the timer has been started and
147
+ stopped (including the current timing, if active)
148
+
149
+ `TimeUp.min(name)` - The shortest recording of the timer (including the current
150
+ one, if active)
151
+
152
+ `TimeUp.max(name)` - The longest recording of the timer (including the current
153
+ one, if active)
154
+
155
+ `TimeUp.mean(name)` - The arithmetic mean of all recorded durations of the timer
139
156
 
140
157
  `TimeUp.total_elapsed` - Returns a `Float` of the sum of `elapsed` for all the
141
158
  timers you've created
@@ -144,12 +161,17 @@ timers you've created
144
161
  `elapsed` values. Handy for grabbing a snapshot of the state of things at a
145
162
  particular point in time without stopping all your timers
146
163
 
164
+ `TimeUp.all_stats` - Returns a hash of timer name keys mapped to another
165
+ hash of their basic statistics (elapsed time, number of recordings, min, max,
166
+ and mean)
167
+
147
168
  `TimeUp.active_timers` - Returns an array of all timers that are currently
148
169
  running. Useful for detecting cases where you might be counting the same time in
149
170
  multiple places simultaneously
150
171
 
151
- `TimeUp.print_summary([IO])` - Pretty-prints a multi-line summary of all your
152
- timers to STDOUT (or the provided IO)
172
+ `TimeUp.print_summary([io])` - Pretty-prints a multi-line summary of all your
173
+ timers to standard output (or the provided
174
+ [IO](https://ruby-doc.org/core-3.0.1/IO.html))
153
175
 
154
176
  `TimeUp.stop_all` - Stops all timers
155
177
 
@@ -166,6 +188,20 @@ reference to them
166
188
 
167
189
  `elapsed` - A `Float` of the total elapsed seconds the timer has been running
168
190
 
191
+ `timings` - Returns an array of each recorded start-to-stop duration of the
192
+ timer (including the current one, if active)
193
+
194
+ `count` - The number of times the timer has been started and stopped (including
195
+ the current timing, if active)
196
+
197
+ `min` - The shortest recording of the timer (including the current one, if
198
+ active)
199
+
200
+ `max` - The longest recording of the timer (including the current one, if
201
+ active)
202
+
203
+ `mean` - The arithmetic mean of all recorded durations of the timer
204
+
169
205
  `active?` - Returns `true` if the timer is running
170
206
 
171
207
  `reset(force: false)` - Resets the timer to 0 elapsed seconds. If `force` is
data/lib/time_up.rb CHANGED
@@ -5,35 +5,29 @@ module TimeUp
5
5
 
6
6
  Thread.current[:time_up_timers] = {}
7
7
 
8
- def self.start(name, &blk)
9
- raise Error.new("Timer name must be a String or Symbol") unless name.is_a?(Symbol) || name.is_a?(String)
10
- timer = __timers[name] ||= Timer.new(name)
11
- timer.start
12
- if blk
13
- blk.call
14
- timer.stop
15
- end
16
- timer
17
- end
18
-
19
- # Delegate methods
20
8
  def self.timer(name)
21
- __timers[name]
9
+ __timers[name] ||= Timer.new(name)
22
10
  end
23
11
 
24
- def self.stop(name)
25
- __ensure_timer(name)
26
- __timers[name].stop
27
- end
28
-
29
- def self.elapsed(name)
30
- __ensure_timer(name)
31
- __timers[name].elapsed
12
+ # Delegate methods
13
+ def self.start(name, &blk)
14
+ timer(name).start(&blk)
32
15
  end
33
16
 
34
- def self.reset(name)
35
- __ensure_timer(name)
36
- __timers[name].reset
17
+ [
18
+ :stop,
19
+ :reset,
20
+ :elapsed,
21
+ :timings,
22
+ :count,
23
+ :min,
24
+ :max,
25
+ :mean
26
+ ].each do |method_name|
27
+ define_singleton_method method_name do |name|
28
+ __ensure_timer(name)
29
+ __timers[name].send(method_name)
30
+ end
37
31
  end
38
32
 
39
33
  # Interrogative methods
@@ -47,6 +41,18 @@ module TimeUp
47
41
  }.to_h
48
42
  end
49
43
 
44
+ def self.all_stats
45
+ __timers.values.map { |timer|
46
+ [timer.name, {
47
+ elapsed: timer.elapsed,
48
+ count: timer.count,
49
+ min: timer.min,
50
+ max: timer.max,
51
+ mean: timer.mean
52
+ }]
53
+ }.to_h
54
+ end
55
+
50
56
  def self.active_timers
51
57
  __timers.values.select(&:active?)
52
58
  end
@@ -94,46 +100,91 @@ module TimeUp
94
100
  attr_reader :name
95
101
 
96
102
  def initialize(name)
103
+ validate!(name)
97
104
  @name = name
98
105
  @start_time = nil
99
- @elapsed = 0.0
106
+ @total_elapsed = 0.0
107
+ @past_timings = []
100
108
  end
101
109
 
102
- def start
110
+ def start(&blk)
103
111
  @start_time ||= now
112
+ if blk
113
+ blk.call.tap do
114
+ stop
115
+ end
116
+ else
117
+ self
118
+ end
104
119
  end
105
120
 
106
121
  def stop
107
122
  if @start_time
108
- @elapsed += now - @start_time
123
+ duration = now - @start_time
124
+ @past_timings.push(duration)
125
+ @total_elapsed += duration
126
+
109
127
  @start_time = nil
110
128
  end
111
- @elapsed
129
+ @total_elapsed
112
130
  end
113
131
 
114
132
  def elapsed
115
133
  if active?
116
- @elapsed + (now - @start_time)
134
+ @total_elapsed + (now - @start_time)
117
135
  else
118
- @elapsed
136
+ @total_elapsed
119
137
  end
120
138
  end
121
139
 
122
- def active?
123
- !!@start_time
124
- end
125
-
126
140
  def reset(force: false)
127
141
  if force
128
142
  @start_time = nil
129
143
  elsif !@start_time.nil?
130
144
  @start_time = now
131
145
  end
132
- @elapsed = 0.0
146
+ @total_elapsed = 0.0
147
+ @past_timings = []
148
+ end
149
+
150
+ def count
151
+ timings.size
152
+ end
153
+
154
+ def min
155
+ timings.min
156
+ end
157
+
158
+ def max
159
+ timings.max
160
+ end
161
+
162
+ def mean
163
+ times = timings
164
+ return if times.empty?
165
+ times.sum / times.size
166
+ end
167
+
168
+ def timings
169
+ if active?
170
+ @past_timings + [now - @start_time]
171
+ else
172
+ @past_timings
173
+ end
174
+ end
175
+
176
+ def active?
177
+ !!@start_time
133
178
  end
134
179
 
135
180
  private
136
181
 
182
+ def validate!(name)
183
+ unless name.is_a?(Symbol) || name.is_a?(String)
184
+ raise Error.new("Timer name must be a String or Symbol")
185
+ end
186
+ end
187
+
137
188
  def now
138
189
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
139
190
  end
@@ -1,3 +1,3 @@
1
1
  module TimeUp
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: time_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Searls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-15 00:00:00.000000000 Z
11
+ date: 2021-07-17 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: