mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 19:43:44 +00:00
add goroutine benchmark to examples
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const N = 500000
|
||||
const Ngoro = 4
|
||||
|
||||
func main() {
|
||||
start := time.Now()
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(Ngoro)
|
||||
for i := 0; i < Ngoro; i++ {
|
||||
go adder(&wg, N)
|
||||
}
|
||||
wg.Wait()
|
||||
elapsed := time.Since(start)
|
||||
goroutineCtxSwitchOverhead := (elapsed / (Ngoro * N)).String()
|
||||
|
||||
elapsedstr := elapsed.String()
|
||||
machine.LED.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
for {
|
||||
println("bench:", elapsedstr, "goroutine ctx switch:", goroutineCtxSwitchOverhead)
|
||||
machine.LED.High()
|
||||
time.Sleep(elapsed)
|
||||
machine.LED.Low()
|
||||
time.Sleep(elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func adder(wg *sync.WaitGroup, num int) {
|
||||
for i := 0; i < num; i++ {
|
||||
runtime.Gosched()
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
Reference in New Issue
Block a user