hitimes 2.0.0 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbcf72d52334876051ff59dbc24a84f2a2be8dbde31979d858819db54eba8f7f
4
- data.tar.gz: c19e0e771b17465b6f57a5e229d11e76b132c1d3c170ff3da1428eeb11cac40a
3
+ metadata.gz: d79a16734531380e103efb7bf1af93e5c4e465185f5a47f1c0ab6d2dcc8bb5e7
4
+ data.tar.gz: deb8a23bdc8e0f35b6dccbd6fd4535ffdfa4c5bdd11ff44fa488a53baa090245
5
5
  SHA512:
6
- metadata.gz: e1005099d605876d72c5d7147007b051f77b1451c83a9eac420807ed221f1e484098dc276d9d2cbc2c1f74dca19ec032334bdd6e81ab3c9304e5991ba8e7ccb9
7
- data.tar.gz: e7b74b1d122348097900175b8e9a422b1cc49782994c82a55e1017f9742de08cadaae70b8ea8b9603f2bc05e0e471278321a0ee76fef06997abde5c6d05561d3
6
+ metadata.gz: 358edcfe776e98c69100f01065d5f9ac12452353736180f8bf1d04e240cddc1bafcc43078b949c26ef40709acce3f2ac28bb65fed28a7e45301dd2a9c607a4ac
7
+ data.tar.gz: 3c6296fd094227654048f8bda301435442d6c0169b12322253ba07b1486a4137549ff4f3f11e7f5f09cd3c12f4a78eb92ba9055ebd99d5b6a4eab452ac4f16db
data/CONTRIBUTING.md CHANGED
@@ -27,7 +27,7 @@ easiest way to contribute.
27
27
  * Fork the [repo][].
28
28
  * Create a new branch for your issue: `git checkout -b issue/my-issue`
29
29
  * Lovingly craft your contribution:
30
- * `rake develop` to get started
30
+ * `bin/setup` to get started
31
31
  * `rake test` to run tests
32
32
  * Make sure that `rake test` passes. Its important, I said it twice.
33
33
  * Add yourself to the contributors section below.
data/HISTORY.md CHANGED
@@ -1,4 +1,21 @@
1
- # Hitimes Changeloga
1
+ # Hitimes Changelog
2
+ ## Version 3.0.0 - 2024-05-01
3
+
4
+ * Migrated to SemaphoreCI for doing full test runs on all active ruby versions.
5
+ * Remove the dependency on `Process.clock_getres` as it is unreliable.
6
+ * This has the effect of deprecating some Hitimes constants that had been documented as public. These are now removed as this is a major version update
7
+ - `Hitimes::CLOCK_RESOLUTION_NANOSECONDS`
8
+ - `Hitimes::CLOCK_RESOLUTION_SECONDS`
9
+ - `Hitimes::INSTANT_CONVERSION_FACTOR`
10
+ - `Hitimes.clock_resolution_description`
11
+ - `Hitimes.clock_description`
12
+ * Added Rubocop for some coding consistency
13
+ * Updated the supported ruby version to be 3.0 and up
14
+ * Updated all dependencies
15
+ * Changed how all the `assert_delta` style tests were done so they were not so flakey
16
+ * Hitimes will now emit a `warn` message if it ends up using `CLOCK_REALTIME`
17
+ * Hitimes will raise an exception if it cannot find a valid clock id. This is a bug and a message to file a report is in the exception
18
+
2
19
  ## Version 2.0.0 2019-09-23
3
20
 
4
21
  * Remove the C and Java extensions as `Process.clock_gettime()` has the same
@@ -83,7 +100,7 @@
83
100
 
84
101
  ## Version 1.0.3 2009-06-28
85
102
 
86
- * Fix bug with time.h on linode (reported by Roger Pack)
103
+ * Fix bug with time.h on linode (reported by Roger Pack)
87
104
  * Fix potential garbage collection issue with Interval class
88
105
  * Windows gem is now a fat binary to support installing in 1.8 or 1.9 from the
89
106
  same gem
@@ -112,7 +129,7 @@
112
129
 
113
130
  * Added new stat 'rate'
114
131
  * Added new stat method to_hash
115
- * Added Hitimes::MutexedStats class for threadsafe stats collection
132
+ * Added Hitimes::MutexedStats class for threadsafe stats collection
116
133
  - not needed when used in MRI 1.8.x
117
134
  * remove stale dependency on mkrf
118
135
 
data/Manifest.txt CHANGED
@@ -1,11 +1,9 @@
1
1
  CONTRIBUTING.md
2
2
  HISTORY.md
3
- LICENSE
3
+ LICENSE.txt
4
4
  Manifest.txt
5
5
  README.md
6
- Rakefile
7
- examples/benchmarks.rb
8
- examples/stats.rb
6
+ hitimes.gemspec
9
7
  lib/hitimes.rb
10
8
  lib/hitimes/initialize.rb
11
9
  lib/hitimes/instant.rb
@@ -18,16 +16,3 @@ lib/hitimes/timed_metric.rb
18
16
  lib/hitimes/timed_value_metric.rb
19
17
  lib/hitimes/value_metric.rb
20
18
  lib/hitimes/version.rb
21
- spec/hitimes_spec.rb
22
- spec/interval_spec.rb
23
- spec/metric_spec.rb
24
- spec/mutex_stats_spec.rb
25
- spec/paths_spec.rb
26
- spec/spec_helper.rb
27
- spec/stats_spec.rb
28
- spec/timed_metric_spec.rb
29
- spec/timed_value_metric_spec.rb
30
- spec/value_metric_spec.rb
31
- spec/version_spec.rb
32
- tasks/default.rake
33
- tasks/this.rb
data/README.md CHANGED
@@ -1,17 +1,14 @@
1
1
  # Hitimes
2
-
3
- [![Build Status](https://travis-ci.org/copiousfreetime/hitimes.svg?branch=master)](https://travis-ci.org/copiousfreetime/hitimes)
4
-
5
- ## Description
6
-
7
- A fast, high resolution timer library for recording peformance metrics.
2
+ [![Build Status](https://copiousfreetime.semaphoreci.com/badges/hitimes/branches/main.svg)](https://copiousfreetime.semaphoreci.com/projects/hitimes)
8
3
 
9
4
  * [Homepage](http://github.com/copiousfreetime/hitimes)
10
5
  * [Github project](http://github.com/copiousfreetime/hitimes)
11
- * email jeremy at copiousfreetime dot org
12
- * `git clone url git://github.com/copiousfreetime/hitimes.git`
13
6
 
14
- ## Table of Contents
7
+ ## DESCRIPTION
8
+
9
+ A fast, high resolution timer library for recording performance metrics.
10
+
11
+ ## TABLE OF CONTENTS
15
12
 
16
13
  * [Requirements](#requirements)
17
14
  * [Usage](#usage)
@@ -20,13 +17,13 @@ A fast, high resolution timer library for recording peformance metrics.
20
17
  * [License](#license)
21
18
 
22
19
 
23
- ## Requirements
20
+ ## REQUIREMENTS
24
21
 
25
22
  Hitimes requires the following to run:
26
23
 
27
24
  * Ruby
28
25
 
29
- ## Usage
26
+ ## USAGE
30
27
 
31
28
  Hitimes easiest to use when installed with `rubygems`:
32
29
 
@@ -37,13 +34,13 @@ gem install hitimes
37
34
  Or as part of your bundler `Gemfile`:
38
35
 
39
36
  ```ruby
40
- gem 'hitimes'
37
+ gem "hitimes"
41
38
  ```
42
39
 
43
40
  You can load it with the standard ruby require statement.
44
41
 
45
42
  ```ruby
46
- require 'hitimes'
43
+ require "hitimes"
47
44
  ```
48
45
 
49
46
  ### Interval
@@ -53,10 +50,10 @@ Returns the time as seconds.
53
50
 
54
51
  ```ruby
55
52
  duration = Hitimes::Interval.measure do
56
- 1_000_000.times do |x|
57
- 2 + 2
58
- end
59
- end
53
+ 1_000_000.times do |x|
54
+ 2 + 2
55
+ end
56
+ end
60
57
 
61
58
  puts duration # => 0.047414297 (seconds)
62
59
  ```
@@ -66,7 +63,7 @@ puts duration # => 0.047414297 (seconds)
66
63
  Use a `Hitimes::TimedMetric` to calculate statistics about an iterative operation
67
64
 
68
65
  ```ruby
69
- timed_metric = Hitimes::TimedMetric.new('operation on items')
66
+ timed_metric = Hitimes::TimedMetric.new("operation on items")
70
67
  ```
71
68
 
72
69
  Explicitly use `start` and `stop`:
@@ -84,7 +81,8 @@ value of the block.
84
81
 
85
82
  ```ruby
86
83
  collection.each do |item|
87
- result_of_do_something = timed_metric.measure { do_something( item ) }
84
+ result_of_do_something = timed_metric.measure { do_something(item) }
85
+ # do something with result_of_do_something
88
86
  end
89
87
  ```
90
88
  And then look at the stats
@@ -96,15 +94,16 @@ puts timed_metric.min
96
94
  puts timed_metric.stddev
97
95
  puts timed_metric.rate
98
96
  ```
97
+
99
98
  ### ValueMetric
100
99
 
101
100
  Use a `Hitimes::ValueMetric` to calculate statistics about measured samples.
102
101
 
103
102
  ``` ruby
104
- value_metric = Hitimes::ValueMetric.new( 'size of thing' )
103
+ value_metric = Hitimes::ValueMetric.new("size of thing")
105
104
  loop do
106
105
  # ... do stuff changing sizes of 'thing'
107
- value_metric.measure( thing.size )
106
+ value_metric.measure(thing.size)
108
107
  # ... do other stuff that may change size of thing
109
108
  end
110
109
 
@@ -120,12 +119,12 @@ puts value_metric.rate
120
119
  Use a `Hitimes::TimedValueMetric` to calculate statistics about batches of samples.
121
120
 
122
121
  ``` ruby
123
- timed_value_metric = Hitimes::TimedValueMetric.new( 'batch times' )
124
- loop do
122
+ timed_value_metric = Hitimes::TimedValueMetric.new("batch times")
123
+ loop do
125
124
  batch = ... # get a batch of things
126
125
  timed_value_metric.start
127
126
  # .. do something with batch
128
- timed_value_metric.stop( batch.size )
127
+ timed_value_metric.stop(batch.size)
129
128
  end
130
129
 
131
130
  puts timed_value_metric.rate
@@ -147,46 +146,42 @@ Hitimes uses the internal ruby `Process::clock_gettime()` to
147
146
  get the highest granularity time increment possible. Generally this is
148
147
  nanosecond resolution, or whatever the hardware in the CPU supports.
149
148
 
150
- ## Support
149
+ ## SUPPORT
151
150
 
152
151
  Hitimes is supported on whatever versions of ruby are currently supported.
153
152
  Hitimes also follows [semantic versioning](http://semver.org/).
154
153
 
155
154
  The current officially supported versions of Ruby are:
156
155
 
157
- * MRI Ruby (all platforms) 2.3 - 2.6
158
- * JRuby 9.1.17.0, 9.2.X.X
156
+ * MRI Ruby (all platforms) 3.0 - current
157
+ * JRuby 9.4.x.x
158
+ * Truffleruby 24
159
159
 
160
- Unofficially supported versions, any version of MRI from Ruby 2.1 and up. Sincd
160
+ Unofficially supported versions, any version of MRI from Ruby 2.1 and up. Since
161
161
  the C Extension has been removed Hitimes should work with any ruby that is 2.1
162
162
  or greater as that is when `Process.clock_gettime()` was implemented.
163
163
 
164
164
  For versions of Ruby before 2.1 please use Hitimes 1.3, the extension code is
165
165
  still in there and they should still work.
166
166
 
167
- ## Contributing
167
+ ## CONTRIBUTING
168
168
 
169
- Please read the [CONTRIBUTING.md](CONTRIBUTING.md)
169
+ Please read [CONTRIBUTING.md](CONTRIBUTING.md) for instructions on development
170
+ and bug reporting.
170
171
 
171
172
  ## Credits
172
173
 
173
- * [Bruce Williams](https://github.com/bruce) for suggesting the idea
174
+ * [Bruce Williams](https://github.com/bruce) for suggesting the idea.
175
+ * [Benoit Daloze](https://github.com/eregon) and [Thomas Hurst](https://github.com/Freaky) for conversations around clock_ids.
174
176
 
175
177
  ## License
176
178
 
177
179
  Hitimes is licensed under the [ISC](https://opensource.org/licenses/ISC)
178
180
  license.
179
181
 
180
- Copyright (c) 2008-2016 Jeremy Hinegardner
181
-
182
- Permission to use, copy, modify, and/or distribute this software for any
183
- purpose with or without fee is hereby granted, provided that the above
184
- copyright notice and this permission notice appear in all copies.
182
+ ## Related Works
185
183
 
186
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
187
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
188
- FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
189
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
190
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
191
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
192
- PERFORMANCE OF THIS SOFTWARE.
184
+ * [monotime](https://github.com/Freaky/monotime) - A sensible interface to Ruby's monotonic clock.
185
+ * [concurrent-ruby](https://github.com/ruby-concurrency/concurrent-ruby) - [Concurrent.monotonic_time](https://github.com/ruby-concurrency/concurrent-ruby) is a straight pass through to `Process.clock_gettime(Process::CLOCK_MONOTONIC,...)`.
186
+ * [Instant](https://doc.rust-lang.org/src/std/time.rs.html) - The rust equivalent.
187
+ * [time.Now](https://pkg.go.dev/time) - The go monotonic time interface is part of this package.
data/hitimes.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # DO NOT EDIT - This file is automatically generated
2
+ # Make changes to Manifest.txt and/or Rakefile and regenerate
3
+ # -*- encoding: utf-8 -*-
4
+ # stub: hitimes 3.0.0 ruby lib
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "hitimes".freeze
8
+ s.version = "3.0.0".freeze
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
11
+ s.metadata = { "bug_tracker_uri" => "https://github.com/copiousfreetime/hitimes/issues", "changelog_uri" => "https://github.com/copiousfreetime/hitimes/blob/master/HISTORY.md", "homepage_uri" => "https://github.com/copiousfreetime/hitimes", "source_code_uri" => "https://github.com/copiousfreetime/hitimes" } if s.respond_to? :metadata=
12
+ s.require_paths = ["lib".freeze]
13
+ s.authors = ["Jeremy Hinegardner".freeze]
14
+ s.bindir = "exe".freeze
15
+ s.date = "2024-05-01"
16
+ s.description = "A fast, high resolution timer library for recording performance metrics.".freeze
17
+ s.email = "jeremy@copiousfreetime.org".freeze
18
+ s.extra_rdoc_files = ["CONTRIBUTING.md".freeze, "HISTORY.md".freeze, "LICENSE.txt".freeze, "Manifest.txt".freeze, "README.md".freeze]
19
+ s.files = ["CONTRIBUTING.md".freeze, "HISTORY.md".freeze, "LICENSE.txt".freeze, "Manifest.txt".freeze, "README.md".freeze, "hitimes.gemspec".freeze, "lib/hitimes.rb".freeze, "lib/hitimes/initialize.rb".freeze, "lib/hitimes/instant.rb".freeze, "lib/hitimes/interval.rb".freeze, "lib/hitimes/metric.rb".freeze, "lib/hitimes/mutexed_stats.rb".freeze, "lib/hitimes/paths.rb".freeze, "lib/hitimes/stats.rb".freeze, "lib/hitimes/timed_metric.rb".freeze, "lib/hitimes/timed_value_metric.rb".freeze, "lib/hitimes/value_metric.rb".freeze, "lib/hitimes/version.rb".freeze]
20
+ s.homepage = "http://github.com/copiousfreetime/hitimes".freeze
21
+ s.licenses = ["ISC".freeze]
22
+ s.rdoc_options = ["--main".freeze, "README.md".freeze, "--markup".freeze, "tomdoc".freeze]
23
+ s.required_ruby_version = Gem::Requirement.new(">= 3.0.0".freeze)
24
+ s.rubygems_version = "3.5.9".freeze
25
+ s.summary = "A fast, high resolution timer library for recording performance metrics.".freeze
26
+ end
@@ -1,64 +1,103 @@
1
- module Hitimes
1
+ # frozen_string_literal: true
2
2
 
3
+ module Hitimes
3
4
  # Internal: Internal setup that is done when the library is loaded
4
5
  #
5
6
  # We want to determine what clock to use for this machine. So we need to
6
- # intropect the ruby environment and then setup some initial constants. These
7
+ # introspect the ruby environment and then setup some initial constants. These
7
8
  # methods are used in lib/hitimes/instant.rb to help setup the CLOCK_ID
8
- # constant
9
+ # constant at load time.
9
10
  #
10
11
  module Initialize
11
-
12
- # Internal: Return the list of clock ids, in general priority order,
13
- # assuming they all have the same resolution.
14
12
  #
15
- # On OSX we probably want to use the MACH time first, and then fall back to
16
- # CLOCK_... Constants on other machines.
13
+ # After a fair bit of experimentaiton, it seems that the only clock_ids that
14
+ # are of any use are the following:
15
+ #
16
+ # POSIX:
17
+ #
18
+ # CLOCK_REALTIME A settable system-wide real-time clock. Measures
19
+ # wall-clock time. Affected by system jumps in time,
20
+ # adjtime(3), and NTP.
21
+ #
22
+ # CLOCK_MONOTONIC A nonsettable system-wide clock that represent
23
+ # monotomic time since some unspecified point in the
24
+ # past. Not affected by jumps in system time, but is
25
+ # affectd by adjtime(3) and NTP.
26
+ #
27
+ # Darwin:
28
+ #
29
+ # CLOCK_MONOTONIC_RAW clock that increments monotonically, tracking the
30
+ # time since an arbitrary point like CLOCK_MONOTONIC.
31
+ # However, this clock is unaffected by frequency or
32
+ # time adjustments.
33
+ #
34
+ # CLOCK_UPTIME_RAW clock that increments monotonically, in the same manner
35
+ # as CLOCK_MONOTONIC_RAW, but that does not increment
36
+ # while the system is asleep. The returned value is
37
+ # identical to the result of mach_absolute_time()
38
+ # after the appropriate mach_timebase conversion is applied.
39
+ #
40
+ # Linux:
41
+ #
42
+ # CLOCK_MONOTONIC_RAW Similar to CLOCK_MONOTONIC, but provides access to
43
+ # a raw hardware-based time that is not subject to NTP
44
+ # adjustments or the incremental adjustments performed
45
+ # by adjtime(3)
17
46
  #
18
- # The one requirement is that they are monotonically increasing clocks
47
+ # CLOCK_BOOTTIME Identical to CLOCK_MONOTONIC, except it also includes any
48
+ # time that the system is suspended.
19
49
  #
20
- # Returns an array of clock ids
21
- def potential_clock_ids
22
- Array.new.tap do |clock_ids|
50
+ # *BSD:
51
+ #
52
+ # All the BSDs seem to have CLOCK_MONOTONIC and CLOCK_REALTIME although on
53
+ # NetBSD CLOCK_MONOTONIC is not affected by adjtime(2). It is unclear if
54
+ # they are affected by adjtime(2) on FreeBSD, OpenBSD, or DragonFlyBSD. -
55
+ # at least according to the man pages.
56
+ #
57
+ # What this boils down to as that pretty much all systems have CLOCK_REALTIME
58
+ # and CLOCK_MONOTONIC. The other clocks are system specific and may or may
59
+ # not exist. We'll try to use the most accurate clock available.
60
+ #
61
+ # So we'll try to use the following clocks in order of preference:
62
+ #
63
+ # On Linux and Darwin
64
+ # CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC, CLOCK_REALTIME
65
+ #
66
+ # Everyone else:
67
+ # CLOCK_MONOTONIC, CLOCK_REALTIME
68
+ #
69
+ # So in reality, well just test for constants on all of the above and use the
70
+ # first one that exists.
71
+ #
72
+ # If CLOCK_REALTIME is chose, we will dump a warning to the user.
73
+ # And if we can't finde one, which is really, really odd, we'll raise an exception.
74
+ POTENTIAL_CLOCK_IDS = %i[CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC CLOCK_REALTIME].freeze
75
+ def determine_clock_id(potential_ids = POTENTIAL_CLOCK_IDS)
76
+ sym = potential_ids.find { |id| Process.const_defined?(id) }
23
77
 
24
- # if we're on OSX this will add in an additional clock_id, although not
25
- # sure why it is just a symbol and not a Process:: constant
26
- #
27
- begin
28
- Process.clock_getres(:MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC)
29
- clock_ids << :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
30
- rescue Errno::EINVAL
31
- # not on OSX
32
- end
78
+ unless sym
79
+ raise Hitimes::Error, <<~ERROR
80
+ Unable to find a high resolution clock at all. THIS IS A BUG!!
33
81
 
34
- # General monotonic constants in general order of priority assuming they
35
- # all have the same resolution
36
- #
37
- %i[ CLOCK_MONOTONIC_RAW CLOCK_BOOTTIME CLOCK_MONOTONIC_PRECISE CLOCK_MONOTONIC ].each do |c|
38
- clock_ids << Process.const_get(c) if Process.const_defined?(c)
39
- end
82
+ RUBY_DESCRIPTION: #{RUBY_DESCRIPTION}
40
83
 
84
+ Please report this bug to the hitimes issue tracker at
85
+ https://github.com/copiousfreetime/hitimes/issues
86
+ ERROR
41
87
  end
42
- end
43
- module_function :potential_clock_ids
44
88
 
45
- # Internal: Determine what clock to use for the machine we are one. We want
46
- # the highest resolution clock possible, which should be nanosecond
47
- # resolution.
48
- #
49
- # Get the resolution of each clock id and then return the higest resolution
50
- # id from the list
51
- #
52
- # Returns the clock id to use on this ruby
53
- def determine_clock_id
54
- ids_and_resolutions = potential_clock_ids.map { |clock_id|
55
- [clock_id, Process.clock_getres(clock_id)]
56
- }
89
+ if sym == :CLOCK_REALTIME
90
+ warn <<~TXT
91
+ Unable to find a high resolution clock. Using CLOCK_REALTIME for timing.
57
92
 
58
- # Sort them by the resolution - we want the smallest one first
59
- ids_and_resolutions.sort_by! { |pair| pair[1] }
93
+ RUBY_DESCRIPTION: #{RUBY_DESCRIPTION}
94
+
95
+ Please report the above information to the hitimes issue tracker at
96
+ https://github.com/copiousfreetime/hitimes/issues
97
+ TXT
98
+ end
60
99
 
61
- return ids_and_resolutions.first[0]
100
+ Process.const_get(sym)
62
101
  end
63
102
  module_function :determine_clock_id
64
103
  end
@@ -1,24 +1,16 @@
1
- require 'hitimes/initialize'
1
+ # frozen_string_literal: true
2
2
 
3
+ require "hitimes/initialize"
4
+
5
+ # Hitimes Constants and module methods
6
+ #
3
7
  module Hitimes
4
8
  # Public: The clock_id to use in Process.clock_gettime
5
9
  CLOCK_ID = Initialize.determine_clock_id.freeze
6
10
 
7
- # Public: The resolution of the clock
8
- CLOCK_RESOLUTION_NANOSECONDS = Process.clock_getres(CLOCK_ID, :nanosecond).freeze
9
-
10
11
  # Internal: The fraction of second of a nanosecond
11
12
  NANOSECONDS_PER_SECOND = 1e9
12
13
 
13
- # Public: The smallest fraction of a second hitimes can do
14
- CLOCK_RESOLUTION_SECONDS = CLOCK_RESOLUTION_NANOSECONDS / NANOSECONDS_PER_SECOND
15
-
16
- # Public: The factor used to convert the instant values to fractional seconds
17
- #
18
- # The raw instant values are divided by this value to get float seconds
19
- INSTANT_CONVERSION_FACTOR = CLOCK_RESOLUTION_NANOSECONDS * NANOSECONDS_PER_SECOND
20
-
21
-
22
14
  # Public: Get the raw instant
23
15
  #
24
16
  # Examples:
@@ -39,27 +31,11 @@ module Hitimes
39
31
  when Symbol
40
32
  CLOCK_ID.to_s
41
33
  else
42
- const = Process.constants.grep(/CLOCK/).find { |c|
43
- CLOCK_ID == Process.const_get(c)
44
- }
45
- "Process::#{const.to_s}"
34
+ const = Process.constants.grep(/CLOCK/).find do |id|
35
+ Process.const_get(id) == CLOCK_ID
36
+ end
37
+ "Process::#{const}"
46
38
  end
47
39
  end
48
40
  module_function :clock_name
49
-
50
- # Internal: The human readable clock resolution
51
- #
52
- # Returns the clock resolution as a string
53
- def clock_resolution_description
54
- "#{CLOCK_RESOLUTION_NANOSECONDS}ns"
55
- end
56
- module_function :clock_resolution_description
57
-
58
- # Internal: The human readable clock description that is used by hitimes
59
- #
60
- # Returns the clock description as a String
61
- def clock_description
62
- "#{clock_name} #{clock_resolution_description}"
63
- end
64
- module_function :clock_description
65
41
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2008 Jeremy Hinegardner
2
4
  # All rights reserved. See LICENSE and/or COPYING for details.
3
5
  #
@@ -20,7 +22,6 @@ module Hitimes
20
22
  # Interval is useful when you only need to track a single interval of time, or
21
23
  # if you do not want to track statistics about an operation.
22
24
  class Interval
23
-
24
25
  # Public: The integer representing the start instant of the Interval. This
25
26
  # valuea is not useful on its own. It is a platform dependent value.
26
27
  attr_reader :start_instant
@@ -48,14 +49,14 @@ module Hitimes
48
49
  # Interval.measure { } -> Float
49
50
  #
50
51
  # Times the execution of the block returning the number of seconds it took
51
- def self.measure(&block)
52
+ def self.measure
52
53
  raise Error, "No block given to Interval.measure" unless block_given?
53
54
 
54
- i = Interval.now
55
+ interval = Interval.now
55
56
  yield
56
- i.stop
57
+ interval.stop
57
58
 
58
- return i.duration
59
+ interval.duration
59
60
  end
60
61
 
61
62
  # call-seq:
@@ -65,7 +66,7 @@ module Hitimes
65
66
  # start_instant equivalent to the stop_interval of self.
66
67
  def split
67
68
  @stop_instant = ::Hitimes.raw_instant
68
- return Interval.new(@stop_instant)
69
+ Interval.new(@stop_instant)
69
70
  end
70
71
 
71
72
  # call-seq:
@@ -76,6 +77,7 @@ module Hitimes
76
77
  # interval is truely started +true+ is returned otherwise +false+.
77
78
  def start
78
79
  return false if started?
80
+
79
81
  @start_instant = ::Hitimes.raw_instant
80
82
  true
81
83
  end
@@ -92,7 +94,7 @@ module Hitimes
92
94
 
93
95
  @stop_instant = ::Hitimes.raw_instant
94
96
 
95
- return duration
97
+ duration
96
98
  end
97
99
 
98
100
  # call-seq:
@@ -104,8 +106,8 @@ module Hitimes
104
106
  def duration_so_far
105
107
  return false unless running?
106
108
 
107
- _now = Hitimes.raw_instant
108
- calculate_duration(@start_instant, _now)
109
+ raw = Hitimes.raw_instant
110
+ calculate_duration(@start_instant, raw)
109
111
  end
110
112
 
111
113
  # call-seq:
@@ -120,7 +122,7 @@ module Hitimes
120
122
  # interval.stopped? -> boolean
121
123
  #
122
124
  # returns whether or not the interval has been stopped
123
- def stopped?
125
+ def stopped?
124
126
  !!@stop_instant
125
127
  end
126
128
 
@@ -151,11 +153,9 @@ module Hitimes
151
153
 
152
154
  return duration_so_far unless stopped?
153
155
 
154
- if @duration < 0 then
155
- @duration = calculate_duration(@start_instant, @stop_instant)
156
- end
156
+ @duration = calculate_duration(@start_instant, @stop_instant) if @duration.negative?
157
157
 
158
- return @duration
158
+ @duration
159
159
  end
160
160
 
161
161
  alias to_f duration
@@ -165,7 +165,7 @@ module Hitimes
165
165
  private
166
166
 
167
167
  def calculate_duration(start, stop)
168
- (stop - start) / ::Hitimes::INSTANT_CONVERSION_FACTOR
168
+ (stop - start) / ::Hitimes::NANOSECONDS_PER_SECOND
169
169
  end
170
170
  end
171
171
  end