File: ruby_prof_spec.rb

package info (click to toggle)
ruby-test-prof 1.6.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,448 kB
  • sloc: ruby: 13,093; sh: 4; makefile: 4
file content (121 lines) | stat: -rw-r--r-- 3,881 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# frozen_string_literal: true

describe TestProf::RubyProf do
  before do
    next if defined?(::RubyProf::WALL_TIME)

    stub_const("::RubyProf::WALL_TIME", 0)
  end

  # Use fresh config all for every example
  after { described_class.remove_instance_variable(:@config) }

  describe ".config" do
    subject { described_class.config }

    specify "defaults", :aggregate_failures do
      expect(subject.printer).to eq :flat
      expect(subject.mode).to eq "wall"
      expect(subject.min_percent).to eq 1
      expect(subject.include_threads).to eq false
    end

    describe "#resolve_printer" do
      it "works with custom class" do
        subject.printer = TestProf
        expect(subject.resolve_printer).to eq(["custom", TestProf])
      end

      it "raises when unknown printer" do
        subject.printer = "unknown"
        expect { subject.resolve_printer }.to raise_error(ArgumentError)
      end
    end
  end

  describe "#profile" do
    let(:ruby_prof) { double("ruby_prof") }
    let(:profile) { double("profile") }

    before do
      stub_const("RubyProf::Profile", ruby_prof)
      allow(profile).to receive(:exclude_common_methods!)
      allow(profile).to receive(:exclude_methods!)
      expect(profile).to receive(:start)
    end

    specify "with default config" do
      expect(ruby_prof).to receive(:new).with({
        include_threads: [Thread.current],
        measure_mode: 0
      }).and_return(profile)

      expect(described_class.profile).to be_a(described_class::Report)
    end

    specify "with custom config" do
      described_class.config.include_threads = true

      expect(ruby_prof).to receive(:new).with({measure_mode: 0}).and_return(profile)

      described_class.profile
    end
  end

  describe "Report#dump" do
    let(:ruby_prof) { double("ruby_prof") }
    let(:profile) { double("profile") }
    let(:result) { double("result") }
    let(:printer_class) { double("printer_class") }
    let(:printer) { double("printer") }

    before do
      stub_const("::RubyProf::Profile", ruby_prof)
      expect(ruby_prof).to receive(:new).and_return(profile)
      expect(profile).to receive(:start)
      allow(profile).to receive(:exclude_methods!)
      expect(profile).to receive(:stop).and_return(result)
    end

    subject { described_class.profile }

    specify "with default config" do
      expect(profile).to receive(:exclude_common_methods!)

      stub_const("::RubyProf::FlatPrinter", printer_class)
      expect(printer_class).to receive(:new).with(result).and_return(printer)
      expect(printer).to receive(:print).with(anything, min_percent: 1).and_return("")

      subject.dump("stub")

      expect(File.exist?(File.join(TestProf.config.output_dir, "ruby-prof-report-flat-wall-stub.txt"))).to eq true
    end

    specify "with custom config" do
      described_class.config.printer = :call_stack
      described_class.config.exclude_common_methods = false
      described_class.config.test_prof_exclusions_enabled = false
      described_class.config.custom_exclusions = {
        TestProf => %i[log print]
      }
      described_class.config.min_percent = 2
      described_class.config.mode = :cpu

      TestProf.config.timestamps = true

      expect(profile).not_to receive(:exclude_common_methods!)
      expect(profile).to receive(:exclude_methods!).once.with(
        TestProf, :log, :print
      )

      stub_const("RubyProf::CallStackPrinter", printer_class)
      expect(printer_class).to receive(:new).with(result).and_return(printer)
      expect(printer).to receive(:print).with(anything, min_percent: 2).and_return("")
      expect(TestProf).to receive(:now).and_return(double("now", to_i: 123_454_321))

      subject.dump("stub")

      expect(File.exist?(File.join(TestProf.config.output_dir, "ruby-prof-report-call_stack-cpu-stub-123454321.html"))).to eq true
    end
  end
end