monotonik 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: cc1fc625cd3b0f6876224b415d45adec4fbed8fa2732b19f54b38dd9928e56bc
4
+ data.tar.gz: 81aabdafb20d90144948e57ecdc3371eeb57011cac4ee980f25a8aefc4a924cb
5
+ SHA512:
6
+ metadata.gz: aab9859dcfce8f099d78b4cc211e7adc5d6ab361a0206ec18970e7276817eebd5ea60743dec218da03624a241b979a0c0d9fd969c4546e39e5db0ac514fe64af
7
+ data.tar.gz: 5ac7470e77a8a4648fb56b7b12e0a09855134933d6540f473dfe59bcdc9475821115c907664de9661144feea4de17436b420911e4ce0e84ffa73cdb2ccd5dd4c
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.idea/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .ruby-version
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,9 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Layout/LineLength:
5
+ Max: 120
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'spec/**/*'
@@ -0,0 +1,10 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5.0
6
+ - 2.5.7
7
+ - 2.6.0
8
+ - 2.6.5
9
+ - 2.7.0
10
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in monotonik.gemspec
6
+ gemspec
@@ -0,0 +1,50 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ monotonik (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ diff-lcs (1.3)
11
+ jaro_winkler (1.5.4)
12
+ parallel (1.19.1)
13
+ parser (2.7.0.2)
14
+ ast (~> 2.4.0)
15
+ rainbow (3.0.0)
16
+ rake (13.0.1)
17
+ rspec (3.9.0)
18
+ rspec-core (~> 3.9.0)
19
+ rspec-expectations (~> 3.9.0)
20
+ rspec-mocks (~> 3.9.0)
21
+ rspec-core (3.9.1)
22
+ rspec-support (~> 3.9.1)
23
+ rspec-expectations (3.9.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.9.0)
26
+ rspec-mocks (3.9.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.9.0)
29
+ rspec-support (3.9.2)
30
+ rubocop (0.79.0)
31
+ jaro_winkler (~> 1.5.1)
32
+ parallel (~> 1.10)
33
+ parser (>= 2.7.0.1)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ ruby-progressbar (~> 1.7)
36
+ unicode-display_width (>= 1.4.0, < 1.7)
37
+ ruby-progressbar (1.10.1)
38
+ unicode-display_width (1.6.1)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ monotonik!
45
+ rake (~> 13.0.0)
46
+ rspec (~> 3.9.0)
47
+ rubocop (~> 0.79.0)
48
+
49
+ BUNDLED WITH
50
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Evgeny Garlukovich
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.
@@ -0,0 +1,92 @@
1
+ # Monotonik
2
+
3
+ ![Monotonik](https://raw.githubusercontent.com/evgenygarl/monotonik/master/logo.PNG)
4
+
5
+ [![Gem Version](http://img.shields.io/gem/v/monotonik.svg)](https://rubygems.org/gems/monotonik) [![Gem Downloads](http://ruby-gem-downloads-badge.herokuapp.com/monotonik)](https://rubygems.org/gems/monotonik) [![Gem Total Downloads](http://ruby-gem-downloads-badge.herokuapp.com/monotonik?type=total)](https://rubygems.org/gems/monotonik)
6
+
7
+ [![License](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT) [![Build Status](https://travis-ci.org/evgenygarl/monotonik.svg?branch=master)](https://travis-ci.org/evgenygarl/monotonik)
8
+
9
+ [Usually](https://github.com/search?utf8=%E2%9C%93&q=elapsed+time.now+language%3ARuby&type=Code), Ruby-developers measure elapsed time this way:
10
+
11
+ ```ruby
12
+ t1 = Time.now
13
+ # time consuming operation
14
+ t2 = Time.now
15
+
16
+ time_elapsed = t2 - t1
17
+ ```
18
+
19
+ But this way of measuring has an important issue: `Time.now` doesn't move only forwards 🤷‍♂️ It can be changed manually by the system administrator. Or system time on your machine may be changed during syncing with [NTP](http://www.ntp.org/) server. You can read more about this issue [here](https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/).
20
+ So, there is no guarantee that the new value returned by `Time.now` will be in the future, and the elapsed time calculated this way may be even a negative value 🤷‍♂️
21
+
22
+ If you want to measure elapsed time the right way, you should use the *monotonic clock*. Each time you request the time of the monotonic clock, time since a specific event is returned: for example, on macOS, this event is the system boot. Ruby provides a method to receive the current value of system monotonic clocks:
23
+
24
+ ```ruby
25
+ t1 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
26
+ # time consuming operation
27
+ t2 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
28
+
29
+ time_elapsed = t2 - t1
30
+ ```
31
+
32
+ In its turn, `monotonik` gem provides tiny wrappers over this functionality to avoid writing a lot of boilerplate code.
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'monotonik'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle install
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install monotonik
49
+
50
+ ## Usage
51
+
52
+ How to measure elapsed time using `monotonik`? Here is an example:
53
+
54
+ ```ruby
55
+ start = Monotonik.clock_time
56
+ # time consuming operation
57
+ finish = Monotonik.clock_time
58
+
59
+ elapsed = finish - start #=> 14.73181
60
+ ```
61
+
62
+ In order to avoid writing such boilerplate code, you can use `Monotonik.measure` method:
63
+
64
+ ```ruby
65
+ result = Monotonik.measure { factorial(100_000) } # time consuming operation
66
+
67
+ result.value #=> very long number...
68
+ result.time #=> => 6.43604
69
+ ```
70
+
71
+ Both `Monotonik.clock_time` and `Monotonik.measure` methods receive type of the return clock time value as the only argument. It can be set to one of the following values:
72
+ * `:float_second` - number of seconds as a float (by default)
73
+ * `:float_millisecond` - number of milliseconds as a float
74
+ * `:float_microsecond` - number of microseconds as a float
75
+ * `:second` - number of seconds as an integer
76
+ * `:millisecond` - number of milliseconds as an integer
77
+ * `:microsecond` - number of microseconds as an integer
78
+ * `:nanosecond` - number of nanoseconds as an integer
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rspec` 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 `bin/rake install`. To release a new version, update the version number in `version.rb`, and then run `bin/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/evgenygarl/monotonik.
89
+
90
+ ## License
91
+
92
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
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
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'monotonik'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
+
29
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
+
29
+ load Gem.bin_path('rspec-core', 'rspec')
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require 'pathname'
12
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path('bundle', __dir__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require 'rubygems'
27
+ require 'bundler/setup'
28
+
29
+ load Gem.bin_path('rubocop', 'rubocop')
@@ -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,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Module to help measure elapsed time the right way.
4
+ module Monotonik
5
+ autoload :ClockTime, 'monotonik/clock_time'
6
+ autoload :Measure, 'monotonik/measure'
7
+ autoload :TimeUnits, 'monotonik/clock_time'
8
+ autoload :VERSION, 'monotonik/version'
9
+
10
+ module_function
11
+
12
+ # @see Monotonik::ClockTime#now
13
+ def clock_time(unit = ClockTime::DEFAULT_TIME_UNIT)
14
+ ClockTime.now(unit)
15
+ end
16
+
17
+ # @see Monotonik::Measure#call
18
+ def measure(unit = ClockTime::DEFAULT_TIME_UNIT, &block)
19
+ Measure.call(unit, &block)
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+
6
+ module Monotonik
7
+ module TimeUnits
8
+ FLOAT_SECOND = :float_second
9
+ FLOAT_MILLISECOND = :float_millisecond
10
+ FLOAT_MICROSECOND = :float_microsecond
11
+ SECOND = :second
12
+ MILLISECOND = :millisecond
13
+ MICROSECOND = :microsecond
14
+ NANOSECOND = :nanosecond
15
+
16
+ ALL = [FLOAT_SECOND, FLOAT_MILLISECOND, FLOAT_MICROSECOND, SECOND, MILLISECOND, MICROSECOND, NANOSECOND].freeze
17
+ end
18
+
19
+ # Class to get monotonic clock time.
20
+ class ClockTime
21
+ include Singleton
22
+
23
+ DEFAULT_TIME_UNIT = TimeUnits::FLOAT_SECOND
24
+
25
+ class << self
26
+ extend Forwardable
27
+
28
+ # @!method now(unit = DEFAULT_TIME_UNIT)
29
+ # @see Monotonik::ClockTime#now
30
+ def_delegator :instance, :now
31
+ end
32
+
33
+ # Returns a monotonic clock time returned by POSIX ::clock_gettime() function.
34
+ #
35
+ # @param unit [Symbol] Specifies a type of the return value, can be any value from `Monotonik::TimeUnits::ALL`.
36
+ #
37
+ # @raise [ArgumentError] When unexpected 'unit' value is given.
38
+ #
39
+ # @return [Float|Integer] A monotonic clock time returned by POSIX ::clock_gettime() function.
40
+ def now(unit = DEFAULT_TIME_UNIT)
41
+ Process.clock_gettime(Process::CLOCK_MONOTONIC, unit)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Monotonik
6
+ # Class to measure elapsed time.
7
+ class Measure
8
+ Result = Struct.new(:time, :value)
9
+
10
+ attr_reader :clock_time
11
+
12
+ class << self
13
+ extend Forwardable
14
+
15
+ # @return [Monotonik::Measure]
16
+ def instance
17
+ @instance ||= new
18
+ end
19
+
20
+ # @!method call(unit = Monotonik::TimeUnits::FLOAT_SECOND)
21
+ # @see Monotonik::Measure#call
22
+ def_delegator :instance, :call
23
+ end
24
+
25
+ def initialize(clock_time: ClockTime)
26
+ @clock_time = clock_time
27
+ end
28
+
29
+ # Measure elapsed time while performing the given block.
30
+ #
31
+ # @param unit [Symbol] Specifies a type of the return elapsed time value, can be any value from
32
+ # `Monotonik::TimeUnits::ALL`.
33
+ #
34
+ # @yield Block to measure elapsed time for.
35
+ #
36
+ # @return [Monotonik::Measure::Result] Elapsed time with result of the given block yield.
37
+ def call(unit = TimeUnits::FLOAT_SECOND)
38
+ raise ArgumentError, 'No block given.' unless block_given?
39
+
40
+ start = clock_time.now(unit)
41
+ result = yield
42
+ finish = clock_time.now(unit)
43
+
44
+ Result.new(finish - start, result)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monotonik
4
+ VERSION = '0.1.0'
5
+ end
Binary file
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/monotonik/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'monotonik'
7
+ spec.version = Monotonik::VERSION
8
+ spec.authors = ['Evgeny Garlukovich']
9
+ spec.email = ['me@evgenygarl.dev']
10
+
11
+ spec.summary = '⏱ Measure elapsed time the right way.'
12
+ spec.description = 'This gem helps you to measure elapsed time the right way - using monotonic clock.'
13
+ spec.homepage = 'https://github.com/evgenygarl/elapsed'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'rake', '~> 13.0.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.9.0'
24
+ spec.add_development_dependency 'rubocop', '~> 0.79.0'
25
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monotonik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Garlukovich
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 13.0.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 13.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.9.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 3.9.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.79.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.79.0
55
+ description: This gem helps you to measure elapsed time the right way - using monotonic
56
+ clock.
57
+ email:
58
+ - me@evgenygarl.dev
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".rubocop.yml"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/rake
74
+ - bin/rspec
75
+ - bin/rubocop
76
+ - bin/setup
77
+ - lib/monotonik.rb
78
+ - lib/monotonik/clock_time.rb
79
+ - lib/monotonik/measure.rb
80
+ - lib/monotonik/version.rb
81
+ - logo.PNG
82
+ - monotonik.gemspec
83
+ homepage: https://github.com/evgenygarl/elapsed
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.5.0
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.1.2
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: "⏱ Measure elapsed time the right way."
106
+ test_files: []