Class: ThemeProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify.rb

Instance Method Summary collapse

Constructor Details

#initializeThemeProfiler

Load all templates into memory, do this now so that we don’t profile IO.



22
23
24
25
26
27
28
29
30
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify.rb', line 22

def initialize
  @tests = Dir[File.dirname(__FILE__) + '/tests/**/*.liquid'].collect do |test|
    next if File.basename(test) == 'theme.liquid'
    
    theme_path = File.dirname(test) + '/theme.liquid'
    
    [File.read(test), (File.file?(theme_path) ? File.read(theme_path) : nil), test]
  end.compact
end

Instance Method Details

#compile_and_render(template, layout, assigns, page_template) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify.rb', line 58

def compile_and_render(template, layout, assigns, page_template)    
  tmpl = Liquid::Template.new
  tmpl.assigns['page_title'] = 'Page title'
  tmpl.assigns['template'] = page_template
  
  content_for_layout = tmpl.parse(template).render(assigns)
  
  if layout
    assigns['content_for_layout'] = content_for_layout      
    tmpl.parse(layout).render(assigns)
  else
    content_for_layout
  end    
end

#profileObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/liquid_cms/templates/vendor/plugins/liquid/performance/shopify.rb', line 33

def profile
  RubyProf.measure_mode = RubyProf::WALL_TIME
  
  # Dup assigns because will make some changes to them
  assigns = Database.tables.dup
  
  @tests.each do |liquid, layout, template_name|
    
    # Compute page_tempalte outside of profiler run, uninteresting to profiler
    html = nil
    page_template = File.basename(template_name, File.extname(template_name))
    
    # Profile compiling and rendering both
    RubyProf.resume { html = compile_and_render(liquid, layout, assigns, page_template) }
    
    # return the result and the MD5 of the content, this can be used to detect regressions between liquid version
    $stdout.puts "* rendered template %s, content: %s" % [template_name, Digest::MD5.hexdigest(html)]
    
    # Uncomment to dump html files to /tmp so that you can inspect for errors
    # File.open("/tmp/#{File.basename(template_name)}.html", "w+") { |fp| fp <<html}
  end

  RubyProf.stop    
end