Measuring TTFB (Time to First Byte) for HTTP Requests in Rust

Published by Philipp Schuster on

The other day I created a small CLI + library written in Rust, that helps to measure the TTFB (time to first byte) for HTTP GET-Requests in Rust. I had this project on my to-do list for a few months now and finally some time for it. I got inspired by the Dev Tools in Google Chrome. You can see an example screenshot from Chrome down below:

Screenshot from Network Timings in Google Chrome Devtools

I wanted to program something similar. I ended up with ttfb (see github) and the code is quite easy. At first I just need to do some parsing of the input, i.e. if it is an IP or a domain and if the scheme is HTTP or HTTPS. I use the crate url for proper URL parsing. For DNS lookup, I use trust-dns-resolver, the TLS-Handshake is done by native-tls, and for TCP the regular standard library (std::net). I just need to glue all these parts together.

Obviously, DNS is done first (if a domain was provided). Afterwards I create the TCP stream. If HTTPS is required, the TLS handshake will happen next. Afterwards, the stream will be used to transfer the HTTP/1.1 GET-Header. The next received byte is the “first byte” and the time until then the TTFB. I implemented the HTTP/1.1-stuff by myself, because HTTP/1.1 is a simple plain text protocol. My crate doesn’t support HTTP/2 yet.

You can install the utility with $ cargo install ttfb and use it like $ ttfb https://phip1611.de.


Philipp Schuster

Hi, I'm Philipp and interested in Computer Science. I especially like low level development, making ugly things nice, and de-mystify "low level magic".

0 Comments

Leave a Reply

Your email address will not be published.