Created
December 19, 2016 10:42
-
-
Save ang3lkar/b818f8a6a0ce1a44bc18ead9a618c02f to your computer and use it in GitHub Desktop.
Benchmark rendering of Rails view in rake file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :benchmarks do | |
desc 'With cache' | |
task cache: :environment do | |
class RakeActionView < ActionView::Base | |
include Rails.application.routes.url_helpers | |
include ::ApplicationHelper | |
def default_url_options | |
{ host: 'localhost:3000' } | |
end | |
def protect_against_forgery? | |
false | |
end | |
end | |
controller = ActionController::Base.new | |
controller.class_eval do | |
include Rails.application.routes.url_helpers | |
include ::ApplicationHelper | |
end | |
controller.request = ActionDispatch::TestRequest.new | |
view = RakeActionView.new(Rails.root.join('app', 'views'), {}, controller) | |
member = Member.find_by_name('Natalie Sung') | |
candidate = Candidate.find(777479348) | |
params = { | |
current_member: member, | |
candidate: candidate, | |
was_unread: candidate.unread_by?(member.id), | |
collaboration: Collaboration.where(job_id: candidate.job_id, member_id: member.id).first | |
} | |
view.assign(params) | |
Benchmark.bmbm() do |x| | |
x.report 'candidate_show' do | |
view.render(inline: "/#{Rails.root}/app/views/backend/api/candidates/show.json.jbuilder", layout: false) | |
end | |
x.report 'candidate_with_cache' do | |
view.render(inline: "/#{Rails.root}/app/views/backend/api/candidates/show.json.jbuilder", layout: false, use_cache: true) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment