mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-09 12:00:26 +08:00
优化代码
This commit is contained in:
33
internal/utils/testutils/memory.go
Normal file
33
internal/utils/testutils/memory.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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 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
|
||||
|
||||
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")
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user