1
0
mirror of https://github.com/jaygooby/ttfb.sh synced 2025-12-10 07:57:21 +01:00

asciicast and gif

This commit is contained in:
Jay Caines-Gooby
2019-04-11 23:06:24 +01:00
parent a2357a6035
commit b6bb67efbd
4 changed files with 18 additions and 190 deletions

21
LICENSE
View File

@@ -1,21 +0,0 @@
MIT License
Copyright (c) 2019 Jay Caines-Gooby
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

169
README.md
View File

@@ -1,169 +0,0 @@
# Introduction
Measures [time-to-first-byte](https://en.wikipedia.org/wiki/Time_to_first_byte) for single or multiple URLs. Can show you quickest, slowest & median TTFB values plus optionally log all response headers.
```
Usage: ttfb [options] url [url...]
-d debug
-l <log file> (infers -d) log response headers. Defaults to ./curl.log
-n <number> of times to test time to first byte
-v verbose output. Show response breakdown (DNS lookup, TLS handshake etc)
```
Implicitly follows a redirection chain using curl's `-L` option.
Can log all response headers (the default log file is `./curl.log`) by calling with `-d`.
Override the default log file by specifying `-l /some/file`.
Get quickest, slowest and median TTFB values by specifying the number of times to call a URL; use `-n2` for 2 tests, `-n5` for 5 and so on.
Uses the calculation `%{time_starttransfer¹} - %{time_appconnect²}` which doesn't include any connection overhead, to better approximate [devtools TTFB figure](https://developers.google.com/web/tools/chrome-devtools/network/understanding-resource-timing#slow_time_to_first_byte).
¹ [`time_starttransfer`](https://github.com/curl/curl/blob/e431daf013ea04cb1a988a2009d820224ef5fb79/docs/cmdline-opts/write-out.d#L141-L144)
> The time, in seconds, it took from the start until the first byte was just about to be transferred. This includes time_pretransfer and also the time the server needed to calculate the result.</blockquote>
² [`time_appconnect`](https://github.com/curl/curl/blob/e431daf013ea04cb1a988a2009d820224ef5fb79/docs/cmdline-opts/write-out.d#L118-L120)
>The time, in seconds, it took from the start until the SSL/SSH/etc
connect/handshake to the remote host was completed.
# Genesis
Based on a [gist](https://gist.github.com/sandeepraju/1f5fbdbdd89551ba7925abe2645f92b5)
by https://github.com/sandeepraju
Modified by jay@gooby.org, [@jaygooby](https://twitter.com/jaygooby)
# Usage
```
Usage: ttfb [options] url [url...]
-d debug
-l <log file> (infers -d) log response headers. Defaults to ./curl.log
-n <number> of times to test time to first byte
-v verbose output. Show response breakdown (DNS lookup, TLS handshake etc)
```
## Examples
Basic usage:
```
$ ttfb example.com
.227436
```
Basic usage with verbose response breakdown:
```
$ ttfb -v https://example.com
DNS lookup: 0.005152 TLS handshake: 0.000000 TTFB including connection: 0.200831 TTFB: .200831 Total time: 0.201132
```
Test multiple times:
```
$ ttfb -n 5 example.com/example/url
.....
fastest .177263 slowest .214302 median .179957
```
Test multiple URLs:
```
$ ttfb bbc.co.uk news.bbc.co.uk
bbc.co.uk .049985
news.bbc.co.uk .054122
```
Test multiple URLs, multiple times:
```
$ ttfb -n 5 bbc.co.uk news.bbc.co.uk
.....
.....
bbc.co.uk fastest .030936 slowest .057755 median .034663
news.bbc.co.uk fastest .031413 slowest .182791 median .035001
```
Verbose response breakdown when multiple tests specified:
```
$ ttfb -v -n 5 bbc.co.uk
DNS lookup: 0.005335 TLS handshake: 0.102314 TTFB including connection: 0.148328 TTFB: .046014 Total time: 0.646115
DNS lookup: 0.005322 TLS handshake: 0.102609 TTFB including connection: 0.150693 TTFB: .048084 Total time: 0.644611
DNS lookup: 0.004277 TLS handshake: 0.102066 TTFB including connection: 0.172199 TTFB: .070133 Total time: 1.196256
DNS lookup: 0.004444 TLS handshake: 0.107375 TTFB including connection: 0.160771 TTFB: .053396 Total time: 0.637290
DNS lookup: 0.005352 TLS handshake: 0.118882 TTFB including connection: 0.168772 TTFB: .049890 Total time: 0.653761
fastest .046014 slowest .070133 median .049890
```
Log all the response headers for multiple tests to multiple URLs:
```
ttfb -d -n 2 bbc.co.uk https://www.bbc.co.uk/weather
..
..
bbc.co.uk fastest .027550 slowest .055215 median .041382
https://www.bbc.co.uk/weather fastest .101020 slowest .297923 median .199471
$ ls *.log
bbc_co_uk-curl.log https___www_bbc_co_uk_weather-curl.log
$ cat https___www_bbc_co_uk_weather-curl.log
HTTP/2 200
server: openresty
x-cache-action: MISS
vary: Accept-Encoding,X-BBC-Edge-Cache,X-BBC-Edge-Scheme,X-CDN
x-cache-age: 0
cache-control: private, stale-while-revalidate=10, max-age=0, must-revalidate
content-type: text/html;charset=utf-8
x-mrid: w1
date: Thu, 11 Apr 2019 17:08:07 GMT
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-lb-nocache: true
x-msig: 24e37f81323984e4e45b8048f9e3c94a
x-frame-options: SAMEORIGIN
content-length: 1077454
HTTP/2 200
server: openresty
x-cache-action: MISS
vary: Accept-Encoding,X-BBC-Edge-Cache,X-BBC-Edge-Scheme,X-CDN
x-cache-age: 0
cache-control: private, stale-while-revalidate=10, max-age=0, must-revalidate
content-type: text/html;charset=utf-8
x-mrid: w1
date: Thu, 11 Apr 2019 17:08:08 GMT
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-lb-nocache: true
x-msig: 24e37f81323984e4e45b8048f9e3c94a
x-frame-options: SAMEORIGIN
content-length: 1077454
```
# More detail on time-to-first-byte
See https://blog.cloudflare.com/a-question-of-timing/
and https://curl.haxx.se/docs/manpage.html for an explanation
of how the curl variables relate to the various stages of
the transfer.
![Diagram showing what each of the curl variable timings refer to against a typical HTTP over TLS 1.2 connection](https://blog.cloudflare.com/content/images/2018/10/Screen-Shot-2018-10-16-at-14.51.29-1.png)
To get a better approximation of devtool's TTFB, we consider
the time without the connection overhead:
`%{time_starttransfer} - %{time_appconnect}`
Uses a dirty `eval` to do the ttfb arithmetic. Depends
on `bc` and `column` commands.
# TODO
* [x] Show progress when more than one request (`-n 2` etc) option is set
* [ ] Sort output by fastest TTFB when multiple URLs are supplied
* [ ] Colour code the `TTFB:` figure in the standard response, according to the speed of the response.

18
demo.cast Normal file
View File

@@ -0,0 +1,18 @@
{"version": 2, "width": 204, "height": 56, "timestamp": 1555020136, "env": {"SHELL": "/bin/bash", "TERM": "xterm-256color"}}
[0.706225, "o", "$ "]
[1.48203, "o", "ttfb -n"]
[1.482541, "o", "3 news.bbc.co.uk https://en.w"]
[1.482668, "o", "ikipedia"]
[1.482914, "o", ".org/wiki/Time_to"]
[1.48303, "o", "_first_by"]
[1.483113, "o", "te"]
[3.722443, "o", "\r\n"]
[4.641905, "o", "."]
[5.978201, "o", "."]
[6.739551, "o", "."]
[6.739693, "o", "\r\n"]
[7.040632, "o", "."]
[7.338501, "o", "."]
[7.623244, "o", ".\r\n"]
[7.631003, "o", "news.bbc.co.uk \u001b[32mfastest \u001b[39m.039445 \u001b[91mslowest \u001b[39m.653135 \u001b[95mmedian \u001b[39m.040998\u001b[39m\r\nhttps://en.wikipedia.org/wiki/Time_to_first_byte \u001b[32mfastest \u001b[39m.049489 \u001b[91mslowest \u001b[39m.050681 \u001b[95mmedian \u001b[39m.049572\u001b[39m\r\n"]
[7.706225, "o", "$ "]

BIN
demo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB