Documentation
¶
Overview ¶
Package performance provides profiling, caching, concurrency, and benchmarking infrastructure for X-UI PRO. It is Phase 7 of the project: Performance Optimization & Load Testing.
Architecture
profiling.go — pprof handlers, runtime metrics, goroutine dumper cache.go — generics-based TTL cache with sharded map pool.go — sync.Pool wrappers for buffer reuse workerpool.go — bounded goroutine pool with job queue benchmark.go — load-test helpers and assertion macros config.go — tunable performance parameters
Index ¶
- Constants
- Variables
- func FormatBenchmarkResults(results []BenchmarkResult) string
- func FormatLoadTestResult(result LoadTestResult) string
- func FormatMetricsText(metrics RuntimeMetrics) string
- func GetBuffer() *bytes.Buffer
- func GetJSONBuffer() []byte
- func GetMapStringAny() map[string]any
- func GetMapStringString() map[string]string
- func GetSmallIntSlice() []int
- func GetStringSlice() []string
- func GetTrafficBuffer() *[]byte
- func GetXrayConfigBuffer() *bytes.Buffer
- func GinPerformanceMiddleware() gin.HandlerFunc
- func HTTPClient() *http.Client
- func InitCacheManager(cfg Config)
- func InitGlobalWorkerPools(cfg Config)
- func Initialize(cfg Config)
- func OptimizedHTTPClient() *http.Client
- func OptimizedHTTPTransport() *http.Transport
- func PutBuffer(buf *bytes.Buffer)
- func PutJSONBuffer(buf []byte)
- func PutMapStringAny(m map[string]any)
- func PutMapStringString(m map[string]string)
- func PutSmallIntSlice(s []int)
- func PutStringSlice(s []string)
- func PutTrafficBuffer(buf *[]byte)
- func PutXrayConfigBuffer(buf *bytes.Buffer)
- func RecordRequest(duration time.Duration)
- func RecordRequestError()
- func RegisterRoutes(router gin.IRouter)
- func RunLoadTestSuite(operations map[string]func(ctx context.Context) error)
- func Shutdown()
- func SimulateHighConcurrency(ctx context.Context, workers int, duration time.Duration, ...) map[int]error
- func StartPeriodicCleanup(interval time.Duration)
- func StopAllWorkerPools()
- func TrackActiveRequest(delta int64)
- func TrackCacheResult(hit bool)
- type BenchmarkResult
- type Cache
- func (c *Cache[K, V]) Cleanup()
- func (c *Cache[K, V]) Clear()
- func (c *Cache[K, V]) Delete(key K)
- func (c *Cache[K, V]) Get(key K) (V, bool)
- func (c *Cache[K, V]) GetOrSet(key K, fn func() (V, error)) (V, error)
- func (c *Cache[K, V]) Len() int
- func (c *Cache[K, V]) Set(key K, value V)
- func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration)
- type CacheManager
- func (cm *CacheManager) CleanupAll()
- func (cm *CacheManager) ClearAll()
- func (cm *CacheManager) GetOrCreateAny(name string) *Cache[string, any]
- func (cm *CacheManager) GetOrCreateInt(name string) *Cache[string, int64]
- func (cm *CacheManager) GetOrCreateString(name string, ttl time.Duration, maxSize int) *Cache[string, string]
- func (cm *CacheManager) Stats() map[string]int
- type Config
- type ConnPool
- type ConnectionSimulator
- type Job
- type LoadProfile
- type LoadTestResult
- type LoadTester
- type PoolStats
- type ProfilingHandler
- type RequestTimer
- type RuntimeMetrics
- type ThroughputResult
- type TrafficBatch
- type TrafficEntry
- type WorkerPool
- type XrayConnPool
Constants ¶
View Source
const (
MaxBodySize = 10 << 20
)
Variables ¶
View Source
var ( ErrPoolQueueFull = errors.New("worker pool queue is full") ErrCacheMiss = errors.New("cache miss") )
View Source
var DefaultLoadProfiles = []LoadProfile{ { Name: "light", Concurrency: 10, Duration: 10 * time.Second, RampUp: 2 * time.Second, MaxErrors: 50, }, { Name: "medium", Concurrency: 50, Duration: 30 * time.Second, RampUp: 5 * time.Second, MaxErrors: 100, }, { Name: "heavy", Concurrency: 200, Duration: 60 * time.Second, RampUp: 10 * time.Second, MaxErrors: 500, }, { Name: "stress", Concurrency: 500, Duration: 120 * time.Second, RampUp: 20 * time.Second, MaxErrors: 1000, }, }
Functions ¶
func FormatBenchmarkResults ¶
func FormatBenchmarkResults(results []BenchmarkResult) string
func FormatLoadTestResult ¶
func FormatLoadTestResult(result LoadTestResult) string
func FormatMetricsText ¶
func FormatMetricsText(metrics RuntimeMetrics) string
func GetJSONBuffer ¶
func GetJSONBuffer() []byte
func GetMapStringAny ¶
func GetMapStringString ¶
func GetSmallIntSlice ¶
func GetSmallIntSlice() []int
func GetStringSlice ¶
func GetStringSlice() []string
func GetTrafficBuffer ¶
func GetTrafficBuffer() *[]byte
func GetXrayConfigBuffer ¶
func GinPerformanceMiddleware ¶
func GinPerformanceMiddleware() gin.HandlerFunc
func HTTPClient ¶
func InitCacheManager ¶
func InitCacheManager(cfg Config)
func InitGlobalWorkerPools ¶
func InitGlobalWorkerPools(cfg Config)
func Initialize ¶
func Initialize(cfg Config)
func OptimizedHTTPClient ¶
func OptimizedHTTPTransport ¶
func PutJSONBuffer ¶
func PutJSONBuffer(buf []byte)
func PutMapStringAny ¶
func PutMapStringString ¶
func PutSmallIntSlice ¶
func PutSmallIntSlice(s []int)
func PutStringSlice ¶
func PutStringSlice(s []string)
func PutTrafficBuffer ¶
func PutTrafficBuffer(buf *[]byte)
func PutXrayConfigBuffer ¶
func RecordRequest ¶
func RecordRequestError ¶
func RecordRequestError()
func RegisterRoutes ¶
func RunLoadTestSuite ¶
func SimulateHighConcurrency ¶
func StartPeriodicCleanup ¶
func StopAllWorkerPools ¶
func StopAllWorkerPools()
func TrackActiveRequest ¶
func TrackActiveRequest(delta int64)
func TrackCacheResult ¶
func TrackCacheResult(hit bool)
Types ¶
type BenchmarkResult ¶
type BenchmarkResult struct {
Name string `json:"name"`
Operations int64 `json:"operations"`
Duration time.Duration `json:"duration"`
OpsPerSecond float64 `json:"opsPerSecond"`
AllocBytes uint64 `json:"allocBytes"`
AllocsPerOp uint64 `json:"allocsPerOp"`
BytesPerOp uint64 `json:"bytesPerOp"`
Goroutines int `json:"goroutines"`
NumGC uint32 `json:"numGC"`
}
func RunBenchmark ¶
func RunBenchmark(name string, fn func() error, iterations int) BenchmarkResult
func RunParallelBenchmark ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
func (*Cache[K, V]) SetWithTTL ¶
type CacheManager ¶
type CacheManager struct {
// contains filtered or unexported fields
}
func GlobalCacheManager ¶
func GlobalCacheManager() *CacheManager
func (*CacheManager) CleanupAll ¶
func (cm *CacheManager) CleanupAll()
func (*CacheManager) ClearAll ¶
func (cm *CacheManager) ClearAll()
func (*CacheManager) GetOrCreateAny ¶
func (cm *CacheManager) GetOrCreateAny(name string) *Cache[string, any]
func (*CacheManager) GetOrCreateInt ¶
func (cm *CacheManager) GetOrCreateInt(name string) *Cache[string, int64]
func (*CacheManager) GetOrCreateString ¶
func (*CacheManager) Stats ¶
func (cm *CacheManager) Stats() map[string]int
type Config ¶
type Config struct {
CacheEnabled bool `json:"cacheEnabled"`
CacheDefaultTTL time.Duration `json:"cacheDefaultTTL"`
CacheCleanupPeriod time.Duration `json:"cacheCleanupPeriod"`
CacheMaxEntries int `json:"cacheMaxEntries"`
WorkerPoolSize int `json:"workerPoolSize"`
WorkerQueueDepth int `json:"workerQueueDepth"`
HealthConcurrency int `json:"healthConcurrency"`
DBMaxOpenConns int `json:"dbMaxOpenConns"`
DBMaxIdleConns int `json:"dbMaxIdleConns"`
DBConnMaxLifetime time.Duration `json:"dbConnMaxLifetime"`
ProfilingEnabled bool `json:"profilingEnabled"`
PprofEnabled bool `json:"pprofEnabled"`
MetricsEnabled bool `json:"metricsEnabled"`
GzipCompressionLevel int `json:"gzipCompressionLevel"`
}
func ConfigFromEnv ¶
func ConfigFromEnv() Config
func DefaultConfig ¶
func DefaultConfig() Config
func GetPerfConfig ¶
func GetPerfConfig() Config
type ConnectionSimulator ¶
type ConnectionSimulator struct {
// contains filtered or unexported fields
}
func NewConnectionSimulator ¶
func NewConnectionSimulator() *ConnectionSimulator
func (*ConnectionSimulator) Connect ¶
func (cs *ConnectionSimulator) Connect() int
func (*ConnectionSimulator) Disconnect ¶
func (cs *ConnectionSimulator) Disconnect(id int)
func (*ConnectionSimulator) SimulateTraffic ¶
func (cs *ConnectionSimulator) SimulateTraffic(ctx context.Context, numConnections int, opsPerConn int)
func (*ConnectionSimulator) Stats ¶
func (cs *ConnectionSimulator) Stats() (current, max int)
type LoadProfile ¶
type LoadTestResult ¶
type LoadTestResult struct {
Name string `json:"name"`
Concurrency int `json:"concurrency"`
Duration time.Duration `json:"duration"`
TotalOps int64 `json:"totalOps"`
SuccessOps int64 `json:"successOps"`
ErrorOps int64 `json:"errorOps"`
OpsPerSecond float64 `json:"opsPerSecond"`
AvgLatency time.Duration `json:"avgLatency"`
P50Latency time.Duration `json:"p50Latency"`
P90Latency time.Duration `json:"p90Latency"`
P99Latency time.Duration `json:"p99Latency"`
MaxLatency time.Duration `json:"maxLatency"`
MinLatency time.Duration `json:"minLatency"`
}
type LoadTester ¶
type LoadTester struct {
// contains filtered or unexported fields
}
func NewLoadTester ¶
func NewLoadTester(profile LoadProfile) *LoadTester
func (*LoadTester) Run ¶
func (lt *LoadTester) Run(operation func(ctx context.Context) error) LoadTestResult
type ProfilingHandler ¶
type ProfilingHandler struct {
// contains filtered or unexported fields
}
func GetProfilingHandler ¶
func GetProfilingHandler() *ProfilingHandler
func NewProfilingHandler ¶
func NewProfilingHandler(pprofEnabled bool) *ProfilingHandler
func (*ProfilingHandler) RegisterGinRoutes ¶
func (h *ProfilingHandler) RegisterGinRoutes(router gin.IRouter)
type RequestTimer ¶
type RequestTimer struct {
// contains filtered or unexported fields
}
func NewRequestTimer ¶
func NewRequestTimer() *RequestTimer
func (*RequestTimer) Finish ¶
func (rt *RequestTimer) Finish()
func (*RequestTimer) FinishWithError ¶
func (rt *RequestTimer) FinishWithError()
type RuntimeMetrics ¶
type RuntimeMetrics struct {
Version string `json:"version"`
UptimeSeconds float64 `json:"uptimeSeconds"`
NumCPU int `json:"numCPU"`
NumGoroutine int `json:"numGoroutine"`
NumCgoCall int64 `json:"numCgoCall"`
AllocMB float64 `json:"allocMB"`
TotalAllocMB float64 `json:"totalAllocMB"`
SysMB float64 `json:"sysMB"`
HeapAllocMB float64 `json:"heapAllocMB"`
HeapSysMB float64 `json:"heapSysMB"`
HeapIdleMB float64 `json:"heapIdleMB"`
HeapInuseMB float64 `json:"heapInuseMB"`
StackInuseMB float64 `json:"stackInuseMB"`
GCStatsNumGC uint32 `json:"gcStatsNumGC"`
GCStatsPauseTotal float64 `json:"gcStatsPauseTotalMs"`
GCStatsPauseAvg float64 `json:"gcStatsPauseAvgMs"`
NextGC float64 `json:"nextGCMB"`
LastGC string `json:"lastGC"`
TotalRequests int64 `json:"totalRequests"`
ActiveRequests int64 `json:"activeRequests"`
RequestErrors int64 `json:"requestErrors"`
AvgRequestTimeMs float64 `json:"avgRequestTimeMs"`
CacheHitRate float64 `json:"cacheHitRate"`
}
func CollectRuntimeMetrics ¶
func CollectRuntimeMetrics() RuntimeMetrics
type ThroughputResult ¶
type ThroughputResult struct {
BytesPerSecond float64 `json:"bytesPerSecond"`
TotalBytes int64 `json:"totalBytes"`
Duration time.Duration `json:"duration"`
}
func MeasureThroughput ¶
func MeasureThroughput(fn func() (int, error), duration time.Duration) ThroughputResult
type TrafficBatch ¶
type TrafficBatch struct {
// contains filtered or unexported fields
}
func NewTrafficBatch ¶
func NewTrafficBatch() *TrafficBatch
func (*TrafficBatch) Add ¶
func (tb *TrafficBatch) Add(key string, up, down int64)
func (*TrafficBatch) Flush ¶
func (tb *TrafficBatch) Flush() map[string]*TrafficEntry
func (*TrafficBatch) Len ¶
func (tb *TrafficBatch) Len() int
type TrafficEntry ¶
type WorkerPool ¶
type WorkerPool struct {
// contains filtered or unexported fields
}
func GlobalAsyncPool ¶
func GlobalAsyncPool() *WorkerPool
func GlobalHealthPool ¶
func GlobalHealthPool() *WorkerPool
func GlobalMetricsPool ¶
func GlobalMetricsPool() *WorkerPool
func NewWorkerPool ¶
func NewWorkerPool(name string, size int, queueDepth int) *WorkerPool
func (*WorkerPool) Start ¶
func (wp *WorkerPool) Start()
func (*WorkerPool) Stats ¶
func (wp *WorkerPool) Stats() PoolStats
func (*WorkerPool) Stop ¶
func (wp *WorkerPool) Stop()
func (*WorkerPool) Submit ¶
func (wp *WorkerPool) Submit(job Job) error
func (*WorkerPool) SubmitWait ¶
func (wp *WorkerPool) SubmitWait(job Job)
func (*WorkerPool) TrySubmit ¶
func (wp *WorkerPool) TrySubmit(job Job) bool
type XrayConnPool ¶
type XrayConnPool struct {
// contains filtered or unexported fields
}
func NewXrayConnPool ¶
func NewXrayConnPool(address string, maxConns int) *XrayConnPool
func (*XrayConnPool) Acquire ¶
func (p *XrayConnPool) Acquire()
func (*XrayConnPool) Release ¶
func (p *XrayConnPool) Release()
Click to show internal directories.
Click to hide internal directories.