monotonic.rb 0.6.6 → 0.6.7

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: 17e3187f97eede654634d62e5eeba6595b19df22dd6e38be3d5a64f5ee9cbc68
4
- data.tar.gz: d72962badf2f68b748d8fe48ebab3e3e1fcf78a8a4fd133d3c1bce663459fc5f
3
+ metadata.gz: a9992e2194aa62416707e8ee507f68f870e4eea0cbee2770879017d07d2ff7e7
4
+ data.tar.gz: 8221c6232e0c2f1df21d9d5faaf42c7292ed1052801c8fe7f8cd6b819fdd4bde
5
5
  SHA512:
6
- metadata.gz: 74ec59b8280c65b333fd22b4293370784c6756a4980d316700833645f6a67305393a06098ce5d1950c9e4b331049d3662b7fbd1915590cbb40074c1aaafd9217
7
- data.tar.gz: 0b31c5c4dad215a636b7053008353af3097db2fbdb0e2fb1654bbf7fd2ed341f065c5a867a9d072c96107d7d9e486458e741e76df012d2cc31cdef941e1b78e0
6
+ metadata.gz: 7a827ddd7420e8ff5911ca543cc5dbf46765063f521fefb55f0be0f883fe73c21b2552f43d5e6890e6a01e826c335c8d86ddfdb8db087983f25fd029d91e3cf0
7
+ data.tar.gz: 35d7a1115045c486bd49074f31098a6d0c6d2e21fa795a0f07fd842120a1b3a407c9f3b76456b31dd8a185c4659212ba8bd85e08cd7968d7bd33beede84c8a19
data/CHANGELOG ADDED
@@ -0,0 +1,143 @@
1
+ # CHANGELOG
2
+
3
+ ## 20260716
4
+
5
+ 0.6.7: Block exception bug fix, relicensing, and gem packaging changes.
6
+
7
+ 1. ~ Monotonic::Timer#time: - the `return total_time` from the ensure clause, since returning from an ensure swallowed any exception raised by the block, handing back a duration in place of the error. The exception is now allowed to propagate. The ensure itself is kept, and narrowed to the block, so that stop still runs when the block is interrupted: the time up to an interruption is worth having, and without the stop total_time would go on running rather than hold at the point of interruption. It is read from the timer, since the return value belongs to the exception.
8
+ 2. ~ test/Monotonic/Timer_test.rb: + a test to demonstrate that an exception raised within the block is no longer swallowed.
9
+ 3. ~ test/Monotonic/Timer_test.rb: + a test to demonstrate that the time up to an interruption holds rather than goes on running.
10
+ 4. - test/monotonic_test.rb: A Rakefile has been introduced to run all files.
11
+ 5. ~ monotonic.rb.gemspec: /Ruby/MIT/ for the license.
12
+ 6. ~ monotonic.rb.gemspec: Gem::Specification#dependencies= and #development_dependencies=, so that dependencies may be enumerated as a list rather than a call apiece.
13
+ 7. ~ monotonic.rb.gemspec: spec.files to include CHANGELOG, Gemfile, the gemspec, the Rakefile, README.md and the tests, rather than lib/**/*.rb alone.
14
+ 8. ~ monotonic.rb.gemspec: spec.require_paths, since spec.files is no longer confined to lib/.
15
+ 9. + monotonic.rb.gemspec: rake as a development dependency.
16
+ 10. + Rakefile: a test task, run by default.
17
+ 11. ~ lib/Monotonic/Time.rb: - empty lines from within the class body.
18
+ 12. ~ lib/Monotonic/Timer.rb: - empty lines from within the class body.
19
+ 13. ~ CHANGELOG: Reformat headers, number the changes, and remove `.md`.
20
+ 14. ~ Monotonic::VERSION: /0.6.6/0.6.7/
21
+
22
+
23
+ ## 20260716
24
+
25
+ 0.6.6: Case-sensitive filesystem fixes.
26
+
27
+ 1. ~ lib/monotonic/ --> lib/Monotonic/: the directory was the only reference using lowercase, while the entry require, the test requires and the gemspec all already used Monotonic/, so the published gem failed to load on case-sensitive (Linux) filesystems.
28
+ 2. ~ lib/Monotonic/VERSION.rb: /class Monotonic/module Monotonic/ so it matches the module reopened in Time.rb and Timer.rb, rather than raising TypeError when loaded alongside them.
29
+ 3. + lib/monotonic.rb: require_relative './Monotonic/VERSION' (as Duration.rb does for its own VERSION), so Monotonic::VERSION is available after requiring the gem. VERSION.rb being off the load path is what let the class/module error above go unnoticed: nothing loaded it alongside Time.rb and Timer.rb, so the conflict never fired.
30
+
31
+
32
+ ## 20240822
33
+
34
+ 0.6.5: Block usage bug fix
35
+
36
+ 1. ~ lib/monotonic/Timer.rb: The total_time must be explicitly returned since values in ensure are not returned as the default return value.
37
+
38
+
39
+ ## 20240822
40
+
41
+ 0.6.4: Correct README.md.
42
+
43
+ 1. ~ README.md: In the Usage section: /MonotonicTime/Monotonic::Time/
44
+ 2. ~ lib/monotonic/VERSION.rb: 0.6.3 --> 0.6.4 because I mistakenly pulled the 0.6.3 gem.
45
+
46
+
47
+ ## 20210627
48
+
49
+ 0.6.3: Using gemspec in Gemfile.
50
+
51
+ 1. ~ Gemfile to use gemspec rather than having dependencies enumerated there
52
+
53
+
54
+ ## 20210625
55
+
56
+ 0.6.2: Fixed missing sys-uptime dependency.
57
+
58
+ 1. ~ montonic.rb.gemspec to include the sys-uptime dependency
59
+
60
+
61
+ ## 20210623
62
+
63
+ 0.6.1: README fix and a small refactor
64
+
65
+ 1. ~ README.md: /monotonic/monotonic.rb/
66
+ 2. ~ Monotonic::Timer.time to make use of the same-named instance method now that it's available.
67
+
68
+
69
+ ## 20210623
70
+
71
+ 0.6.0: Version number bump due to name change from monotony.rb to monotonic.rb
72
+
73
+ 1. /Monotony//, Monotonic::Timer is shorter than Monotony::MonotonicTimer
74
+
75
+
76
+ ## 20210607
77
+
78
+ 0.5.0: Added timing block on an instance of MonotonicTimer
79
+
80
+ 1. + MonotonicTime#+
81
+ 2. + MonotonicTime#-
82
+ 3. ~ MonotonicTimer#start: - use of MonotonicTime#to_time (using MonotonicTime#- instead)
83
+ 4. ~ MonotonicTimer#stop: - use of MonotonicTime#to_time (using MonotonicTime#- instead)
84
+ 5. + MonotonicTimer#time
85
+
86
+
87
+ ## 20210607
88
+
89
+ 0.4.1: Fixed starting and stopping
90
+
91
+ 1. - MonotonicTimer#initialize: - @total_time as it just made things more complicated unless needing splits
92
+ 2. ~ MonotonicTimer#start: - @start_monotonic_time as it wasn't really needed
93
+ 3. ~ MonotonicTimer#stop: - @stop_monotonic_time as it wasn't really needed
94
+ 4. ~ MonotonicTimer#total_time: - @total_time as it just made things more complicated unless needing splits
95
+
96
+
97
+ ## 20210606
98
+
99
+ 0.4.0: Named Monotony and prepped as a gem.
100
+
101
+ 1. /monotonic_time.rb/MonotonicTime.rb/
102
+ 2. /monotonic_timer.rb/MonotonicTimer.rb/
103
+ 3. + Monotony namespace
104
+ 4. + monotony.gemspec
105
+
106
+
107
+ ## 20210604
108
+
109
+ 0.3.0
110
+
111
+ 1. ~ MonotonicTimer.time, so that it returns the total time to the value of the block
112
+ 2. + ./test/monotonic_time_test.rb
113
+ 3. ~ ./test/monotonic_timer_test.rb
114
+
115
+
116
+ ## 20210510
117
+
118
+ 0.2.0: Added somewhat proper testing
119
+
120
+ 1. + ./test/monotonic_timer_test.rb
121
+ 2. ~ ./lib/monotonic_timer.rb: - self-run section test
122
+
123
+
124
+ ## 20200510
125
+
126
+ 0.1.1
127
+
128
+ 1. ~ ./lib/monotonic_timer.rb: + self-run section test
129
+
130
+
131
+ ## 20200115
132
+
133
+ 0.1.0: Added classes for better structure
134
+
135
+ 1. + MonotonicTime
136
+ 2. + MonotonicTimer
137
+
138
+
139
+ ## 20200115
140
+
141
+ 0.0.0
142
+
143
+ 1. + timer()
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://www.rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,99 @@
1
+ # monotonic.rb
2
+
3
+ ## Description
4
+
5
+ Create accurate timings of excution in Ruby.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+ ```ruby
11
+ gem 'monotonic.rb'
12
+ ```
13
+ And then execute:
14
+ ```bash
15
+ $ bundle
16
+ ```
17
+ Or install it yourself as:
18
+ ```bash
19
+ $ gem install monotonic.rb
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ ### Monotonic::Time
25
+
26
+ ```ruby
27
+ require 'monotonic.rb'
28
+ monotonic_time = Monotonic::Time.new
29
+ monotonic_time.seconds_since_boot
30
+ # => 1208799.325906
31
+ monotonic_time + Monotonic::Time.now
32
+ # => 2417598.681896
33
+ monotonic_time - Monotonic::Time.now
34
+ # => -0.044104999862611294
35
+ monotonic_time.to_s
36
+ # => "1164320.268127 seconds since boot."
37
+ monotonic_time.to_time
38
+ # => 2021-06-07 09:27:08 8249692651179/8388608000000 +1000
39
+ ```
40
+
41
+ ### Monotonic::Timer without a block
42
+
43
+ ```ruby
44
+ require 'monotonic.rb'
45
+ timer = Monotonic::Timer.new
46
+ timer.start
47
+ i = 0
48
+ 1_000_000.times{puts i += 1}
49
+ timer.stop
50
+ timer.total_time
51
+ # => 7.166559999808669
52
+ ```
53
+
54
+ ### Monotonic::Timer with a block
55
+
56
+ ```ruby
57
+ require 'monotonic.rb'
58
+ time = Monotonic::Timer.time do
59
+ i = 0
60
+ 1_000_000.times{puts i += 1}
61
+ end
62
+ time
63
+ # => 6.975823000073433
64
+ ```
65
+
66
+ ### Monotonic::Timer with a block and block variable
67
+
68
+ ```ruby
69
+ require 'monotonic.rb'
70
+ time = Monotonic::Timer.time do |timer|
71
+ i = 0
72
+ 500_000.times{puts i += 1}
73
+ p timer.total_time
74
+ 500_000.times{puts i += 1}
75
+ end
76
+ time
77
+ # => 6.975823000073433
78
+ ```
79
+
80
+ ### Monotonic::Timer with a block on a timer instance
81
+
82
+ ```ruby
83
+ require 'monotonic.rb'
84
+ timer = Monotonic::Timer.new
85
+ time = timer.time do
86
+ i = 0
87
+ 1_000_000.times{puts i += 1}
88
+ end
89
+ time
90
+ # => 7.033131000120193
91
+ ```
92
+
93
+ ## Contributing
94
+
95
+ 1. Fork it ( https://github.com/thoran/monotonic.rb/fork )
96
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 4. Push to the branch (`git push origin my-new-feature`)
99
+ 5. Create a new pull request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # Rakefile
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ task default: :test
@@ -5,13 +5,10 @@ require 'sys-uptime'
5
5
 
6
6
  module Monotonic
7
7
  class Time
8
-
9
8
  class << self
10
-
11
9
  def now
12
10
  self.new
13
11
  end
14
-
15
12
  end # class << self
16
13
 
17
14
  attr_reader :seconds_since_boot
@@ -36,6 +33,5 @@ module Monotonic
36
33
  def to_time
37
34
  @boot_time + @seconds_since_boot
38
35
  end
39
-
40
36
  end
41
37
  end
@@ -5,14 +5,11 @@ require_relative './Time'
5
5
 
6
6
  module Monotonic
7
7
  class Timer
8
-
9
8
  class << self
10
-
11
9
  def time(&block)
12
10
  timer = Timer.new
13
11
  timer.time(&block)
14
12
  end
15
-
16
13
  end # class << self
17
14
 
18
15
  def start
@@ -34,11 +31,12 @@ module Monotonic
34
31
 
35
32
  def time
36
33
  start
37
- yield self
38
- ensure
39
- stop
40
- return total_time
34
+ begin
35
+ yield self
36
+ ensure
37
+ stop
38
+ end
39
+ total_time
41
40
  end
42
-
43
41
  end
44
42
  end
@@ -2,5 +2,5 @@
2
2
  # Monotonic::VERSION
3
3
 
4
4
  module Monotonic
5
- VERSION = '0.6.6'
5
+ VERSION = '0.6.7'
6
6
  end
@@ -0,0 +1,47 @@
1
+ require_relative './lib/Monotonic/VERSION'
2
+
3
+ class Gem::Specification
4
+ def dependencies=(gems)
5
+ gems.each{|gem| add_dependency(*gem)}
6
+ end
7
+
8
+ def development_dependencies=(gems)
9
+ gems.each{|gem| add_development_dependency(*gem)}
10
+ end
11
+ end
12
+
13
+ Gem::Specification.new do |spec|
14
+ spec.name = 'monotonic.rb'
15
+ spec.version = Monotonic::VERSION
16
+
17
+ spec.summary = "Monotonic timing made easy."
18
+ spec.description = "Create accurate timings of excution in Ruby."
19
+
20
+ spec.author = 'thoran'
21
+ spec.email = 'code@thoran.com'
22
+ spec.homepage = 'https://github.com/thoran/monotonic.rb'
23
+ spec.license = 'MIT'
24
+
25
+ spec.require_paths = ['lib']
26
+ spec.required_ruby_version = '>= 2.5'
27
+
28
+ spec.files = [
29
+ 'CHANGELOG',
30
+ 'Gemfile',
31
+ Dir['lib/**/*.rb'],
32
+ 'monotonic.rb.gemspec',
33
+ 'Rakefile',
34
+ 'README.md',
35
+ Dir['test/**/*.rb'],
36
+ ].flatten
37
+
38
+ spec.dependencies = %w{
39
+ sys-uptime
40
+ }
41
+
42
+ spec.development_dependencies = %w{
43
+ minitest
44
+ minitest-spec-context
45
+ rake
46
+ }
47
+ end
@@ -0,0 +1,50 @@
1
+ require_relative '../../lib/Monotonic/Time'
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest-spec-context'
5
+
6
+ describe Monotonic::Time do
7
+ subject{Monotonic::Time.now}
8
+
9
+ describe "#initialize" do
10
+ it "the time spent in the block is returned as the value of the block" do
11
+ expect(subject.instance_variable_get(:@boot_time)) \
12
+ .must_equal(Sys::Uptime.boot_time)
13
+ end
14
+
15
+ it "the time spent in the block is returned as the value of the block" do
16
+ expect(subject.instance_variable_get(:@seconds_since_boot).round(2)) \
17
+ .must_equal(Process.clock_gettime(Process::CLOCK_MONOTONIC).round(2))
18
+ end
19
+ end
20
+
21
+ describe "#seconds_since_boot" do
22
+ it "returns an instance of string" do
23
+ expect((subject.seconds_since_boot).class).must_equal(Float)
24
+ end
25
+ end
26
+
27
+ describe "#+" do
28
+ it "returns an instance of string" do
29
+ expect((subject + Monotonic::Time.now).class).must_equal(Float)
30
+ end
31
+ end
32
+
33
+ describe "#-" do
34
+ it "returns an instance of time" do
35
+ expect((subject - Monotonic::Time.now).class).must_equal(Float)
36
+ end
37
+ end
38
+
39
+ describe "#to_s" do
40
+ it "returns an instance of string" do
41
+ expect(subject.to_s.class).must_equal(String)
42
+ end
43
+ end
44
+
45
+ describe "#to_time" do
46
+ it "returns an instance of time" do
47
+ expect(subject.to_time.class).must_equal(Time)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,110 @@
1
+ require_relative '../../lib/Monotonic/Timer'
2
+
3
+ require 'minitest/autorun'
4
+ require 'minitest-spec-context'
5
+
6
+ describe Monotonic::Timer do
7
+ context "with a block" do
8
+ it "the time spent in the block is returned as the value of the block" do
9
+ block_time = Monotonic::Timer.time do |timer|
10
+ sleep 3
11
+ end
12
+ expect(block_time.round).must_equal(3)
13
+ end
14
+
15
+ # Test introduced in 0.6.5 to demonstrate that the bug had been fixed.
16
+ it "the time spent in the block is returned as the value of the block and not a value at the end of the block" do
17
+ block_time = Monotonic::Timer.time do |timer|
18
+ sleep 3
19
+ 4
20
+ end
21
+ expect(block_time.round).must_equal(3)
22
+ end
23
+
24
+ # Test introduced in 0.6.7 to demonstrate that the bug had been fixed.
25
+ it "an exception raised within the block is not swallowed" do
26
+ expect{Monotonic::Timer.time{raise(ArgumentError)}}.must_raise(ArgumentError)
27
+ end
28
+
29
+ it "allows the timer to be started and stopped within the block" do
30
+ Monotonic::Timer.time do |timer|
31
+ sleep 1
32
+ expect(timer.total_time.round).must_equal(1)
33
+ timer.stop
34
+ expect(timer.total_time.round).must_equal(1)
35
+ timer.start
36
+ expect(timer.total_time.round).must_equal(0)
37
+ sleep 1
38
+ expect(timer.total_time.round).must_equal(1)
39
+ sleep 1
40
+ expect(timer.total_time.round).must_equal(2)
41
+ timer.start
42
+ expect(timer.total_time.round).must_equal(0)
43
+ end
44
+ end
45
+
46
+ it "alters the block time if it is started and stopped within the block" do
47
+ block_time = Monotonic::Timer.time do |timer|
48
+ sleep 1
49
+ timer.stop
50
+ sleep 1
51
+ timer.start
52
+ sleep 1
53
+ end
54
+ expect(block_time.round).must_equal(1)
55
+ end
56
+ end
57
+
58
+ context "without a block" do
59
+ it 'works' do
60
+ timer = Monotonic::Timer.new
61
+ timer.start
62
+ sleep 1
63
+ expect(timer.total_time.round).must_equal(1)
64
+ timer.stop
65
+ expect(timer.total_time.round).must_equal(1)
66
+ timer.start
67
+ expect(timer.total_time.round).must_equal(0)
68
+ sleep 1
69
+ expect(timer.total_time.round).must_equal(1)
70
+ sleep 1
71
+ expect(timer.total_time.round).must_equal(2)
72
+ timer.start
73
+ expect(timer.total_time.round).must_equal(0)
74
+ end
75
+ end
76
+
77
+ context "with a block on an timer instance" do
78
+ it 'works' do
79
+ timer = Monotonic::Timer.new
80
+ time = timer.time do |timer|
81
+ sleep 1
82
+ expect(timer.total_time.round).must_equal(1)
83
+ timer.stop
84
+ expect(timer.total_time.round).must_equal(1)
85
+ sleep 1
86
+ expect(timer.total_time.round).must_equal(1)
87
+ timer.start
88
+ expect(timer.total_time.round).must_equal(0)
89
+ sleep 1
90
+ end
91
+ expect(time.round).must_equal(1)
92
+ expect(timer.total_time.round).must_equal(1)
93
+ end
94
+
95
+ # Test introduced in 0.6.7 to demonstrate that the time up to an
96
+ # interruption is not left running.
97
+ it "the time up to an interruption remains available from the timer" do
98
+ timer = Monotonic::Timer.new
99
+ begin
100
+ timer.time do
101
+ sleep 1
102
+ raise(ArgumentError)
103
+ end
104
+ rescue ArgumentError
105
+ end
106
+ sleep 1
107
+ expect(timer.total_time.round).must_equal(1)
108
+ end
109
+ end
110
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monotonic.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.6
4
+ version: 0.6.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - thoran
@@ -51,19 +51,40 @@ dependencies:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
54
68
  description: Create accurate timings of excution in Ruby.
55
69
  email: code@thoran.com
56
70
  executables: []
57
71
  extensions: []
58
72
  extra_rdoc_files: []
59
73
  files:
74
+ - CHANGELOG
75
+ - Gemfile
76
+ - README.md
77
+ - Rakefile
60
78
  - lib/Monotonic/Time.rb
61
79
  - lib/Monotonic/Timer.rb
62
80
  - lib/Monotonic/VERSION.rb
63
81
  - lib/monotonic.rb
82
+ - monotonic.rb.gemspec
83
+ - test/Monotonic/Time_test.rb
84
+ - test/Monotonic/Timer_test.rb
64
85
  homepage: https://github.com/thoran/monotonic.rb
65
86
  licenses:
66
- - Ruby
87
+ - MIT
67
88
  metadata: {}
68
89
  rdoc_options: []
69
90
  require_paths: