Skip to content
Open
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"
)
48 changes: 45 additions & 3 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 @@ -520,6 +524,10 @@ func (s *Server) Upload(noPrealloc, silent, useBytes, useMebi bool, requests int
uploadDone := make(chan struct{}, 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() {
defer wg.Done()
Expand Down Expand Up @@ -556,12 +564,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 <- struct{}{}:
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 Down Expand Up @@ -634,9 +662,23 @@ Loop:
// 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. Any other failure with no answer at all is a broken path.
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()
}

// 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
69 changes: 69 additions & 0 deletions defs/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,72 @@ func TestUploadSendsContentLength(t *testing.T) {
t.Errorf("counter reported %d bytes, want more than one payload", total)
}
}

// flakyUploadServer drops the connection of the first `failures` POSTs
// without answering, then answers every POST. It returns the server and the number
// of body bytes it read from the answered POSTs.
func flakyUploadServer(t *testing.T, failures int64) (*Server, *atomic.Int64) {
t.Helper()
var seen, answered atomic.Int64
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if seen.Add(1) <= failures {
conn, _, err := http.NewResponseController(w).Hijack()
if err == nil {
conn.Close()
}
return
}
n, _ := io.Copy(io.Discard, r.Body)
answered.Add(n)
}))
t.Cleanup(ts.Close)
return &Server{Server: ts.URL, UploadURL: "/"}, &answered
}

// A request that fails mid-test is retried instead of dropped: the body
// cannot be replayed, so the transport does not retry it, and with a single
// stream the run would otherwise upload nothing and report 0 Mbps.
func TestUploadRetriesAFailedRequest(t *testing.T) {
s, answered := flakyUploadServer(t, 1)
_, total, err := s.Upload(false, true, false, false, 1, 64, time.Second)
if err != nil {
t.Fatalf("Upload returned error: %v", err)
}
if total == 0 || answered.Load() == 0 {
t.Fatalf("uploaded %d bytes, server read %d: the failed request was not retried", total, answered.Load())
}
}

// When every request fails there is no measurement, and Upload says so
// instead of returning 0 Mbps as a result.
func TestUploadFailsWhenNothingIsUploaded(t *testing.T) {
s, _ := flakyUploadServer(t, 1<<30)
if _, total, err := s.Upload(false, true, false, false, 1, 64, time.Second); err == nil {
t.Fatalf("Upload of %d bytes returned no error while every request failed", total)
}
}

// TestUploadCountsRequestsThatTimeOut: with --no-pre-allocate the body never
// ends, so every request ends at the client timeout. The server still got
// what was sent, and the result must count it.
func TestUploadCountsRequestsThatTimeOut(t *testing.T) {
var received atomic.Int64
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
n, _ := io.Copy(io.Discard, r.Body)
received.Add(n)
}))
t.Cleanup(ts.Close)
defer func(old time.Duration) { http.DefaultClient.Timeout = old }(http.DefaultClient.Timeout)
http.DefaultClient.Timeout = 300 * time.Millisecond

s := &Server{Server: ts.URL, UploadURL: "/"}
_, total, err := s.Upload(true, true, false, false, 1, 64, 1500*time.Millisecond)
if err != nil {
t.Fatalf("Upload returned error: %v", err)
}
// let the server finish counting the last request
time.Sleep(100 * time.Millisecond)
if got := received.Load(); total < uint64(got)/2 {
t.Fatalf("reported %d bytes while the server received %d", total, got)
}
}
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/briandowns/spinner v1.23.2
github.com/gocarina/gocsv v0.0.0-20240520201108-78e41c74b4b1
github.com/prometheus-community/pro-bing v0.9.1
github.com/quic-go/quic-go v0.65.0
github.com/urfave/cli/v2 v2.27.7
golang.org/x/sys v0.50.0
)
Expand All @@ -16,9 +17,12 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/net v0.57.0 // indirect
golang.org/x/sync v0.21.0 // indirect
golang.org/x/term v0.45.0 // indirect
golang.org/x/crypto v0.59.0 // indirect
golang.org/x/net v0.62.0 // indirect
golang.org/x/sync v0.25.0 // indirect
golang.org/x/term v0.48.0 // indirect
golang.org/x/text v0.44.0 // indirect
)
28 changes: 22 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,35 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/prometheus-community/pro-bing v0.9.1 h1:kpuAr6AU2oRtzGihJSUcetHtjc7ku7h6PxeuW9RVrQw=
github.com/prometheus-community/pro-bing v0.9.1/go.mod h1:z79wYTxAOf6FpTng0QdIhZ3j/Nd5l3+gnFSCIT+SiSQ=
github.com/quic-go/go-ossfuzz-seeds v0.1.0 h1:APacT+iIaNF6fd8AGEiN3bT/Jtkd2jz4v4TzM7MFjy0=
github.com/quic-go/go-ossfuzz-seeds v0.1.0/go.mod h1:3IOHRbJIc+L6YKMwfDtJAM9Vj9k0YY4muhuyUYk5tbk=
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
github.com/quic-go/quic-go v0.65.0 h1:BJQSjdTvLPLmWtaCupUphRbgxgpZhdO/GMpr9Eoe3B4=
github.com/quic-go/quic-go v0.65.0/go.mod h1:RAro2j2yN9a9EiPACLHT9IB2NXCvGQmmo/alT0yYI0w=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/urfave/cli/v2 v2.27.7 h1:bH59vdhbjLv3LAvIu6gd0usJHgoTTPhCFib8qqOwXYU=
github.com/urfave/cli/v2 v2.27.7/go.mod h1:CyNAG/xg+iAOg0N4MPGZqVmv2rCoP267496AOXUZjA4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU=
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
go.uber.org/mock v0.5.2 h1:LbtPTcP8A5k9WPXj54PPPbjcI4Y6lhyOZXn+VS7wNko=
go.uber.org/mock v0.5.2/go.mod h1:wLlUxC2vVTPTaE3UD51E0BGOAElKrILxhVSDYQLld5o=
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.59.0 h1:xS2jhqYbzNrpvkWP2YTDf4TJwBy3ykR03i8vFDxEc7c=
golang.org/x/crypto v0.59.0/go.mod h1:veDyUwATGs3c+vN3VfTbzViMGXeTBk3WyhBOkn+F/4U=
golang.org/x/net v0.62.0 h1:18ELalfM5+XNRwzM2nYqXsZQ9ByS1uhj2O/U7+5jqRY=
golang.org/x/net v0.62.0/go.mod h1:WgDCOTH8iwtB66T4/GfI94AHo/hhdb7Hah9NDseQfiI=
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0=
golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w=
golang.org/x/term v0.48.0 h1:G6UU1eiSB8nl3DzeUGt7Mg+2SctI8tPS6Z5/mBt6cOk=
golang.org/x/term v0.48.0/go.mod h1:BRDLHCZr2SUjIeBHHpRyQeC9xzSAVbzY2rvKgafnHf4=
golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
10 changes: 9 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,20 @@ func main() {
Usage: "firewall mark to set on socket.",
Value: 0,
},
&cli.StringFlag{
Name: defs.OptionHTTPVersion,
Usage: "HTTP `VERSION` for test servers: 1.1, 2, 3, or auto to\n" +
"\tswitch to HTTP/3 when advertised and fall back if it fails.\n" +
"\tHTTP/3 needs https (see --secure) and is not used through\n" +
"\tan HTTP(S)_PROXY",
Value: "auto",
},
},
}

// run main function with cli options
err := app.Run(os.Args)
if err != nil {
output.Fatalf("Terminated due to error: %s", err)
output.Fatalf("Terminated due to error: %s", output.Sanitize(err.Error()))
}
}
2 changes: 2 additions & 0 deletions report/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type JSONReport struct {
// it -- so two otherwise identical runs can differ several-fold for a
// reason the numbers alone do not show.
TLS *TLSReport `json:"tls,omitempty"`

Protocol string `json:"protocol,omitempty"` // HTTP/1.1, HTTP/2.0 or HTTP/3.0
}

// TLSReport names the negotiated TLS parameters a measurement ran over.
Expand Down
Loading