Exemplo n.º 1
0
// This is an benchmark tool work with the echo_server.
//
// Start echo_server with 'bench' flag
//     go run echo_server.go -bench
//
// Start benchmark with echo_server address
//     go run echo_benchmark.go
//     go run echo_benchmark.go -num=100
//     go run echo_benchmark.go -size=1024
//     go run echo_benchmark.go -time=20
//     go run echo_benchmark.go -addr="127.0.0.1:10010"
func main() {
	flag.Parse()

	if MultiProcess() {
		return
	}

	var (
		msg       = make([]byte, *messageSize)
		timeout   = time.Now().Add(time.Second * time.Duration(*runTime))
		initWait  = new(sync.WaitGroup)
		startChan = make(chan int)
		conns     = make([]*CountConn, 0, *clientNum)
	)

	var codecType link.CodecType

	if *pro {
		pool := binary.NewBufferPool(2, 1, 32)
		codecType = link.PacketPro(2, *messageSize, *messageSize*2, link.LittleEndian, pool, TestCodec{})
	} else {
		codecType = link.Packet(2, *messageSize, *messageSize*2, link.LittleEndian, TestCodec{})
	}

	for i := 0; i < *clientNum; i++ {
		conn, err := net.DialTimeout("tcp", *serverAddr, time.Second*3)
		if err != nil {
			panic(err)
		}

		countConn := &CountConn{Conn: conn}
		conns = append(conns, countConn)

		initWait.Add(2)
		go client(initWait, countConn, startChan, timeout, msg, codecType)
	}
	initWait.Wait()
	close(startChan)

	time.Sleep(time.Second * time.Duration(*runTime))
	var sum CountConn
	for i := 0; i < *clientNum; i++ {
		conn := conns[i]
		conn.Conn.Close()
		sum.SendCount += conn.SendCount
		sum.RecvCount += conn.RecvCount
		sum.ReadCount += conn.ReadCount
		sum.WriteCount += conn.WriteCount
	}
	fmt.Printf(OutputFormat, sum.SendCount, sum.RecvCount, sum.ReadCount, sum.WriteCount)
}
Exemplo n.º 2
0
func main() {
	var addr string
	var pro bool

	flag.StringVar(&addr, "addr", ":10010", "echo server address")
	flag.BoolVar(&pro, "pro", true, "use PacketPro()")
	flag.Parse()

	var codecType link.CodecType

	if pro {
		pool := binary.NewBufferPool(2, 1, 32)
		codecType = link.PacketPro(2, 1024*1024, 1024, link.LittleEndian, pool, TestCodec{})
	} else {
		codecType = link.Packet(2, 1024*1024, 1024, link.LittleEndian, TestCodec{})
	}

	server, err := link.Serve("tcp", addr, codecType)
	if err != nil {
		panic(err)
	}

	println("server start:", server.Listener().Addr().String())
	for {
		session, err := server.Accept()
		if err != nil {
			break
		}
		go func() {
			//addr := session.Conn().RemoteAddr().String()
			//println("client", addr, "connected")
			for {
				var msg []byte
				if err = session.Receive(&msg); err != nil {
					break
				}
				if err = session.Send(msg); err != nil {
					break
				}
			}
			//println("client", addr, "closed")
		}()
	}
}
Exemplo n.º 3
0
func Test_PacketPro(t *testing.T) {
	pool := binary.NewBufferPool(2, 1, 32)
	SessionTest(t, PacketPro(2, 1024, 1024, LittleEndian, pool, Json()), ObjectTest)
}