chore: add test for memory module

This commit is contained in:
wwqgtxx 2025-09-24 02:35:04 +08:00
parent 92ecdfcc00
commit 29eaa4d699
3 changed files with 44 additions and 4 deletions

View File

@ -2,7 +2,28 @@
// modify from https://github.com/shirou/gopsutil/tree/v4.25.8/process
package memory
import (
"errors"
"fmt"
"math"
)
var ErrNotImplementedError = errors.New("not implemented yet")
type MemoryInfoStat struct {
RSS uint64 `json:"rss"` // bytes
VMS uint64 `json:"vms"` // bytes
}
// PrettyByteSize convert size in bytes to Bytes, Kilobytes, Megabytes, GB and TB
// https://gist.github.com/anikitenko/b41206a49727b83a530142c76b1cb82d?permalink_comment_id=4467913#gistcomment-4467913
func PrettyByteSize(b uint64) string {
bf := float64(b)
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
if math.Abs(bf) < 1024.0 {
return fmt.Sprintf("%3.1f%sB", bf, unit)
}
bf /= 1024.0
}
return fmt.Sprintf("%.1fYiB", bf)
}

View File

@ -2,10 +2,6 @@
package memory
import "errors"
var ErrNotImplementedError = errors.New("not implemented yet")
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
return nil, ErrNotImplementedError
}

View File

@ -0,0 +1,23 @@
package memory
import (
"errors"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestMemoryInfo(t *testing.T) {
v, err := GetMemoryInfo(int32(os.Getpid()))
if errors.Is(err, ErrNotImplementedError) {
t.Skip("not implemented")
}
require.NoErrorf(t, err, "getting memory info error %v", err)
empty := MemoryInfoStat{}
if v == nil || *v == empty {
t.Errorf("could not get memory info %v", v)
} else {
t.Logf("memory info {RSS:%s, VMS:%s}", PrettyByteSize(v.RSS), PrettyByteSize(v.VMS))
}
}