global.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "sync"
  20. "time"
  21. "github.com/coreos/etcd/clientv3"
  22. "github.com/spf13/cobra"
  23. "golang.org/x/time/rate"
  24. )
  25. // shared flags
  26. var (
  27. totalClientConnections int // total number of client connections to be made with server
  28. endpoints []string
  29. dialTimeout time.Duration
  30. rounds int // total number of rounds to run; set to <= 0 to run forever.
  31. reqRate int // maximum number of requests per second.
  32. )
  33. type roundClient struct {
  34. c *clientv3.Client
  35. progress int
  36. acquire func() error
  37. validate func() error
  38. release func() error
  39. }
  40. func newClient(eps []string, timeout time.Duration) *clientv3.Client {
  41. c, err := clientv3.New(clientv3.Config{
  42. Endpoints: eps,
  43. DialTimeout: time.Duration(timeout) * time.Second,
  44. })
  45. if err != nil {
  46. log.Fatal(err)
  47. }
  48. return c
  49. }
  50. func doRounds(rcs []roundClient, rounds int, requests int) {
  51. var mu sync.Mutex
  52. var wg sync.WaitGroup
  53. wg.Add(len(rcs))
  54. finished := make(chan struct{})
  55. limiter := rate.NewLimiter(rate.Limit(reqRate), reqRate)
  56. for i := range rcs {
  57. go func(rc *roundClient) {
  58. defer wg.Done()
  59. for rc.progress < rounds || rounds <= 0 {
  60. if err := limiter.WaitN(context.Background(), requests/len(rcs)); err != nil {
  61. log.Panicf("rate limiter error %v", err)
  62. }
  63. for rc.acquire() != nil { /* spin */
  64. }
  65. mu.Lock()
  66. if err := rc.validate(); err != nil {
  67. log.Fatal(err)
  68. }
  69. mu.Unlock()
  70. time.Sleep(10 * time.Millisecond)
  71. rc.progress++
  72. finished <- struct{}{}
  73. mu.Lock()
  74. for rc.release() != nil { /* spin */
  75. mu.Unlock()
  76. mu.Lock()
  77. }
  78. mu.Unlock()
  79. }
  80. }(&rcs[i])
  81. }
  82. start := time.Now()
  83. for i := 1; i < len(rcs)*rounds+1 || rounds <= 0; i++ {
  84. select {
  85. case <-finished:
  86. if i%100 == 0 {
  87. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  88. start = time.Now()
  89. }
  90. case <-time.After(time.Minute):
  91. log.Panic("no progress after 1 minute!")
  92. }
  93. }
  94. wg.Wait()
  95. for _, rc := range rcs {
  96. rc.c.Close()
  97. }
  98. }
  99. func endpointsFromFlag(cmd *cobra.Command) []string {
  100. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  101. if err != nil {
  102. ExitWithError(ExitError, err)
  103. }
  104. return endpoints
  105. }
  106. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  107. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  108. if err != nil {
  109. ExitWithError(ExitError, err)
  110. }
  111. return dialTimeout
  112. }