如何使用rack-mini-profiler分析返回json响应的rails控制器? [英] How to profile a rails controller that returns a json response with rack-mini-profiler?

查看:49
本文介绍了如何使用rack-mini-profiler分析返回json响应的rails控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using rack-mini-profiler in my rails 3.2 project.

在 gemfile 中:

In gemfile:

gem 'rack-mini-profiler'

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

Everything works great. But my application is mostly a set of json endpoints. So while it is very useful to be to able to inspect the performance of html pages, I would like to also be able to see the performance of controllers that return 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.

If I go to localhost:3000/users/json_method, I see my json response but not the profiler ui.

推荐答案

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

In development, by default, the rack-mini-profiler gem collects the previous JSON call and presents it in the menu accessible from the HTML page. No code change required.

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

So, make your JSON request, then hit any other HTML page and it will be available in the list. We use this to great effect. If your Rails app is a JSON API service only, make sure you have a 404.html in your public folder at least, then hit something like:

http://localhost/404.html

这篇关于如何使用rack-mini-profiler分析返回json响应的rails控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆