-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
(FACT-1380) Restore --timing option to native facter
- Loading branch information
Showing
9 changed files
with
83 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'benchmark' | ||
|
||
module Facter | ||
module Framework | ||
module Benchmarking | ||
class Timer | ||
class << self | ||
def measure(fact_name) | ||
if Options[:timing] | ||
time = Benchmark.measure { yield } | ||
|
||
puts "fact `#{fact_name}`, took: #{time.format('%r')} seconds" | ||
else | ||
yield | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facter::Framework::Benchmarking::Timer do | ||
let(:tms_mock) { instance_spy(Benchmark::Tms) } | ||
|
||
describe '#measure' do | ||
context 'when timing option is true' do | ||
before do | ||
allow(Facter::Options).to receive(:[]).with(:timing).and_return(true) | ||
allow(tms_mock).to receive(:format).with('%r').and_return('(0.123)') | ||
allow(Benchmark).to receive(:measure).and_return(tms_mock) | ||
end | ||
|
||
it 'prints fact name and time it took to resolve it' do | ||
expect do | ||
Facter::Framework::Benchmarking::Timer.measure('my_fact') {} | ||
end.to output("fact `my_fact`, took: (0.123) seconds\n").to_stdout | ||
end | ||
end | ||
|
||
context 'when timing option is false' do | ||
before do | ||
allow(Facter::Options).to receive(:[]).with(:timing).and_return(false) | ||
end | ||
|
||
it 'does not print any message' do | ||
expect do | ||
Facter::Framework::Benchmarking::Timer.measure('my_fact') {} | ||
end.not_to output.to_stdout | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,8 @@ | |
config: nil, | ||
cache: true, | ||
color: false, | ||
trace: false | ||
trace: false, | ||
timing: false | ||
) | ||
end | ||
end | ||
|