如何配置使用rack-mini-profiler返回json响应的rails控制器?

Ben*_*ier 14 profiling ruby-on-rails ruby-on-rails-3 rack-mini-profiler

我在rails 3.2项目中使用rack-mini-profiler.

在gemfile中:

gem 'rack-mini-profiler'
Run Code Online (Sandbox Code Playgroud)

一切都很好.但我的应用程序主要是一组json端点.因此,虽然能够检查html页面的性能非常有用,但我还希望能够看到返回json的控制器的性能.

我的控制器示例:

class UsersController < BaseController
  def json_method
    # you don't see the mini profiler ui for this controller
    render json: { users: [:foo, :bar]}
  end
end
Run Code Online (Sandbox Code Playgroud)

如果我去localhost:3000/users/json_method,我看到我的json响应,但不是profiler ui.

hla*_*les 17

在开发过程中,默认情况下,rack-mini-profiler gem会收集以前的JSON调用,并将其显示在可从HTML页面访问的菜单中.无需更改代码.

因此,发出您的JSON请求,然后点击任何其他HTML页面,它将在列表中可用.我们用这个效果很好.如果您的Rails应用程序仅是JSON API服务,请确保您的公用文件夹中至少有一个404.html,然后点击:

http://localhost/404.html
Run Code Online (Sandbox Code Playgroud)

  • 打开两个选项卡:一个用于 JSON,另一个用于任何*不是* JSON 的页面。进行 JSON 调用,然后刷新非 JSON 页面,瞧,JSON 调用将位于顶部附近。这不是最有效的方法,但它绝对有效! (2认同)

San*_*ati 9

在第二个选项卡中,您可以访问此URL,/rack-mini-profiler/requests您可以在其中看到最后的请求日志


esh*_*iju 5

如果rack mini profiler无法显示结果,它将收集结果,直到可以在下一个HTML页面上显示为止。因此,我正在使用的解决方案是:

发出JSON请求,然后点击我选择的HTML页面。结果将与最新的HTML配置文件一起显示。

http://tech.eshaiju.in/blog/2016/02/25/profile-api-endpoints-using-rack-mini-profiler/


Ben*_*ier 1

作为第一个解决方案,您可以将格式设置为 html,并在 html 页面内渲染:

控制器:

class UsersController < BaseController
  def json_method
    @users_json { users: [:foo, :bar]}
    render 'index', formats: ['html']
  end
end
Run Code Online (Sandbox Code Playgroud)

在 app/views/users/index.html.erb 中:

Users:<br/>
<%= @json.inspect %>
Run Code Online (Sandbox Code Playgroud)

我不太关心我的 json 结果,现在我有了分析用户界面。

如果我拥有分析用户界面而无需更改控制器的解决方案会更好。