mirror of
https://github.com/lzh-1625/go_process_manager.git
synced 2025-09-27 12:22:13 +08:00
45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package cgroup_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
_ "github.com/lzh-1625/go_process_manager/boot"
|
|
"github.com/lzh-1625/go_process_manager/internal/app/eum"
|
|
"github.com/lzh-1625/go_process_manager/internal/app/logic"
|
|
"github.com/lzh-1625/go_process_manager/internal/app/model"
|
|
|
|
"github.com/containerd/cgroups/v3/cgroup1"
|
|
"github.com/opencontainers/runtime-spec/specs-go"
|
|
)
|
|
|
|
func TestCgroup(t *testing.T) {
|
|
period := uint64(100000) // 100ms = 100000微秒
|
|
// 设置 CPU 配额为 20% (20ms)
|
|
quota := int64(20000 * 8) // 20ms = 20000微秒
|
|
control, err := cgroup1.New(cgroup1.StaticPath("/test"), &specs.LinuxResources{
|
|
CPU: &specs.LinuxCPU{
|
|
Period: &period,
|
|
Quota: "a,
|
|
},
|
|
Memory: &specs.LinuxMemory{},
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer control.Delete()
|
|
p, err := logic.ProcessCtlLogic.RunNewProcess(model.Process{
|
|
Name: "test",
|
|
Cmd: "bash",
|
|
Cwd: `/root`,
|
|
TermType: eum.TerminalPty,
|
|
})
|
|
if err != nil {
|
|
t.FailNow()
|
|
}
|
|
control.AddProc(uint64(p.Pid))
|
|
time.Sleep(time.Second * 20)
|
|
p.Kill()
|
|
control.Delete()
|
|
}
|