1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
require 'spec_helper'
require 'puppet/util/profiler'
describe Puppet::Util::Profiler::Logging do
let(:logger) { SimpleLog.new }
let(:identifier) { "Profiling ID" }
let(:logging_profiler) { TestLoggingProfiler.new(logger, identifier) }
let(:profiler) do
p = Puppet::Util::Profiler::AroundProfiler.new
p.add_profiler(logging_profiler)
p
end
it "logs the explanation of the profile results" do
profiler.profile("Testing", ["test"]) { }
expect(logger.messages.first).to match(/the explanation/)
end
it "describes the profiled segment" do
profiler.profile("Tested measurement", ["test"]) { }
expect(logger.messages.first).to match(/PROFILE \[#{identifier}\] \d Tested measurement/)
end
it "indicates the order in which segments are profiled" do
profiler.profile("Measurement", ["measurement"]) { }
profiler.profile("Another measurement", ["measurement"]) { }
expect(logger.messages[0]).to match(/1 Measurement/)
expect(logger.messages[1]).to match(/2 Another measurement/)
end
it "indicates the nesting of profiled segments" do
profiler.profile("Measurement", ["measurement1"]) do
profiler.profile("Nested measurement", ["measurement2"]) { }
end
profiler.profile("Another measurement", ["measurement1"]) do
profiler.profile("Another nested measurement", ["measurement2"]) { }
end
expect(logger.messages[0]).to match(/1.1 Nested measurement/)
expect(logger.messages[1]).to match(/1 Measurement/)
expect(logger.messages[2]).to match(/2.1 Another nested measurement/)
expect(logger.messages[3]).to match(/2 Another measurement/)
end
class TestLoggingProfiler < Puppet::Util::Profiler::Logging
def do_start(metric, description)
"the start"
end
def do_finish(context, metric, description)
{:msg => "the explanation of #{context}"}
end
end
class SimpleLog
attr_reader :messages
def initialize
@messages = []
end
def call(msg)
@messages << msg
end
end
end
|