210 lines
4.3 KiB
Go
210 lines
4.3 KiB
Go
package dynamics
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"fmt"
|
|
"github.com/panjf2000/gnet/v2"
|
|
"github.com/spf13/viper"
|
|
"io"
|
|
"joylink.club/bj-rtsts-server/config"
|
|
"net"
|
|
"runtime"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRunUdpServer(t *testing.T) {
|
|
viper.AddConfigPath("../")
|
|
config.LoadConfig()
|
|
RegisterTrainInfoHandler(func(info *TrainInfo) {
|
|
fmt.Println("处理列车信息...")
|
|
fmt.Println(info)
|
|
})
|
|
RunUdpServer()
|
|
}
|
|
|
|
func TestSendTurnoutInfo(t *testing.T) {
|
|
viper.AddConfigPath("../")
|
|
config.LoadConfig()
|
|
tick := time.Tick(50 * time.Millisecond)
|
|
for range tick {
|
|
for i := 1; i <= 9; i++ {
|
|
SendTurnoutInfo(&TurnoutInfo{
|
|
Code: uint16(i),
|
|
NPosition: true,
|
|
RPosition: false,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkUdpServer_OnTraffic(b *testing.B) {
|
|
runtime.GOMAXPROCS(1) //1个协程
|
|
buf, _ := hex.DecodeString("0009012EE009000000070380000006000100020003000A000A000A000A000A000A000A")
|
|
// 创建 udpServer 对象
|
|
server := &udpServer{}
|
|
// 创建虚拟的 gnet.Conn 对象
|
|
conn := &virtualConn{
|
|
readBuffer: buf,
|
|
}
|
|
|
|
// 在 Benchmark 中运行测试
|
|
for i := 0; i < b.N; i++ {
|
|
server.OnTraffic(conn)
|
|
}
|
|
}
|
|
|
|
type virtualConn struct {
|
|
readBuffer []byte
|
|
}
|
|
|
|
func (vc *virtualConn) Read(p []byte) (n int, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Write(p []byte) (n int, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Context() (ctx interface{}) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetContext(ctx interface{}) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Close() (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) WriteTo(w io.Writer) (n int64, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Next(n int) (buf []byte, err error) {
|
|
//TODO implement me
|
|
return vc.readBuffer, nil
|
|
}
|
|
|
|
func (vc *virtualConn) Peek(n int) (buf []byte, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Discard(n int) (discarded int, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) InboundBuffered() (n int) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) ReadFrom(r io.Reader) (n int64, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Writev(bs [][]byte) (n int, err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Flush() (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) OutboundBuffered() (n int) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) AsyncWrite(buf []byte, callback gnet.AsyncCallback) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) AsyncWritev(bs [][]byte, callback gnet.AsyncCallback) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Fd() int {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Dup() (int, error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetReadBuffer(bytes int) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetWriteBuffer(bytes int) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetLinger(sec int) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetKeepAlivePeriod(d time.Duration) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetNoDelay(noDelay bool) error {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) LocalAddr() (addr net.Addr) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) RemoteAddr() (addr net.Addr) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) Wake(callback gnet.AsyncCallback) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) CloseWithCallback(callback gnet.AsyncCallback) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetDeadline(t time.Time) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetReadDeadline(t time.Time) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|
|
|
|
func (vc *virtualConn) SetWriteDeadline(t time.Time) (err error) {
|
|
//TODO implement me
|
|
panic("implement me")
|
|
}
|