WhyCan Forum

人过留名,雁过留声,感谢各位朋友不离不弃地支持。 QQ: 516333132 (挖坑网/填坑网) admin@whycan.cn

您尚未登录。

#1 2019-11-08 00:28:12

kekemuyu
会员
注册时间: 2018-12-13
累计积分: 323

燃烧吧cpu(go语言版cpuburn)

适用于能交叉编译go的各种平台,原来cpuburn就是让cpu傻傻空转。
https://github.com/patrickmn/cpuburn/blob/master/cpuburn.go

package main

import (
	"flag"
	"fmt"
	"runtime"
	"time"
)

var (
	numBurn        int
	updateInterval int
)

func cpuBurn() {
	for {
		for i := 0; i < 2147483647; i++ {
		}
		runtime.Gosched()
	}
}

func init() {
	flag.IntVar(&numBurn, "n", 0, "number of cores to burn (0 = all)")
	flag.IntVar(&updateInterval, "u", 10, "seconds between updates (0 = don't update)")
	flag.Parse()
	if numBurn <= 0 {
		numBurn = runtime.NumCPU()
	}
}

func main() {
	runtime.GOMAXPROCS(numBurn)
	fmt.Printf("Burning %d CPUs/cores\n", numBurn)
	for i := 0; i < numBurn; i++ {
		go cpuBurn()
	}
	if updateInterval > 0 {
		t := time.Tick(time.Duration(updateInterval) * time.Second)
		for secs := updateInterval; ; secs += updateInterval {
			<-t
			fmt.Printf("%d seconds\n", secs)
		}
	} else {
		select {} // wait forever
	}
}

在线

#2 2019-11-08 08:11:23

晕哥
Administrator
注册时间: 2017-09-06
累计积分: 8,230

Re: 燃烧吧cpu(go语言版cpuburn)

还有这种操作 ^_^

离线

页脚