greeter_client.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "math/rand"
  7. "sync"
  8. "sync/atomic"
  9. "time"
  10. "go-common/library/net/netutil/breaker"
  11. "go-common/library/net/rpc/warden"
  12. pb "go-common/library/net/rpc/warden/proto/testproto"
  13. xtime "go-common/library/time"
  14. )
  15. var (
  16. ccf = &warden.ClientConfig{
  17. Dial: xtime.Duration(time.Second * 10),
  18. Timeout: xtime.Duration(time.Second * 10),
  19. Breaker: &breaker.Config{
  20. Window: xtime.Duration(3 * time.Second),
  21. Sleep: xtime.Duration(3 * time.Second),
  22. Bucket: 10,
  23. Ratio: 0.3,
  24. Request: 20,
  25. },
  26. }
  27. cli pb.GreeterClient
  28. wg sync.WaitGroup
  29. reqSize int
  30. concurrency int
  31. request int
  32. all int64
  33. )
  34. func init() {
  35. flag.IntVar(&reqSize, "s", 10, "request size")
  36. flag.IntVar(&concurrency, "c", 10, "concurrency")
  37. flag.IntVar(&request, "r", 1000, "request per routine")
  38. }
  39. func main() {
  40. flag.Parse()
  41. name := randSeq(reqSize)
  42. cli = newClient()
  43. for i := 0; i < concurrency; i++ {
  44. wg.Add(1)
  45. go sayHello(&pb.HelloRequest{Name: name})
  46. }
  47. wg.Wait()
  48. fmt.Printf("per request cost %v\n", all/int64(request*concurrency))
  49. }
  50. func sayHello(in *pb.HelloRequest) {
  51. defer wg.Done()
  52. now := time.Now()
  53. for i := 0; i < request; i++ {
  54. cli.SayHello(context.TODO(), in)
  55. }
  56. delta := time.Since(now)
  57. atomic.AddInt64(&all, int64(delta/time.Millisecond))
  58. }
  59. var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
  60. func randSeq(n int) string {
  61. b := make([]rune, n)
  62. for i := range b {
  63. b[i] = letters[rand.Intn(len(letters))]
  64. }
  65. return string(b)
  66. }
  67. func newClient() (cli pb.GreeterClient) {
  68. client := warden.NewClient(ccf)
  69. conn, err := client.Dial(context.TODO(), "127.0.0.1:9999")
  70. if err != nil {
  71. return
  72. }
  73. cli = pb.NewGreeterClient(conn)
  74. return
  75. }