I can't figure out how to calculate the time it takes to send a request to a website. I'm trying to benchmark the request speeds. Do any of the crates offer a shortcut?
Are you trying to measure the response time for your server or for requests that you are sending as a client?
For example, before I start sending POST requests to an api and collect the response data, I want to log a test connection speed in ms
Here is one example on how you might do it:
use std::time::{Duration, Instant};
fn main() {
let start = Instant::now();
post_stuff();
let duration = start.elapsed();
println!("Time elapsed in post_stuff() is: {:?}", duration);
}
But I'd use tower for this. I think it would be a much nicer solution than a manual implementation.
But isn't this for the execution part only? Not the actual request.
rtime_rustlang = requests.get("https://api.rustlang.org/").elapsed.total_seconds()
This is how I implemented it in Python.
There's several relevant parts of a request lifecycle, it's not clear exactly what you're looking for.
There's the DNS lookup time, the TCP and TLS connection establishment, potentially http2 upgrade, then the actual request network time, then server overhead, including proxy overhead in many cases, then the actual server processing time, then response overhead and network time, then the transfer time for the body.
Many of these are cached/reused or can be inlined into previous steps, so they aren't generally separately displayed, the big relevant steps are:
- DNS
- Host connection
- Request / response
- Response body
A high level library like reqwest generally doesn't expose apis for most this, but if you're just wanting debugging, ClientBuilder:: connection_verbose might be what you're after, otherwise you can get the combined DNS -> response by timing the send Future, before reading the body with the Response methods like text().
If you want lower level details as an API, you'll probably need to build your own reqwest from a lower level library like hyper, but this is kind of a pain to do well.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.