Skip to content

Instantly share code, notes, and snippets.

@hulucc
Created May 24, 2021 09:57
Show Gist options
  • Save hulucc/1f90041e0a1785316aae845fe34f32c1 to your computer and use it in GitHub Desktop.
Save hulucc/1f90041e0a1785316aae845fe34f32c1 to your computer and use it in GitHub Desktop.
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