Skip to content

Commit

Permalink
feature: support eat -c 35% -m 35%
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn-bluce committed Jul 4, 2024
1 parent 8f53a1e commit 8c66aa2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@

Developer will encounter the need to quickly occupy CPU and memory, I am also deeply troubled, so I developed a tool named `eat` to help you quickly occupy a specified amount of CPU and memory.

# Todo

- [x] Support `eat -c 35%` and `eat -m 35%`
- [] CPU Affinity
- [] Memory read/write, prevent memory from being swapped out
- [] Dynamic adjustment of CPU and memory usage
- [] Eat GPU

# Usage

```shell
eat -c 4 # eating 4 CPU core
eat -c 35% # eating 35% CPU core (CPU count * 35%)
eat -c 100% # eating all CPU core
eat -m 4g # eating 4GB memory
eat -m 20m # eating 20MB memory
eat -m 35% # eating 35% memory (total memory * 35%)
eat -m 100% # eating all memory
eat -c 2.5 -m 1.5g # eating 2.5 CPU core and 1.5GB memory
eat -c 3 -m 200m # eating 3 CPU core and 200MB memory
Expand All @@ -30,13 +40,23 @@ go build -o eat

开发者们经常会遇到需要快速占用 CPU 和内存的需求,我也是。所以我开发了一个名为 `eat` 的小工具来快速占用指定数量的 CPU 和内存。

# 待办

- [x] 支持`eat -c 35%``eat -m 35%`
- [] CPU亲和性
- [] 内存读写,防止内存被交换出去
- [] 动态调整CPU和内存使用
- [] 吃GPU

# 使用

```shell
eat -c 4 # 占用4个CPU核
eat -c 35% # 占用35%CPU核(CPU核数 * 35%)
eat -c 100% # 占用所有CPU核
eat -m 4g # 占用4GB内存
eat -m 20m # 占用20MB内存
eat -m 35% # 占用35%内存(总内存 * 35%)
eat -m 100% # 占用所有内存
eat -c 2.5 -m 1.5g # 占用2.5个CPU核和1.5GB内存
eat -c 3 -m 200m # 占用3个CPU核和200MB内存
Expand Down
18 changes: 18 additions & 0 deletions cmd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ func parseEatCPUCount(c string) float64 {
if c == "100%" {
return float64(runtime.NumCPU())
} else {
if len(c) > 1 && (c[len(c)-1] == '%') {
cEat, err := strconv.ParseFloat(c[:len(c)-1], 32)
if err != nil {
fmt.Println("Error: invalid cpu count")
return 0
} else {
return cEat / 100 * float64(runtime.NumCPU())
}
}

cEat, err := strconv.ParseFloat(c, 32)
if err != nil {
fmt.Println("Error: invalid cpu count")
Expand Down Expand Up @@ -46,6 +56,14 @@ func parseEatMemoryBytes(m string) uint64 {
return mEatBytes * 1024
}
}

// process percent
if len(m) > 1 && m[len(m)-1] == '%' {
mEatPercent, err := strconv.ParseFloat(m[:len(m)-1], 32)
if err == nil {
return uint64(float64(memory.TotalMemory()) * mEatPercent / 100)
}
}
}
return 0
}
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func eatFunction(cmd *cobra.Command, args []string) {
cEat := parseEatCPUCount(c)
mEat := parseEatMemoryBytes(m)

fmt.Printf("Want to eat %2.1fC%s.\n", cEat, m)
fmt.Printf("Want to eat %2.1fCPU, %s Memory\n", cEat, m)
eatMemory(mEat)
eatCPU(cEat)
waitForever()
Expand Down

0 comments on commit 8c66aa2

Please sign in to comment.