Golang 性能测试,发现多核机器一直不能跑满,单核机器可以跑满,集合https://blog.csdn.net/liangzhiyang/article/details/52669851?utm_source=blogxgwz3 的调度理论,想看下某个goroutine 一秒内是否会被切走,如果被切走,就说明go在调度时会对每个核“负载均衡”。
linux 环境获取threadID的方法如下:
package main
import (
"fmt"
"golang.org/x/sys/unix"
"time"
)
func see(s string) {
//runtime.LockOSThread()
a := time.Now()
for i := 1; i < 20000; i++ {
//runtime.Gosched()
var sum int
sum = 1
sum = sum * i
fmt.Println("sum:", sum)
fmt.Println("s:", s)
getid("see " + s)
//fmt.Println("cpus:", runtime.StackRecord{})
//fmt.Println("cpus:", runtime.LockOSThread)
}
fmt.Println(time.Since(a))
}
func getid(s string) {
pid := unix.Getpid()
fmt.Println(s+" Getpid", pid)
fmt.Println(s+" Getppid", unix.Getppid())
pgid, _ := unix.Getpgid(pid)
fmt.Println(s+" Getpgid", pgid)
fmt.Println(s+" Gettid", unix.Gettid())
sid, _ := unix.Getsid(pid)
fmt.Println(s+" Getsid", sid)
fmt.Println(s+" Getegid", unix.Getegid())
fmt.Println(s+" Geteuid", unix.Geteuid())
fmt.Println(s+" Getgid", unix.Getgid())
fmt.Println(s+" Getuid", unix.Getuid())
}
func main() {
go see("hello")
go see("world")
getid("main")
time.Sleep(20 * time.Second)
}
运行结果:
main Getpid 15413
main Getppid 15361
main Getpgid 15361
main Gettid 15413
main Getsid 15333
main Getegid 1000
main Geteuid 1000
main Getgid 1000
main Getuid 1000
sum: 1
s: hello
see hello Getpid 15413
see hello Getppid 15361
see hello Getpgid 15361
see hello Gettid 15416
see hello Getsid 15333
see hello Getegid 1000
see hello Geteuid 1000
see hello Getgid 1000
see hello Getuid 1000
73.762µs
sum: 1
s: world
see world Getpid 15413
see world Getppid 15361
see world Getpgid 15361
see world Gettid 15415
see world Getsid 15333
see world Getegid 1000
see world Geteuid 1000
see world Getgid 1000
see world Getuid 1000
119.73µs
说明:window平台获取线程ID方法:
windows.GetCurrentThreadId()