Files
EdgeNode/internal/stats/bandwidth_stat_manager_test.go

86 lines
2.2 KiB
Go
Raw Normal View History

2022-07-05 20:37:00 +08:00
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package stats_test
import (
2023-04-07 10:52:09 +08:00
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
2022-07-05 20:37:00 +08:00
"github.com/TeaOSLab/EdgeNode/internal/stats"
2023-04-07 10:52:09 +08:00
"runtime"
2022-07-05 20:37:00 +08:00
"testing"
"time"
)
func TestBandwidthStatManager_Add(t *testing.T) {
var manager = stats.NewBandwidthStatManager()
manager.AddBandwidth(1, 1, 10, 10)
manager.AddBandwidth(1, 1, 10, 10)
manager.AddBandwidth(1, 1, 10, 10)
2022-07-05 20:37:00 +08:00
time.Sleep(1 * time.Second)
manager.AddBandwidth(1, 1, 85, 85)
2022-07-05 20:37:00 +08:00
time.Sleep(1 * time.Second)
manager.AddBandwidth(1, 1, 25, 25)
manager.AddBandwidth(1, 1, 75, 75)
2022-07-05 20:37:00 +08:00
manager.Inspect()
}
func TestBandwidthStatManager_Loop(t *testing.T) {
var manager = stats.NewBandwidthStatManager()
manager.AddBandwidth(1, 1, 10, 10)
manager.AddBandwidth(1, 1, 10, 10)
manager.AddBandwidth(1, 1, 10, 10)
2022-07-05 20:37:00 +08:00
err := manager.Loop()
if err != nil {
t.Fatal(err)
}
2022-07-07 09:21:18 +08:00
}
2023-04-07 10:52:09 +08:00
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
}
}
}