client.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "sync/atomic"
  7. "time"
  8. "go-common/library/exp/feature"
  9. "go-common/library/log"
  10. "go-common/library/net/rpc/warden"
  11. pb "go-common/library/net/rpc/warden/proto/testproto"
  12. "go-common/library/net/rpc/warden/resolver"
  13. "go-common/library/net/rpc/warden/resolver/direct"
  14. )
  15. var addrs string
  16. var cli pb.GreeterClient
  17. var concurrency int
  18. var name string
  19. var req int64
  20. var qps int64
  21. func init() {
  22. log.Init(&log.Config{Stdout: false})
  23. flag.StringVar(&addrs, "addr", "127.0.0.1:8000,127.0.0.1:8001", "-addr 127.0.0.1:8080,127.0.0.1:8081")
  24. flag.IntVar(&concurrency, "c", 3, "-c 5")
  25. flag.StringVar(&name, "name", "test", "-name test")
  26. }
  27. func main() {
  28. go calcuQPS()
  29. feature.DefaultGate.AddFlag(flag.CommandLine)
  30. flag.Parse()
  31. feature.DefaultGate.SetFromMap(map[string]bool{"dwrr": true})
  32. resolver.Register(direct.New())
  33. c := warden.NewClient(nil)
  34. conn, err := c.Dial(context.Background(), fmt.Sprintf("direct://d/%s", addrs))
  35. if err != nil {
  36. panic(err)
  37. }
  38. cli = pb.NewGreeterClient(conn)
  39. for i := 0; i < concurrency; i++ {
  40. go func() {
  41. for {
  42. say()
  43. time.Sleep(time.Millisecond * 5)
  44. }
  45. }()
  46. }
  47. time.Sleep(time.Hour)
  48. }
  49. func calcuQPS() {
  50. var creq, breq int64
  51. for {
  52. time.Sleep(time.Second * 5)
  53. creq = atomic.LoadInt64(&req)
  54. delta := creq - breq
  55. atomic.StoreInt64(&qps, delta/5)
  56. breq = creq
  57. fmt.Println("HTTP QPS: ", atomic.LoadInt64(&qps))
  58. }
  59. }
  60. func say() {
  61. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  62. defer cancel()
  63. reply, err := cli.SayHello(ctx, &pb.HelloRequest{Name: name, Age: 10})
  64. if err == nil && reply.Success {
  65. atomic.AddInt64(&req, 1)
  66. }
  67. }