Interesting!
There could be a number of reasons for this, so it's not a simple thing to test. Variables include stuff like
- default window sizes
- tls negotiation
- time it takes for a client to initialise
- connection pooling
- http2 vs http
If you want to do lots of requests, it's definitely a good idea to run the test multiple times in a loop. Or run them in concurrently
This is because the cost of setting up the connection (tls etc) is only a factor for the first connections in the pool.
I tried hitting pinterest in a loop, but they quickly thottled my IP, so that test didn't go so well. Here are my results for hitting www.google.com on https, in a loop.
Test Request Time Elapsed in Go: 110 Milliseconds
Test Request Time Elapsed in Go: 120 Milliseconds
Test Request Time Elapsed in Go: 56 Milliseconds
Test Request Time Elapsed in Go: 59 Milliseconds
Test Request Time Elapsed in Go: 56 Milliseconds
Test Request Time Elapsed in Go: 60 Milliseconds
Test Request Time Elapsed in Go: 55 Milliseconds
Test Request Time Elapsed in Go: 75 Milliseconds
Test Request Time Elapsed in Go: 64 Milliseconds
Test Request Time Elapsed in Go: 51 Milliseconds
And in Rust
Test Request Time Elapsed in Rust: 125 Milliseconds
Test Request Time Elapsed in Rust: 59 Milliseconds
Test Request Time Elapsed in Rust: 56 Milliseconds
Test Request Time Elapsed in Rust: 59 Milliseconds
Test Request Time Elapsed in Rust: 60 Milliseconds
Test Request Time Elapsed in Rust: 53 Milliseconds
Test Request Time Elapsed in Rust: 60 Milliseconds
Test Request Time Elapsed in Rust: 55 Milliseconds
Test Request Time Elapsed in Rust: 56 Milliseconds
Test Request Time Elapsed in Rust: 51 Milliseconds
These results are almost identical -- we're just measuring the network time, really.
func main() {
client := resty.New()
for i := 0; i < 10; i++ {
start := time.Now()
//resp, err := client.R().Get(`https://www.pinterest.com/pin/378724649895155348/`)
resp, err := client.R().Get(`https://www.google.com`)
if err == nil {
_ = resp.String()
} else {
fmt.Println(err)
}
duration := time.Since(start)
fmt.Println("Test Request Time Elapsed in Go: ", duration.Milliseconds(), "Milliseconds")
}
}
#[tokio::main]
async fn main() {
//let req_url = "https://www.pinterest.com/pin/378724649895155348/";
let req_url = "https://www.google.com";
let client = reqwest::Client::builder().build().unwrap();
for _ in 0..10 {
let now = Instant::now();
let result = client.get(req_url).send().await;
match result {
Ok(response) => {
let _res_str = response.text().await.unwrap();
}
Err(err) => {
println!("{:?}", err)
}
}
let elapsed = now.elapsed();
println!(
"Test Request Time Elapsed in Rust: {:?} Milliseconds",
elapsed.as_millis()
);
}
}
I'm using reqwest's async interface here, but blocking is the same in terms of latency. You'll want to use async if you're planning on doing a bunch of requests simultaneously, though.
I'm not sure this gives you a clear answer, but it looks like timings are equivalent when doing multiple requests and reusing the client. At least with www.google.com -- there might be some other stuff going on with pinterest. Maybe try hitting some other sites too 