如何使用NET :: HTTP获得下载速度

时间:2014-02-01 13:57:27

标签: ruby net-http

使用NET :: HTTP下载文件时,有没有办法获得当前的下载速度?

1 个答案:

答案 0 :(得分:0)

require 'open-uri'

time  = Time.now

stuff = open('http://www.giovannicapuano.net').read
File.write 'stuff', stuff

time_spent = Time.now - time
file_size  = File.size('stuff') / 1024.0 / 1024.0
speed      = file_size / time_spent

...或

require 'net/http'

time  = Time.now

uri = URI 'http://www.giovannicapuano.net'
stuff = Net::HTTP.get_response(uri).body
File.write 'stuff', stuff

time_spent = Time.now - time
file_size  = File.size('stuff') / 1024.0 / 1024.0
speed      = file_size / time_spent