github.com/rolandhe/saber@v0.0.4/example/nfour/rpc/client/echoclient.go (about) 1 package main 2 3 import ( 4 "fmt" 5 "github.com/rolandhe/saber/example/nfour/rpc/server/handler" 6 "github.com/rolandhe/saber/nfour" 7 "github.com/rolandhe/saber/nfour/duplex" 8 "github.com/rolandhe/saber/nfour/rpc/proto" 9 "github.com/rolandhe/saber/utils/sortutil" 10 "log" 11 "strconv" 12 "strings" 13 "sync" 14 "time" 15 ) 16 17 func main() { 18 //wait := &sync.WaitGroup{} 19 //wait.Add(2) 20 //start := time.Now().UnixNano() 21 //go func() { 22 // core("a") 23 // wait.Done() 24 //}() 25 //go func() { 26 // core("b") 27 // wait.Done() 28 //}() 29 // 30 //wait.Wait() 31 //nfour.NFourLogger.InfoLn("xxxx---", time.Now().UnixNano()-start) 32 33 core("single") 34 } 35 36 func core(name string) { 37 conf := duplex.NewTransConf(time.Second*2, 5000) 38 t, err := duplex.NewTrans("localhost:11011", conf, name) 39 if err != nil { 40 log.Println(err) 41 return 42 } 43 44 client := proto.NewJsonRpcClient(t) 45 46 concurrentSend(50000, client) 47 48 client.Shutdown(name + "-main") 49 50 } 51 52 type req struct { 53 val string 54 sortId int 55 } 56 57 func concurrentSend(taskCount int, c proto.JsonClient) { 58 reqs := buildRequests(taskCount) 59 reqTimeout := &duplex.ReqTimeout{ 60 WaitConcurrent: time.Millisecond * 1000, 61 } 62 63 wg := sync.WaitGroup{} 64 wg.Add(taskCount) 65 66 lock := sync.Mutex{} 67 68 var rarray []string 69 70 start := time.Now().UnixNano() 71 trigger := sync.WaitGroup{} 72 trigger.Add(1) 73 for _, req := range reqs { 74 go func(r *proto.JsonProtoReq) { 75 trigger.Wait() 76 s := "" 77 tryCount := 0 78 for { 79 resp, err := c.SendRequest(r, reqTimeout) 80 if err != nil { 81 s = "err:" + "###" + err.Error() 82 if tryCount < 2 { 83 time.Sleep(time.Millisecond * 10) 84 tryCount++ 85 } else { 86 break 87 } 88 } else { 89 jsonResult, _ := proto.ParseJsonProtoRes[handler.Result[string]](resp, func() *handler.Result[string] { 90 return &handler.Result[string]{} 91 }) 92 if jsonResult.Code == 500 { 93 s = "err:" + "###" + jsonResult.Message 94 } else { 95 s = jsonResult.Data 96 } 97 break 98 } 99 } 100 101 lock.Lock() 102 rarray = append(rarray, s) 103 lock.Unlock() 104 wg.Done() 105 }(req) 106 107 } 108 trigger.Done() 109 110 wg.Wait() 111 112 cost := time.Now().UnixNano() - start 113 114 nfour.NFourLogger.InfoLn("--------------------------------") 115 nArrays := convertBatchResult(rarray) 116 errCount := 0 117 last := -1 118 lostCount := 0 119 for _, v := range nArrays { 120 if strings.HasPrefix(v.val, "err:") { 121 nfour.NFourLogger.InfoLn(v.val) 122 errCount++ 123 } else { 124 //fmt.Println(v.val) 125 if (v.sortId - last) != 1 { 126 lostCount++ 127 } 128 last = v.sortId 129 } 130 } 131 nfour.NFourLogger.Info("......end(%d)..error(%d)..lost(%d)..\n", len(rarray), errCount, lostCount) 132 nfour.NFourLogger.InfoLn(cost, "nano") 133 } 134 135 func convertBatchResult(res []string) []*req { 136 var array []*req 137 for _, s := range res { 138 pos := strings.LastIndex(s, "-") 139 ids := s[pos+1:] 140 id, _ := strconv.Atoi(strings.TrimLeft(ids, "0")) 141 array = append(array, &req{s, id}) 142 } 143 144 sortutil.Cmp[*req](func(p1, p2 **req) bool { 145 return (*p1).sortId < (*p2).sortId 146 }).Sort(array) 147 148 return array 149 } 150 151 func buildRequests(num int) []*proto.JsonProtoReq { 152 var ret []*proto.JsonProtoReq 153 for i := 0; i < num; i++ { 154 num := fmt.Sprintf("%08d", i) 155 v := "hello worldjjjjjjjjjjjjjjkadsjfkdjlasfjkldklsafjkdsafjkldsajlfjkdsajkfdjksafjkldjkslafjkldsajkfjkldsajkfjkdlsajkfdjkasfjkdsajkfjkldsajklfdjksafjkdjkasfjkdasjkfjkldsajkfjkdlasjfkfdasjkfjdklasfjkdsaf ok-" + num 156 ret = append(ret, &proto.JsonProtoReq{"rpc.test", []byte(v)}) 157 } 158 return ret 159 }