Skip to content

Commit

Permalink
Sample latency three times
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonjchen committed Nov 15, 2015
1 parent 1c20d28 commit a1a3e10
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/speedtest.rs
Expand Up @@ -296,14 +296,21 @@ pub fn get_best_server_based_on_latency(servers: &[SpeedTestServer])
try!(path.parent().ok_or(Error::LatencyTestInvalidPath))
.display());
info!("Downloading: {:?}", latency_path);
let start_time = now();
let res = try!(client.get(&latency_path)
.header(Connection::close())
.header(UserAgent("hyper/speedtest-rust 0.01".to_owned()))
.send());
res.bytes().last();
let latency = now() - start_time;
info!("It took {} ms", latency.num_milliseconds());
let mut latency_measurements = vec![];
for _ in (0..3) {
let start_time = now();
let res = try!(client.get(&latency_path)
.header(Connection::close())
.header(UserAgent("hyper/speedtest-rust 0.01".to_owned()))
.send());
res.bytes().last();
let latency_measurement = now() - start_time;
info!("Sampled {} ms", latency_measurement.num_milliseconds());
latency_measurements.push(latency_measurement);
}
let latency = latency_measurements.iter().fold(Duration::zero(), |a, &i| a + i) /
latency_measurements.iter().count() as i32;
info!("Averaged to {} ms", latency.num_milliseconds());

if latency < fastest_latency {
fastest_server = Some(server);
Expand Down

0 comments on commit a1a3e10

Please sign in to comment.