github.com/TeaOSLab/EdgeNode@v1.3.8/internal/stats/bandwidth_stat_manager_test.go (about) 1 // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . 2 3 package stats_test 4 5 import ( 6 "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" 7 "github.com/TeaOSLab/EdgeNode/internal/stats" 8 "runtime" 9 "testing" 10 "time" 11 ) 12 13 func TestBandwidthStatManager_Add(t *testing.T) { 14 var manager = stats.NewBandwidthStatManager() 15 manager.AddBandwidth(1, 0, 1, 10, 10) 16 manager.AddBandwidth(1, 0, 1, 10, 10) 17 manager.AddBandwidth(1, 0, 1, 10, 10) 18 time.Sleep(1 * time.Second) 19 manager.AddBandwidth(1, 0, 1, 85, 85) 20 time.Sleep(1 * time.Second) 21 manager.AddBandwidth(1, 0, 1, 25, 25) 22 manager.AddBandwidth(1, 0, 1, 75, 75) 23 manager.Inspect() 24 } 25 26 func TestBandwidthStatManager_Loop(t *testing.T) { 27 var manager = stats.NewBandwidthStatManager() 28 manager.AddBandwidth(1, 0, 1, 10, 10) 29 manager.AddBandwidth(1, 0, 1, 10, 10) 30 manager.AddBandwidth(1, 0, 1, 10, 10) 31 err := manager.Loop() 32 if err != nil { 33 t.Fatal(err) 34 } 35 } 36 37 func BenchmarkBandwidthStatManager_Add(b *testing.B) { 38 var manager = stats.NewBandwidthStatManager() 39 b.RunParallel(func(pb *testing.PB) { 40 var i int 41 for pb.Next() { 42 i++ 43 manager.AddBandwidth(1, 0, int64(i%100), 10, 10) 44 } 45 }) 46 } 47 48 func BenchmarkBandwidthStatManager_Slice(b *testing.B) { 49 runtime.GOMAXPROCS(1) 50 51 for i := 0; i < b.N; i++ { 52 var pbStats = []*pb.ServerBandwidthStat{} 53 for j := 0; j < 100; j++ { 54 var stat = &stats.BandwidthStat{} 55 pbStats = append(pbStats, &pb.ServerBandwidthStat{ 56 Id: 0, 57 UserId: stat.UserId, 58 ServerId: stat.ServerId, 59 Day: stat.Day, 60 TimeAt: stat.TimeAt, 61 Bytes: stat.MaxBytes / 2, 62 TotalBytes: stat.TotalBytes, 63 CachedBytes: stat.CachedBytes, 64 AttackBytes: stat.AttackBytes, 65 CountRequests: stat.CountRequests, 66 CountCachedRequests: stat.CountCachedRequests, 67 CountAttackRequests: stat.CountAttackRequests, 68 CountWebsocketConnections: stat.CountWebsocketConnections, 69 NodeRegionId: 1, 70 }) 71 } 72 _ = pbStats 73 } 74 } 75 76 func BenchmarkBandwidthStatManager_Slice2(b *testing.B) { 77 runtime.GOMAXPROCS(1) 78 79 for i := 0; i < b.N; i++ { 80 var statsSlice = []*stats.BandwidthStat{} 81 for j := 0; j < 100; j++ { 82 var stat = &stats.BandwidthStat{} 83 statsSlice = append(statsSlice, stat) 84 } 85 _ = statsSlice 86 } 87 } 88 89 func BenchmarkBandwidthStatManager_Slice3(b *testing.B) { 90 runtime.GOMAXPROCS(1) 91 92 for i := 0; i < b.N; i++ { 93 var statsSlice = make([]*stats.BandwidthStat, 2000) 94 for j := 0; j < 100; j++ { 95 var stat = &stats.BandwidthStat{} 96 statsSlice[j] = stat 97 } 98 } 99 }