Add Mutant::Timer
This commit is contained in:
parent
b9462ecd92
commit
b4fdc7b7c6
3 changed files with 74 additions and 0 deletions
|
@ -177,6 +177,7 @@ require 'mutant/expression/method'
|
|||
require 'mutant/expression/methods'
|
||||
require 'mutant/expression/namespace'
|
||||
require 'mutant/test'
|
||||
require 'mutant/timer'
|
||||
require 'mutant/integration'
|
||||
require 'mutant/selector'
|
||||
require 'mutant/selector/expression'
|
||||
|
|
21
lib/mutant/timer.rb
Normal file
21
lib/mutant/timer.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Mutant
|
||||
module Timer
|
||||
# Monotonic elapsed time of block execution
|
||||
#
|
||||
# @return [Float]
|
||||
def self.elapsed
|
||||
start = now
|
||||
yield
|
||||
now - start
|
||||
end
|
||||
|
||||
# The now monotonic time
|
||||
#
|
||||
# @return [Float]
|
||||
def self.now
|
||||
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
||||
end
|
||||
end # Timer
|
||||
end # Mutant
|
52
spec/unit/mutant/clock_monotonic_spec.rb
Normal file
52
spec/unit/mutant/clock_monotonic_spec.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Mutant::Timer do
|
||||
let(:events) { [] }
|
||||
|
||||
let(:times) { [1.0, 2.0] }
|
||||
|
||||
before do
|
||||
allow(Process).to receive(:clock_gettime) do |argument|
|
||||
expect(argument).to be(Process::CLOCK_MONOTONIC)
|
||||
|
||||
events << :clock_gettime
|
||||
|
||||
times.fetch(events.count(:clock_gettime).pred)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.elapsed' do
|
||||
def apply
|
||||
described_class.elapsed { events << :yield }
|
||||
end
|
||||
|
||||
it 'executes events in expected sequence' do
|
||||
expect { apply }
|
||||
.to change(events, :to_a)
|
||||
.from([])
|
||||
.to(%i[clock_gettime yield clock_gettime])
|
||||
end
|
||||
|
||||
it 'returns elapsed time' do
|
||||
expect(apply).to be(1.0)
|
||||
end
|
||||
end
|
||||
|
||||
describe '.now' do
|
||||
def apply
|
||||
described_class.now
|
||||
end
|
||||
|
||||
it 'returns current monotonic time' do
|
||||
expect(apply).to be(1.0)
|
||||
expect(apply).to be(2.0)
|
||||
end
|
||||
|
||||
it 'calls expected system API' do
|
||||
expect { apply }
|
||||
.to change(events, :to_a)
|
||||
.from([])
|
||||
.to(%i[clock_gettime])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue