Skip to content

Commit

Permalink
use Process.clock_gettime instead of Time.now, resolves #13
Browse files Browse the repository at this point in the history
  • Loading branch information
toy committed Mar 31, 2021
1 parent 34d794a commit baf1618
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Expand Up @@ -2,6 +2,8 @@

## unreleased

* Use `Process.clock_gettime` instead of `Time.now` which is faster and should give proper estimates even if sleeping in the middle [#13](https://github.com/toy/progress/issues/13) [@toy](https://github.com/toy)

## v3.5.2 (2019-07-14)

* Remove deprecated `rubyforge_project` attribute from gemspec [rubygems/rubygems#2436](https://github.com/rubygems/rubygems/pull/2436) [@toy](https://github.com/toy)
Expand Down
6 changes: 4 additions & 2 deletions lib/progress/class_methods.rb
@@ -1,6 +1,8 @@
# encoding: UTF-8
# frozen_string_literal: true

require 'progress/elapsed_time'

class Progress
# Class methods of Progress
module ClassMethods
Expand Down Expand Up @@ -165,14 +167,14 @@ def restart_beeper
end

def time_to_print?
!@next_time_to_print || @next_time_to_print <= Time.now
!@next_time_to_print || @next_time_to_print <= ElapsedTime.now
end

def print_message(options = {})
force = options[:force]
lock force do
if force || time_to_print?
@next_time_to_print = Time.now + 0.3
@next_time_to_print = ElapsedTime.now + 0.3
restart_beeper
io << message_for_output(options)
end
Expand Down
26 changes: 26 additions & 0 deletions lib/progress/elapsed_time.rb
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class Progress
# Use Process.clock_gettime if available to get time more fitting to calculate elapsed time
module ElapsedTime
CLOCK_NAME = %w[
CLOCK_UPTIME_RAW
CLOCK_UPTIME
CLOCK_MONOTONIC_RAW
CLOCK_MONOTONIC
CLOCK_REALTIME
].find{ |name| Process.const_defined?(name) }

CLOCK_ID = CLOCK_NAME && Process.const_get(CLOCK_NAME)

module_function

def now
if CLOCK_ID
Process.clock_gettime(CLOCK_ID)
else
Time.now.to_f
end
end
end
end
8 changes: 5 additions & 3 deletions lib/progress/eta.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true

require 'progress/elapsed_time'

class Progress
# Estimate time of arrival
class Eta
def initialize
@started_at = Time.now
@started_at = ElapsedTime.now
end

def left(completed)
Expand All @@ -15,7 +17,7 @@ def left(completed)
end

def elapsed
seconds_to_string(Time.now - @started_at)
seconds_to_string(ElapsedTime.now - @started_at)
end

private
Expand All @@ -36,7 +38,7 @@ def seconds_to_string(seconds)
end

def seconds_left(completed)
now = Time.now
now = ElapsedTime.now
return unless completed > 0 && now - @started_at >= 1

current_eta = @started_at + (now - @started_at) / completed
Expand Down
13 changes: 13 additions & 0 deletions spec/elapsed_time_spec.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'progress/elapsed_time'

describe Progress::ElapsedTime do
let(:timeout){ 0.01 }

describe '.now' do
it 'returns incrementing value' do
expect{ sleep timeout }.to change{ described_class.now }.by_at_least(timeout)
end
end
end

0 comments on commit baf1618

Please sign in to comment.