speedtest

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package speedtest measures network throughput and latency between the caller and a gospeedtest server. The same package is used by the CLI and the embedded web client (the web client speaks the same HTTP protocol from JavaScript).

Protocol:

GET  /ping              -> 204 No Content, used for RTT samples.
GET  /download?bytes=N  -> N bytes of incompressible random data.
POST /upload            -> drains the body, replies with the byte count.

All three are plain HTTP so a stock load balancer or reverse proxy works without special handling.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	ServerURL    string        // base URL, e.g. "http://localhost:8080"
	HTTPClient   *http.Client  // optional; one is built if nil
	PingSamples  int           // default 10
	TestDuration time.Duration // per phase, default 10s
	Streams      int           // parallel HTTP streams per phase, default 4
	ChunkBytes   int64         // bytes per request in download/upload, default 25 MiB
	SampleEvery  time.Duration // how often to emit a Sample, default 100ms
}

Config controls a full test run. Zero values get sensible defaults.

type Phase

type Phase string

Phase identifies which part of the test produced a sample.

const (
	PhasePing     Phase = "ping"
	PhaseDownload Phase = "download"
	PhaseUpload   Phase = "upload"
	PhaseDone     Phase = "done"
)

type Result

type Result struct {
	PingMs       float64       `json:"ping_ms"`
	JitterMs     float64       `json:"jitter_ms"`
	DownloadMbps float64       `json:"download_mbps"`
	UploadMbps   float64       `json:"upload_mbps"`
	BytesDown    int64         `json:"bytes_down"`
	BytesUp      int64         `json:"bytes_up"`
	Duration     time.Duration `json:"duration"`
	StartedAt    time.Time     `json:"started_at"`
}

Result is the final summary of one test.

func Run

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

Run executes ping, download and upload sequentially against cfg.ServerURL, streaming samples to onSample (which may be nil). It returns the final Result or the first error encountered.

type Sample

type Sample struct {
	Phase   Phase
	Elapsed time.Duration // time since the phase began
	Bytes   int64         // cumulative bytes for download/upload
	Mbps    float64       // instantaneous bits/sec, megabits
	PingMs  float64       // most-recent RTT for the ping phase
}

Sample is a snapshot emitted while a phase is in progress.

Jump to

Keyboard shortcuts

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