English 中文(简体)
Rails3-为什么我的视图加载时间这么长?
原标题:Rails3 - why my view loading times are so long?

所以一周前,我开始将用Rails 2.3.5编写的旧应用程序迁移到新的3.0。不幸的是,我发现有些视图加载速度慢得惊人。

示例:

Rails 2.3.5 Ruby 1.8.7 -> WEBrick: Completed in 297ms (View: 143, DB: 40)
Rails 3.0.0 Ruby 1.8.7 -> WEBrick: Completed in 3081ms (View: 261, ActiveRecord: 108)
Rails 3.0.0 Ruby 1.8.7 -> WEBrick: Completed in 2495ms (View: 356, ActiveRecord: 76), slightly improved version -> less SQL queries, more counting in the view
Rails 3.0.0 Ruby 1.9.2 -> WEBrick: Completed in 2941ms (View: 423, ActiveRecord: 90), imp. version

当然,每次重新加载的时间都不同,但你仍然可以理解——在Rails3中,无论我使用Ruby 1.8.7还是1.9.2,这个视图的加载速度都慢10倍。我在开发和生产环境中以及在Heroku上检查了这一点——每次看起来都是一样的。其他视图加载时间在3.0中大约慢2倍。

在这里,您可以找到所有日志:

http://img811.imageshack.us/img811/8514/1rails235ruby187oldver.jpg

http://img217.imageshack.us/img217/5521/2rails300ruby187oldver.jpg

http://img7.imageshack.us/img7/3089/3rails300ruby187diffver.jpg

http://img534.imageshack.us/img534/5138/4rails300ruby192diffver.jpg

这里怎么了?我忙了一周,把我所有的控制器、视图、宝石、插件和配置都适应Rails3,现在我被卡住了。。。我觉得我真的应该学习一些PHP,因为它可以让我更好地控制实际发生的事情。

视图如下:

    <%= render :partial =>  menu  %>

<div id="preload">
    <%= image_tag("/images/next_big.png") %>
</div>  

    <div class="clearer"><hr /></div>

    <div class="picture">
        <%= link_to image_tag(@picture.img.url(:page), :alt => @picture.title.gsub("; "," "), :border => 0), :action => "fullsize", :link => @picture.link %>
    </div>

    <div class="right">
        <div class="title"><%= raw @picture.title.gsub("; ","<br />") %></div>
        <div class="tags">
            <% taglist = @picture.cached_tag_list.split(", ") %>
            <% x = taglist.length %>
            <% y = 0 %>
            <% x.times do %>
                <a href = "/tag/<%= taglist[y].gsub(" ", "_") %>"><%= taglist[y] %></a><% y += 1 %><% if y != x %>,<% end %>
            <% end %>           
        </div>
        <div class="datetime">Posted <%= time_ago_in_words(@picture.created_at) %> ago</div>
        <% this_voting = "voting" %>
        <div id="<%= this_voting %>" class="voting">
            <%= link_to image_tag("/images/yes.png", :alt => "Vote up", :class =>  vote_button ), {:action => :yes_vote, :id => @picture.id}, :remote => true %> 
            <span id="<%= this_voting +  _result  %>"><%= @picture.voting %></span> 
            <%= link_to image_tag("/images/no.png", :alt => "Vote against", :class =>  vote_button ), {:action => :no_vote, :id => @picture.id}, :remote => true %> 
            Pageviews: <%= @picture.views %>    
        </div>
        <div class="link">Link:<br /><input type="text" value="http://bzzzzzzzzzzz.com/<%= @picture.link %>" /></div>
        <div class="share">Share:<br />
            <a href="http://facebook.com/sharer.php?u=http://bzzzzzzzzzzz.com/<%= @picture.link %>" target="_blank">
                <img src="/images/mfb.png" alt="Facebook" onmouseover="puttext(fb, 0)" onmouseout="cleartext(0)" border="0" /></a>
            <a href="http://twitter.com/home?status=http://bzzzzzzzzzzz.com/<%= @picture.link %>" target="_blank">
                <img src="/images/mtwitter.png" alt="Twitter" onmouseover="puttext(twitter, 0)" onmouseout="cleartext(0)" border="0" /></a>
            <a href="http://digg.com/submit?url=http://bzzzzzzzzzzz.com/<%= @picture.link %>&amp;title=<%= @picture.title.gsub("; "," ") %>&amp;media=image&amp;topic=comedy" target="_blank">
                <img src="/images/mdigg.png" alt="Digg" onmouseover="puttext(digg, 0)" onmouseout="cleartext(0)" border="0" /></a>
            <a href="http://reddit.com/submit?url=http://bzzzzzzzzzzz.com/<%= @picture.link %>&amp;title=<%= @picture.title.gsub("; "," ") %>" target="_blank">
                <img src="/images/mreddit.png" alt="Reddit" onmouseover="puttext(reddit, 0)" onmouseout="cleartext(0)" border="0" /></a>
            <a href="http://stumbleupon.com/submit?url=http://bzzzzzzzzzzz.com/<%= @picture.link %>" target="_blank">
                <img src="/images/msu.png" alt="StumbleUpon" onmouseover="puttext(su, 0)" onmouseout="cleartext(0)" border="0" /></a>
        </div>
            <div id="share_caption0" class="share_caption"></div>
            <div class="small_clearer"></div>
        <% if @picture.last_comment_body %>
        <div class="links_to_c">
            <a href="#comments"><%= @picture.comments_count %> comment<% if @picture.comments_count > 1 %>s<% end %></a> 
            | <%= link_to "Write a comment", :anchor => "add_comment" %>
        </div>
        <% else %>
        <div class="links_to_c">
            <%= link_to "No comments, write a comment", :anchor => "add_comment" %>
        </div>
        <% end %>
        <% if @picture.fullsize == true %>
            <div class="fullsize">
                <%= link_to "Fullsize", :action => "fullsize", :link => @picture.link %>
            </div>
        <% end %>       
    </div>
    <div class="next">
    <% if @picture.id != 1 %>
        <% next_pict_link = Picture.find(@picture.id.to_i - 1).link %>
        <%= link_to image_tag("/images/next.png", :alt =>  Next picture , :border=>0, :onmouseover => "this.src= /images/next_big.png ", :onmouseout => "this.src= /images/next.png "), :action =>  show , :link => next_pict_link %>
    <% else %>
        <% last_pict_link = Picture.find(:last).link %>
        <%= link_to image_tag("/images/next.png", :alt =>  Latest picture , :border=>0, :onmouseover => "this.src= /images/next_big.png ", :onmouseout => "this.src= /images/next.png "), :action =>  show , :link => last_pict_link %>
    <% end %>
    </div>

    <div class="clearer" style="padding-bottom: 0px"><hr /></div>
    <h1><%= @thumbnails_text %></h1>
    <% @pictures.each do |pict| %>
        <%= link_to image_tag(pict.img.url(:thumb), :alt => "", :border => 1, :class =>  thumbnail ), :action => "show", :link => pict.link %>
    <% end %>   
    <div class="clearer" style="padding-top: 6px; padding-bottom: 15px"><hr /></div>
        <% if @picture.last_comment_body != nil %>
            <a name="comments"><h1>Comments:</h1></a>
            <% @picture.comments.each do |comment| %>
            <div id="<%= comment.id %>" class="comment">
                <font class="c_author"><%= comment.author %></font>
                <font class="c_datetime"><%= time_ago_in_words(comment.created_at) %> ago</font>
                <font class="c_id">#<%= comment.id %></font><br />
                <div class="c_body"><%= comment.body.gsub(/
/,  <br /> ) %></div>
             </div>
            <% end %>
        <% end %>       
    <div id="insert_here"></div>
    <a name="add_comment"></a>
    <%= form_for [@picture, Comment.new], :remote => true do |f| %>
    <div id="add_comment" <% if @picture.last_comment_body == nil %>style="margin-top: 15px"<% end %>>
        <div class="f_section">
            <div class="f_type" style="padding-right: 46px"><%= f.label :author, "Nick:" %></div>
            <div class="f_field"><%= f.text_field :author, :maxlength => 40 %></div>
        </div>
        <div class="f_section">
            <div class="f_type"><%= f.label :body, "Comment:" %></div>
            <div class="f_field"><%= f.text_area :body, :maxlength => 2000, :rows => 6 %></div>
        </div>
        <div class="f_section">
            <div class="f_type" style="padding-right: 20px"><%= f.label :captcha, "Captcha:" %></div>
            <div class="f_captcha"><%= raw recaptcha_tags %></div>
        </div>
        <div class="f_submit">
            <%= f.submit "Submit", :class =>  submit  %>
        </div>
    </div>
    <% end %>
<div class="clearer bottom_clearer fifty_from_top"><hr /></div>
问题回答

在任何环境中都有很多可能出错的地方,尤其是PHP。

这里的问题可能是将WEBrick与Rails3一起使用。使用混血儿或乘客这样的替代品可以帮助缩小这个问题。

您可能会遇到这样的情况:本应工作的东西超时了,例如,Memcache配置不正确,或者发出了CURL请求,但最终失败了。

有一个非常基本的视图,其中没有任何内容可以用来对应用程序的基线性能进行基准测试,这总是很方便的。一个简单的控制器,只需一个动作和一个空视图,就可以做到这一点。诊断问题的方法是将一些控制器代码添加到这个空操作中,直到您可以触发问题,或者从视图中剥离部分以查看是否是原因。

我通常会开始注释部分,看看其中是否有一个导致了拖动,然后从那里移回控制器,密切关注加载的内容。

所以…我想我已经找到了问题所在。我看到WEBrick给了我两个贬低警告(与RAILS_ROOT和RAILS_ENV有关)。我没有自己使用这些短语,所以我开始在我的插件和宝石中寻找它们。我发现回形针(2.3.4)和recatcha gem(ambethia 0.2.3的recatcha)正在使用它们。然后我从我的Gemfile中删除了回形针,并评论了我模型中所有与回形针相关的设置。加载时间降至400-500ms。

所以我想我需要等到有人修理回形针。。。我是一个红宝石新手,所以我自己无法做到这一点:/





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签