summaryrefslogtreecommitdiff
path: root/spec/unit/util/profiler/logging_spec.rb
blob: 3f6a728dda81e3e6013961457bc75a408a291f3e (plain)
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"]) { }

    logger.messages.first.should =~ /the explanation/
  end

  it "describes the profiled segment" do
    profiler.profile("Tested measurement", ["test"]) { }

    logger.messages.first.should =~ /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"]) { }

    logger.messages[0].should =~ /1 Measurement/
    logger.messages[1].should =~ /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

    logger.messages[0].should =~ /1.1 Nested measurement/
    logger.messages[1].should =~ /1 Measurement/
    logger.messages[2].should =~ /2.1 Another nested measurement/
    logger.messages[3].should =~ /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