Files
EdgeAPI/internal/utils/testutils/memory.go

53 lines
1.2 KiB
Go
Raw Normal View History

2024-05-17 18:27:26 +08:00
// Copyright 2022 GoEdge CDN goedge.cdn@gmail.com. All rights reserved.
package testutils
import (
"fmt"
"runtime"
"testing"
"time"
2024-07-27 14:15:25 +08:00
timeutil "github.com/iwind/TeaGo/utils/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) {
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
t.Log(timeutil.Format("H:i:s"), "HeapInuse:", fmt.Sprintf("%.2fM", float64(stat.HeapInuse)/1024/1024), "NumGC:", stat.NumGC)
}
}()
}