wgcfg: add benchmark for FromUAPI

Adds a benchmark for FromUAPI in wgcfg.
It appears that it's not actually that slow, the main allocations are from the scanner and new
config.
Updates #1912.

Signed-off-by: julianknodt <julianknodt@gmail.com>
josh/deflake-pipe-again
julianknodt 3 years ago committed by Julian Knodt
parent 5b845631ce
commit 664edbe566

@ -175,7 +175,7 @@ func (cfg *Config) handlePeerLine(peer *Peer, key, value string, valueBytes []by
if value != "1" { if value != "1" {
return fmt.Errorf("invalid protocol version: %v", value) return fmt.Errorf("invalid protocol version: %v", value)
} }
case "preshared_key", "last_handshake_time_sec", "last_handshake_time_nsec", "tx_bytes", "rx_bytes": case "replace_allowed_ips", "preshared_key", "last_handshake_time_sec", "last_handshake_time_nsec", "tx_bytes", "rx_bytes":
// ignore // ignore
default: default:
return fmt.Errorf("unexpected IpcGetOperation key: %v", key) return fmt.Errorf("unexpected IpcGetOperation key: %v", key)

@ -5,9 +5,14 @@
package wgcfg package wgcfg
import ( import (
"bufio"
"bytes"
"reflect" "reflect"
"runtime" "runtime"
"testing" "testing"
"inet.af/netaddr"
"tailscale.com/types/wgkey"
) )
func noError(t *testing.T, err error) bool { func noError(t *testing.T, err error) bool {
@ -53,3 +58,42 @@ func TestParseEndpoint(t *testing.T) {
t.Error("Error was expected") t.Error("Error was expected")
} }
} }
func BenchmarkFromUAPI(b *testing.B) {
newPrivateKey := func() (wgkey.Key, wgkey.Private) {
b.Helper()
pk, err := wgkey.NewPrivate()
if err != nil {
b.Fatal(err)
}
return wgkey.Key(pk.Public()), wgkey.Private(pk)
}
k1, pk1 := newPrivateKey()
ip1 := netaddr.MustParseIPPrefix("10.0.0.1/32")
peer := Peer{
PublicKey: k1,
AllowedIPs: []netaddr.IPPrefix{ip1},
Endpoints: Endpoints{PublicKey: k1},
}
cfg1 := &Config{
PrivateKey: wgkey.Private(pk1),
Peers: []Peer{peer, peer, peer, peer},
}
buf := new(bytes.Buffer)
w := bufio.NewWriter(buf)
if err := cfg1.ToUAPI(w, &Config{}); err != nil {
b.Fatal(err)
}
w.Flush()
r := bytes.NewReader(buf.Bytes())
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := FromUAPI(r)
if err != nil {
b.Errorf("failed from UAPI: %v", err)
}
r.Seek(0, 0)
}
}

Loading…
Cancel
Save