func BenchmarkTCPReconnectRouter(b *testing.B) { r, err := NewRouter(nil, ServiceProcessPayload) if err != nil { b.FailNow() } hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) r.Run() <-time.Tick(1 * time.Millisecond) network := "tcp" address := "localhost:10000" if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil { b.Log(err) b.FailNow() } <-time.Tick(1 * time.Millisecond) b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { name := "scheduler" req := pbt.NewResourceReq() req.Id = proto.Uint64(1) r, err := NewRouter(nil, ServiceProcessPayload) if err != nil { b.FailNow() } hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) r.Run() if err := r.Dial(name, network, address, hf); err != nil { b.Log(err) b.FailNow() } if _, err := r.CallWait(name, "rpc", req, 5); err != nil { b.Log(err) b.FailNow() } r.Stop() } }) r.Stop() }
func BenchmarkPipeShareRouter(b *testing.B) { r, err := NewRouter(nil, ServiceProcessPayload) if err != nil { b.FailNow() } hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) r.Run() <-time.Tick(1 * time.Millisecond) name := "scheduler" n := ConcurrentNum m := GoRoutineRequests for i := 0; i < n; i++ { c, s := net.Pipe() ep_c := r.newRouterEndPoint(name+string(i), c, hf) ep_s := r.newRouterEndPoint("client"+string(n), s, hf) r.AddEndPoint(ep_c) r.AddEndPoint(ep_s) } <-time.Tick(1 * time.Millisecond) testShareRouter(b, r, n, m) }
func BenchmarkTCPShareRouter(b *testing.B) { r, err := NewRouter(nil, ServiceProcessPayload) if err != nil { b.FailNow() } hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) r.Run() <-time.Tick(1 * time.Millisecond) network := "tcp" address := "localhost:10001" if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil { b.Log(err) b.FailNow() } name := "scheduler" n := ConcurrentNum m := GoRoutineRequests for i := 0; i < n; i++ { if err := r.Dial(name+string(i), network, address, hf); err != nil { b.Log(err) b.FailNow() } } <-time.Tick(1 * time.Millisecond) testShareRouter(b, r, n, m) }
func BenchmarkPipeReadWriter(b *testing.B) { s, c := net.Pipe() ch_c_w := make(chanPayload, 1024) ch_s_w := make(chanPayload, 1024) ch_d := make(chanPayload, 1024) hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) ep_c := NewEndPoint("c", c, ch_c_w, ch_d, hf, nil, nil) ep_s := NewEndPoint("s", s, ch_s_w, ch_s_w, hf, nil, nil) ep_c.Run() ep_s.Run() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { req := pbt.NewResourceReq() req.Id = proto.Uint64(1) for pb.Next() { ch_c_w <- req <-ch_d } }) }
func TestMockMsg(t *testing.T) { pr, pw := io.Pipe() ch := make(chanPayload, 128) hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) w := NewWriter(pw, ch, hf.NewBuffer(), nil) r := NewReader(pr, ch, hf.NewBuffer(), nil) w.Run() r.Run() req := pbt.NewResourceReq() req.Id = proto.Uint64(10000) w.Write(req) <-time.After(1 * time.Second) select { case resp := <-ch: if resp.(*pbt.ResourceReq).GetId() != 10000 { t.Log(req, resp) t.Fail() } case <-time.After(1 * time.Second): t.Log("timeout") t.FailNow() } r.Stop() w.Stop() }
func NewServer(opt ...ServerOption) *Server { var err error s := new(Server) if s.r, err = rpc.NewRouter(nil, nil); err != nil { return nil } s.ln = "grpc-api-listener" s.mf = rpc.NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) return s }
func Dial(target string, opts ...DialOption) (*ClientConn, error) { var err error cc := new(ClientConn) if cc.r, err = rpc.NewRouter(nil, nil); err != nil { return nil, err } cc.r.Run() cc.cn = "grpc-api-connector" cc.mf = rpc.NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) if err = cc.r.Dial(cc.cn, "tcp", target, cc.mf); err != nil { cc.r.Stop() return nil, err } return cc, nil }
func TestRouterMultiple(t *testing.T) { r, err := NewRouter(nil, ServiceProcessPayload) if err != nil { t.FailNow() } hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) name := "scheduler" network := "tcp" address := "localhost:10000" r.Run() if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil { t.Log(err) t.FailNow() } if err := r.Dial(name, network, address, hf); err != nil { t.Log(err) t.FailNow() } time.Sleep(1) for i := 1; i < 10000; i++ { req := pbt.NewResourceReq() req.Id = proto.Uint64(uint64(i)) if _, err := r.CallWait(name, "rpc", req, 5); err != nil { t.Log(i, ":", err) t.FailNow() } } r.DelEndPoint("scheduler") r.DelListener("client") r.Stop() }
func BenchmarkTCPReadWriter(b *testing.B) { network := "tcp" address := "localhost:10008" l, err := net.Listen(network, address) if err != nil { b.FailNow() } c, err := net.Dial(network, address) if err != nil { b.FailNow() } s, err := l.Accept() if err != nil { b.FailNow() } ch_c_w := make(chanPayload, 1024) ch_s_w := make(chanPayload, 1024) ch_d := make(chanPayload, 1024) hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory()) ep_c := NewEndPoint("c", c, ch_c_w, ch_d, hf, nil, nil) ep_s := NewEndPoint("s", s, ch_s_w, ch_s_w, hf, nil, nil) ep_c.Run() ep_s.Run() b.ResetTimer() b.RunParallel(func(pb *testing.PB) { req := pbt.NewResourceReq() req.Id = proto.Uint64(1) for pb.Next() { ch_c_w <- req <-ch_d } }) }