benchmarks

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Configuration constants
	BenchmarkMaxTimeout    = 150 * time.Second // detect resource timeout
	BenchmarkMaxTries      = 3                 // maximum number of attempts
	BenchmarkDetectTimeout = 30 * time.Second  // for select fast mirror
)
View Source
const DefaultCacheTTL = 24 * time.Hour

DefaultCacheTTL is the default time-to-live for cached benchmark results.

View Source
const MaxBenchmarkConcurrency = 8

MaxBenchmarkConcurrency caps how many mirror benchmarks run in parallel. Mirror lists can have 50+ entries; spawning that many concurrent TCP connections wastes resources and can trip rate limits on shared CDNs.

Variables

This section is empty.

Functions

func Benchmark

func Benchmark(ctx context.Context, base, query string, times int) (time.Duration, error)

Benchmark is the package-level shim that delegates to the default engine.

func ClearBenchmarkCache added in v0.8.0

func ClearBenchmarkCache()

ClearBenchmarkCache clears the default engine's cached benchmark results. New, per-Server callers should use Engine.ClearCache instead.

func GetDefaultMirror added in v0.8.0

func GetDefaultMirror(mirrors []string) string

GetDefaultMirror returns the first mirror from the list as a fallback. This is used when async benchmark is in progress or when benchmark fails.

func GetTheFastestMirror

func GetTheFastestMirror(mirrors []string, testURL string) (string, error)

GetTheFastestMirror is the package-level shim that delegates to the default engine.

func GetTheFastestMirrorAsync added in v0.8.0

func GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, callback AsyncBenchmarkCallback)

GetTheFastestMirrorAsync is the package-level shim that delegates to the default engine.

func GetTheFastestMirrorWithCache added in v0.8.0

func GetTheFastestMirrorWithCache(distType int, mirrors []string, testURL string) (string, error)

GetTheFastestMirrorWithCache is the package-level shim that delegates to the default engine.

Types

type AsyncBenchmarkCallback added in v0.8.0

type AsyncBenchmarkCallback func(result AsyncBenchmarkResult)

AsyncBenchmarkCallback is called when an async benchmark completes.

type AsyncBenchmarkResult added in v0.8.0

type AsyncBenchmarkResult struct {
	DistType      int
	FastestMirror string
	Error         error
}

AsyncBenchmarkResult represents the result of an async benchmark operation.

type BenchmarkCache added in v0.8.0

type BenchmarkCache struct {
	// contains filtered or unexported fields
}

BenchmarkCache stores cached benchmark results to avoid repeated testing. Results are cached per distribution type with TTL-based expiration.

func GetBenchmarkCache added in v0.8.0

func GetBenchmarkCache() *BenchmarkCache

GetBenchmarkCache returns the default engine's benchmark cache. New, per-Server callers should use Engine.Cache instead.

func NewBenchmarkCache added in v0.12.0

func NewBenchmarkCache() *BenchmarkCache

NewBenchmarkCache returns a fresh, empty cache.

func (*BenchmarkCache) ClearCache added in v0.8.0

func (bc *BenchmarkCache) ClearCache()

ClearCache clears all cached benchmark results.

func (*BenchmarkCache) GetCachedResult added in v0.8.0

func (bc *BenchmarkCache) GetCachedResult(distType int) (string, bool)

GetCachedResult returns a cached benchmark result if available and not expired.

func (*BenchmarkCache) SetCachedResult added in v0.8.0

func (bc *BenchmarkCache) SetCachedResult(distType int, fastestMirror string, ttl time.Duration)

SetCachedResult stores a benchmark result in the cache.

type CachedResult added in v0.8.0

type CachedResult struct {
	FastestMirror string
	CachedAt      time.Time
	TTL           time.Duration
}

CachedResult represents a cached benchmark result with expiration.

func (CachedResult) IsExpired added in v0.8.0

func (c CachedResult) IsExpired() bool

IsExpired returns true if the cached result has expired.

type Engine added in v0.12.0

type Engine struct {
	// contains filtered or unexported fields
}

Engine encapsulates everything that used to live as package-level state: the result cache, the singleflight group that collapses concurrent cache-miss requests, and the HTTP client used to probe mirrors.

Each Server should construct its own Engine via NewEngine so a refresh on one Server does not flush another Server's mirror selection cache. The package-level helpers (GetTheFastestMirror, ClearBenchmarkCache, ...) are thin wrappers around a process-wide Default() Engine, kept for backward compatibility with existing tests and any single-Server callers.

func Default added in v0.12.0

func Default() *Engine

Default returns the process-wide engine used by the package-level helpers.

func NewEngine added in v0.12.0

func NewEngine() *Engine

NewEngine returns a fresh, independent Engine. Use one per Server.

func (*Engine) Benchmark added in v0.12.0

func (e *Engine) Benchmark(ctx context.Context, base, query string, times int) (time.Duration, error)

Benchmark performs HTTP GET requests against base+query using the engine's shared HTTP client and reports the average response time.

func (*Engine) Cache added in v0.12.0

func (e *Engine) Cache() *BenchmarkCache

Cache exposes the engine's result cache for advanced callers / tests.

func (*Engine) ClearCache added in v0.12.0

func (e *Engine) ClearCache()

ClearCache drops all cached benchmark results held by this engine.

func (*Engine) GetTheFastestMirror added in v0.12.0

func (e *Engine) GetTheFastestMirror(mirrors []string, testURL string) (string, error)

GetTheFastestMirror finds the fastest responding mirror from the provided list. Concurrency is capped at MaxBenchmarkConcurrency. Once `maxResults` valid results are collected, the parent context is cancelled so in-flight benchmarks abort promptly instead of running to completion.

func (*Engine) GetTheFastestMirrorAsync added in v0.12.0

func (e *Engine) GetTheFastestMirrorAsync(distType int, mirrors []string, testURL string, callback AsyncBenchmarkCallback)

GetTheFastestMirrorAsync runs benchmark in the background and calls the callback when complete. This allows the application to start immediately with a default mirror while benchmarking runs.

Concurrent async calls for the same distType are collapsed via the engine's singleflight group: only one goroutine probes mirrors, and every caller's callback is invoked with the same result. This matches the deduplication behaviour of GetTheFastestMirrorWithCache so a SIGHUP reload that fires sync + async benchmarks in quick succession does not double-probe upstream mirrors.

func (*Engine) GetTheFastestMirrorWithCache added in v0.12.0

func (e *Engine) GetTheFastestMirrorWithCache(distType int, mirrors []string, testURL string) (string, error)

GetTheFastestMirrorWithCache finds the fastest mirror, using cache if available. This is the preferred method for production use as it avoids repeated benchmarking. Concurrent cache-miss callers for the same distType are collapsed into a single execution via the engine's singleflight group.

type Result

type Result struct {
	URL      string
	Duration time.Duration
}

Result stores benchmark results for a URL

type Results

type Results []Result

Results implements sort.Interface for []Result based on Duration

func (Results) Len

func (r Results) Len() int

func (Results) Less

func (r Results) Less(i, j int) bool

func (Results) Swap

func (r Results) Swap(i, j int)

Jump to

Keyboard shortcuts

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