Skip to content

Commit

Permalink
Use a single FacterDB.get_facts call
Browse files Browse the repository at this point in the history
The major cost is FacterDB.get_facts. Instead of calling it once for
every OS version this instead calls it once with a complex filter. It
then uses JGrep to simulate another smaller DB and futher query that.

In my non-scientific test this reduces the load time of puppet-example
from 3.5 to 2.5 seconds.
  • Loading branch information
ekohl committed Jul 20, 2023
1 parent 5c92f4f commit ae76698
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/rspec-puppet-facts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,21 @@ def on_supported_os_implementation(opts = {})
loose_requirement = RspecPuppetFacts::facter_version_to_loose_requirement(facterversion)
received_facts = []

db = FacterDB.get_facts(filter)
unless db.any?
RspecPuppetFacts.warning "No facts were found in the FacterDB for: #{filter.inspect}"
return {}
end

# FacterDB may have newer versions of facter data for which it contains a subset of all possible
# facter data (see FacterDB 0.5.2 for Facter releases 3.8 and 3.9). In this situation we need to
# cycle through and downgrade Facter versions per platform type until we find matching Facter data.
filter.each do |filter_spec|
versions = FacterDB.get_facts(filter_spec).to_h { |facts| [Gem::Version.new(facts[:facterversion]), facts] }
versions = JGrep.jgrep(db.to_json, FacterDB.generate_filter_str(filter_spec)) \
.to_h do |facts|
result = facts.to_h { |k, v| [k.to_sym, v] }
[Gem::Version.new(result[:facterversion]), result]
end

version, facts = versions.select { |v, _f| strict_requirement =~ v }.max_by { |v, _f| v }

Expand Down

0 comments on commit ae76698

Please sign in to comment.