我在rails 3.2项目中使用rack-mini-profiler。
在gemfile中:
gem 'rack-mini-profiler'
一切都很好。但我的应用程序主要是一组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
如果我转到localhost:3000/users/json_method
,我会看到我的json响应,但不会看到探查器ui。
答案 0 :(得分:16)
在开发过程中,默认情况下,rack-mini-profiler gem会收集以前的JSON调用,并将其显示在可从HTML页面访问的菜单中。无需更改代码。
所以,发出你的JSON请求,然后点击任何其他HTML页面,它将在列表中可用。我们用这个效果很好。如果您的Rails应用程序仅是JSON API服务,请确保至少在您的公用文件夹中有404.html,然后点击类似:
http://localhost/404.html
答案 1 :(得分:4)
如果rack mini profiler无法显示结果,它将收集它们直到它可以在下一个HTML页面上。所以,我使用的解决方案是:
发出JSON请求 然后点击我选择的HTML页面。 将显示结果以及最新的HTML配置文件。
http://tech.eshaiju.in/blog/2016/02/25/profile-api-endpoints-using-rack-mini-profiler/
答案 2 :(得分:1)
作为第一个解决方案,你可以将格式设置为html,并在html页面内渲染:
控制器:
class UsersController < BaseController
def json_method
@users_json { users: [:foo, :bar]}
render 'index', formats: ['html']
end
end
在app / views / users / index.html.erb中:
Users:<br/>
<%= @json.inspect %>
我不太关心我的json结果,现在我有了剖析ui。
我在不更改控制器的情况下使用剖析ui的解决方案会好得多。
答案 3 :(得分:1)
在第二个选项卡中,您可以访问此 URL /rack-mini-profiler/requests
您可以在其中查看最后一个请求日志
答案 4 :(得分:0)
请注意,在使用rails new api_name --api
初始化的新Rails API项目中,ApplicationController
继承自ActionController::API
,而不是ActionAcontroller::Base
。在这种情况下,显示HTML页面时可能不会加载mini-profiler。
我必须将基类更改为ActionController::Base
才能起作用。如果在您的应用程序中,您没有在HTML页面上看到从mini-profiler加载资源的请求,则您可能想尝试此更改。花了我很长时间才弄清楚。
还请注意,您确实需要在模板中至少包含<body>
标签才能呈现,否则将无法正确注入mini-profiler div。