func main() { client, err := proxy.NewWithDefaultConfig().ServantByAddr(":9001") if err != nil { panic(err) } defer client.Recycle() ctx := rpc.NewContext() ctx.Reason = "go.duplex" ctx.Rid = 189 for i := 0; i < 10; i++ { go func() { t1 := time.Now() r, err := client.Ping(ctx) if err != nil { log.Println(err) } else { log.Println(r, time.Since(t1)) } }() } <-make(chan struct{}) }
func main() { proxy := proxy.NewWithDefaultConfig() if host == "" { pingCluster(proxy) return } // ping a single faed client, err := proxy.ServantByAddr(host + ":" + port) if err != nil { fmt.Println(err) return } defer client.Recycle() ctx := rpc.NewContext() ctx.Reason = REASON ctx.Rid = time.Now().UnixNano() pong, err := client.Ping(ctx) if err != nil { fmt.Println(err) } else { fmt.Println(pong) } }
func main() { cf := config.NewDefaultProxy() cf.PoolCapacity = 2 peer := proxy.New(cf) for { time.Sleep(time.Duration(interval) * time.Second) fmt.Println() client, err := peer.ServantByAddr(host + ":" + port) if err != nil { fmt.Println(err) continue } ctx := rpc.NewContext() ctx.Reason = "broken.test" ctx.Rid = time.Now().UnixNano() pong, err := client.Ping(ctx) if err != nil { fmt.Println("err:", err) if proxy.IsIoError(err) { client.Close() } } else { fmt.Println(pong) } client.Recycle() } }
func (this *FunServantPeer) NewContext(reason string, uid int64) *rpc.Context { ctx := rpc.NewContext() ctx.Rid = this.pool.nextTxn() + time.Now().UnixNano() // roughly unique, maybe enough ctx.Reason = reason ctx.Uid = uid return ctx }
func BenchmarkGetSession(b *testing.B) { servant := setupServant() b.ReportAllocs() ctx := rpc.NewContext() ctx.Reason = "Map:enterKingdomBlock" for i := 0; i < b.N; i++ { ctx.Rid = time.Now().UnixNano() servant.getSession(ctx) } }
func BenchmarkMcSet(b *testing.B) { servant := setupServant() b.ReportAllocs() ctx := rpc.NewContext() ctx.Caller = "me" for i := 0; i < b.N; i++ { servant.McSet(ctx, "foo", []byte("bar"), 0) } }
func BenchmarkMcSet(b *testing.B) { servant := setupServant() b.ReportAllocs() ctx := rpc.NewContext() ctx.Reason = "benchmark" ctx.Rid = 1 data := rpc.NewTMemcacheData() data.Data = []byte("bar") for i := 0; i < b.N; i++ { servant.McSet(ctx, "default", "foo", data, 0) } }
func BenchmarkServantMcSet(b *testing.B) { servant := Servant(":9001") defer servant.Transport.Close() ctx := rpc.NewContext() ctx.Caller = "bench" b.ReportAllocs() for i := 0; i < b.N; i++ { servant.McSet(ctx, "foo", []byte("bar"), 0) } b.SetBytes(10) }
func BenchmarkServantPing(b *testing.B) { servant := Servant(":9001") defer servant.Transport.Close() ctx := rpc.NewContext() ctx.Caller = "bench" b.ReportAllocs() for i := 0; i < b.N; i++ { servant.Ping(ctx) } b.SetBytes(10) }
func main() { t1 := time.Now() client := proxy.Servant(":9001") defer client.Transport.Close() ctx := rpc.NewContext() ctx.Caller = "me" for i := 0; i < 10; i++ { r, _ := client.Ping(ctx) fmt.Println(r, time.Since(t1)) t1 = time.Now() } }
func TestContextInfo(t *testing.T) { ctx := rpc.NewContext() ctx.Caller = "POST+/facebook/getPaymentRequestId/+34ca2cf6" fun := FunServantImpl{} info := fun.contextInfo(ctx) assert.Equal(t, true, info.Valid()) assert.Equal(t, "POST", info.httpMethod) assert.Equal(t, "/facebook/getPaymentRequestId/", info.uri) assert.Equal(t, "34ca2cf6", info.seqId) ctx.Caller = "" info = fun.contextInfo(ctx) assert.Equal(t, false, info.Valid()) }
func BenchmarkPingOnLocalhost(b *testing.B) { b.ReportAllocs() cf := &config.ConfigProxy{PoolCapacity: 1} client, err := proxy.New(cf).ServantByAddr("localhost:9001") if err != nil { b.Fatal(err) } defer client.Recycle() ctx := rpc.NewContext() ctx.Reason = "benchmark" ctx.Rid = 2 for i := 0; i < b.N; i++ { client.Ping(ctx) } }
func pingCluster(proxy *proxy.Proxy) { if err := etclib.Dial([]string{zk}); err != nil { fmt.Printf("zk: %s", err.Error()) return } go proxy.StartMonitorCluster() proxy.AwaitClusterTopologyReady() peers := proxy.ClusterPeers() if len(peers) == 0 { fmt.Println("found no fae peers") return } for i := 0; i < loops; i++ { for _, peerAddr := range peers { client, err := proxy.ServantByAddr(peerAddr) if err != nil { fmt.Printf("[%6d] %21s: %s\n", i+1, peerAddr, err) continue } ctx := rpc.NewContext() ctx.Reason = REASON ctx.Rid = time.Now().UnixNano() + int64(i) pong, err := client.Ping(ctx) if err != nil { client.Close() fmt.Printf("[%6d] %21s: %s\n", i+1, peerAddr, err) } else { fmt.Printf("[%6d] %21s: %s\n", i+1, peerAddr, pong) } client.Recycle() } } }
func main() { t1 := time.Now() client, err := proxy.NewWithDefaultConfig().ServantByAddr(":9001") if err != nil { panic(err) } defer client.Recycle() ctx := rpc.NewContext() ctx.Reason = "gotest" ctx.Rid = 189 for i := 0; i < 10; i++ { r, err := client.Ping(ctx) if err != nil { panic(err) } fmt.Println(r, time.Since(t1)) t1 = time.Now() } }
// auto fills Rid field. func NewContext(reason string) *rpc.Context { ctx := rpc.NewContext() ctx.Reason = reason ctx.Rid = time.Now().UnixNano() // TODO strict enough? return ctx }
func runSession(proxy *proxy.Proxy, wg *sync.WaitGroup, round int, seq int) { report.updateConcurrency(1) report.incSessions() defer func() { wg.Done() report.updateConcurrency(-1) //log.Printf("session{round^%d seq^%d} done", round, seq) }() t1 := time.Now() client, err := proxy.ServantByAddr(host + ":9001") if err != nil { report.incConnErrs() log.Printf("session{round^%d seq^%d} error: %v", round, seq, err) return } defer client.Recycle() // when err occurs, do we still need recycle? var enableLog = false if !logTurnOff && sampling(SampleRate) { enableLog = true } if enableLog { log.Printf("session{round^%d seq^%d} connected within %s", round, seq, time.Since(t1)) } ctx := rpc.NewContext() ctx.Reason = "stress.go" for i := 0; i < LoopsPerSession; i++ { //time.Sleep(time.Millisecond * 5) ctx.Rid = time.Now().UnixNano() if Cmd&CallNoop != 0 { var r int32 r, err = client.Noop(1) if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d noop}: %v", round, seq, err) } client.Close() return } else { report.incCallOk() if enableLog { log.Printf("session{round^%d seq^%d noop}: %v", round, seq, r) } } } if Cmd&CallPing != 0 { var r string r, err = client.Ping(ctx) if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d ping}: %v", round, seq, err) } client.Close() return } else { report.incCallOk() if enableLog { log.Printf("session{round^%d seq^%d ping}: %v", round, seq, r) } } } if Cmd&CallLCache != 0 { key := fmt.Sprintf("lc_stress:%d", rand.Int()) value := []byte("value of " + key) _, err = client.LcSet(ctx, key, value) if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d lc.set}: %v", round, seq, err) } client.Close() return } else { report.incCallOk() } value, _, err = client.LcGet(ctx, key) if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d lc.get}: %v", round, seq, err) } client.Close() return } else { report.incCallOk() if enableLog { log.Printf("session{round^%d seq^%d lcache}: %s => %s", round, seq, key, string(value)) } } } if Cmd&CallIdGen != 0 { var r int64 r, err = client.IdNext(ctx) if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d idgen}: %v", round, seq, err) } client.Close() return } else { if enableLog { log.Printf("session{round^%d seq^%d idgen}: %d", round, seq, r) } report.incCallOk() } } if Cmd&CallMysql != 0 { // with cache if true { r, err := client.MyQuery(ctx, "UserShard", "UserInfo", 1, "SELECT * FROM UserInfo WHERE uid=?", []string{"1"}, "user:1") if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d mysql.cache}: %v", round, seq, err) } client.Close() return } else { if enableLog { log.Printf("session{round^%d seq^%d mysql}: %+v", round, seq, r) } report.incCallOk() } } // without cache if true { var rows *rpc.MysqlResult rows, err = client.MyQuery(ctx, "UserShard", "UserInfo", 1, "SELECT * FROM UserInfo WHERE uid=?", []string{"1"}, "") if err != nil { recordIoError(err) report.incCallErr() if !errOff { log.Printf("session{round^%d seq^%d mysql.nocache}: %v", round, seq, err) } client.Close() return } else { if enableLog { log.Printf("session{round^%d seq^%d mysql}: %+v", round, seq, rows.Rows) } report.incCallOk() } } } continue // TODO var ( key string value []byte mcValue = rpc.NewTMemcacheData() result []byte ) key = fmt.Sprintf("mc_stress:%d", rand.Int()) value = []byte("value of " + key) mcValue.Data = value if Cmd&CallMemcache != 0 { _, err = client.McSet(ctx, MC_POOL, key, mcValue, 36000) if err != nil { report.incCallErr() log.Printf("session{round^%d seq^%d mc_set} %v", round, seq, err) } else { report.incCallOk() } _, miss, err := client.McGet(ctx, MC_POOL, key) if miss != nil || err != nil { report.incCallErr() log.Printf("session{round^%d seq^%d mc_get} miss^%v, err^%v", round, seq, miss, err) } else { report.incCallOk() } } if Cmd&CallMongo != 0 { mgQuery, _ := bson.Marshal(bson.M{"snsid": "100003391571259"}) mgFields, _ := bson.Marshal(bson.M{}) result, _, err = client.MgFindOne(ctx, "default", "idmap", 0, mgQuery, mgFields) if err != nil { report.incCallErr() log.Printf("session{round^%d seq^%d mg_findOne} %v", round, seq, err) } else { report.incCallOk() if false { log.Println(result) } } } } if enableLog { log.Printf("session{round^%d seq^%d} done", round, seq) } }