當一個 goroutine 創建新的 goroutine 時,scheduler 會選誰優先執行?

  1. consume function 接收一個 input channel,並創建一個 goroutine 去 select input channel。
  2. produce function 接收一個 output channel,隨意產生一些數學運算並將結果傳入 output channel。
  3. BenchmarkProducerFirst 會先將 buffer 丟給 produce function 執行100000 次,再將 buffer channel 傳給 consume 消化。
  4. BenchmarkComsumerFirst 則相反,buffer channel 先丟給 consume 消化 1000 次,再呼叫 produce 去填充 buffer。
BenchmarkProducerFirst-4        2000000000               0.06 ns/op
BenchmarkComsumerFirst-4 2000000000 0.15 ns/op
Scheduler latency profile of BenchmarkProducerFirst
Scheduler latency profile of BenchmarkComsumerFirst

--

--

--

I am Genchi, a backend engineer in Taiwan.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Genchi Lu

Genchi Lu

I am Genchi, a backend engineer in Taiwan.

More from Medium

Use pprof to view go program stack traces

Druid complex Lookup using other dimensions, with native query or Golang

Golang — basic unit testing and benchmarking