pretty_stopwatch 0.1.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ba3811db87aefda87dc46290686c31917665972a1f14d80739015d4fe288ddf1
4
- data.tar.gz: 23b860704b818babfb475e803211fe1055065ac1ecfd29aa6a2711a99f88796c
3
+ metadata.gz: 7fcddcb209f62c6aa2dd5590919a588c549cd40a9d2b5719870b9d2d5d5b8f22
4
+ data.tar.gz: 36244820de0bcee5d516eb7c78691dfd0dd3c1b4ad45d35a8e9151da79c41aa3
5
5
  SHA512:
6
- metadata.gz: 4b5d2a2482c85fa0098d03b213583645e21de723152750704b38b164d73a7888b1fee5316767df34449bec2b40d10bbe35cb2051ad17e38dc271bed39062964f
7
- data.tar.gz: e0f4a8cb22040e7a42037fbf33620060b00b2dec59c03fd46557614930c7881c8fdff5be8084b21b639c70bdaf370a762eb8698be936589317fb02201a6ca97d
6
+ metadata.gz: cb1fc44fb109dd7ed499330f537274071fbdbcae2c9fc0f4ca6811635f63a16fd64c41a3eba3007a137facaa9501866083ed9a320dfec9c36ab4565be8971834
7
+ data.tar.gz: 7fb43e0f69b476c2dc2ab6d41cfed5b85f84501d3044749f129ad161db8f073292618f96d205e52af35f2ddd0e0c7f929fea1e259ec97d703ecd29bc0699d51c
data/Rakefile CHANGED
@@ -1,10 +1,10 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- RSpec::Core::RakeTask.new(:spec)
7
-
8
- require "standard/rake"
9
-
10
- task default: %i[spec standard]
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -1,5 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
- module PrettyStopwatch
4
- VERSION = "0.1.0"
5
- end
1
+ # frozen_string_literal: true
2
+
3
+ module PrettyStopwatch
4
+ VERSION = "0.1.2"
5
+ end
@@ -1,160 +1,152 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "pretty_stopwatch/version"
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"
15
- #
16
- # = Named Example:
17
- # stopwatch = Stopwatch::create_started(:foo)
18
- # sleep(0.1)
19
- # puts "slept for #{stopwatch.get(:foo)}"
20
- # Output: "slept for 0.1014ms"
21
- #
22
- # = Block Example:
23
- # stopwatch = Stopwatch::time{sleep 0.1}
24
- #
25
- # = Lambda Example:
26
- # lambda = -> {sleep 0.15}
27
- # stopwatch = Stopwatch::time(lambda)
28
- #
29
- # = Proc Example:
30
- # proc = Proc.new {sleep 0.15}
31
- # stopwatch = Stopwatch::time(proc)
32
-
33
- # stopwatch = Stopwatch::create_started(:foo)
34
- # sleep(0.1)
35
- # puts "slept for #{stopwatch.get(:foo)}"
36
- # Output: "slept for 0.1014ms"
37
- class Stopwatch
38
-
39
- # Stopwatch methods are not idempotent; it is an error to start or stop a stopwatch that is already in the desired state.
40
- class IllegalStateError < StandardError
41
- end
42
-
43
- @start_nanos
44
- attr_reader :name
45
- attr_reader :running
46
- attr_reader :elapsed_nanos
47
-
48
- def initialize(name = nil, elapsed_nanos) # optional variable
49
- @name = name
50
- @start_nanos = nil
51
- @running = false
52
- @elapsed_nanos = elapsed_nanos
53
- end
54
-
55
- def running?
56
- @running
57
- end
58
-
59
- def stopped?
60
- !@running
61
- end
62
-
63
- private_class_method :new # private constructor
64
-
65
- class << self
66
-
67
- def create_started(name = nil, elapsed_nanos: 0)
68
- self.new(name, elapsed_nanos).start
69
- end
70
-
71
- def create_unstarted(name = nil, elapsed_nanos: 0)
72
- self.new(name, elapsed_nanos)
73
- end
74
-
75
- def time(callable = nil, &block)
76
- stopwatch = self.create_started
77
- if callable
78
- callable.call
79
- elsif block_given?
80
- block.call # yield also valid
81
- else
82
- raise "no callable/block given" # todo
83
- end
84
- stopwatch.stop
85
- end
86
- end
87
-
88
- def start
89
- raise IllegalStateError, "Stopwatch is already running" if @running
90
- @running = true
91
- @start_nanos = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
92
- self
93
- end
94
-
95
- def stop
96
- raise IllegalStateError, "Stopwatch is already stopped" unless @running
97
- @elapsed_nanos += Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - @start_nanos
98
- @running = false
99
- self
100
- end
101
-
102
- # reset the elapsed time and stop the stopwatch
103
- def reset
104
- @running = false
105
- @elapsed_nanos = 0
106
- end
107
-
108
- def elapsed_nanos
109
- if running?
110
- now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
111
- return (now - @start_nanos) + @elapsed_nanos
112
- end
113
- @elapsed_nanos
114
- end
115
-
116
- def elapsed_millis
117
- elapsed_nanos / 1_000_000
118
- end
119
-
120
- def to_s
121
- value_with_unit = PrettyUnitFormatter::scale_nanos_with_unit(elapsed_nanos)
122
- return "'#{@name}' elapsed: #{value_with_unit}" if @name
123
- "#{value_with_unit}"
124
- end
125
-
126
- class PrettyUnitFormatter
127
- @units = [
128
- { name: 'day', divisor: 1_000_000_000 * 60 * 60 * 24 },
129
- { name: 'hour', divisor: 1_000_000_000 * 60 * 60 },
130
- { name: 'min', divisor: 1_000_000_000 * 60 },
131
- { name: 's', divisor: 1_000_000_000 },
132
- { name: 'ms', divisor: 1_000_000 },
133
- { name: 'μs', divisor: 1_000 },
134
- { name: 'ns', divisor: 1 }
135
- ]
136
-
137
- class << self
138
-
139
- def get_unit(nanos)
140
- found_unit = @units.find { |unit| nanos >= unit[:divisor] }
141
- raise "No matching unit found for #{nanos}" if found_unit.nil?
142
- found_unit
143
- end
144
-
145
- def scale_nanos_with_unit(nanos)
146
- return "0 ns" if nanos.zero?
147
- unit = get_unit(nanos)
148
- value = nanos / unit[:divisor].to_f
149
- "#{format_float(value)} #{unit[:name]}"
150
- end
151
-
152
- # format float to 3dp & remove trailing zeros
153
- private def format_float(float)
154
- sprintf('%f', float.round(3)).sub(/\.?0*$/, '')
155
- end
156
- end
157
-
158
- end
159
-
160
- end
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "pretty_stopwatch/version"
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"
15
+ #
16
+ # = Named Example:
17
+ # stopwatch = Stopwatch::create_started(:foo)
18
+ # sleep(0.1)
19
+ # puts "slept for #{stopwatch.get(:foo)}"
20
+ # Output: "slept for 0.1014ms"
21
+ #
22
+ # = Block Example:
23
+ # stopwatch = Stopwatch::time{sleep 0.1}
24
+ #
25
+ # = Lambda Example:
26
+ # lambda = -> {sleep 0.15}
27
+ # stopwatch = Stopwatch::time(lambda)
28
+ #
29
+ # = Proc Example:
30
+ # proc = Proc.new {sleep 0.15}
31
+ # stopwatch = Stopwatch::time(proc)
32
+
33
+ # stopwatch = Stopwatch::create_started(:foo)
34
+ # sleep(0.1)
35
+ # puts "slept for #{stopwatch.get(:foo)}"
36
+ # Output: "slept for 0.1014ms"
37
+ 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
+ class IllegalStateError < StandardError
40
+ end
41
+ attr_reader :name
42
+ attr_reader :running
43
+
44
+ def initialize(name = nil, elapsed_nanos = 0) # optional variable
45
+ @name = name
46
+ @start_nanos = nil
47
+ @running = false
48
+ @elapsed_nanos = elapsed_nanos
49
+ end
50
+
51
+ def running?
52
+ @running
53
+ end
54
+
55
+ def stopped?
56
+ !@running
57
+ end
58
+
59
+ private_class_method :new # private constructor
60
+
61
+ class << self
62
+ def create_started(name = nil, elapsed_nanos: 0)
63
+ new(name, elapsed_nanos).start
64
+ end
65
+
66
+ def create_unstarted(name = nil, elapsed_nanos: 0)
67
+ new(name, elapsed_nanos)
68
+ end
69
+
70
+ def time(callable = nil, &block)
71
+ stopwatch = create_started
72
+ if callable
73
+ callable.call
74
+ elsif block
75
+ block.call # yield also valid
76
+ else
77
+ raise "no callable/block given" # todo
78
+ end
79
+ stopwatch.stop
80
+ end
81
+ end
82
+
83
+ def start
84
+ raise IllegalStateError, "Stopwatch is already running" if @running
85
+ @running = true
86
+ @start_nanos = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
87
+ self
88
+ end
89
+
90
+ def stop
91
+ raise IllegalStateError, "Stopwatch is already stopped" unless @running
92
+ @elapsed_nanos += Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond) - @start_nanos
93
+ @running = false
94
+ self
95
+ end
96
+
97
+ # reset the elapsed time and stop the stopwatch
98
+ def reset
99
+ @running = false
100
+ @elapsed_nanos = 0
101
+ end
102
+
103
+ def elapsed_nanos
104
+ if running?
105
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
106
+ return (now - @start_nanos) + @elapsed_nanos
107
+ end
108
+ @elapsed_nanos
109
+ end
110
+
111
+ def elapsed_millis
112
+ elapsed_nanos / 1_000_000
113
+ end
114
+
115
+ def to_s
116
+ value_with_unit = PrettyUnitFormatter.scale_nanos_with_unit(elapsed_nanos)
117
+ return "'#{@name}' elapsed: #{value_with_unit}" if @name
118
+ value_with_unit.to_s
119
+ end
120
+
121
+ class PrettyUnitFormatter
122
+ @units = [
123
+ {name: "day", divisor: 1_000_000_000 * 60 * 60 * 24},
124
+ {name: "hour", divisor: 1_000_000_000 * 60 * 60},
125
+ {name: "min", divisor: 1_000_000_000 * 60},
126
+ {name: "s", divisor: 1_000_000_000},
127
+ {name: "ms", divisor: 1_000_000},
128
+ {name: "μs", divisor: 1_000},
129
+ {name: "ns", divisor: 1}
130
+ ]
131
+
132
+ class << self
133
+ def get_unit(nanos)
134
+ found_unit = @units.find { |unit| nanos >= unit[:divisor] }
135
+ raise "No matching unit found for #{nanos}" if found_unit.nil?
136
+ found_unit
137
+ end
138
+
139
+ def scale_nanos_with_unit(nanos)
140
+ return "0 ns" if nanos.zero?
141
+ unit = get_unit(nanos)
142
+ value = nanos / unit[:divisor].to_f
143
+ "#{format_float(value)} #{unit[:name]}"
144
+ end
145
+
146
+ # format float to 3dp & remove trailing zeros
147
+ private def format_float(float)
148
+ sprintf("%f", float.round(3)).sub(/\.?0*$/, "")
149
+ end
150
+ end
151
+ end
152
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_stopwatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Mcfadyen
@@ -39,12 +39,12 @@ files:
39
39
  - lib/pretty_stopwatch.rb
40
40
  - lib/pretty_stopwatch/version.rb
41
41
  - sig/pretty_stopwatch.rbs
42
- homepage: https://github.com/BenMcFadyen/pretty_stopwatch_test
42
+ homepage: https://github.com/BenMcFadyen/pretty_stopwatch
43
43
  licenses:
44
44
  - MIT
45
45
  metadata:
46
46
  allowed_push_host: https://rubygems.org
47
- homepage_uri: https://github.com/BenMcFadyen/pretty_stopwatch_test
47
+ homepage_uri: https://github.com/BenMcFadyen/pretty_stopwatch
48
48
  source_code_uri: https://github.com/BenMcFadyen/pretty_stopwatch
49
49
  changelog_uri: https://github.com/BenMcFadyen/pretty_stopwatch
50
50
  post_install_message: