Created
May 24, 2021 09:57
-
-
Save hulucc/1f90041e0a1785316aae845fe34f32c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cache | |
import ( | |
"net" | |
"testing" | |
) | |
func BenchmarkChan(b *testing.B) { | |
ch := make(chan []byte) | |
go func() { | |
for { | |
select { | |
case b := <-ch: | |
_ = b | |
} | |
} | |
}() | |
buffer := []byte{0} | |
for i := 0; i < b.N; i++ { | |
ch <- buffer | |
} | |
} | |
func BenchmarkChan1(b *testing.B) { | |
l, err := net.ListenPacket("udp4", "0.0.0.0:0") | |
if err != nil { | |
panic(err) | |
} | |
addr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:4321") | |
if err != nil { | |
panic(err) | |
} | |
buffer := []byte{0} | |
for i := 0; i < b.N; i++ { | |
l.WriteTo(buffer, addr) | |
} | |
} | |
func BenchmarkChan3(b *testing.B) { | |
l, err := net.ListenPacket("udp4", "0.0.0.0:0") | |
if err != nil { | |
panic(err) | |
} | |
addr, err := net.ResolveUDPAddr("udp4", "127.0.0.1:4321") | |
if err != nil { | |
panic(err) | |
} | |
ch := make(chan []byte) | |
go func() { | |
for { | |
select { | |
case b := <-ch: | |
l.WriteTo(b, addr) | |
} | |
} | |
}() | |
buffer := []byte{0} | |
for i := 0; i < b.N; i++ { | |
ch <- buffer | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment