benchmarks

package
v0.10.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 performs HTTP GET requests and measures response time. It uses a shared HTTP client with connection pooling for better performance.

func ClearBenchmarkCache added in v0.8.0

func ClearBenchmarkCache()

ClearBenchmarkCache clears all cached benchmark results. This is useful when forcing a re-benchmark, e.g., on SIGHUP reload.

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 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 GetTheFastestMirrorAsync added in v0.8.0

func 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. The callback will be called with the result when the benchmark completes.

func GetTheFastestMirrorWithCache added in v0.8.0

func 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.

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 global benchmark cache instance. This can be used for testing or advanced cache management.

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 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