time_up 0.0.4 → 0.0.5

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: acd14bdf2f241538214317bdc29461d543d95988458b24f17cd0ee9265ddcc0d
4
- data.tar.gz: 3e4a9033800d1ddfa4cf40b2a9c84b0bfb5ef8c7c049d2d5801159c4d3710ad6
3
+ metadata.gz: f6241f3e0e1e66bc75c67a7e7cfc76424ae4e711c5a535b87da3bdf4fa78c153
4
+ data.tar.gz: 33c77eb409cb4d4d4142207ff93a938a1124b87768047f29be0d51057c4eb5fb
5
5
  SHA512:
6
- metadata.gz: 0bdeee99d7c5e49754786901bf8d5c9560e759a2b93d681c50f535cc039a8418a92a024779c901ad9fb4dd46fa3843486955a77e1c85c78a29897b035794814c
7
- data.tar.gz: 135c285c49557d24bd4306fdd77ca243247a3d523e8f5d55408fef34fd2cd05c0712bcfa54420673bf5c512266177eb3c4dcc595c7924c74ca7eaea1a8bea57d
6
+ metadata.gz: df9e90669ceef219dcea0bf8b21548252b97abe40b950b922f6c9ee17987494d145fdd130b5c4735a6e57daffaf5ba2d915057044f424ba68656d9b55b5f7820
7
+ data.tar.gz: ad014a9ec0471bfbc3f315b673988d7d5b5115ad29d0cc664ad63eaa81547086bff539de50e311078261c99a9616a0e683c75f9e552bf0a254d26e14d0ef702d
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.0.5
2
+
3
+ - Add `median` and `percentile` timer statistics, and added them to
4
+ `print_detailed_summary`
5
+
1
6
  # 0.0.4
2
7
 
3
8
  - Add `TimeUp.print_detailed_summary`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- time_up (0.0.4)
4
+ time_up (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -11,6 +11,9 @@ process and that you want to measure in aggregate. (For example, to see how
11
11
  much time your test suite spends creating factories, truncating the database, or
12
12
  invoking a critical code path.)
13
13
 
14
+ Here's a [blog post about time_up](https://blog.testdouble.com/posts/2021-07-19-benchmarking-your-ruby-with-time_up/) and
15
+ a [great example of when it can be useful](https://gist.github.com/searls/feee0b0eac7c329b390fed90c4714afb).
16
+
14
17
  ## Install
15
18
 
16
19
  Just run `gem install time_up` or add time_up to your Gemfile:
@@ -122,13 +125,13 @@ statistics in the print-out, you can call `TimeUp.print_detailed_summary`, which
122
125
  will produce this:
123
126
 
124
127
  ```
125
- =========================================================
126
- Name | Elapsed | Count | Min | Max | Mean
127
- ---------------------------------------------------------
128
- :roast | 0.08641 | 3 | 0.00127 | 0.07261 | 0.02880
129
- :veggies | 0.03759 | 1 | 0.03759 | 0.03759 | 0.03759
130
- :pasta | 0.01256 | 11 | 0.00000 | 0.01255 | 0.00114
131
- :souffle* | 0.00006 | 1 | 0.00007 | 0.00007 | 0.00007
128
+ =============================================================================
129
+ Name | Elapsed | Count | Min | Max | Mean | Median | 95th %
130
+ -----------------------------------------------------------------------------
131
+ :roast | 0.08454 | 3 | 0.00128 | 0.07280 | 0.02818 | 0.01046 | 0.06657
132
+ :veggies | 0.03779 | 1 | 0.03779 | 0.03779 | 0.03779 | 0.03779 | 0.03779
133
+ :pasta | 0.01260 | 11 | 0.00000 | 0.01258 | 0.00115 | 0.00000 | 0.00630
134
+ :souffle* | 0.00024 | 1 | 0.00024 | 0.00025 | 0.00025 | 0.00025 | 0.00026
132
135
 
133
136
  * Denotes that the timer is still active
134
137
  ```
@@ -168,6 +171,12 @@ the current timing, if the timer is running)
168
171
 
169
172
  `TimeUp.mean(name)` - The arithmetic mean of all recordings by the timer
170
173
 
174
+ `TimeUp.median(name)` - The median of all recordings by the timer
175
+
176
+ `TimeUp.percentile(name, percent)` - The timing for the given
177
+ [percentile](https://en.wikipedia.org/wiki/Percentile) of all recordings by the
178
+ timer
179
+
171
180
  `TimeUp.total_elapsed` - Returns a `Float` of the sum of `elapsed` across all
172
181
  the timers you've created (note that because you can easily run multiple logical
173
182
  timers simultaneously, this figure may exceed the total time spent by the
@@ -219,6 +228,12 @@ timer (including the current one, if the timer is running)
219
228
 
220
229
  `mean` - The arithmetic mean of all recorded durations of the timer
221
230
 
231
+ `median(name)` - The median of all recordings by the timer
232
+
233
+ `percentile(name, percent)` - The timing for the given
234
+ [percentile](https://en.wikipedia.org/wiki/Percentile) of all recordings by the
235
+ timer
236
+
222
237
  `active?` - Returns `true` if the timer is running
223
238
 
224
239
  `reset(force: false)` - Resets the timer to 0 elapsed seconds. If `force` is
data/lib/time_up.rb CHANGED
@@ -22,7 +22,8 @@ module TimeUp
22
22
  :count,
23
23
  :min,
24
24
  :max,
25
- :mean
25
+ :mean,
26
+ :median
26
27
  ].each do |method_name|
27
28
  define_singleton_method method_name do |name|
28
29
  __ensure_timer(name)
@@ -30,6 +31,11 @@ module TimeUp
30
31
  end
31
32
  end
32
33
 
34
+ def self.percentile(name, percentage)
35
+ __ensure_timer(name)
36
+ __timers[name].percentile(percentage)
37
+ end
38
+
33
39
  # Interrogative methods
34
40
  def self.total_elapsed
35
41
  __timers.values.sum(&:elapsed)
@@ -48,7 +54,9 @@ module TimeUp
48
54
  count: timer.count,
49
55
  min: timer.min,
50
56
  max: timer.max,
51
- mean: timer.mean
57
+ mean: timer.mean,
58
+ median: timer.median,
59
+ "95th": timer.percentile(95)
52
60
  }]
53
61
  }.to_h
54
62
  end
@@ -80,7 +88,9 @@ module TimeUp
80
88
  count: ["Count"],
81
89
  min: ["Min"],
82
90
  max: ["Max"],
83
- mean: ["Mean"]
91
+ mean: ["Mean"],
92
+ median: ["Median"],
93
+ "95th": ["95th %"]
84
94
  }
85
95
  __timers.values.each { |timer|
86
96
  cols[:names] << "#{timer.name.inspect}#{"*" if timer.active?}"
@@ -89,6 +99,8 @@ module TimeUp
89
99
  cols[:min] << "%.5f" % timer.min
90
100
  cols[:max] << "%.5f" % timer.max
91
101
  cols[:mean] << "%.5f" % timer.mean
102
+ cols[:median] << "%.5f" % timer.median
103
+ cols[:"95th"] << "%.5f" % timer.percentile(95)
92
104
  }
93
105
 
94
106
  widths = cols.map { |name, vals|
@@ -211,6 +223,25 @@ module TimeUp
211
223
  times.sum / times.size
212
224
  end
213
225
 
226
+ def median
227
+ times = timings.sort
228
+ return if times.empty?
229
+ (times[(times.size - 1) / 2] + times[times.size / 2]) / 2.0
230
+ end
231
+
232
+ def percentile(percent)
233
+ times = timings.sort
234
+ return if times.empty?
235
+ return 0 if percent <= 0
236
+ return max if percent >= 100
237
+ return times.first if times.size == 1
238
+ position = (percent / 100.0) * (times.size - 1)
239
+
240
+ partial_ratio = position - position.floor
241
+ whole, partial = times[position.floor, 2]
242
+ whole + (partial - whole) * partial_ratio
243
+ end
244
+
214
245
  def timings
215
246
  if active?
216
247
  @past_timings + [now - @start_time]
@@ -1,3 +1,3 @@
1
1
  module TimeUp
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
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.4
4
+ version: 0.0.5
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-19 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: