SusSourceSusClock

class Clock

Represents a clock for measuring elapsed time during test execution.

Definitions

def self.start!

Create a new clock and start it immediately.

Signature

returns Clock

A new started clock.

Implementation

def self.start!
	self.new.tap(&:start!)
end

def start!

Start the clock.

Implementation

def start!
	@start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(duration = 0.0)

Initialize a new clock.

Signature

parameter duration Float

Initial duration in seconds.

Implementation

def initialize(duration = 0.0)
	@duration = duration
end

def duration

Get the current elapsed duration.

Signature

returns Float

The elapsed duration in seconds.

Implementation

def duration
	if @start_time
		now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
		@duration += now - @start_time
		@start_time = now
	end
	
	return @duration
end

def <=>(other)

Compare this clock's duration with another value.

Signature

parameter other Numeric

The value to compare against.

returns Integer

-1, 0, or 1 depending on comparison result.

Implementation

def <=>(other)
	duration <=> other.to_f
end

def to_f

Convert the duration to a float.

Signature

returns Float

The duration in seconds.

Implementation

def to_f
	duration
end

def ms

Get the duration in milliseconds.

Signature

returns Float

The duration in milliseconds.

Implementation

def ms
	duration * 1000.0
end

def to_s

Get a human-readable string representation of the duration.

Signature

returns String

A formatted duration string (e.g., "1.5ms", "2.3s").

Implementation

def to_s
	duration = self.duration
	
	if duration < 0.001
		"#{(duration * 1_000_000).round(1)}µs"
	elsif duration < 1.0
		"#{(duration * 1_000).round(1)}ms"
	else
		"#{duration.round(1)}s"
	end
end

def reset!(duration = 0.0)

Reset the clock to a specific duration.

Signature

parameter duration Float

The duration to reset to.

Implementation

def reset!(duration = 0.0)
	@duration = duration
end

def stop!

Stop the clock and return the final duration.

Signature

returns Float

The final duration in seconds.

Implementation

def stop!
	if @start_time
		now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
		@duration += now - @start_time
		@start_time = nil
	end
	
	return duration
end