performance

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: GPL-3.0 Imports: 19 Imported by: 0

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

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 GetBuffer

func GetBuffer() *bytes.Buffer

func GetJSONBuffer

func GetJSONBuffer() []byte

func GetMapStringAny

func GetMapStringAny() map[string]any

func GetMapStringString

func GetMapStringString() map[string]string

func GetSmallIntSlice

func GetSmallIntSlice() []int

func GetStringSlice

func GetStringSlice() []string

func GetTrafficBuffer

func GetTrafficBuffer() *[]byte

func GetXrayConfigBuffer

func GetXrayConfigBuffer() *bytes.Buffer

func GinPerformanceMiddleware

func GinPerformanceMiddleware() gin.HandlerFunc

func HTTPClient

func HTTPClient() *http.Client

func InitCacheManager

func InitCacheManager(cfg Config)

func InitGlobalWorkerPools

func InitGlobalWorkerPools(cfg Config)

func Initialize

func Initialize(cfg Config)

func OptimizedHTTPClient

func OptimizedHTTPClient() *http.Client

func OptimizedHTTPTransport

func OptimizedHTTPTransport() *http.Transport

func PutBuffer

func PutBuffer(buf *bytes.Buffer)

func PutJSONBuffer

func PutJSONBuffer(buf []byte)

func PutMapStringAny

func PutMapStringAny(m map[string]any)

func PutMapStringString

func PutMapStringString(m map[string]string)

func PutSmallIntSlice

func PutSmallIntSlice(s []int)

func PutStringSlice

func PutStringSlice(s []string)

func PutTrafficBuffer

func PutTrafficBuffer(buf *[]byte)

func PutXrayConfigBuffer

func PutXrayConfigBuffer(buf *bytes.Buffer)

func RecordRequest

func RecordRequest(duration time.Duration)

func RecordRequestError

func RecordRequestError()

func RegisterRoutes

func RegisterRoutes(router gin.IRouter)

func RunLoadTestSuite

func RunLoadTestSuite(operations map[string]func(ctx context.Context) error)

func Shutdown

func Shutdown()

func SimulateHighConcurrency

func SimulateHighConcurrency(ctx context.Context, workers int, duration time.Duration, task func(id int) error) map[int]error

func StartPeriodicCleanup

func StartPeriodicCleanup(interval time.Duration)

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

func RunParallelBenchmark(name string, fn func(ctx context.Context) error, workers, iterations int) BenchmarkResult

type Cache

type Cache[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func NewCache

func NewCache[K comparable, V any](ttl time.Duration, maxSize int) *Cache[K, V]

func (*Cache[K, V]) Cleanup

func (c *Cache[K, V]) Cleanup()

func (*Cache[K, V]) Clear

func (c *Cache[K, V]) Clear()

func (*Cache[K, V]) Delete

func (c *Cache[K, V]) Delete(key K)

func (*Cache[K, V]) Get

func (c *Cache[K, V]) Get(key K) (V, bool)

func (*Cache[K, V]) GetOrSet

func (c *Cache[K, V]) GetOrSet(key K, fn func() (V, error)) (V, error)

func (*Cache[K, V]) Len

func (c *Cache[K, V]) Len() int

func (*Cache[K, V]) Set

func (c *Cache[K, V]) Set(key K, value V)

func (*Cache[K, V]) SetWithTTL

func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration)

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 (cm *CacheManager) GetOrCreateString(name string, ttl time.Duration, maxSize int) *Cache[string, string]

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 ConnPool

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

func NewConnPool

func NewConnPool(size int, dial func() (net.Conn, error)) *ConnPool

func (*ConnPool) Close

func (p *ConnPool) Close()

func (*ConnPool) Get

func (p *ConnPool) Get() (net.Conn, error)

func (*ConnPool) Put

func (p *ConnPool) Put(conn net.Conn)

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 Job

type Job func(ctx context.Context)

type LoadProfile

type LoadProfile struct {
	Name        string        `json:"name"`
	Concurrency int           `json:"concurrency"`
	Duration    time.Duration `json:"duration"`
	RampUp      time.Duration `json:"rampUp"`
	MaxErrors   int64         `json:"maxErrors"`
}

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 PoolStats

type PoolStats struct {
	Name      string `json:"name"`
	Size      int    `json:"size"`
	QueueSize int    `json:"queueSize"`
	QueueCap  int    `json:"queueCap"`
	Submitted int64  `json:"submitted"`
	Completed int64  `json:"completed"`
	Failed    int64  `json:"failed"`
	Running   int64  `json:"running"`
}

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 TrafficEntry struct {
	Up   int64
	Down int64
}

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

Jump to

Keyboard shortcuts

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