speed

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: LGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.0.1

type Config struct {
	Host    string
	Port    int
	GinMode string
}

func GetDefaultConfig added in v1.0.1

func GetDefaultConfig() *Config

GetDefaultConfig 获取默认配置,优先使用环境变量

type HistoryManager added in v1.0.2

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

HistoryManager 历史记录管理器

func NewHistoryManager added in v1.0.2

func NewHistoryManager(maxSize int) *HistoryManager

NewHistoryManager 创建历史记录管理器

func (*HistoryManager) AddOrUpdateRecord added in v1.0.2

func (h *HistoryManager) AddOrUpdateRecord(testID, clientIP string, result TestResult)

AddOrUpdateRecord 添加或更新记录

func (*HistoryManager) Clear added in v1.0.2

func (h *HistoryManager) Clear()

Clear 清空所有记录

func (*HistoryManager) GetRecords added in v1.0.2

func (h *HistoryManager) GetRecords() []*TestRecord

GetRecords 获取所有记录

type MemoryPool added in v1.0.2

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

MemoryPool 内存池,用于高效管理数据块的分配和回收

func NewMemoryPool added in v1.0.2

func NewMemoryPool(size int) *MemoryPool

NewMemoryPool 创建一个新的内存池

func (*MemoryPool) Get added in v1.0.2

func (mp *MemoryPool) Get() []byte

Get 从内存池获取一个数据块

func (*MemoryPool) Put added in v1.0.2

func (mp *MemoryPool) Put(data []byte)

Put 将数据块放回内存池

type ParallelTestMetrics added in v1.0.2

type ParallelTestMetrics struct {
	TestID     string
	TotalBytes int64
	StartTime  time.Time
	EndTime    time.Time
	Duration   time.Duration
	Throughput float64 // Mbps
}

ParallelTestMetrics 存储单个并行测试的指标

type PerformanceMetrics added in v1.0.2

type PerformanceMetrics struct {
	TotalBytes     int64
	StartTime      time.Time
	EndTime        time.Time
	Duration       time.Duration
	ThroughputMbps float64 // Mbps
	ActiveTests    int64
	ParallelTests  map[string]*ParallelTestMetrics // 并行测试详情
}

PerformanceMetrics 存储性能指标

type PerformanceMonitor added in v1.0.2

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

PerformanceMonitor 性能监控器

func NewPerformanceMonitor added in v1.0.2

func NewPerformanceMonitor() *PerformanceMonitor

NewPerformanceMonitor 创建新的性能监控器

func (*PerformanceMonitor) EndTest added in v1.0.2

func (pm *PerformanceMonitor) EndTest(testID string, bytes int64)

EndTest 结束测试

func (*PerformanceMonitor) GetMetrics added in v1.0.2

func (pm *PerformanceMonitor) GetMetrics() PerformanceMetrics

GetMetrics 获取当前性能指标

func (*PerformanceMonitor) ResetMetrics added in v1.0.2

func (pm *PerformanceMonitor) ResetMetrics()

ResetMetrics 重置性能指标

func (*PerformanceMonitor) StartTest added in v1.0.2

func (pm *PerformanceMonitor) StartTest(testID string)

StartTest 开始测试

type Server

type Server struct {
	Addr    string // 监听地址,如 "0.0.0.0"
	Port    int    // 端口,如 8080
	Version string // 版本信息
	// contains filtered or unexported fields
}

Server 代表一个LibreSpeed测速服务器实例

func NewServer

func NewServer(addr string, port int, version string, fs http.FileSystem, cssFS http.FileSystem, jsFS http.FileSystem, htmlFS http.FileSystem) *Server

NewServer 创建一个新的Server实例

func (*Server) CompleteTest added in v1.0.2

func (s *Server) CompleteTest(c *gin.Context)

CompleteTest 结束测试并保存结果(用于下载、上传和Ping测试)

func (*Server) DownloadTest

func (s *Server) DownloadTest(c *gin.Context)

DownloadTest 实现LibreSpeed风格的下载测试 基于testId的分段数据累积,强制要求测试ID

func (*Server) GetClientIP added in v1.0.2

func (s *Server) GetClientIP(c *gin.Context)

GetClientIP 专门用于获取客户端IP的接口

func (*Server) GetHistory added in v1.0.2

func (s *Server) GetHistory(c *gin.Context)

GetHistory 获取测试历史记录

func (*Server) GetTestStatus added in v1.0.2

func (s *Server) GetTestStatus(c *gin.Context)

GetTestStatus 查询测试会话状态

func (*Server) PerformanceHandler added in v1.0.2

func (s *Server) PerformanceHandler(c *gin.Context)

PerformanceHandler 获取性能监控指标

func (*Server) PingTest

func (s *Server) PingTest(c *gin.Context)

PingTest 实现LibreSpeed风格的ping测试 这个接口用于Ping测试的单次请求 支持两种模式: 1. 传统模式(无testId):返回客户端IP,用于获取IP 2. 会话模式(有testId):累积延迟数据到会话

func (*Server) SetupRoutes

func (s *Server) SetupRoutes()

SetupRoutes 设置所有路由

func (*Server) Start

func (s *Server) Start() error

Start 启动服务器

func (*Server) UploadTest

func (s *Server) UploadTest(c *gin.Context)

UploadTest 实现LibreSpeed风格的上传测试 基于testId的分段数据累积,强制要求测试ID 使用流式处理避免内存占用,支持动态数据传输 优化:使用更大的缓冲区和更少的系统调用以提高上传性能

func (*Server) WebSocketPingTest added in v1.0.2

func (s *Server) WebSocketPingTest(c *gin.Context)

WebSocketPingTest WebSocket延迟测试(可选,用于统一WebSocket接口)

func (*Server) WebSocketUploadTest added in v1.0.2

func (s *Server) WebSocketUploadTest(c *gin.Context)

WebSocketUploadTest WebSocket上传测试 基于testId的分段数据累积,强制要求测试ID 通过WebSocket接收数据,避免浏览器crypto.getRandomValues限制 支持动态数据传输:当客户端停止发送数据时立即返回结果

type Session added in v1.0.2

type Session struct {
	ID         string    `json:"id"`
	Type       string    `json:"type"` // download, upload, ping
	ClientIP   string    `json:"client_ip"`
	StartTime  time.Time `json:"start_time"`
	TotalBytes int64     `json:"total_bytes"` // 下载/上传使用
	TotalTime  float64   `json:"total_time"`  // 下载/上传使用(秒)
	LastUpdate time.Time `json:"last_update"`

	// Ping测试专用字段
	PingCount    int     `json:"ping_count"`    // Ping次数
	TotalLatency float64 `json:"total_latency"` // 累计延迟(毫秒)
	MinLatency   float64 `json:"min_latency"`   // 最小延迟
	MaxLatency   float64 `json:"max_latency"`   // 最大延迟
	TotalJitter  float64 `json:"total_jitter"`  // 累计抖动
}

Session 测试会话

type SessionManager added in v1.0.2

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

SessionManager 会话管理器

func NewSessionManager added in v1.0.2

func NewSessionManager(expiry time.Duration) *SessionManager

NewSessionManager 创建会话管理器

func (*SessionManager) AddData added in v1.0.2

func (sm *SessionManager) AddData(id string, bytes int64, duration float64)

AddData 累积数据到会话(用于下载/上传)

func (*SessionManager) AddPingData added in v1.0.2

func (sm *SessionManager) AddPingData(id string, latency float64, jitter float64)

AddPingData 累积Ping测试数据到会话

func (*SessionManager) CleanupExpired added in v1.0.2

func (sm *SessionManager) CleanupExpired()

CleanupExpired 清理过期会话

func (*SessionManager) CompletePingSession added in v1.0.2

func (sm *SessionManager) CompletePingSession(id string) (count int, avgLatency float64, avgJitter float64, minLatency float64, maxLatency float64, exists bool)

CompletePingSession 完成Ping会话并返回结果

func (*SessionManager) CompleteSession added in v1.0.2

func (sm *SessionManager) CompleteSession(id string) (int64, float64, bool)

CompleteSession 完成会话并返回结果(下载/上传)

func (*SessionManager) GetOrCreateSession added in v1.0.2

func (sm *SessionManager) GetOrCreateSession(id, sessionType, clientIP string) *Session

GetOrCreateSession 获取或创建会话

func (*SessionManager) GetSession added in v1.0.2

func (sm *SessionManager) GetSession(id string) *Session

GetSession 获取会话

func (*SessionManager) GetSessionCount added in v1.0.2

func (sm *SessionManager) GetSessionCount() int

GetSessionCount 获取会话数量

type TestRecord added in v1.0.2

type TestRecord struct {
	TestID    string       `json:"test_id"` // 全局唯一测试ID
	ClientIP  string       `json:"client_ip"`
	Timestamp time.Time    `json:"timestamp"`
	Results   []TestResult `json:"results"` // 包含所有测试类型的结果
}

TestRecord 测试记录 - 按test_id聚合所有测试结果

type TestResult added in v1.0.2

type TestResult struct {
	Type      string  `json:"type"` // download, upload, ping
	DataSize  int64   `json:"data_size"`
	Duration  float64 `json:"duration"`
	SpeedMbps float64 `json:"speed_mbps"`
	Latency   float64 `json:"latency,omitempty"` // 仅用于ping
	Jitter    float64 `json:"jitter,omitempty"`  // 仅用于ping
}

TestResult 测试结果数据

Jump to

Keyboard shortcuts

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