From 52085bdc1ce766ae3d78fb9b526dbd6150bf7f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 7 Apr 2023 10:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/stats/bandwidth_stat_manager_test.go | 52 +++++++++++++++++++ internal/stats/traffic_stat_manager_test.go | 13 +++-- 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/internal/stats/bandwidth_stat_manager_test.go b/internal/stats/bandwidth_stat_manager_test.go index 5e2d3b1..43e403b 100644 --- a/internal/stats/bandwidth_stat_manager_test.go +++ b/internal/stats/bandwidth_stat_manager_test.go @@ -3,7 +3,9 @@ package stats_test import ( + "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeNode/internal/stats" + "runtime" "testing" "time" ) @@ -31,3 +33,53 @@ func TestBandwidthStatManager_Loop(t *testing.T) { t.Fatal(err) } } + +func BenchmarkBandwidthStatManager_Slice(b *testing.B) { + runtime.GOMAXPROCS(1) + + for i := 0; i < b.N; i++ { + var pbStats = []*pb.ServerBandwidthStat{} + for j := 0; j < 100; j++ { + var stat = &stats.BandwidthStat{} + pbStats = append(pbStats, &pb.ServerBandwidthStat{ + Id: 0, + UserId: stat.UserId, + ServerId: stat.ServerId, + Day: stat.Day, + TimeAt: stat.TimeAt, + Bytes: stat.MaxBytes / 2, + TotalBytes: stat.TotalBytes, + CachedBytes: stat.CachedBytes, + AttackBytes: stat.AttackBytes, + CountRequests: stat.CountRequests, + CountCachedRequests: stat.CountCachedRequests, + CountAttackRequests: stat.CountAttackRequests, + NodeRegionId: 1, + }) + } + } +} + +func BenchmarkBandwidthStatManager_Slice2(b *testing.B) { + runtime.GOMAXPROCS(1) + + for i := 0; i < b.N; i++ { + var statsSlice = []*stats.BandwidthStat{} + for j := 0; j < 100; j++ { + var stat = &stats.BandwidthStat{} + statsSlice = append(statsSlice, stat) + } + } +} + +func BenchmarkBandwidthStatManager_Slice3(b *testing.B) { + runtime.GOMAXPROCS(1) + + for i := 0; i < b.N; i++ { + var statsSlice = make([]*stats.BandwidthStat, 2000) + for j := 0; j < 100; j++ { + var stat = &stats.BandwidthStat{} + statsSlice[j] = stat + } + } +} diff --git a/internal/stats/traffic_stat_manager_test.go b/internal/stats/traffic_stat_manager_test.go index fb3db47..214af4d 100644 --- a/internal/stats/traffic_stat_manager_test.go +++ b/internal/stats/traffic_stat_manager_test.go @@ -3,6 +3,7 @@ package stats import ( "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" + "math/rand" "runtime" "testing" ) @@ -30,8 +31,12 @@ func TestTrafficStatManager_Upload(t *testing.T) { func BenchmarkTrafficStatManager_Add(b *testing.B) { runtime.GOMAXPROCS(1) - manager := NewTrafficStatManager() - for i := 0; i < b.N; i++ { - manager.Add(1, 1, "goedge.cn", 1024, 1, 0, 0, 0, 0, false, 0) - } + var manager = NewTrafficStatManager() + b.ResetTimer() + + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + manager.Add(1, 1, "goedge.cn"+types.String(rand.Int63()%10), 1024, 1, 0, 0, 0, 0, false, 0) + } + }) }