summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRein Henrichs <rein@puppetlabs.com>2010-10-05 10:42:52 -0700
committerRein Henrichs <rein@puppetlabs.com>2010-10-05 10:42:52 -0700
commitdce8f5727c4b6ee0332500c189775cc7c036e6d0 (patch)
tree8f334730a79a5b94e5f752dcb55f73ecebd90867
parent74ce28698714acace4d588a7639bd8bd70144f3a (diff)
parent07f186d5a858412c0608c368806825e1e44efd4c (diff)
downloadfacter-dce8f5727c4b6ee0332500c189775cc7c036e6d0.tar.gz
facter-dce8f5727c4b6ee0332500c189775cc7c036e6d0.tar.xz
facter-dce8f5727c4b6ee0332500c189775cc7c036e6d0.zip
Merge branch 'ticket/master/4552' into next
Conflicts: bin/facter lib/facter/application.rb
-rwxr-xr-xbin/facter3
-rw-r--r--lib/facter.rb26
-rw-r--r--lib/facter/application.rb8
-rw-r--r--lib/facter/util/resolution.rb7
-rwxr-xr-xspec/unit/facter.rb27
5 files changed, 70 insertions, 1 deletions
diff --git a/bin/facter b/bin/facter
index 328d5fc..9d3b49c 100755
--- a/bin/facter
+++ b/bin/facter
@@ -36,6 +36,9 @@
# trace::
# Enable backtraces.
#
+# timing::
+# Enable timing.
+#
# = Example
#
# facter kernel
diff --git a/lib/facter.rb b/lib/facter.rb
index 42d5371..f48138a 100644
--- a/lib/facter.rb
+++ b/lib/facter.rb
@@ -48,6 +48,7 @@ module Facter
GREEN = ""
RESET = ""
@@debug = 0
+ @@timing = 0
# module methods
@@ -77,6 +78,15 @@ module Facter
@@debug != 0
end
+ # show the timing information
+ def self.show_time(string)
+ puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing?
+ end
+
+ def self.timing?
+ @@timing != 0
+ end
+
# Return a fact object by name. If you use this, you still have to call
# 'value' on it to retrieve the actual value.
def self.[](name)
@@ -177,6 +187,22 @@ module Facter
end
end
+ # Set timing on or off.
+ def self.timing(bit)
+ if bit
+ case bit
+ when TrueClass; @@timing = 1
+ when Fixnum
+ if bit > 0
+ @@timing = 1
+ else
+ @@timing = 0
+ end
+ end
+ else
+ @@timing = 0
+ end
+ end
def self.warn(msg)
if Facter.debugging? and msg and not msg.empty?
diff --git a/lib/facter/application.rb b/lib/facter/application.rb
index b80d07c..11a29c6 100644
--- a/lib/facter/application.rb
+++ b/lib/facter/application.rb
@@ -59,8 +59,8 @@ module Facter
OptionParser.new do |opts|
opts.on("-y", "--yaml") { |v| options[:yaml] = v }
opts.on( "--trace") { |v| options[:trace] = v }
-
opts.on("-d", "--debug") { |v| Facter.debugging(1) }
+ opts.on("-t", "--timing") { |v| Facter.timing(1) }
opts.on("-p", "--puppet") { |v| load_puppet }
opts.on_tail("-v", "--version") do
@@ -75,6 +75,12 @@ module Facter
puts RDoc.usage
ensure
exit
+ rescue LoadError
+ $stderr.puts "No help available unless your RDoc has RDoc.usage"
+ exit(1)
+ rescue => e
+ $stderr.puts "fatal: #{e}"
+ exit(1)
end
end
end.parse!
diff --git a/lib/facter/util/resolution.rb b/lib/facter/util/resolution.rb
index f837f64..4a99c35 100644
--- a/lib/facter/util/resolution.rb
+++ b/lib/facter/util/resolution.rb
@@ -135,6 +135,9 @@ class Facter::Util::Resolution
def value
result = nil
return result if @code == nil and @interpreter == nil
+
+ starttime = Time.now.to_f
+
begin
Timeout.timeout(limit) do
if @code.is_a?(Proc)
@@ -156,6 +159,10 @@ class Facter::Util::Resolution
return nil
end
+ finishtime = Time.now.to_f
+ ms = (finishtime - starttime) * 1000
+ Facter.show_time "#{self.name}: #{"%.2f" % ms}ms"
+
return nil if result == ""
return result
end
diff --git a/spec/unit/facter.rb b/spec/unit/facter.rb
index 9f7b4cf..20f9ed1 100755
--- a/spec/unit/facter.rb
+++ b/spec/unit/facter.rb
@@ -135,6 +135,14 @@ describe Facter do
Facter.should respond_to(:debugging?)
end
+ it "should have a method to query timing mode" do
+ Facter.should respond_to(:timing?)
+ end
+
+ it "should have a method to show timing information" do
+ Facter.should respond_to(:show_time)
+ end
+
it "should have a method to warn" do
Facter.should respond_to(:warn)
end
@@ -204,6 +212,25 @@ describe Facter do
end
end
+ describe "when setting timing mode" do
+ it "should have timing enabled using 1" do
+ Facter.timing(1)
+ Facter.should be_timing
+ end
+ it "should have timing enabled using true" do
+ Facter.timing(true)
+ Facter.should be_timing
+ end
+ it "should have timing disabled using 0" do
+ Facter.timing(0)
+ Facter.should_not be_timing
+ end
+ it "should have timing disabled using false" do
+ Facter.timing(false)
+ Facter.should_not be_timing
+ end
+ end
+
describe "when registering directories to search" do
after { Facter.instance_variable_set("@search_path", []) }