Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This is a command line interface for LibreSpeed speed test backends, written in
- Result sharing (telemetry) *[optional]*
- Test with multiple servers in a single run
- Use your own server list or telemetry endpoints
- HTTP/1.1, HTTP/2 and HTTP/3 (QUIC), selectable with `--http-version`
- Tested with PHP and Go backends

[![asciicast](https://asciinema.org/a/J17bUAilWI3qR12JyhfGvPwu2.svg)](https://asciinema.org/a/J17bUAilWI3qR12JyhfGvPwu2)
Expand Down Expand Up @@ -198,6 +199,10 @@ GLOBAL OPTIONS:
--ca-cert value Use the specified CA certificate PEM bundle file instead
of the system certificate trust store
--skip-cert-verify Skip verifying SSL certificate for HTTPS connections (self-signed certs) (default: false)
--http-version VERSION HTTP VERSION for test servers: 1.1, 2, 3, or auto to
switch to HTTP/3 when advertised and fall back if it fails.
HTTP/3 needs https (see --secure) and is not used through
an HTTP(S)_PROXY (default: "auto")
--no-pre-allocate Do not pre allocate upload data. Pre allocation is
enabled by default to improve upload performance. To
support systems with insufficient memory, use this
Expand Down
1 change: 1 addition & 0 deletions defs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ const (
OptionTelemetryShare = "telemetry-share"
OptionTelemetryExtra = "telemetry-extra"
OptionFwmark = "fwmark"
OptionHTTPVersion = "http-version"
)
128 changes: 106 additions & 22 deletions defs/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"slices"
"strconv"
"sync"
"sync/atomic"
"time"

"github.com/briandowns/spinner"
Expand All @@ -45,6 +46,7 @@ type Server struct {
// plain HTTP. Filled by CheckUp (and IsUp, which wraps it), which runs
// before every measurement.
NegotiatedTLS *TLSInfo `json:"-"`
Protocol string `json:"-"` // e.g. HTTP/3.0, filled by IsUp too
}

// TLSInfo names the negotiated TLS parameters of a connection.
Expand Down Expand Up @@ -90,6 +92,8 @@ func (s *Server) CheckUp() error {
}
defer resp.Body.Close()

s.Protocol = resp.Proto

// Report what the connection actually negotiated. On hardware without AES
// acceleration the cipher, not the link, is what bounds the result, and
// under TLS 1.3 the server picks it: the client offers a set and has no say
Expand Down Expand Up @@ -301,7 +305,7 @@ func (s *Server) PingAndJitter(count int) (float64, float64, error) {
}

for _, addr := range remotes {
output.WriteDebug("Pinging %s over TCP (%s)\n", addr, addressFamily(addr))
output.WriteDebug("Pinging %s over %s (%s)\n", addr, s.Protocol, addressFamily(addr))
}

// discard first result due to handshake overhead
Expand Down Expand Up @@ -398,24 +402,44 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu
req.Header.Set("User-Agent", UserAgent)
req.Header.Set("Accept-Encoding", "identity")

downloadDone := make(chan struct{}, requests)
// each finished request hands its worker back for a replacement
downloadDone := make(chan int, requests)

var wg sync.WaitGroup
// the last error of a failed request, returned when no request got an
// answer, so a broken path is not reported as a 0 Mbps result
var lastErr atomic.Pointer[error]
var succeeded atomic.Bool

// one read buffer per worker, reused by its replacement requests: only
// one request per worker runs at a time
bufs := make([][]byte, requests)
for i := range bufs {
bufs[i] = make([]byte, downloadReadSize)
}

doDownload := func() {
doDownload := func(worker int) {
defer wg.Done()

reqClone := req.Clone(ctx)
reqClone := req.Clone(WithWorker(ctx, worker))
resp, err := http.DefaultClient.Do(reqClone)
if err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when making HTTP request: %s\n", err)
}
if ctx.Err() == nil {
lastErr.Store(&err)
}
return
}
defer resp.Body.Close()
succeeded.Store(true)

if _, err = io.Copy(io.Discard, io.TeeReader(resp.Body, counter)); err != nil {
// io.Copy into io.Discard would read in 8 KiB chunks (its ReadFrom),
// and Go's HTTP/2 client sends a WINDOW_UPDATE frame, in its own
// write, after every read that frees 4 KiB. Hiding ReadFrom makes
// CopyBuffer read into the larger buffer instead.
if _, err = io.CopyBuffer(struct{ io.Writer }{io.Discard}, io.TeeReader(resp.Body, counter), bufs[worker]); err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when reading HTTP response: %s\n", err)
}
Expand All @@ -424,14 +448,14 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu
// let the main loop start a replacement request, but never block on it
// once the test is over, otherwise this goroutine is leaked
select {
case downloadDone <- struct{}{}:
case downloadDone <- worker:
case <-ctx.Done():
}
}

spawnDownload := func() {
spawnDownload := func(worker int) {
wg.Add(1)
go doDownload()
go doDownload(worker)
}

counter.Start()
Expand Down Expand Up @@ -461,7 +485,7 @@ func (s *Server) Download(silent bool, useBytes, useMebi bool, requests int, chu
}

for i := 0; i < requests; i++ {
spawnDownload()
spawnDownload(i)
time.Sleep(200 * time.Millisecond)
}

Expand All @@ -476,15 +500,24 @@ Loop:
case <-timeout:
cancel()
break Loop
case <-downloadDone:
spawnDownload()
case worker := <-downloadDone:
spawnDownload(worker)
}
}

// let the cancelled requests unwind before reading the counter, so the
// result doesn't change under us while it's being reported
wg.Wait()

// No request got a response: whatever went out is not a measurement of
// 0 Mbps, it is a broken path. A request that ran into the client
// timeout with a response in hand counts as a result.
if !succeeded.Load() {
if err := lastErr.Load(); err != nil {
return 0, 0, *err
}
return 0, 0, errors.New("no download request was answered before the test ended")
}
return counter.AvgMbps(), counter.Total(), nil
}

Expand Down Expand Up @@ -517,11 +550,16 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
defer cancel()
u.Path = path.Join(u.Path, s.UploadURL)

uploadDone := make(chan struct{}, requests)
// each finished request hands its worker back for a replacement
uploadDone := make(chan int, requests)

var wg sync.WaitGroup
// the last error of a failed request, returned when no request got an
// answer, so a broken path is not reported as a 0 Mbps result
var lastErr atomic.Pointer[error]
var answered atomic.Bool

doUpload := func() {
doUpload := func(worker int) {
defer wg.Done()

var bodyReader io.Reader
Expand All @@ -532,7 +570,7 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
}
countingReader := io.TeeReader(bodyReader, counter)

uploadReq, err := http.NewRequestWithContext(ctx, http.MethodPost, u.String(), countingReader)
uploadReq, err := http.NewRequestWithContext(WithWorker(ctx, worker), http.MethodPost, u.String(), countingReader)
if err != nil {
output.WriteDebug("Failed when creating HTTP request: %s\n", err)
return
Expand All @@ -556,12 +594,32 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int

resp, err := http.DefaultClient.Do(uploadReq)
if err != nil {
if !errors.Is(err, context.Canceled) && !errors.Is(err, context.DeadlineExceeded) {
output.WriteDebug("Failed when making HTTP request: %s\n", err)
if ctx.Err() != nil {
// cancellation is how the test ends, so it is not a failure
return
}
output.WriteDebug("Failed when making HTTP request: %s\n", err)
lastErr.Store(&err)
// The body cannot be replayed, so the transport does not retry
// it. Let the main loop start a new request instead, after a
// pause so a refused connection does not spin. In auto mode a
// failed HTTP/3 request has already moved the server to TCP, so
// the new request goes over TCP. The bytes this one sent stay
// counted: with --no-pre-allocate the body never ends, every
// request ends at --timeout, and the server got all of them.
select {
case <-time.After(uploadRetryDelay):
case <-ctx.Done():
return
}
select {
case uploadDone <- worker:
case <-ctx.Done():
}
return
}
defer resp.Body.Close()
answered.Store(true)

if _, err := io.Copy(io.Discard, resp.Body); err != nil {
// cancellation is how the test ends, so it is not a failure
Expand All @@ -573,14 +631,14 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
// let the main loop start a replacement request, but never block on it
// once the test is over, otherwise this goroutine is leaked
select {
case uploadDone <- struct{}{}:
case uploadDone <- worker:
case <-ctx.Done():
}
}

spawnUpload := func() {
spawnUpload := func(worker int) {
wg.Add(1)
go doUpload()
go doUpload(worker)
}

counter.Start()
Expand Down Expand Up @@ -610,7 +668,7 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
}

for i := 0; i < requests; i++ {
spawnUpload()
spawnUpload(i)
time.Sleep(200 * time.Millisecond)
}

Expand All @@ -625,18 +683,44 @@ Loop:
case <-timeout:
cancel()
break Loop
case <-uploadDone:
spawnUpload()
case worker := <-uploadDone:
spawnUpload(worker)
}
}

// let the cancelled requests unwind before reading the counter, so the
// result doesn't change under us while it's being reported
wg.Wait()

// A request that ran into the client timeout was still sending, as with
// --no-pre-allocate where the body never ends, so its bytes count as a
// result. Nothing answered and nothing was sent is not a result of 0 Mbps.
if !answered.Load() && counter.Total() == 0 {
if err := lastErr.Load(); err != nil {
return 0, 0, *err
}
return 0, 0, errors.New("no upload request was answered before the test ended")
}
if err := lastErr.Load(); err != nil && !answered.Load() && !isTimeout(*err) {
return 0, 0, *err
}
return counter.AvgMbps(), counter.Total(), nil
}

func isTimeout(err error) bool {
var netErr net.Error
return errors.As(err, &netErr) && netErr.Timeout()
}

// downloadReadSize is how much each download read asks for. On loopback,
// where the CPU is the limit, HTTP/2 downloads got faster up to 128-256 KiB
// and no faster beyond. It stays well below the 4 MiB stream window of Go's
// HTTP/2 client, so the server is never left waiting for window.
const downloadReadSize = 256 << 10

// uploadRetryDelay spaces the retries of failed upload requests.
const uploadRetryDelay = 200 * time.Millisecond

// GetIPInfo accesses the backend's getIP.php endpoint and get current client's IP information
func (s *Server) GetIPInfo(distanceUnit string) (*GetIPResult, error) {
t := time.Now()
Expand Down
Loading