speedtest

package
v0.1.80-dev.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2026 License: AGPL-3.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package speedtest measures how fast a node reaches the internet, for the panel's "network capacity" suggestion (agent.proto "BANDWIDTH TEST"). It is an estimate from a few short tests against a public server that needs no account: three runs back to back, each with parallel downloads for a few seconds and then parallel uploads, the best run per direction wins; a hard cap on the bytes moved and a deadline on every phase. In the counted window of every run the node's main network interface is read too, so the traffic of the people already using the node counts as part of the capacity instead of being lost from it. The servers are tried in this order: the nearest Ookla (Speedtest.net) servers to the node (the public server list is geolocated by the node's address, the two with the lowest latency are used), then Cloudflare, OVH and CacheFly. The result says how many runs worked and why the others did not. It asks nothing of the host and keeps nothing.

Index

Constants

View Source
const (
	ReasonRateLimited = "rate_limited" // HTTP 429 that did not clear within the phase
	ReasonTimeout     = "timeout"      // no data before the deadline, or no time was left to start the run
	ReasonUnreachable = "unreachable"  // refused, reset, DNS or TLS failure, an empty answer
)

Reasons a run did not work (Result.Failures); the panel passes them on and the UI words them. http_<code> is any other status the server answered.

Variables

View Source
var ErrUnreachable = errors.New("no speed test server answered")

ErrUnreachable means no endpoint gave a usable answer (blocked, down, or the node has no route out).

Functions

This section is empty.

Types

type Config

type Config struct {
	// OoklaList is the URL of the public Speedtest.net server list (JSON, geolocated by the requester's address); "" = no
	// Ookla. The nearest servers by latency go before Endpoints; when the list or every server fails, Endpoints alone are used.
	OoklaList string
	Endpoints []Endpoint
	// Client overrides the HTTP client (tests). Nil = a client without proxy or HTTP/2: every stream is its own TCP connection.
	Client *http.Client
	// Runs of download + upload one after another (at least 1), Pause between them.
	Runs  int
	Pause time.Duration
	// DownStreams and UpStreams run in parallel.
	DownStreams, UpStreams int
	// DownFor and UpFor are the lengths of the two phases of a run; in-flight uploads may finish up to upGrace later.
	DownFor, UpFor time.Duration
	// Warm is the start of the download that does not count: TCP slow start. A run cut short (the cap) counts as a whole.
	Warm time.Duration
	// DownCap and UpCap are hard limits on the bytes moved in a phase of one run. Runs times both is the cap of a click.
	DownCap, UpCap int64
	// Overall ends everything, whatever the endpoints do. A run is not started when what is left would not fit it.
	Overall time.Duration
	// Counters is the host's interface counters; nil = the test alone is measured.
	Counters Counters
	// contains filtered or unexported fields
}

Config is the whole test. Default() is the production one; tests shrink it and point it at a local server.

func Default

func Default(counters Counters) Config

Default is the production test: three runs of 6 streams down for 5 s (the first second is not counted) and 4 up for 2 s, a second apart, at most 400 MB down and 100 MB up per run (1.5 GB in all), 45 s in all. The nearest Ookla servers come first (a server in the node's own country answers in milliseconds and is not throttled the way Cloudflare is in some). Then Cloudflare, which answers any size up to 50 MB per request (the largest it takes: it rate-limits an address with 429, so the runs are 3 s apart and a 429 is waited out) and takes uploads; the two last are plain files for a node that cannot reach the others. counters may be nil.

type Counters

type Counters func() (iface string, rx, tx uint64, ok bool)

Counters reads the byte counters of the node's main network interface: its name (a changed name between two reads means the route moved and the pair is useless), bytes received and bytes sent since boot. ok = false when it cannot.

type Endpoint

type Endpoint struct {
	// Name is what the result says, e.g. "speed.cloudflare.com" or "Ookla".
	Name string
	// Detail says which server of the provider, e.g. the sponsor and city of an Ookla server; "" = the name is enough.
	Detail string
	// DownURL answers a GET with a body to read; the stream reads it to the end and asks again until the time is up.
	DownURL string
	// UpURL takes a POST with a body of any size up to a few MB; "" = this server has no upload, the result says 0.
	UpURL string
	// Pause between runs on this server; 0 = Config.Pause. A server that rate-limits is given more room.
	Pause time.Duration
	// contains filtered or unexported fields
}

Endpoint is one public test server.

type Result

type Result struct {
	// Mbps are megabits per second (10^6 bit/s), the unit of node.bandwidth_mbps. Each is the better of what the test alone
	// moved and what the interface carried in the same window, so people's traffic is part of it. UpMbps is 0 when it
	// could not be measured.
	DownMbps, UpMbps float64
	// PeopleDownMbps and PeopleUpMbps are what the interface carried beyond the test in the best run of that direction
	// (traffic of people using the node), 0 when it was not more than the test's own headers, or there are no counters.
	PeopleDownMbps, PeopleUpMbps float64
	Server                       string
	// Detail is the sponsor and city of an Ookla server ("" for the others).
	Detail      string
	DownStreams int
	// Runs that gave a result, of RunsTotal asked for; Failures has one reason code (Reason*, http_<code>) per run that did not.
	Runs, RunsTotal int
	Failures        []string
	DownBytes       int64 // all runs together
	UpBytes         int64
	Seconds         float64
}

Result is what the panel gets: the best run per direction.

func Run

func Run(ctx context.Context, cfg Config) (Result, error)

Run measures Runs times with the first endpoint that works for the first run, and keeps the best run per direction. A failed upload does not fail a run (its up stays 0); a download that moved no byte moves the first run on to the next endpoint; a later run that fails, or does not fit in what is left of Overall, is skipped and named in Result.Failures. It fails only when no run worked.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL