Rails / Ruby - 如何检查用户下载和上传速度?

时间:2011-12-10 23:12:25

标签: ruby-on-rails ruby ruby-on-rails-3 performance

如何以MB / s检查用户上传和下载速度?

1 个答案:

答案 0 :(得分:6)

要下载时间,您可以在行动中执行以下操作:

def download
  start_time = Time.now
  file_size = File.size('never_gonna_give_you_up.mp3') / 1024.0 / 1024.0
  send_file("never_gonna_give_you_up.mp3")
  logger.info("Download took #{Time.now - start_time}s at #{file_size / (Time.now - start_time)} MB/s")
 end

对于用户上传的计时,您将无法检查用户在Ruby或Rails中严格上传内容所需的时间,因为在用户完成上载之前不会处理请求。您可以做的是在提交表单时使用javascript填充字段(例如upload_start_time),然后在操作开头减去时间,如下所示:

def upload
  upload_time = Time.parse(params[:upload_start_time]) - Time.now
  file_size = params[:file].size / 1024.0 / 1024.0 # assuming 'file' is the name of the field
  logger.info("Upload took #{upload_time} at #{file_size / upload_time} MB/s")
end

这不准确,但应该给你一个很好的近似值。