func ServiceProcessPayload(r *rpc.Router, name string, p rpc.Payload) rpc.Payload { if req, ok := p.(*testpb.TestReq); ok { rep := testpb.NewTestRep() rep.Id = req.Id return rep } else if b, ok := p.([]byte); ok { req := testpb.NewTestReq() if proto.Unmarshal(b, req) != nil { panic("proto.Unmarshal error") } rep := testpb.NewTestRep() rep.Id = req.Id return rep } else { panic("ServiceProcessPayload receieve wrong info") } }
func main() { address := flag.String("address", ":10000", "benchmark server address") router_per_conn := flag.Bool("router_per_conn", false, "benchmark share Router") conn_num := flag.Uint64("conn_num", 1, "benchmark connection number") req_num := flag.Uint64("req_num", 1000000, "benchmark request number") burst_num := flag.Uint64("burst_num", 1, "benchmark burst number") flag.Parse() task := NewTask(*req_num, *conn_num, *burst_num) hf := rpc.NewRPCHeaderFactory(rpc.NewProtobufFactory()) _ = hf // index 0 is not used var routers []*rpc.Router for i := 0; i <= task.conn_num; i++ { var r *rpc.Router var err error if *router_per_conn || i == 0 { r, err = rpc.NewRouter(nil, nil) } else { r, err = routers[0], nil } if err != nil { fmt.Println(err) return } else { r.Run() defer r.Stop() } routers = append(routers, r) } ep_name := "benchmark-" for i := 1; i <= task.conn_num; i++ { if err := routers[i].Dial(ep_name+strconv.Itoa(i), "tcp", *address, hf); err != nil { fmt.Println(err) return } } var conn_wg sync.WaitGroup start := time.Now() for conn_id := 1; conn_id <= task.conn_num; conn_id++ { conn_wg.Add(1) // connection go func(r *rpc.Router, conn_wg *sync.WaitGroup, task *Task, conn_id int) { name := ep_name + strconv.Itoa(conn_id) conn_task := task.task[conn_id] for burst_id := 1; burst_id <= len(conn_task); burst_id++ { burst_task := conn_task[burst_id] var burst_wg sync.WaitGroup for id, _ := range burst_task { burst_wg.Add(1) req := testpb.NewTestReq() req.Id = proto.Uint64(id) burst_task[id].start = time.Now() burst_task[id].arg = &burst_wg r.Call(name, "rpc", req, ClientProcessReponseWaitGroup, burst_task[id], 0) } burst_wg.Wait() } conn_wg.Done() }(routers[conn_id], &conn_wg, task, conn_id) } conn_wg.Wait() stop := time.Now() total_time := stop.Sub(start) qps := float64(*req_num*1000*1000*1000) / float64(total_time) fmt.Printf("total time: %v total request: %v qps: %v\n", total_time, *req_num, qps) c := benchmark.NewCollecter(1000 * 1000) task.Do(c) fmt.Printf("max: %vus\n", c.Max()) fmt.Printf("min: %vus\n", c.Min()) fmt.Printf("mean: %vus\n", c.Mean()) for _, p := range []float64{50.0, 70.0, 90.0, 95.0, 99.0} { fmt.Printf("%.1f%% request done: %vus\n", p, c.Percentile(p)) } }
func main() { flag.Parse() // profiling if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { return } pprof.StartCPUProfile(f) defer pprof.StopCPUProfile() } task := NewTask(*req_num, *conn_num, *burst_num) hf := rpc.NewRPCHeaderFactory(rpc.NewProtobufFactory()) _ = hf // index 0 is not used var routers []*rpc.Router for i := 0; i <= task.conn_num; i++ { var r *rpc.Router var err error if *router_per_conn || i == 0 { r, err = rpc.NewRouter(nil, nil) } else { r, err = routers[0], nil } if err != nil { fmt.Println(err) return } else { r.Run() defer r.Stop() } routers = append(routers, r) } ep_name := "benchmark-" for i := 1; i <= task.conn_num; i++ { if err := routers[i].Dial(ep_name+strconv.Itoa(i), "tcp", *address, hf); err != nil { fmt.Println(err) return } } var conn_wg sync.WaitGroup start := time.Now() for conn_id := 1; conn_id <= task.conn_num; conn_id++ { conn_wg.Add(1) // connection go func(r *rpc.Router, conn_wg *sync.WaitGroup, task *Task, conn_id int) { name := ep_name + strconv.Itoa(conn_id) conn_task := task.task[conn_id] ch := make(chan struct{}, task.burst_num) for i := 0; i < task.burst_num; i++ { ch <- struct{}{} } for id, _ := range conn_task { <-ch req := testpb.NewTestReq() req.Id = id conn_task[id].start = time.Now() conn_task[id].arg = ch r.Call(name, "rpc", req, ClientProcessReponseWaitGroup, conn_task[id], 1) } for i := 0; i < task.burst_num; i++ { <-ch } close(ch) conn_wg.Done() }(routers[conn_id], &conn_wg, task, conn_id) } conn_wg.Wait() stop := time.Now() total_time := stop.Sub(start) qps := float64(*req_num*1000*1000*1000) / float64(total_time) fmt.Printf("total time: %v total request: %v qps: %.2f\n", total_time, *req_num, qps) c := benchmark.NewCollecter(1000 * 1000) task.Do(c) fmt.Printf("max: %vus ", c.Max()) fmt.Printf("min: %vus ", c.Min()) fmt.Printf("avg: %.2fus ", c.Mean()) fmt.Println() a := []float64{50.0, 70.0, 90.0, 95.0, 99.0} for _, p := range a { fmt.Printf("%7.1f%% ", p) } fmt.Println() for _, p := range a { fmt.Printf("%6dus ", c.Percentile(p)) } fmt.Println() }