Initial commit (code only without large binaries)

This commit is contained in:
robin
2026-02-15 18:58:44 +08:00
commit 35df75498f
9442 changed files with 1495866 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
package testutils
import (
"fmt"
timeutil "github.com/iwind/TeaGo/utils/time"
"runtime"
"testing"
"time"
)
func StartMemoryStatsGC(t *testing.T) {
var ticker = time.NewTicker(1 * time.Second)
go func() {
var stat = &runtime.MemStats{}
var lastHeapInUse uint64
for range ticker.C {
runtime.ReadMemStats(stat)
if stat.HeapInuse == lastHeapInUse {
return
}
lastHeapInUse = stat.HeapInuse
var before = time.Now()
runtime.GC()
var cost = time.Since(before).Seconds()
t.Log(timeutil.Format("H:i:s"), "HeapInuse:", fmt.Sprintf("%.2fM", float64(stat.HeapInuse)/1024/1024), "NumGC:", stat.NumGC, "Cost:", fmt.Sprintf("%.4f", cost*1000), "ms")
}
}()
}
func StartMemoryStats(t *testing.T, callbacks ...func()) {
var ticker = time.NewTicker(1 * time.Second)
go func() {
var stat = &runtime.MemStats{}
var lastHeapInUse uint64
for range ticker.C {
runtime.ReadMemStats(stat)
if stat.HeapInuse == lastHeapInUse {
continue
}
lastHeapInUse = stat.HeapInuse
t.Log(timeutil.Format("H:i:s"), "HeapInuse:", fmt.Sprintf("%.2fM", float64(stat.HeapInuse)/1024/1024), "NumGC:", stat.NumGC)
if len(callbacks) > 0 {
for _, callback := range callbacks {
callback()
}
}
}
}()
}
func ReadMemoryStat() *runtime.MemStats {
var stat = &runtime.MemStats{}
runtime.ReadMemStats(stat)
return stat
}

View File

@@ -0,0 +1,24 @@
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
package testutils
import (
"fmt"
"math/rand"
"os"
)
// IsSingleTesting 判断当前测试环境是否为单个函数测试
func IsSingleTesting() bool {
for _, arg := range os.Args {
if arg == "-test.run" {
return true
}
}
return false
}
// RandIP 生成一个随机IP用于测试
func RandIP() string {
return fmt.Sprintf("%d.%d.%d.%d", rand.Int()%255, rand.Int()%255, rand.Int()%255, rand.Int()%255)
}