Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rails render benchmark code + templates
# test/template/benchmark/render_erb_test.rb
# encoding: utf-8
require 'abstract_unit'
require 'controller/fake_models'
require 'benchmark/ips'
class TestController < ActionController::Base
end
class RenderErbBenchmarks < ActiveSupport::TestCase
def setup_view(paths)
controller = TestController.new
controller.params = Hash.new
@assigns = { :expected_time => 'not long' }
@view = ActionView::Base.new(paths, @assigns, controller)
@controller_view = controller.view_context
end
private :setup_view
def setup
view_paths = ActionController::Base.view_paths
assert_equal ActionView::OptimizedFileSystemResolver, view_paths.first.class
setup_view(view_paths)
end
def test_small_erb_template
Benchmark.ips do |b|
b.report('small erb template') do
@view.render(:template => "benchmark/erb/small.html.erb", :formats => [:html])
end
b.compare!
end
end
def test_small_erb_template_with_1_partial
Benchmark.ips do |b|
b.report('small erb template with 1 partial') do
@view.render(:template => "benchmark/erb/small_with_1_partial.html.erb", :formats => [:html])
end
b.compare!
end
end
def test_small_erb_template_with_2_partials
Benchmark.ips do |b|
b.report('small erb template with 2 partials') do
@view.render(:template => "benchmark/erb/small_with_2_partials.html.erb", :formats => [:html])
end
b.compare!
end
end
end
#require 'ruby-prof'
if defined?(RubyProf)
RubyProf.start
end
MiniTest.after_run do
if defined?(RubyProf)
result = RubyProf.stop
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT)
end
end
=================================
# test/fixtures/benchmark/erb/small.html.erb
<div class="small-template">
<p class="erb-bench-description">
I should take: <%= @expected_time %>
</p>
</div>
=================================
# test/fixtures/benchmark/small_with_1_partial.html.erb
<div class="small-template">
<%= render 'benchmark/erb/partials/small_with_1_partial' %>
</div>
# test/fixtures/benchmark/partials/_small_with_1_partial.html.erb
<p class="erb-bench-description">
I should take: <%= @expected_time %>
</p>
==================================
# test/fixtures/benchmark/small_with_2_partials.html.erb
<div class="small-template">
<%= render 'benchmark/erb/partials/small_with_2_partials_1' %>
<%= render 'benchmark/erb/partials/small_with_2_partials_2' %>
</div>
# test/fixtures/benchmark/partials/_small_with_2_partials_1.html.erb
<p class="erb-bench-description">
# test/fixtures/benchmark/partials/_small_with_2_partials_2.html.erb
I should take: <%= @expected_time %>
</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment