Files
zt/router/test/single_router_perf_test.go
zeekay 241625e7d9 rename: ziti → zt across all paths
- Repo names: ziti-* → zt-*
- Go modules: github.com/hanzozt/ziti → github.com/hanzozt/zt
- Docker images: hanzozt/ziti-* → hanzozt/zt-*
- C/Java/JS symbols: ziti_* → zt_*
- File and directory names updated
2026-02-18 18:34:19 -08:00

195 lines
4.9 KiB
Go

//go:build perf
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package test
import (
"encoding/binary"
"errors"
"fmt"
"io"
"sync/atomic"
"testing"
"time"
"github.com/hanzozt/channel/v4"
"github.com/hanzozt/metrics"
"github.com/hanzozt/metrics/metrics_pb"
"github.com/hanzozt/sdk-golang/xgress"
"github.com/hanzozt/zt/v2/common/pb/ctrl_pb"
"github.com/hanzozt/zt/v2/router/env"
"github.com/hanzozt/zt/v2/router/forwarder"
"github.com/hanzozt/zt/v2/router/handler_xgress"
"github.com/hanzozt/zt/v2/router/xgress_router"
"github.com/stretchr/testify/require"
)
type testSrcConn struct {
sendCount atomic.Uint32
}
func (self *testSrcConn) Close() error {
return nil
}
func (self *testSrcConn) LogContext() string {
return "test"
}
func (self *testSrcConn) ReadPayload() ([]byte, map[uint8][]byte, error) {
if self.sendCount.Load() > 1000 {
return nil, nil, io.EOF
}
count := self.sendCount.Add(1)
now := time.Now().UnixMilli()
buf := make([]byte, 10240)
binary.BigEndian.PutUint32(buf, count)
binary.BigEndian.PutUint64(buf[4:], uint64(now))
return buf, nil, nil
}
func (self *testSrcConn) WritePayload(bytes []byte, m map[uint8][]byte) (int, error) {
return len(bytes), nil
}
func (self *testSrcConn) HandleControlMsg(xgress.ControlType, channel.Headers, xgress.ControlReceiver) error {
return nil
}
type testDstConn struct {
closeNotify chan struct{}
recvCount atomic.Uint32
done atomic.Bool
notifyDone chan struct{}
}
func (self *testDstConn) waitForDone(timeout time.Duration) error {
select {
case <-self.notifyDone:
return nil
case <-time.After(timeout):
return errors.New("timed out")
}
}
func (self *testDstConn) Close() error {
return nil
}
func (self *testDstConn) LogContext() string {
return "test"
}
func (self *testDstConn) ReadPayload() ([]byte, map[uint8][]byte, error) {
<-self.closeNotify
return nil, nil, io.EOF
}
func (self *testDstConn) WritePayload(bytes []byte, m map[uint8][]byte) (int, error) {
count := binary.BigEndian.Uint32(bytes)
start := binary.BigEndian.Uint64(bytes[4:])
val := self.recvCount.Add(1)
fmt.Printf("%v/%v: %v\n", val, count, time.Now().UnixMilli()-int64(start))
if val > 1000 {
if self.done.CompareAndSwap(false, true) {
close(self.notifyDone)
}
}
return len(bytes), nil
}
func (self *testDstConn) HandleControlMsg(xgress.ControlType, channel.Headers, xgress.ControlReceiver) error {
return nil
}
type testFaultReceiver struct{}
func (t testFaultReceiver) Report(circuitId string, ctrlId string) {}
func (t testFaultReceiver) NotifyInvalidLink(linkId string) {}
type testXgCloseHandler struct{}
func (t testXgCloseHandler) HandleXgressClose(x *xgress.Xgress) {
}
type eventSink struct{}
func (e eventSink) AcceptMetrics(message *metrics_pb.MetricsMessage) {
}
func Test_SingleRouterPerf(t *testing.T) {
closeNotify := make(chan struct{})
defer close(closeNotify)
options := xgress.DefaultOptions()
srcConn := &testSrcConn{}
dstConn := &testDstConn{
closeNotify: closeNotify,
notifyDone: make(chan struct{}),
}
registryConfig := metrics.DefaultUsageRegistryConfig("router", closeNotify)
registry := metrics.NewUsageRegistry(registryConfig)
registry.StartReporting(&eventSink{}, time.Minute, 10)
fwd := forwarder.NewForwarder(registry, testFaultReceiver{}, env.DefaultForwarderOptions(), closeNotify)
dataPlaneAdapter := handler_xgress.NewXgressDataPlaneAdapter(handler_xgress.DataPlaneAdapterConfig{
Acker: xgress_router.NewAcker(fwd, registry, closeNotify),
Forwarder: fwd,
Retransmitter: xgress.NewRetransmitter(fwd, fwd, registry, closeNotify),
PayloadIngester: xgress.NewPayloadIngester(closeNotify),
Metrics: xgress.NewMetrics(registry),
})
bindHandler := handler_xgress.NewBindHandler(dataPlaneAdapter, testXgCloseHandler{}, fwd)
srcXg := xgress.NewXgress("test", "ctrl", "src", srcConn, xgress.Initiator, options, nil)
bindHandler.HandleXgressBind(srcXg)
dstXg := xgress.NewXgress("test", "ctrl", "dst", dstConn, xgress.Terminator, options, nil)
bindHandler.HandleXgressBind(dstXg)
req := require.New(t)
err := fwd.Route("ctrl", &ctrl_pb.Route{
CircuitId: "test",
Attempt: 0,
Forwards: []*ctrl_pb.Route_Forward{
{
SrcAddress: "src",
DstAddress: "dst",
DstType: ctrl_pb.DestType_End,
},
{
SrcAddress: "dst",
DstAddress: "src",
DstType: ctrl_pb.DestType_Start,
},
},
})
req.NoError(err)
dstXg.Start()
srcXg.Start()
err = dstConn.waitForDone(2 * time.Second)
req.NoError(err)
}