2010-10-27 113 views
3

所以一个星期前,我开始将我用Rails 2.3.5编写的旧应用程序移动到新的3.0。不幸的是,我发现一些意见的加载速度非常慢。Rails3 - 为什么我的视图加载时间太长了?

例子:

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 

次,当然每一个重载不同,但还是可以得到的想法 - 在Rails 3中这种观点负荷慢10倍,不管我使用Ruby 1.8.7或1.9 0.2。我在开发和生产环境以及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(/\n/, '<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> 
+1

我们需要你的看法来说明你发生了什么 – shingara 2010-10-27 17:11:14

回答

1

有很多可以去错在任何环境中,特别是PHP。

可能是什么这里的问题是使用的WEBrick和Rails 3.使用像杂种或乘客的替代可以帮助缩小这一问题。

你可以有地方的东西,应该是工作已超时,例如,内存缓存配置不正确的情况下,或卷曲的请求正在取得最终失败。

它总是很方便的与什么也没有,你可以用它来测试过应用程序的基准性能非常基本看法。一个简单的控制器只有一个动作和一个空的视图可以在这里实现。诊断问题的方法是将一些控制器代码添加到此空行为中,直到触发问题,或从视图中删除部分内容以查看是否导致问题。

我通常开始注释掉的谐音,看看其中的一个原因造成的阻力,并从那里搬回到控制器上密切注意什么是越来越加载。

+0

我再次检查每一个视图,它似乎几乎所有的人都加载至少2比2.3.5慢了很多倍,所以现在我不认为这是这个问题。另一个奇怪的是:3081ms(查看:261,ActiveRecord:108)。分贝108,视图261以及其他2.7秒发生了什么? :)我已经将新版本的应用程序上传到Heroku,同时我浏览了他们两个,很明显,新的很慢很多,所以问题也不在于WEBrick我猜... – sNiCKY 2010-10-27 18:27:36

+0

这很奇特。如果你在3.0中打出一个空的Rails应用程序,它是否会给你带来同样的问题?我有一种感觉,它拖延了大约三秒钟。这可以解释为什么时间没有在视图或数据库的度量中考虑。我不认为这是一个3.0的事情。也许尝试碰到最新的3.0.1,可能会修复它。 – tadman 2010-10-27 21:57:57

+0

在Postgres上使用3.0时发生了一些奇怪的查询。我现在在3.0.2pre和MySQL上,看起来问题已经消失,但仍然是查询大约比2.3.5慢2倍。我正在等待AREL2。 – sNiCKY 2010-11-13 20:30:06

1

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

所以我想我需要等待,直到有人将修复回形针...我是一个红宝石新手,所以我不能自己做到这一点:/

+1

嘿sNICKY,我使用Rails 3.0的边回形针,我没有看到延迟......我会建议给边缘回形针尝试从git回购: – bouchard 2010-11-04 07:49:01