pretty_stopwatch 0.1.3 → 0.1.4
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 +4 -4
- data/CHANGELOG.md +8 -0
- data/lib/pretty_stopwatch/version.rb +1 -1
- data/lib/pretty_stopwatch.rb +54 -35
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9a6ceb7faaa5c8fb188e743dbe175e30eeb1036beed7127163d35f087f78acf2
|
|
4
|
+
data.tar.gz: c8bc70a9c8ca23a056d99a4cda1f6a3006ac708f199552160793214efb840772
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3760e63b88048882ebbfe8592972b132556c3a426eeaf3473cc017458b2a930d0f6b9bb1e74bc6f0e7a24c8315820eb309736da5e53f59a7879efaf5044b1dfc
|
|
7
|
+
data.tar.gz: eac0582afb63bc7aff999bfaf83ae599e9ad72f2248f85a94dedfa8fc52db9c9f03f5e49c4977ad015200681d77bfd4e406e3abf628b50821e422c5e65250cd2
|
data/CHANGELOG.md
ADDED
data/lib/pretty_stopwatch.rb
CHANGED
|
@@ -1,43 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
#
|
|
6
|
-
# Based on the Guava Stopwatch com.google.common.base.Stopwatch
|
|
7
|
-
# Credit: The Guava Authors
|
|
8
|
-
#
|
|
9
|
-
# = Basic Example:
|
|
10
|
-
# stopwatch = Stopwatch::create_started
|
|
11
|
-
# sleep(0.1)
|
|
12
|
-
# stopwatch.stop # optional
|
|
13
|
-
# puts "slept for #{stopwatch}" # to_s optional
|
|
14
|
-
# Output: "slept for 0.1014ms"
|
|
3
|
+
# A simple Stopwatch with nanosecond precision and readable formatting.
|
|
15
4
|
#
|
|
16
|
-
#
|
|
17
|
-
# stopwatch = Stopwatch::create_started(:foo)
|
|
18
|
-
# sleep(0.1)
|
|
19
|
-
# puts "slept for #{stopwatch.get(:foo)}"
|
|
20
|
-
# Output: "slept for 0.1014ms"
|
|
5
|
+
# Uses: Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) to measure elapsed time.
|
|
21
6
|
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
7
|
+
# "The state-changing methods are not idempotent; it is an error to start or stop a stopwatch
|
|
8
|
+
# that is already in the desired state."
|
|
24
9
|
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
10
|
+
# Implementation based on the Guava Stopwatch class: 'com.google.common.base.Stopwatch' Credit: The Guava Authors
|
|
11
|
+
# @example Basic Usage
|
|
12
|
+
# stopwatch = Stopwatch::create_started
|
|
13
|
+
# sleep(0.1)
|
|
14
|
+
# stopwatch.stop # optional
|
|
15
|
+
# puts "slept for #{stopwatch}" # to_s optional
|
|
16
|
+
# # slept for 100.02 ms
|
|
17
|
+
# @example Named Example:
|
|
18
|
+
# stopwatch = Stopwatch::create_started(:foo)
|
|
19
|
+
# sleep(0.2)
|
|
20
|
+
# puts "#{stopwatch}"
|
|
21
|
+
# # 'foo' elapsed: 200.235 ms
|
|
22
|
+
# @example Block:
|
|
23
|
+
# stopwatch = Stopwatch::time{sleep 0.1}
|
|
24
|
+
# @example Lambda:
|
|
25
|
+
# lambda = -> {sleep 0.15}
|
|
26
|
+
# stopwatch = Stopwatch::time(lambda)
|
|
27
|
+
# @example Proc:
|
|
28
|
+
# proc = Proc.new {sleep 0.15}
|
|
29
|
+
# stopwatch = Stopwatch::time(proc)
|
|
37
30
|
class Stopwatch
|
|
38
|
-
# Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
|
|
39
31
|
class IllegalStateError < StandardError
|
|
40
32
|
end
|
|
33
|
+
|
|
41
34
|
attr_reader :name
|
|
42
35
|
attr_reader :running
|
|
43
36
|
|
|
@@ -48,10 +41,12 @@ class Stopwatch
|
|
|
48
41
|
@elapsed_nanos = elapsed_nanos
|
|
49
42
|
end
|
|
50
43
|
|
|
44
|
+
# @return [Boolean]
|
|
51
45
|
def running?
|
|
52
46
|
@running
|
|
53
47
|
end
|
|
54
48
|
|
|
49
|
+
# @return [Boolean]
|
|
55
50
|
def stopped?
|
|
56
51
|
!@running
|
|
57
52
|
end
|
|
@@ -59,14 +54,29 @@ class Stopwatch
|
|
|
59
54
|
private_class_method :new # private constructor
|
|
60
55
|
|
|
61
56
|
class << self
|
|
57
|
+
# Creates a new Stopwatch, and starts it.
|
|
58
|
+
#
|
|
59
|
+
# @param name [String] (optional) the name of the Stopwatch
|
|
60
|
+
# @param elapsed_nanos [Integer] (optional) elapsed_nanos (use for testing)
|
|
61
|
+
# @return [Stopwatch]
|
|
62
62
|
def create_started(name = nil, elapsed_nanos: 0)
|
|
63
63
|
new(name, elapsed_nanos).start
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
# Creates a new Stopwatch, but does not start it.
|
|
67
|
+
#
|
|
68
|
+
# @param name [String] (optional) the name of the Stopwatch
|
|
69
|
+
# @param elapsed_nanos [Integer] (optional) elapsed_nanos (use for testing)
|
|
70
|
+
# @return [Stopwatch]
|
|
66
71
|
def create_unstarted(name = nil, elapsed_nanos: 0)
|
|
67
72
|
new(name, elapsed_nanos)
|
|
68
73
|
end
|
|
69
74
|
|
|
75
|
+
# Times the execution of the given callable/block using a new Stopwatch.
|
|
76
|
+
#
|
|
77
|
+
# @param callable [Callable] callable to execute and time
|
|
78
|
+
# @param block [Block] block to execute and time
|
|
79
|
+
# @return [Stopwatch] Stopped Stopwatch
|
|
70
80
|
def time(callable = nil, &block)
|
|
71
81
|
stopwatch = create_started
|
|
72
82
|
if callable
|
|
@@ -80,6 +90,8 @@ class Stopwatch
|
|
|
80
90
|
end
|
|
81
91
|
end
|
|
82
92
|
|
|
93
|
+
# Starts the Stopwatch
|
|
94
|
+
# @return [Stopwatch]
|
|
83
95
|
def start
|
|
84
96
|
raise IllegalStateError, "Stopwatch is already running" if @running
|
|
85
97
|
@running = true
|
|
@@ -87,6 +99,8 @@ class Stopwatch
|
|
|
87
99
|
self
|
|
88
100
|
end
|
|
89
101
|
|
|
102
|
+
# Stops the Stopwatch
|
|
103
|
+
# @return [Stopwatch]
|
|
90
104
|
def stop
|
|
91
105
|
raise IllegalStateError, "Stopwatch is already stopped" unless @running
|
|
92
106
|
@elapsed_nanos += Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - @start_nanos
|
|
@@ -94,12 +108,15 @@ class Stopwatch
|
|
|
94
108
|
self
|
|
95
109
|
end
|
|
96
110
|
|
|
97
|
-
#
|
|
111
|
+
# Stops the Stopwatch and resets the elapsed time
|
|
112
|
+
# @return [Stopwatch]
|
|
98
113
|
def reset
|
|
99
114
|
@running = false
|
|
100
115
|
@elapsed_nanos = 0
|
|
116
|
+
self
|
|
101
117
|
end
|
|
102
118
|
|
|
119
|
+
# @return [Integer]
|
|
103
120
|
def elapsed_nanos
|
|
104
121
|
if running?
|
|
105
122
|
now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
|
|
@@ -108,10 +125,12 @@ class Stopwatch
|
|
|
108
125
|
@elapsed_nanos
|
|
109
126
|
end
|
|
110
127
|
|
|
128
|
+
# @return [Integer]
|
|
111
129
|
def elapsed_millis
|
|
112
130
|
elapsed_nanos / 1_000_000
|
|
113
131
|
end
|
|
114
132
|
|
|
133
|
+
# @return [String]
|
|
115
134
|
def to_s
|
|
116
135
|
value_with_unit = PrettyUnitFormatter.scale_nanos_with_unit(elapsed_nanos)
|
|
117
136
|
return "'#{@name}' elapsed: #{value_with_unit}" if @name
|
|
@@ -137,13 +156,13 @@ class Stopwatch
|
|
|
137
156
|
end
|
|
138
157
|
|
|
139
158
|
def scale_nanos_with_unit(nanos)
|
|
140
|
-
return "0 ns" if nanos
|
|
159
|
+
return "0 ns" if nanos < 1
|
|
141
160
|
unit = get_unit(nanos)
|
|
142
161
|
value = nanos / unit[:divisor].to_f
|
|
143
162
|
"#{format_float(value)} #{unit[:name]}"
|
|
144
163
|
end
|
|
145
164
|
|
|
146
|
-
#
|
|
165
|
+
# @return [String] Rounded to 3dp and with trailing zeros removed
|
|
147
166
|
private def format_float(float)
|
|
148
167
|
sprintf("%f", float.round(3)).sub(/\.?0*$/, "")
|
|
149
168
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pretty_stopwatch
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ben Mcfadyen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-
|
|
11
|
+
date: 2024-10-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rspec
|
|
@@ -33,6 +33,7 @@ extra_rdoc_files: []
|
|
|
33
33
|
files:
|
|
34
34
|
- ".rspec"
|
|
35
35
|
- ".standard.yml"
|
|
36
|
+
- CHANGELOG.md
|
|
36
37
|
- LICENSE.txt
|
|
37
38
|
- README.md
|
|
38
39
|
- Rakefile
|