POST特定JSON时缓慢的rails性能

时间:2014-07-03 13:32:30

标签: ruby json ruby-on-rails-4

我一直在测试我的应用程序的性能,发现从发布数据到执行第一行动方法需要1秒钟。我在空的rails 4 app(使用rails new app_name创建)和ruby 1.9.3-p448上测试了这个。我只添加了一个控制器:

class TestController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def testt
    render json: {new: true}
  end
end

和路线:

post "api/v1/tt" => "test#testt"

这是我发布的JSON:

{
  "params": {
    "updatedBy": "f092d32a-1e38-4f07-8b76-185393138d86",
    "data": [
      {
        "typeName": "test",
        "total": 995,
        "timeOffset": 13,
        "timestamp": 1404323549565,
        "hidden": false,
        "guid": "9fc91203-e558-43e1-b585-aefbd281c5f5",
        "modificationDate": 1404316375054,
        "deleted": false
      },
      {
        "typeName": "test",
        "total": 995,
        "timeOffset": 13,
        "timestamp": 1404323549565,
        "hidden": false,
        "guid": "9fc91203-e558-43e1-b585-aefbd281c5f5",
        "modificationDate": 1404316375054,
        "deleted": false
      },
      {
        "typeName": "test",
        "total": 995,
        "timeOffset": 13,
        "timestamp": 1404323549565,
        "hidden": false,
        "guid": "9fc91203-e558-43e1-b585-aefbd281c5f5",
        "modificationDate": 1404316375054,
        "deleted": false
      },
      {
        "typeName": "test",
        "total": 995,
        "timeOffset": 13,
        "timestamp": 1404323549565,
        "hidden": false,
        "guid": "9fc91203-e558-43e1-b585-aefbd281c5f5",
        "modificationDate": 1404316375054,
        "deleted": false
      },
      {
        "typeName": "test",
        "total": 995,
        "timeOffset": 13,
        "timestamp": 1404323549565,
        "hidden": false,
        "guid": "9fc91203-e558-43e1-b585-aefbd281c5f5",
        "modificationDate": 1404316375054,
        "deleted": false
      }
    ]
  }
}

使用此命令:

curl -H "Content-Type: application/json" -b cookies -c cookies --request POST "http://localhost/api/v1/tt" --data "@upload.json" -w "@timings-format.txt"

timings-format.txt包含:

    time_namelookup:  %{time_namelookup}\n
       time_connect:  %{time_connect}\n
    time_appconnect:  %{time_appconnect}\n
   time_pretransfer:  %{time_pretransfer}\n
      time_redirect:  %{time_redirect}\n
 time_starttransfer:  %{time_starttransfer}\n
                    ----------\n
         time_total:  %{time_total}\n

当我运行命令时,我会得到这样的结果:

{"new":true}
    time_namelookup:  0,001
       time_connect:  0,001
    time_appconnect:  0,000
   time_pretransfer:  0,001
      time_redirect:  0,000
 time_starttransfer:  1,003
                    ----------
         time_total:  1,010

如果我像这样缩小JSON:

{"params":{"updatedBy":"f092d32a-1e38-4f07-8b76-185393138d86","data":[{"typeName":"test","total":995,"timeOffset":13,"timestamp":1404323549565,"hidden":false,"guid":"9fc91203-e558-43e1-b585-aefbd281c5f5","modificationDate":1404316375054,"deleted":false},{"typeName":"test","total":995,"timeOffset":13,"timestamp":1404323549565,"hidden":false,"guid":"9fc91203-e558-43e1-b585-aefbd281c5f5","modificationDate":1404316375054,"deleted":false},{"typeName":"test","total":995,"timeOffset":13,"timestamp":1404323549565,"hidden":false,"guid":"9fc91203-e558-43e1-b585-aefbd281c5f5","modificationDate":1404316375054,"deleted":false},{"typeName":"test","total":995,"timeOffset":13,"timestamp":1404323549565,"hidden":false,"guid":"9fc91203-e558-43e1-b585-aefbd281c5f5","modificationDate":1404316375054,"deleted":false},{"typeName":"test","total":995,"timeOffset":13,"timestamp":1404323549565,"hidden":false,"guid":"9fc91203-e558-43e1-b585-aefbd281c5f5","modificationDate":1404316375054,"deleted":false}]}}

再次运行命令,我得到:

{"new":true}
    time_namelookup:  0,001
       time_connect:  0,002
    time_appconnect:  0,000
   time_pretransfer:  0,002
      time_redirect:  0,000
 time_starttransfer:  0,008
                    ----------
         time_total:  0,008

有没有人知道发生了什么?我也有更大的JSON,但它仍然需要1秒才能执行空操作...

2 个答案:

答案 0 :(得分:0)

你处于开发模式,事情本质上就慢了。你也可能使用Webrick作为开发服务器,它也很慢。使用nginx + unicorn或nginx + passenger检查生产模式下的实际生产部署。执行多个请求,因为它也需要预热。

我真的会抓住rvm并获得ruby-2.1.2,这是迄今为止最坚如磐石的MRI Ruby实现

答案 1 :(得分:0)

原来问题出在cURL中。在ruby中实现相同的东西(只是阅读文件和POST日期)后,我得到了更快的时间(cca 20-30ms)。 这是详细解释:

https://stackoverflow.com/a/17390776/579843