Use rspec with capybara-webkit or selenium to measure page load times - ruby

I'm hoping to find a way to write rspec scripts (or something else) that can be used to measure page load times. So similar to how firebug can tell when a page has fully loaded, is there a similar way to do that in rspec?
Or even any other ruby gem?
Thanks!

You can use benchmark in your views:
<% benchmark("Showing projects partial") do %>
<%= yield %>
<% end %>
If you do this in your application layout you'll have a log with page loads:
Showing projects partial (0.0ms)

Related

Rails 4: why is one way of rendering partials so much faster?

I'm not sure if this is a Rails specific issue, hence the reason I tagged it ruby as well.
I'm rendering a collection of event records via a partial. However, I found that rendering the partial differently results in drastic performance differences.
Both version use the exact same data, the only thing changing is the code used to render the partials.
Why the heck is one version consistently 4x faster than the other? Makes me wonder what other performance hits I'm taking...
Slow version (950ms total request time):
<% events.each do |event| %>
<%= render partial: "events/event", locals: { event: event } %>
<% end %>
# Log output
Rendered events/_event.html.erb (1.1ms)
Rendered events/_event.html.erb (1.1ms)
...
Faster version (250ms total request time):
<%= render partial: "events/event", collection: events, as: :event %>
# Log output
Rendered events/_event.html.erb (58.7ms)
example 1:
you are rendering a partial x times (depending on events).
which means you are compiling html x times(once each time the loop runs). which is slow
example 2:
you are rendering one partial with a collection of events
the html is compiled once(as there is only one partial). which is fast

Is there a good rails gem to output the view equivalent of rake routes?

What I'm looking for is a gem that could output a complete webpage version of each view a rails app has.
I realize I might not have correctly worded this. What I mean is I would like to see each route that has html output, but what the view actually looks like, not just a line saying what the view is.
In development (rails 4.x) you can simply go to any non-existent page and it will return all of the routes.
Example Screenshot: https://dl.dropboxusercontent.com/u/23115266/Screen%20Shot%202014-02-27%20at%209.21.03%20AM.png
The sextant gem allows you to see all your application's views in development by navigating to the /rails/routes URL.
https://github.com/schneems/sextant
Please correct me if I'm wrong, but it looks like you want a listing of view templates. Not sure there is a gem that can do this, but you could easily do it yourself with the following:
module ShowViewsHelper
def all_views_from_path(path)
helpers = Array(path).flat_map do |_path|
extract = /^#{Regexp.quote(_path.to_s)}\/?(.*)_.erb$/
names = Dir["#{_path}/**/*.erb"].map { |file| file.sub(extract, '\1') }
names.sort!
end
helpers.uniq!
helpers
end
end
create a template somewhere with the following:
<ul>
<% all_views_from_path("app/views").each do |v| %>
<li><%= v %></li>
<% end %>
</ul>

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

rendering Ruby statements inside of rails views?

So in my DB I want stored a ruby/HTML statement in the Database Table such as -
p This site is owned from 2000 - #{Time.now.year} by Acme Widget Co. /p
Or let's say that I want people to be able to include in other code snippits - e.g.
A client wants to run in a show view in the middle of a paragraph some magical partial such as #{render 'my_magical_code'}
On the view I have this being rendered as <%= raw(#page.content) %>
But its like a double rendering and rails will just put on the page #{render 'my_magical_code'} or #{Time.now.year}
So can / how do I solve this?
Thanks!
There are work arounds but I think the most proper way to solve your problem is to store your information in a better way. Add a migration to your model with a year_founded and company rows. You can then call these in your views rather than storing the HTML. Eg. This site is owned from <%= #object.year_founded %> to <%= Time.now.year %> by <%= #object.company %>

Remote Method Understanding?

In rails 3
While using remote method form submit, it affects the database twice with the same form values.
controller file:
#user_message = UserMessage.new(params[:user_message])
if #user_message.save
render :update
else
render :nothing => true
end
view file:
<%= form_tag ('/feedback/user_message'), :method =>'post', :remote=> true, :id=>'user_message' do%>
<%= hidden_field_tag 'user_message[user_id]', #user.user_id %>
<h2><%= #question %></h2>
<%= text_area_tag 'user_message[msg]',"", :size=>"40x5" %>
<%= submit_tag "Submit"%>
<% end %>
When I hit the submit button it creates two records on the table.
Why?
There may be couple of things that might be causing this
may be the ujs file is included twice may may be like
//= require jquery_ujs // expected to load from the rails-jquery gem
//= require_tree . // if any file is present in assets directory hierarchy it will be loaded
Or just a bug taking your sleep away
https://github.com/rails/jquery-ujs/issues/208
check out more solutions
Jquery Rails 3... form submits twice... deletes twice... help
Rails 3.1 remote requests submitting twice
This may also be related to the understanding of asset pipeline when and how use pecompiled assets.
I always use this config in development mode
config.server_static_assets = false
this forces the app to call the assets from app assets
and use precompiled assets from public in production mode
The problem most likely related to asset pipeline
You should precompiled the asset pipeline before.
This will create two copy of rails.js, one in your assets and one in application.js
This is a bug or gotcha in rails 3
See here http://www.ruby.code-experiments.com/blog/2011/10/another-gotcha-with-the-rails-31-asset-pipeline-or-why-are-my-jquery-ujs-ajax-requests-triggered-twi.html
Hope this help

Resources