mirror of
https://github.com/fumiama/WireGold.git
synced 2026-06-04 23:40:26 +08:00
feat(tunnel): add benchmark
This commit is contained in:
@@ -56,6 +56,44 @@ func TestTunnelIPSmallMTU(t *testing.T) {
|
||||
testTunnelNetwork(t, "ip", 1024)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelUDP(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "udp", 4096)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelUDPSmallMTU(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "udp", 1024)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelUDPLite(b *testing.B) {
|
||||
if runtime.GOOS == "darwin" {
|
||||
return
|
||||
}
|
||||
benchmarkTunnelNetwork(b, "udplite", 4096)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelUDPLiteSmallMTU(b *testing.B) {
|
||||
if runtime.GOOS == "darwin" {
|
||||
return
|
||||
}
|
||||
benchmarkTunnelNetwork(b, "udplite", 1024)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelTCP(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "tcp", 4096)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelTCPSmallMTU(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "tcp", 1024)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelIP(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "ip", 4096)
|
||||
}
|
||||
|
||||
func BenchmarkTunnelIPSmallMTU(b *testing.B) {
|
||||
benchmarkTunnelNetwork(b, "ip", 1024)
|
||||
}
|
||||
|
||||
func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) {
|
||||
fmt.Println("start", nw, "testing")
|
||||
selfpk, err := curve.New(nil)
|
||||
@@ -215,6 +253,110 @@ func testTunnel(t *testing.T, nw string, isplain, isbase14 bool, pshk *[32]byte,
|
||||
}
|
||||
}
|
||||
|
||||
func benchmarkTunnel(b *testing.B, sz int, nw string, isplain, isbase14 bool, pshk *[32]byte, mtu uint16) {
|
||||
selfpk, err := curve.New(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
peerpk, err := curve.New(nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
epm := "127.0.0.1"
|
||||
if nw != "ip" {
|
||||
epm += ":0"
|
||||
}
|
||||
// under macos you need to run
|
||||
//
|
||||
// sudo ifconfig lo0 alias 127.0.0.2
|
||||
epp := "127.0.0.2"
|
||||
if nw != "ip" {
|
||||
epp += ":0"
|
||||
}
|
||||
|
||||
m := link.NewMe(&link.MyConfig{
|
||||
MyIPwithMask: "192.168.1.2/32",
|
||||
MyEndpoint: epm,
|
||||
Network: nw,
|
||||
PrivateKey: selfpk.Private(),
|
||||
SrcPort: 1,
|
||||
DstPort: 1,
|
||||
MTU: mtu,
|
||||
Base14: isbase14,
|
||||
})
|
||||
defer m.Close()
|
||||
|
||||
p := link.NewMe(&link.MyConfig{
|
||||
MyIPwithMask: "192.168.1.3/32",
|
||||
MyEndpoint: epp,
|
||||
Network: nw,
|
||||
PrivateKey: peerpk.Private(),
|
||||
SrcPort: 1,
|
||||
DstPort: 1,
|
||||
MTU: mtu,
|
||||
Base14: isbase14,
|
||||
})
|
||||
defer p.Close()
|
||||
|
||||
ppp := peerpk.Public()
|
||||
spp := selfpk.Public()
|
||||
if isplain {
|
||||
ppp = nil
|
||||
spp = nil
|
||||
}
|
||||
|
||||
m.AddPeer(&link.PeerConfig{
|
||||
PeerIP: "192.168.1.3",
|
||||
EndPoint: p.EndPoint().String(),
|
||||
AllowedIPs: []string{"192.168.1.3/32"},
|
||||
PubicKey: ppp,
|
||||
PresharedKey: pshk,
|
||||
MTU: mtu,
|
||||
MTURandomRange: mtu / 2,
|
||||
UseZstd: true,
|
||||
DoublePacket: true,
|
||||
})
|
||||
p.AddPeer(&link.PeerConfig{
|
||||
PeerIP: "192.168.1.2",
|
||||
EndPoint: m.EndPoint().String(),
|
||||
AllowedIPs: []string{"192.168.1.2/32"},
|
||||
PubicKey: spp,
|
||||
PresharedKey: pshk,
|
||||
MTU: mtu,
|
||||
MTURandomRange: mtu / 2,
|
||||
UseZstd: true,
|
||||
})
|
||||
tunnme, err := Create(&m, "192.168.1.3")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
tunnme.Start(1, 1, 4096)
|
||||
tunnpeer, err := Create(&p, "192.168.1.2")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
tunnpeer.Start(1, 1, 4096)
|
||||
|
||||
time.Sleep(time.Second) // wait link up
|
||||
|
||||
b.SetBytes(int64(sz))
|
||||
b.ResetTimer()
|
||||
sendb := make([]byte, sz)
|
||||
for i := 0; i < b.N; i++ {
|
||||
rand.Read(sendb)
|
||||
go tunnme.Write(sendb)
|
||||
buf := make([]byte, sz)
|
||||
_, err = io.ReadFull(&tunnpeer, buf)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
}
|
||||
b.StopTimer()
|
||||
tunnme.Stop()
|
||||
tunnpeer.Stop()
|
||||
}
|
||||
|
||||
func testTunnelNetwork(t *testing.T, nw string, mtu uint16) {
|
||||
logrus.SetLevel(logrus.DebugLevel)
|
||||
logrus.SetFormatter(&logFormat{enableColor: false})
|
||||
@@ -239,6 +381,38 @@ func testTunnelNetwork(t *testing.T, nw string, mtu uint16) {
|
||||
testTunnel(t, nw, false, true, &buf, mtu) // test preshared
|
||||
}
|
||||
|
||||
func benchmarkTunnelNetwork(b *testing.B, nw string, mtu uint16) {
|
||||
logrus.SetLevel(logrus.ErrorLevel)
|
||||
logrus.SetFormatter(&logFormat{enableColor: false})
|
||||
|
||||
for i := 1; i <= 4; i++ {
|
||||
sz := 1024 * i
|
||||
b.Run(fmt.Sprintf("%d-plain-nob14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, true, false, nil, mtu) // test plain text
|
||||
})
|
||||
b.Run(fmt.Sprintf("%d-normal-nob14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, false, false, nil, mtu) // test normal
|
||||
})
|
||||
b.Run(fmt.Sprintf("%d-plain-b14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, true, true, nil, mtu) // test plain text
|
||||
})
|
||||
b.Run(fmt.Sprintf("%d-normal-b14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, false, true, nil, mtu) // test normal
|
||||
})
|
||||
var buf [32]byte
|
||||
_, err := rand.Read(buf[:])
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
b.Run(fmt.Sprintf("%d-preshared-nob14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, false, false, &buf, mtu) // test preshared
|
||||
})
|
||||
b.Run(fmt.Sprintf("%d-preshared-b14", sz), func(b *testing.B) {
|
||||
benchmarkTunnel(b, sz, nw, false, true, &buf, mtu) // test preshared
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// logFormat specialize for go-cqhttp
|
||||
type logFormat struct {
|
||||
enableColor bool
|
||||
|
||||
Reference in New Issue
Block a user