monotime 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8f48959572be85d87513cfc071e48eea56af6c15e62d7c8d2b253fecf1b23d2
4
+ data.tar.gz: 73f63a474ba0949c4ddc2e665e9102f89524ab9031c52476a3420b49cb3656e2
5
+ SHA512:
6
+ metadata.gz: e03126c84a579e4096b28c56afd93eab7099bc782ea9c6ec0f2be8a9d8375e1ed90ce8efc3b22f59a93356c104b76c0945f18fd36bae0232cc0cf0f65fb68f98
7
+ data.tar.gz: 1f74dd4465f686198e4e1fc098d04387528a7cc99d78ba850773cc137c99a3ab62e23b34c5424c9c6d03e7bb34ee9f383d2a6173c196a857eff31df25c62c108
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.16.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in monotime.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ monotime (0.1.0)
5
+ dry-equalizer
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ dry-equalizer (0.2.1)
11
+ minitest (5.11.3)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 1.16)
19
+ minitest (~> 5.0)
20
+ monotime!
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 1.16.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Thomas Hurst
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,92 @@
1
+ # Monotime
2
+
3
+ A sensible interface to Ruby's monotonic clock, inspired by Rust.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'monotime'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install monotime
20
+
21
+ ## Usage
22
+
23
+ The typical way everyone does "correct" elapsed-time measurements in Ruby is
24
+ this pile of nonsense:
25
+
26
+ ```ruby
27
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
28
+ do_something
29
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
30
+ ```
31
+
32
+ Not only is it long-winded, it's imprecise, converting to floating point instead
33
+ of working off precise timestamps.
34
+
35
+ `Monotime` offers this alternative:
36
+
37
+ ```ruby
38
+ include Monotime
39
+
40
+ start = Instant.now
41
+ do_something
42
+ elapsed = start.elapsed
43
+
44
+ # or
45
+ elapsed = Duration.measure { do_something }
46
+ ```
47
+
48
+ `elapsed` is not a dimensionless `Float`, but a `Duration` type, and internally
49
+ both `Instant` and `Duration` operate in *nanoseconds* to most closely match
50
+ the native timekeeping types used by most operating systems.
51
+
52
+ `Duration` knows how to format itself:
53
+
54
+ ```ruby
55
+ Duration.from_millis(42).to_s # => "42ms"
56
+ Duration.from_nanos(12345).to_s # => "12.345μs"
57
+ Duration.from_secs(1.12345).to_s(2) # => "1.12s"
58
+ ```
59
+
60
+ And how to do basic maths on itself:
61
+
62
+ ```ruby
63
+ (Duration.from_millis(42) + Duration.from_secs(1)).to_s # => "1.042s"
64
+ (Duration.from_millis(42) - Duration.from_secs(-1)).to_s # => "-958ms"
65
+ ```
66
+
67
+ `Instant` does some simple maths too:
68
+
69
+ ```ruby
70
+ # Instant - Duration => Instant
71
+ (Instant.now - Duration.from_secs(1)).elapsed.to_s # => "1.000014627s"
72
+
73
+ # Instant - Instant => Duration
74
+ (Instant.now - Instant.now).to_s # => "3.439μs"
75
+ ```
76
+
77
+ `Duration` and `Instant` are also `Comparable` with other instances of their
78
+ type, and support `#hash` for use in, er, hashes.
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/monotime.
89
+
90
+ ## License
91
+
92
+ 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
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "monotime"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monotime
4
+ VERSION = '0.1.0'
5
+ end
data/lib/monotime.rb ADDED
@@ -0,0 +1,151 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'monotime/version'
4
+
5
+ require 'dry-equalizer'
6
+
7
+ module Monotime
8
+ class Instant
9
+ attr_reader :ns
10
+
11
+ include Dry::Equalizer(:ns)
12
+ include Comparable
13
+
14
+ def initialize(ns = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond))
15
+ raise TypeError, 'Not an Integer' unless ns.is_a? Integer
16
+
17
+ @ns = ns
18
+ end
19
+
20
+ def self.now
21
+ new
22
+ end
23
+
24
+ def duration_since(earlier)
25
+ case earlier
26
+ when Instant then self - earlier
27
+ else raise TypeError, 'Not an Instant'
28
+ end
29
+ end
30
+
31
+ def elapsed
32
+ duration_since(self.class.now)
33
+ end
34
+
35
+ def +(other)
36
+ case other
37
+ when Duration then Instant.new(self.ns + other.ns)
38
+ when Integer then Instant.new(self.ns + other)
39
+ else raise TypeError, 'Not a Duration or Integer'
40
+ end
41
+ end
42
+
43
+ def -(other)
44
+ case other
45
+ when Instant then Duration.new(other.ns - self.ns)
46
+ when Duration then Instant.new(self.ns - other.ns)
47
+ when Integer then Instant.new(self.ns - other)
48
+ else raise TypeError, 'Not an Instant, Duration or Integer'
49
+ end
50
+ end
51
+
52
+ def <=>(other)
53
+ case other
54
+ when self.class then self.ns <=> other.ns
55
+ else raise TypeError, 'Not a #{self.class}'
56
+ end
57
+ end
58
+ end
59
+
60
+ class Duration
61
+ attr_reader :ns
62
+
63
+ include Dry::Equalizer(:ns)
64
+ include Comparable
65
+
66
+ def initialize(ns = 0)
67
+ raise TypeError, 'Not an Integer' unless ns.is_a? Integer
68
+
69
+ @ns = ns
70
+ end
71
+
72
+ class << self
73
+ def from_secs(secs)
74
+ new(Integer(Float(secs) * 1_000_000_000))
75
+ end
76
+
77
+ def from_millis(millis)
78
+ new(Integer(Float(millis) * 1_000_000))
79
+ end
80
+
81
+ def from_micros(micros)
82
+ new(Integer(Float(micros) * 1_000))
83
+ end
84
+
85
+ def from_nanos(nanos)
86
+ new(Integer(nanos))
87
+ end
88
+
89
+ def measure
90
+ Instant.now.tap { yield }.elapsed
91
+ end
92
+ end
93
+
94
+ def +(other)
95
+ case other
96
+ when Duration then Duration.new(self.ns + other.ns)
97
+ when Numeric then Duration.new(self.ns + other)
98
+ else raise TypeError, 'Not a Duration or Numeric'
99
+ end
100
+ end
101
+
102
+ def -(other)
103
+ case other
104
+ when Duration then Duration.new(self.ns - other.ns)
105
+ when Numeric then Duration.new(self.ns - other)
106
+ else raise TypeError, 'Not a Duration or Numeric'
107
+ end
108
+ end
109
+
110
+ def <=>(other)
111
+ case other
112
+ when self.class then self.ns <=> other.ns
113
+ else raise TypeError, "Not a #{self.class}"
114
+ end
115
+ end
116
+
117
+ def to_secs
118
+ @ns / 1_000_000_000
119
+ end
120
+
121
+ def to_millis
122
+ @ns / 1_000_000
123
+ end
124
+
125
+ def to_micros
126
+ @ns * 1_000
127
+ end
128
+
129
+ def to_nanos
130
+ @ns
131
+ end
132
+
133
+ def to_s(precision = 9)
134
+ postfix = 's'
135
+ ns = self.ns.abs
136
+ num = "#{'-' if self.ns < 0}%.#{precision}f" % if ns >= 1_000_000_000
137
+ ns / 1_000_000_000.0
138
+ elsif ns >= 1_000_000
139
+ postfix = 'ms'
140
+ ns / 1_000_000.0
141
+ elsif ns >= 1_000
142
+ postfix = 'μs'
143
+ ns / 1_000.0
144
+ else
145
+ postfix = 'ns'
146
+ ns
147
+ end
148
+ num.sub(/\.?0*$/, '') << postfix
149
+ end
150
+ end
151
+ end
data/monotime.gemspec ADDED
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "monotime/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "monotime"
8
+ spec.version = Monotime::VERSION
9
+ spec.authors = ["Thomas Hurst"]
10
+ spec.email = ["tom@hur.st"]
11
+
12
+ spec.summary = %q{A sensible interface to the monotonic clock}
13
+ spec.homepage = "https://github.com/Freaky/monotime"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "dry-equalizer", '~> 0'
35
+
36
+ spec.add_development_dependency "bundler", "~> 1.16"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "minitest", "~> 5.0"
39
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monotime
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Hurst
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-equalizer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ description:
70
+ email:
71
+ - tom@hur.st
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".travis.yml"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/monotime.rb
86
+ - lib/monotime/version.rb
87
+ - monotime.gemspec
88
+ homepage: https://github.com/Freaky/monotime
89
+ licenses:
90
+ - MIT
91
+ metadata:
92
+ allowed_push_host: https://rubygems.org
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.6
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A sensible interface to the monotonic clock
113
+ test_files: []