5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-12-23 12:15:39 +00:00
yggdrasil-go/misc/tests/channelbenchmark.go

37 lines
697 B
Go
Raw Normal View History

2017-12-29 04:16:20 +00:00
package main
import "time"
import "fmt"
import "sync"
func main() {
2018-01-04 22:37:51 +00:00
fmt.Println("Testing speed of recv+send loop")
const count = 10000000
c := make(chan []byte, 1)
c <- []byte{}
var wg sync.WaitGroup
worker := func() {
for idx := 0; idx < count; idx++ {
p := <-c
select {
case c <- p:
default:
}
}
wg.Done()
}
nIter := 0
start := time.Now()
for idx := 0; idx < 1; idx++ {
go worker()
nIter += count
wg.Add(1)
}
wg.Wait()
stop := time.Now()
timed := stop.Sub(start)
fmt.Printf("%d iterations in %s\n", nIter, timed)
fmt.Printf("%f iterations per second\n", float64(nIter)/timed.Seconds())
fmt.Printf("%s per iteration\n", timed/time.Duration(nIter))
2017-12-29 04:16:20 +00:00
}