You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
224 lines
5.4 KiB
224 lines
5.4 KiB
package proxy
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestNewProxyStats(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
if stats == nil {
|
|
t.Fatal("NewProxyStats returned nil")
|
|
}
|
|
|
|
if stats.SOCKS5Errors == nil {
|
|
t.Error("SOCKS5Errors map should be initialized")
|
|
}
|
|
|
|
if time.Since(stats.StartTime) > time.Second {
|
|
t.Error("StartTime should be recent")
|
|
}
|
|
}
|
|
|
|
func TestProxyStatsCounters(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
// 测试连接计数
|
|
stats.IncrementConnections()
|
|
stats.IncrementConnections()
|
|
|
|
snapshot := stats.GetStats()
|
|
if snapshot.TotalConnections != 2 {
|
|
t.Errorf("Expected 2 total connections, got %d", snapshot.TotalConnections)
|
|
}
|
|
|
|
if snapshot.ActiveConnections != 2 {
|
|
t.Errorf("Expected 2 active connections, got %d", snapshot.ActiveConnections)
|
|
}
|
|
|
|
// 测试减少活跃连接
|
|
stats.DecrementActiveConnections()
|
|
snapshot = stats.GetStats()
|
|
if snapshot.ActiveConnections != 1 {
|
|
t.Errorf("Expected 1 active connection, got %d", snapshot.ActiveConnections)
|
|
}
|
|
|
|
// 测试请求计数
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.IncrementFailedRequests()
|
|
|
|
snapshot = stats.GetStats()
|
|
if snapshot.SuccessfulRequests != 2 {
|
|
t.Errorf("Expected 2 successful requests, got %d", snapshot.SuccessfulRequests)
|
|
}
|
|
|
|
if snapshot.FailedRequests != 1 {
|
|
t.Errorf("Expected 1 failed request, got %d", snapshot.FailedRequests)
|
|
}
|
|
}
|
|
|
|
func TestProxyStatsBytesTransferred(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
stats.AddBytesTransferred(100, 200)
|
|
stats.AddBytesTransferred(50, 75)
|
|
|
|
snapshot := stats.GetStats()
|
|
if snapshot.BytesSent != 150 {
|
|
t.Errorf("Expected 150 bytes sent, got %d", snapshot.BytesSent)
|
|
}
|
|
|
|
if snapshot.BytesReceived != 275 {
|
|
t.Errorf("Expected 275 bytes received, got %d", snapshot.BytesReceived)
|
|
}
|
|
|
|
totalBytes := snapshot.GetTotalBytes()
|
|
if totalBytes != 425 {
|
|
t.Errorf("Expected 425 total bytes, got %d", totalBytes)
|
|
}
|
|
}
|
|
|
|
func TestProxyStatsSOCKS5Errors(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
stats.IncrementSOCKS5Error("connection_failed")
|
|
stats.IncrementSOCKS5Error("auth_failed")
|
|
stats.IncrementSOCKS5Error("connection_failed")
|
|
|
|
snapshot := stats.GetStats()
|
|
|
|
if snapshot.SOCKS5Errors["connection_failed"] != 2 {
|
|
t.Errorf("Expected 2 connection_failed errors, got %d",
|
|
snapshot.SOCKS5Errors["connection_failed"])
|
|
}
|
|
|
|
if snapshot.SOCKS5Errors["auth_failed"] != 1 {
|
|
t.Errorf("Expected 1 auth_failed error, got %d",
|
|
snapshot.SOCKS5Errors["auth_failed"])
|
|
}
|
|
}
|
|
|
|
func TestProxyStatsSnapshot(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
// 添加一些测试数据
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.IncrementFailedRequests()
|
|
|
|
snapshot := stats.GetStats()
|
|
|
|
// 测试成功率计算
|
|
successRate := snapshot.GetSuccessRate()
|
|
expected := 75.0 // 3 successful out of 4 total = 75%
|
|
if successRate != expected {
|
|
t.Errorf("Expected success rate %.1f%%, got %.1f%%", expected, successRate)
|
|
}
|
|
|
|
// 测试零请求时的成功率
|
|
emptyStats := NewProxyStats()
|
|
emptySnapshot := emptyStats.GetStats()
|
|
if emptySnapshot.GetSuccessRate() != 0 {
|
|
t.Errorf("Expected 0%% success rate for empty stats, got %.1f%%",
|
|
emptySnapshot.GetSuccessRate())
|
|
}
|
|
}
|
|
|
|
func TestProxyStatsAverageConnections(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
// 由于uptime很短,我们模拟一些连接
|
|
stats.IncrementConnections()
|
|
stats.IncrementConnections()
|
|
|
|
snapshot := stats.GetStats()
|
|
avg := snapshot.GetAverageConnectionsPerHour()
|
|
|
|
// 应该大于0,具体值取决于测试运行的时间
|
|
if avg <= 0 {
|
|
t.Error("Average connections per hour should be greater than 0")
|
|
}
|
|
}
|
|
|
|
func TestConcurrentStatsAccess(t *testing.T) {
|
|
stats := NewProxyStats()
|
|
|
|
// 并发测试
|
|
done := make(chan bool)
|
|
|
|
// 启动多个goroutine来并发更新统计
|
|
for i := 0; i < 10; i++ {
|
|
go func() {
|
|
for j := 0; j < 100; j++ {
|
|
stats.IncrementConnections()
|
|
stats.IncrementSuccessfulRequests()
|
|
stats.AddBytesTransferred(10, 20)
|
|
stats.IncrementSOCKS5Error("test_error")
|
|
}
|
|
done <- true
|
|
}()
|
|
}
|
|
|
|
// 等待所有goroutine完成
|
|
for i := 0; i < 10; i++ {
|
|
<-done
|
|
}
|
|
|
|
snapshot := stats.GetStats()
|
|
|
|
// 验证最终计数
|
|
expectedConnections := int64(1000) // 10 goroutines * 100 increments
|
|
if snapshot.TotalConnections != expectedConnections {
|
|
t.Errorf("Expected %d total connections, got %d",
|
|
expectedConnections, snapshot.TotalConnections)
|
|
}
|
|
|
|
if snapshot.SuccessfulRequests != expectedConnections {
|
|
t.Errorf("Expected %d successful requests, got %d",
|
|
expectedConnections, snapshot.SuccessfulRequests)
|
|
}
|
|
|
|
expectedBytesSent := int64(10000) // 10 * 100 * 10
|
|
if snapshot.BytesSent != expectedBytesSent {
|
|
t.Errorf("Expected %d bytes sent, got %d",
|
|
expectedBytesSent, snapshot.BytesSent)
|
|
}
|
|
|
|
if snapshot.SOCKS5Errors["test_error"] != expectedConnections {
|
|
t.Errorf("Expected %d test_error occurrences, got %d",
|
|
expectedConnections, snapshot.SOCKS5Errors["test_error"])
|
|
}
|
|
}
|
|
|
|
// BenchmarkStatsOperations 性能测试
|
|
func BenchmarkStatsOperations(b *testing.B) {
|
|
stats := NewProxyStats()
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
stats.IncrementConnections()
|
|
stats.AddBytesTransferred(100, 200)
|
|
stats.IncrementSuccessfulRequests()
|
|
}
|
|
}
|
|
|
|
func BenchmarkStatsSnapshot(b *testing.B) {
|
|
stats := NewProxyStats()
|
|
|
|
// 添加一些数据
|
|
for i := 0; i < 100; i++ {
|
|
stats.IncrementConnections()
|
|
stats.AddBytesTransferred(100, 200)
|
|
stats.IncrementSOCKS5Error("test_error")
|
|
}
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
_ = stats.GetStats()
|
|
}
|
|
}
|
|
|