mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-20 00:50:06 +08:00
chore: add test for memory module
This commit is contained in:
parent
92ecdfcc00
commit
29eaa4d699
@ -2,7 +2,28 @@
|
|||||||
// modify from https://github.com/shirou/gopsutil/tree/v4.25.8/process
|
// modify from https://github.com/shirou/gopsutil/tree/v4.25.8/process
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
|
var ErrNotImplementedError = errors.New("not implemented yet")
|
||||||
|
|
||||||
type MemoryInfoStat struct {
|
type MemoryInfoStat struct {
|
||||||
RSS uint64 `json:"rss"` // bytes
|
RSS uint64 `json:"rss"` // bytes
|
||||||
VMS uint64 `json:"vms"` // 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)
|
||||||
|
}
|
||||||
|
|||||||
@ -2,10 +2,6 @@
|
|||||||
|
|
||||||
package memory
|
package memory
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var ErrNotImplementedError = errors.New("not implemented yet")
|
|
||||||
|
|
||||||
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
func GetMemoryInfo(pid int32) (*MemoryInfoStat, error) {
|
||||||
return nil, ErrNotImplementedError
|
return nil, ErrNotImplementedError
|
||||||
}
|
}
|
||||||
|
|||||||
23
component/memory/memory_test.go
Normal file
23
component/memory/memory_test.go
Normal 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))
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user