延迟发布请求RoR [英] slow post request RoR

查看:102
本文介绍了延迟发布请求RoR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在RoR应用程序中发布请求缓慢的问题. 该项目是一个分布式系统,客户端发送大量照片,服务器将照片保存在数据库中. 我使用MySql服务器5.1,客户端和服务器都在同一个本地网络中,并且它们是RoR中的程序员. 客户在一个请求中发送了10张照片.这10张照片以yaml格式以数组结构发送. 请求是:

I've a problem with a slow post request in a RoR app. the project is a distribuited system wher the client send a lot of photos and the server save those in DB. I use MySql server 5.1, both client and the server are in the same local network and they are programmer in RoR.... the client send 10 photo in a single request. the 10 photos are send in an array structure in yaml format. the request is:

res = Net::HTTP.post_form(uri, :mac => 'String', :value => 'Yaml_array_images_data', :num => 10)

,日志显示:

`Started POST "/events/save_photo" for 192.168.0.113 at 2012-03-30 09:45:10 +0200
  Processing by EventsController#save_photo as */*
  Parameters: {"data_type"=>"image", "value"=>"--- \n- !binary |\n  /9j/2wCEAAoH
BwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYI...", "created_at"=>"--- \n- 2012-03-30 09:26:29 +02:00 \n-....", "mac"=>"00606E91E5D2"}
(0.3ms)  SET PROFILING=1
  Device Load (1.2ms)  SELECT SQL_NO_CACHE `devices`.* FROM `devices` 
WHERE `devices`.`mac` = '00606E91E51E' LIMIT 1
  CACHE (0.0ms)  SELECT `devices`.* FROM `devices` WHERE `devices`.
`mac` = '00606E91E51E' LIMIT 1
   (0.3ms)  BEGIN
  SQL (2.9ms)  INSERT INTO `multimedia` (`created_at`, `data`, `eve
nt_id`, `updated_at`) VALUES ('2012-03-30 07:45:00', x'f22b19237b63f1c8c40da49ae
5eb68969dd3cf28193ba6a3704fe2f286494439eea4f9071bb6ef9de530ccaaab235a402e2d94aad
2f4f1b90df022c23078f543bc6ca1c6fe8b17af45eae865bb....)
(0.3ms)  SET PROFILING=1
  Device Load (1.2ms)  SELECT SQL_NO_CACHE `devices`.* FROM `devices` 
WHERE `devices`.`mac` = '00606E91E51E' LIMIT 1
  CACHE (0.0ms)  SELECT `devices`.* FROM `devices` WHERE `devices`.
`mac` = '00606E91E51E' LIMIT 1
   (0.3ms)  BEGIN
  SQL (2.9ms)  INSERT INTO `multimedia` (`created_at`, `data`, `eve
nt_id`, `updated_at`) VALUES ('2012-03-30 07:45:00', x'f22b19237b63f1c8c40da49ae
5eb68969dd3cf28193ba6a3704fe2f286494439eea4f9071bb6ef9de530ccaaab235a402e2d94aad
2f4f1b90df022c23078f543bc6ca1c6fe8b17af45eae865bb...)
(44.0ms)  COMMIT
   (0.3ms)  BEGIN
  SQL (0.8ms)  INSERT INTO `events` (`created_at`, `data_type`, `devic
e_id`, `element_id`, `multimedia_id`, `name`, `status`, `updated_at`, `value`) V
ALUES ('2012-03-30 07:45:00', 'image', 19, 413397, 1066303, 'photo', NULL, '2012
-03-30 07:45:11', '--- 0\n...\n')
   (38.2ms)  COMMIT
   (0.3ms)  BEGIN
  SQL (2.9ms)  INSERT INTO `multimedia` (`created_at`, `data`, `eve
nt_id`, `updated_at`) VALUES ('2012-03-30 07:45:01', x'f22b19237b63f1c8c40da49ae
5eb68969dd3cf28193ba6a3704fe2f28...)
....
for 10 times insert a record in multimedia and one in events table
...
(33.0ms)  COMMIT
   (0.3ms)  BEGIN
   (0.4ms)  SET PROFILING=1
   (1.3ms)  SELECT SQL_NO_CACHE 1 FROM `devices` WHERE (`devices`.`mac
` = BINARY '00606E91E51E' AND `devices`.`id` != 19) LIMIT 1
   (0.7ms)  UPDATE `devices` SET `elem_photo_id` = 413408, `updated
_at` = '2012-03-30 07:45:12' WHERE `devices`.`id` = 19
   (34.5ms)  COMMIT
Rendered events/save_photo.html.erb (0.2ms)
Completed 200 OK in 1957ms (Views: 10.2ms | ActiveRecord: 1466.6ms)`

我的第一个问题是总时间以及观看时间和活动记录时间之间的总和.

My first problem is the difference by total time and the sum between views time and activerecord time.

此后,此时间记录在服务器的日志中,因此这些就是请求的执行时间.如果我花时间在客户端中,从发送请求到接收响应,则是3.6秒!为什么?我认为在本地网络中传输200k不需要2秒!

After that this times are in the log of the server, so those are the execution time of the request. If I take the time, in the client, from send request to receive respond, it's 3,6 seconds! Why? I don't think that in local network are needed 2 second to transfer 200k!

推荐答案

客户端发送请求和服务器发送响应之间发生了很多事情

there's a lot of things happening between client sending request and server sending response

仅举几例:

  1. 客户端解析dns(通常是缓存的)
  2. 客户端启动http连接
  3. 客户实际上发布了数据
  4. 无论您在哪里的任何服务器都接受发布请求,并将其排队以由Rails Stack处理
  5. 请求会先经过您拥有的所有机架中间件,然后才真正触及控制器
  6. 您的请求由控制器处理并生成响应
  7. 然后,
  8. 响应被传输到客户端,浏览器对其进行解析并呈现
  1. client resolves dns(usually it's cached)
  2. client initiates http connection
  3. client actually posts the data
  4. whatever server you have there accepts post request and enqueues it to be processed by your rails stack
  5. request goes through all the rack middlewares you have before actually hitting your controller
  6. your request is processed by controller and response is generated
  7. response is then transferred to client and browser parses and renders it

您在服务器日志中看到的内容可能仅是第6点,其他所有内容都取决于您的应用设置和响应的属性(带有很多CSS,JavaScript,图像等的html)

what you see in your server logs is probably only point 6, everything else depends on your app setup and properties of your response(html with lots of css, javascripts, images, etc)

关于此行中的时差问题:

regarding your problem with time differences in this line:

Completed 200 OK in 1957ms (Views: 10.2ms | ActiveRecord: 1466.6ms)

(用括号括起来的)详细的时间消耗没有完全详细说明.它浪费了GC时间(如果您以yaml格式的参数发送文件,则可能会调用该时间),也浪费了之前的过滤器和操作本身所花费的时间.

detailed time consumption(in parenthesis) is not entirely detailed. it is missing GC time(which probably was invoked if you send files as yaml-formatted parameters), it is also missing time spent in your before filters and action itself.

这篇关于延迟发布请求RoR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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