pretty_stopwatch 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ba3811db87aefda87dc46290686c31917665972a1f14d80739015d4fe288ddf1
4
+ data.tar.gz: 23b860704b818babfb475e803211fe1055065ac1ecfd29aa6a2711a99f88796c
5
+ SHA512:
6
+ metadata.gz: 4b5d2a2482c85fa0098d03b213583645e21de723152750704b38b164d73a7888b1fee5316767df34449bec2b40d10bbe35cb2051ad17e38dc271bed39062964f
7
+ data.tar.gz: e0f4a8cb22040e7a42037fbf33620060b00b2dec59c03fd46557614930c7881c8fdff5be8084b21b639c70bdaf370a762eb8698be936589317fb02201a6ca97d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Ben Mcfadyen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # PrettyStopwatch
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/pretty_stopwatch`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/pretty_stopwatch.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +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]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PrettyStopwatch
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,160 @@
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
@@ -0,0 +1,4 @@
1
+ module PrettyStopwatch
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_stopwatch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Mcfadyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-09-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: Pretty Stopwatch
28
+ email:
29
+ - ben.mcfadyen3@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - ".standard.yml"
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - lib/pretty_stopwatch.rb
40
+ - lib/pretty_stopwatch/version.rb
41
+ - sig/pretty_stopwatch.rbs
42
+ homepage: https://github.com/BenMcFadyen/pretty_stopwatch_test
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ allowed_push_host: https://rubygems.org
47
+ homepage_uri: https://github.com/BenMcFadyen/pretty_stopwatch_test
48
+ source_code_uri: https://github.com/BenMcFadyen/pretty_stopwatch
49
+ changelog_uri: https://github.com/BenMcFadyen/pretty_stopwatch
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: 3.0.0
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubygems_version: 3.5.11
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Pretty Stopwatch
69
+ test_files: []