File: memory_profile.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (63 lines) | stat: -rw-r--r-- 1,606 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require 'benchmark/ips'
require 'memory_profiler'
require 'terminal-table'
require_relative 'theme_runner'

class Profiler
  LOG_LABEL   = "Profiling: ".rjust(14).freeze
  REPORTS_DIR = File.expand_path('.memprof', __dir__).freeze

  def self.run
    puts
    yield new
  end

  def initialize
    @allocated = []
    @retained  = []
    @headings  = []
  end

  def profile(phase, &block)
    print(LOG_LABEL)
    print("#{phase}.. ".ljust(10))
    report = MemoryProfiler.report(&block)
    puts 'Done.'
    @headings  << phase.capitalize
    @allocated << "#{report.scale_bytes(report.total_allocated_memsize)} (#{report.total_allocated} objects)"
    @retained  << "#{report.scale_bytes(report.total_retained_memsize)} (#{report.total_retained} objects)"

    return if ENV['CI']

    require 'fileutils'
    report_file = File.join(REPORTS_DIR, "#{sanitize(phase)}.txt")
    FileUtils.mkdir_p(REPORTS_DIR)
    report.pretty_print(to_file: report_file, scale_bytes: true)
  end

  def tabulate
    table = Terminal::Table.new(headings: @headings.unshift('Phase')) do |t|
      t << @allocated.unshift('Total allocated')
      t << @retained.unshift('Total retained')
    end

    puts
    puts table
    puts "\nDetailed report(s) saved to #{REPORTS_DIR}/" unless ENV['CI']
  end

  def sanitize(string)
    string.downcase.gsub(/[\W]/, '-').squeeze('-')
  end
end

Liquid::Template.error_mode = ARGV.first.to_sym if ARGV.first

runner = ThemeRunner.new
Profiler.run do |x|
  x.profile('parse') { runner.compile }
  x.profile('render') { runner.render }
  x.tabulate
end