问题描述
如何以 MB/s 为单位检查用户的上传和下载速度?
How is it possible to check a users upload and download speed in MB/s ?
推荐答案
要定时下载,您可以在操作中执行以下操作:
To time a download you could do something like this in your action:
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),然后在操作开始时减去时间,如下所示:
For timing a user upload you wouldn't be able to check how long it took the user to upload something strictly in Ruby or Rails since the request is not processed until the user is finished uploading. What you could do is have javascript populate a field (e.g. upload_start_time) when the form is submitted and then subtract the time at the beginning of the action like this:
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
这不会很精确,但它应该给你一个很好的近似值.
This won't be precise but it should give you a good approximation.
这篇关于Rails/Ruby - 如何检查用户下载和上传速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!