1
0
mirror of https://github.com/golang/go synced 2024-11-12 09:20:22 -07:00

net: add benchmarks for network interface identification

Current results on linux/amd64:
BenchmarkInterfaces                      20000             80902 ns/op
BenchmarkInterfaceByIndex                50000             71591 ns/op
BenchmarkInterfaceByName                 20000             79908 ns/op
BenchmarkInterfaceAddrs                   2000            836413 ns/op
BenchmarkInterfacesAndAddrs               5000            605946 ns/op
BenchmarkInterfacesAndMulticastAddrs     10000            169029 ns/op

Update #4866.

R=golang-dev, bradfitz
CC=golang-dev
https://golang.org/cl/7368046
This commit is contained in:
Mikio Hara 2013-02-22 01:19:04 +09:00
parent 867121585a
commit 0ad88a481d

View File

@ -9,6 +9,21 @@ import (
"testing"
)
// LoopbackInterface returns a logical network interface for loopback
// tests.
func loopbackInterface() *Interface {
ift, err := Interfaces()
if err != nil {
return nil
}
for _, ifi := range ift {
if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 {
return &ifi
}
}
return nil
}
func sameInterface(i, j *Interface) bool {
if i == nil || j == nil {
return false
@ -29,10 +44,10 @@ func TestInterfaces(t *testing.T) {
for _, ifi := range ift {
ifxi, err := InterfaceByIndex(ifi.Index)
if err != nil {
t.Fatalf("InterfaceByIndex(%q) failed: %v", ifi.Index, err)
t.Fatalf("InterfaceByIndex(%v) failed: %v", ifi.Index, err)
}
if !sameInterface(ifxi, &ifi) {
t.Fatalf("InterfaceByIndex(%q) = %v, want %v", ifi.Index, *ifxi, ifi)
t.Fatalf("InterfaceByIndex(%v) = %v, want %v", ifi.Index, *ifxi, ifi)
}
ifxn, err := InterfaceByName(ifi.Name)
if err != nil {
@ -102,3 +117,67 @@ func testMulticastAddrs(t *testing.T, ifmat []Addr) {
}
}
}
func BenchmarkInterfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := Interfaces(); err != nil {
b.Fatalf("Interfaces failed: %v", err)
}
}
}
func BenchmarkInterfaceByIndex(b *testing.B) {
ifi := loopbackInterface()
if ifi == nil {
return
}
for i := 0; i < b.N; i++ {
if _, err := InterfaceByIndex(ifi.Index); err != nil {
b.Fatalf("InterfaceByIndex failed: %v", err)
}
}
}
func BenchmarkInterfaceByName(b *testing.B) {
ifi := loopbackInterface()
if ifi == nil {
return
}
for i := 0; i < b.N; i++ {
if _, err := InterfaceByName(ifi.Name); err != nil {
b.Fatalf("InterfaceByName failed: %v", err)
}
}
}
func BenchmarkInterfaceAddrs(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := InterfaceAddrs(); err != nil {
b.Fatalf("InterfaceAddrs failed: %v", err)
}
}
}
func BenchmarkInterfacesAndAddrs(b *testing.B) {
ifi := loopbackInterface()
if ifi == nil {
return
}
for i := 0; i < b.N; i++ {
if _, err := ifi.Addrs(); err != nil {
b.Fatalf("Interface.Addrs failed: %v", err)
}
}
}
func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
ifi := loopbackInterface()
if ifi == nil {
return
}
for i := 0; i < b.N; i++ {
if _, err := ifi.MulticastAddrs(); err != nil {
b.Fatalf("Interface.MulticastAddrs failed: %v", err)
}
}
}