mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
- Added new configuration structures for loading, saving, and validation settings in MetricsConfig. - Introduced constants for load retry counts, intervals, and validation parameters to improve configurability. - Enhanced metrics collector with data validation and consistency checks during metrics loading. - Implemented backup and restore functionality for metrics data, ensuring data integrity and recoverability. - Improved logging for metrics operations, providing better insights into the metrics collection process. These changes enhance the flexibility and reliability of the metrics system, ensuring accurate data handling and improved monitoring capabilities.
36 lines
798 B
Go
36 lines
798 B
Go
package metrics
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// NewTestCollector creates a new collector for testing
|
|
func NewTestCollector() *Collector {
|
|
return &Collector{
|
|
startTime: time.Now(),
|
|
pathStats: sync.Map{},
|
|
statusStats: [6]atomic.Int64{},
|
|
latencyBuckets: [10]atomic.Int64{},
|
|
}
|
|
}
|
|
|
|
func TestDataConsistency(t *testing.T) {
|
|
c := NewTestCollector()
|
|
|
|
// 测试基础指标
|
|
c.RecordRequest("/test", 200, time.Second, 1024, "127.0.0.1", nil)
|
|
if err := c.CheckDataConsistency(); err != nil {
|
|
t.Errorf("Data consistency check failed: %v", err)
|
|
}
|
|
|
|
// 测试错误数据
|
|
c.persistentStats.totalErrors.Store(100)
|
|
c.persistentStats.totalRequests.Store(50)
|
|
if err := c.CheckDataConsistency(); err == nil {
|
|
t.Error("Expected error for invalid data")
|
|
}
|
|
}
|