Skip to content

Instantly share code, notes, and snippets.

@mvitaly
Last active February 3, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvitaly/12dbdf608171e9808436efb03de67499 to your computer and use it in GitHub Desktop.
Save mvitaly/12dbdf608171e9808436efb03de67499 to your computer and use it in GitHub Desktop.
$ go test -bench=.
goos: darwin
goarch: amd64
pkg: gopl.io/ch1/ex-ext
BenchmarkEmptySelect-4 300000000 4.69 ns/op
BenchmarkEmptyLen-4 2000000000 0.58 ns/op
BenchmarkEmptyLenThenSelect-4 2000000000 0.59 ns/op
BenchmarkFullSelect-4 30000000 39.0 ns/op
BenchmarkFullLen-4 200000000 11.2 ns/op
BenchmarkFullLenThenSelect-4 200000000 10.2 ns/op
Benchmark2ChanFullSelect-4 10000000 160 ns/op
Benchmark2ChanFullLenThenSelect-4 200000000 35.1 ns/op
PASS
ok gopl.io/ch1/ex-ext 21.310s
package main
// Run with:
// go test -bench=.
// Inspired by:
// https://stackoverflow.com/questions/3398490/checking-if-a-channel-has-a-ready-to-read-value-using-go
// https://gist.github.com/phemmer/428685f5366c4b71b39d1037e6124a73
import (
"fmt"
"testing"
"time"
)
var chEmpty = make(chan int, 1000)
var chFull = make(chan int, 1000)
var chFull2 = make(chan int, 1000)
func init() {
go func() {
time.Sleep(time.Hour)
chEmpty <- 0
}()
go func() {
for {
chFull <- 1
}
}()
go func() {
for {
chFull2 <- 1
}
}()
}
func BenchmarkEmptySelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
select {
case v := <-chEmpty:
val++
if val > 1000000 {
val = 0
fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
func BenchmarkEmptyLen(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
if len(chEmpty) != 0 {
v := <-chEmpty
val++
if val > 1000000 {
val = 0
fmt.Printf("have value: %v\n", v)
}
}
}
}
func BenchmarkEmptyLenThenSelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
if len(chEmpty) != 0 {
select {
case v := <-chEmpty:
val++
if val > 1000000 {
val = 0
fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
}
func BenchmarkFullSelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
select {
case <-chFull:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
func BenchmarkFullLen(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
if len(chFull) != 0 {
<-chFull
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
}
}
}
func BenchmarkFullLenThenSelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
if len(chFull) != 0 {
select {
case <-chFull:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
}
func Benchmark2ChanFullSelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
select {
case <-chFull:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
case <-chFull2:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
func Benchmark2ChanFullLenThenSelect(b *testing.B) {
val := 0
for n := 0; n < b.N; n++ {
if len(chFull) != 0 || len(chFull2) != 0 {
select {
case <-chFull:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
case <-chFull2:
val++
if val > 1000000 {
val = 0
//fmt.Printf("have value: %v\n", v)
}
default:
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment