人为提升服务器CPU、内存、硬盘使用率
一、CPU使用率
vikyd/go-cpu-load: Generate CPU load on Windows/Linux/Mac (github.com)
所有CPU核心负载30%运行10秒钟
./cpu -p 30 -t 10
所有CPU核心负载30%持续运行
./cpu -p 30
后台运行,CPU使用率不低于20%
nohup ./cpu -p 21 >> cpu.log 2>&1 &
只让2个CPU核心负载30%运行10秒钟
./cpu -p 30 -c 2 -t 10
all CPU load
= (num of parac
_ num ofp
) / (all cores count of CPU _ 100)- may not specify cores run the load only, it just promise the
all CPU load
, and not promise each cores run the same load
参数
--coresCount value, -c value how many cores (optional, default: 8) --timeSeconds value, -t value how long (optional, default: 2147483647) --percentage value, -p value percentage of each specify cores (required) --help, -h show help
源代码
package main import ( "log" "os" "runtime" cli "github.com/urfave/cli/v2" ) // EINVAL Error Code: #define EINVAL 22 /* Invalid argument */ const EINVAL = 23 const maxInt32 = 2147483647 const unLimitedTime = maxInt32 const missPercentageVal = 0 func main() { var coresCount int var timeSeconds int var percentage int app := cli.NewApp() // app.Version = "0.0.2" app.Flags = []cli.Flag{ &cli.IntFlag{ Name: "coresCount", Aliases: []string{"c"}, Value: runtime.NumCPU(), Usage: "how many cores", Destination: &coresCount, }, &cli.IntFlag{ Name: "timeSeconds", Aliases: []string{"t"}, Value: unLimitedTime, Usage: "how long", Destination: &timeSeconds, }, &cli.IntFlag{ Name: "percentage", Aliases: []string{"p"}, Value: missPercentageVal, Usage: "percentage of each specify cores", Destination: &percentage, }, } app.Action = func(c *cli.Context) error { // fmt.Println("coresCount: ", coresCount) // fmt.Println("timeSeconds: ", timeSeconds) // fmt.Println("percentage: ", percentage) // fmt.Println("------") if coresCount < 1 || coresCount > runtime.NumCPU() { return cli.NewExitError("coresCount not correct must between 1 - `max CPU cores`", EINVAL) } if timeSeconds <= 0 { return cli.NewExitError("timeSeconds not correct must be positive int", EINVAL) } if percentage <= 0 || percentage > 100 { return cli.NewExitError("percentage must between 1 - 100", EINVAL) } RunCPULoad(coresCount, timeSeconds, percentage) return nil } err := app.Run(os.Args) if err != nil { log.Fatal(err) } }
二、内存使用率
快速进行内存泄露至2GB
$ ./memory -d 5 -l 2048 Leaked: 2048 MiB ███████▒▒▒ Holding at 2048 MiB
后台运行内存泄露至13GB
nohup ./memory -l 13312 >> memory.log 2>&1 &
参数
./memory [-d <泄露延迟 ms; 默认 100>] -d int 选项: 调整泄漏率的延迟(毫秒); 默认 100ms -l int 选项: 内存泄漏上限 (MiB); 默认无上限
源代码
// A utility simulates a memory leak for testing, diagnostic purposes package main import ( "flag" "fmt" "os" "runtime" "runtime/debug" "time" "github.com/briandowns/spinner" ) func main() { // define and set default command parameter flags var dFlag = flag.Int("d", 100, "Optional: delay is ms to adjust the leak rate; default is 100") var lFlag = flag.Int("l", 1048576, "Optional: limit the leak to this many MiBs") var hFlag = flag.Bool("h", false, "print usage information") // usage function that's executed if a required flag is missing or user asks for help (-h) flag.Usage = func() { fmt.Fprintf(os.Stderr, "\nUsage: %s [-d <leak delay in ms; deafaults to 100> -l <leak limit in MiB>]\n", os.Args[0]) flag.PrintDefaults() fmt.Println() } flag.Parse() //provide help (-h) if *hFlag == true { flag.Usage() os.Exit(0) } // a channel to use a hold when the memory limit it reached hold := make(chan bool) // a spinner that displays how much memory has leaked and when it holding go func(hold chan bool, lFlag int) { s := spinner.New(spinner.CharSets[35], 250*time.Millisecond) for { mem := memUsage() s.Prefix = fmt.Sprintf("Leaked: %d MiB ", mem) s.Start() s.Color("magenta") time.Sleep(1 * time.Second) s.Restart() // if we've reached the limit, update display and hold if mem >= uint64(lFlag) { s.Color("green") s.Prefix = fmt.Sprintf("Holding at %d MiB ", mem) s.UpdateCharSet(spinner.CharSets[28]) s.UpdateSpeed(1 * time.Second) s.Restart() <-hold } } }(hold, *lFlag) // Although the "leak" var should contiue to grow, the GC is somehow getting in the way, disabling debug.SetGCPercent(-1) var leak string KB := `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ` // start leaking indefinitely unless a limit has been provided and met for { leak += KB time.Sleep(time.Duration(*dFlag) * time.Millisecond) mem := memUsage() // if we've reached the limit, hold if mem >= uint64(*lFlag) { <-hold } } } func memUsage() uint64 { var m runtime.MemStats runtime.ReadMemStats(&m) return bToMb(m.Alloc) } func bToMb(b uint64) uint64 { return b / 1024 / 1024 }