我在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
仅举几例:
您在服务器日志中看到的内容可能仅是第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
发布评论