Method: Puppet::Util.benchmark

Defined in:
lib/puppet/util.rb

.benchmark(*args) ⇒ Object

execute a block of work and based on the logging level provided, log the provided message with the seconds taken The message ‘msg’ should include string ‘ in %seconds seconds’ as part of the message and any content should escape any percent signs ‘%’ so that they are not interpreted as formatting commands

escaped_str = str.gsub(/%/, '%%')

Parameters:

  • msg (String)

    the message to be formated to assigned the %seconds seconds take to execute, other percent signs ‘%’ need to be escaped

  • level (Symbol)

    the logging level for this message

  • object (Object)

    The object use for logging the message

Raises:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/puppet/util.rb', line 222

def benchmark(*args)
  msg = args.pop
  level = args.pop
  object = if args.empty?
             if respond_to?(level)
               self
             else
               Puppet
             end
           else
             args.pop
           end

  #TRANSLATORS 'benchmark' is a method name and should not be translated
  raise Puppet::DevError, _("Failed to provide level to benchmark") unless level

  unless level == :none or object.respond_to? level
    raise Puppet::DevError, _("Benchmarked object does not respond to %{value}") % { value: level }
  end

  # Only benchmark if our log level is high enough
  if level != :none and Puppet::Util::Log.sendlevel?(level)
    seconds = Benchmark.realtime {
      yield
    }
    object.send(level, msg % { seconds: "%0.2f" % seconds })
    return seconds
  else
    yield
  end
end