我在RoR应用中遇到缓慢的帖子请求问题。 该项目是一个分布式系统,客户端发送大量照片,服务器保存在数据库中。 我使用MySql服务器5.1,客户端和服务器都在同一个本地网络中,他们是RoR的程序员.... 客户端在一个请求中发送10张照片。 10张照片以yaml格式发送到阵列结构中。 请求是:
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)`
我的第一个问题是总时间与观看时间和主动记录时间之间的差异。
之后,这些时间都在服务器的日志中,因此这些是请求的执行时间。如果我花时间在客户端,从发送请求到接收响应,那就是3,6秒!为什么?我不认为在本地网络中需要2秒才能传输200k!
答案 0 :(得分:1)
客户端发送请求和服务器发送响应之间发生了很多事情
仅举几例:
你在服务器日志中看到的可能只是第6点,其他一切都取决于你的应用程序设置和你的响应属性(html包含大量的CSS,javascripts,图像等)
关于此行中时差的问题:
Completed 200 OK in 1957ms (Views: 10.2ms | ActiveRecord: 1466.6ms)
详细的时间消耗(括号内)并不完全详细。它缺少GC时间(如果您将文件作为yaml格式的参数发送,可能会调用它),它也会错过在过滤器和操作本身之前花费的时间。