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)
如果rack mini profiler无法显示结果,它将收集结果,直到可以在下一个HTML页面上显示为止。因此,我正在使用的解决方案是:
发出JSON请求,然后点击我选择的HTML页面。结果将与最新的HTML配置文件一起显示。
http://tech.eshaiju.in/blog/2016/02/25/profile-api-endpoints-using-rack-mini-profiler/
作为第一个解决方案,您可以将格式设置为 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 结果,现在我有了分析用户界面。
如果我拥有分析用户界面而无需更改控制器的解决方案会更好。