summaryrefslogtreecommitdiff
path: root/lib/peek/rblineprof/custom_controller_helpers.rb
blob: 2ecda58c669d52eb78324ec6f9fca8d5504fcad3 (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
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
module Peek
  module Rblineprof
    module CustomControllerHelpers
      extend ActiveSupport::Concern

      def pygmentize(file_name, code, lexer = nil)
        if lexer.present?
          Gitlab::Highlight.highlight(file_name, code)
        else
          "<pre>#{Rack::Utils.escape_html(code)}</pre>"
        end
      end

      # rubocop:disable Metrics/AbcSize
      def inject_rblineprof
        ret = nil
        profile = lineprof(rblineprof_profiler_regex) do
          ret = yield
        end

        if response.content_type =~ %r|text/html|
          sort = params[:lineprofiler_sort]
          mode = params[:lineprofiler_mode] || 'cpu'
          min  = (params[:lineprofiler_min] || 5).to_i * 1000
          summary = params[:lineprofiler_summary]

          # Sort each file by the longest calculated time
          per_file = profile.map do |file, lines|
            total, _child, excl, total_cpu, _child_cpu, excl_cpu = lines[0]

            wall = summary == 'exclusive' ? excl : total
            cpu  = summary == 'exclusive' ? excl_cpu : total_cpu
            idle = summary == 'exclusive' ? (excl - excl_cpu) : (total - total_cpu)

            sort_method =
              case sort
              when 'idle'
                idle
              when 'cpu'
                cpu
              else
                wall
              end

            [
              file, lines,
              wall, cpu, idle,
              sort_method
            ]
          end
          per_file = per_file.sort_by { |_a, _b, _c, _d, _e, f| -f }

          output = ''
          per_file.each do |file_name, lines, file_wall, file_cpu, file_idle, file_sort|
            output << "<div class='peek-rblineprof-file'><div class='heading'>"

            show_src = file_sort > min
            tmpl = show_src ? "<a href='#' class='js-lineprof-file'>%s</a>" : "%s"

            output <<
              if mode == 'cpu'
                sprintf("<span class='duration'>% 8.1fms + % 8.1fms</span> #{tmpl}", file_cpu / 1000.0, file_idle / 1000.0, file_name.sub(Rails.root.to_s + '/', ''))
              else
                sprintf("<span class='duration'>% 8.1fms</span> #{tmpl}", file_wall / 1000.0, file_name.sub(Rails.root.to_s + '/', ''))
              end

            output << "</div>" # .heading

            next unless show_src

            output << "<div class='data'>"
            code = []
            times = []
            File.readlines(file_name).each_with_index do |line, i|
              code << line
              wall, cpu, calls = lines[i + 1]

              if calls && calls > 0
                if mode == 'cpu'
                  idle = wall - cpu
                  times << sprintf("% 8.1fms + % 8.1fms (% 5d)", cpu / 1000.0, idle / 1000.0, calls)
                else
                  times << sprintf("% 8.1fms (% 5d)", wall / 1000.0, calls)
                end
              else
                times << ' '
              end
            end
            output << "<pre class='duration'>#{times.join("\n")}</pre>"
            # The following line was changed from
            # https://github.com/peek/peek-rblineprof/blob/8d3b7a283a27de2f40abda45974516693d882258/lib/peek/rblineprof/controller_helpers.rb#L125
            output << "<pre class='code highlight white'>#{pygmentize(file_name, code.join, 'ruby')}</pre>"
            output << "</div></div>" # .data then .peek-rblineprof-file
          end

          response.body += "<div class='peek-rblineprof-modal' id='line-profile'>#{output}</div>".html_safe
        end

        ret
      end
    end
  end
end