Rails花费了大量的时间来呈现页面

时间:2016-06-14 20:03:30

标签: ruby-on-rails performance

这更像是“我从哪里开始”的问题。这些观点似乎占用了大部分时间。我使用slim和coffeescript,但令人困惑的是为什么异步json请求,视图渲染仍然需要很长时间。

回复html(2205ms):

Started GET "/message_boards?pp=full-backtrace" for 10.0.2.2 at 2016-06-14 19:56:24 +0000
Processing by MessageBoardsController#index as HTML
  Parameters: {"pp"=>"full-backtrace"}
  User Load (2.7ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 5  ORDER BY `users`.`id` ASC LIMIT 1
  Role Load (4.0ms)  SELECT  `roles`.* FROM `roles` WHERE `roles`.`id` = 5 LIMIT 1
  MessageBoard::Message Load (2.3ms)  SELECT  `message_board_messages`.* FROM `message_board_messages`  ORDER BY `message_board_messages`.`created_at` DESC LIMIT 20 OFFSET 0
   (3.6ms)  SELECT COUNT(*) FROM `message_board_messages`
  Rendered message_boards/index.html.slim within layouts/application (0.9ms)
  Exchange Load (3.4ms)  SELECT  `exchanges`.* FROM `exchanges`  ORDER BY `exchanges`.`id` ASC LIMIT 1
  CACHE (0.3ms)  SELECT  `exchanges`.* FROM `exchanges`  ORDER BY `exchanges`.`id` ASC LIMIT 1
  Rendered layouts/_menu.html.slim (322.1ms)
Completed 200 OK in 2205ms (Views: 2026.9ms | ActiveRecord: 16.2ms) 

响应异步json请求(350ms)

Started GET "/message_boards.json?page=2" for 10.0.2.2 at 2016-06-14 19:56:58 +0000
Processing by MessageBoardsController#index as JSON
  Parameters: {"page"=>"2"}
  User Load (4.8ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 5  ORDER BY `users`.`id` ASC LIMIT 1
  Role Load (2.7ms)  SELECT  `roles`.* FROM `roles` WHERE `roles`.`id` = 5 LIMIT 1
  MessageBoard::Message Load (3.1ms)  SELECT  `message_board_messages`.* FROM `message_board_messages`  ORDER BY `message_board_messages`.`created_at` DESC LIMIT 20 OFFSET 20
   (2.9ms)  SELECT COUNT(*) FROM `message_board_messages`
Completed 200 OK in 358ms (Views: 140.5ms | ActiveRecord: 13.5ms)

控制器代码(非常简单):

class MessageBoardsController < ApplicationController
  def index
    authorize!(:follow, :message_board)
    @events = MessageBoard::Message.order(created_at: :desc).page(params[:page]).per(20)
    gon.events = @events.as_json
    gon.total_items = @events.total_count

    respond_to do |format|
      format.html
      format.json { render json: {events: @events, total_items: @events.total_count} }
    end
  end
end

查看:

div ng-controller='MessageBoardController' ng-init='init()'
  .row
    .search-message-board
      h3.small-16.columns.medium-6.columns.left Showing {{ filteredEvents.length }} of {{ totalItems }} Results
      div.small-16.medium-6.columns.right
        form.search-form
          input.radius.search-text maxlength="120" type="text" ng-model='search' placeholder='Search'
          span.searchclear.fi-x-circle ng-click="search = ''"
  #messages
    .message-panel.radius ng-repeat='event in filteredEvents = (events | filter: search)'
      p
        strong ID:
        |  {{ event.id }}

      p
        strong Event:
        |  {{ event.title }}
      p
        strong Message:
        |  {{ event.body }}
      p
        strong Timestamp:
        |  {{ event.created_at }}

  pagination page="currentPage" total-items="totalItems" items-per-page="itemsPerPage"

这是指向我development.rb的链接 - 是assets.debug设置为false

0 个答案:

没有答案