global.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 runner
  15. import (
  16. "context"
  17. "fmt"
  18. "log"
  19. "sync"
  20. "time"
  21. "go.etcd.io/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: 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 wg sync.WaitGroup
  52. wg.Add(len(rcs))
  53. finished := make(chan struct{})
  54. limiter := rate.NewLimiter(rate.Limit(reqRate), reqRate)
  55. for i := range rcs {
  56. go func(rc *roundClient) {
  57. defer wg.Done()
  58. for rc.progress < rounds || rounds <= 0 {
  59. if err := limiter.WaitN(context.Background(), requests/len(rcs)); err != nil {
  60. log.Panicf("rate limiter error %v", err)
  61. }
  62. for rc.acquire() != nil { /* spin */
  63. }
  64. if err := rc.validate(); err != nil {
  65. log.Fatal(err)
  66. }
  67. time.Sleep(10 * time.Millisecond)
  68. rc.progress++
  69. finished <- struct{}{}
  70. for rc.release() != nil { /* spin */
  71. }
  72. }
  73. }(&rcs[i])
  74. }
  75. start := time.Now()
  76. for i := 1; i < len(rcs)*rounds+1 || rounds <= 0; i++ {
  77. select {
  78. case <-finished:
  79. if i%100 == 0 {
  80. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  81. start = time.Now()
  82. }
  83. case <-time.After(time.Minute):
  84. log.Panic("no progress after 1 minute!")
  85. }
  86. }
  87. wg.Wait()
  88. for _, rc := range rcs {
  89. rc.c.Close()
  90. }
  91. }
  92. func endpointsFromFlag(cmd *cobra.Command) []string {
  93. eps, err := cmd.Flags().GetStringSlice("endpoints")
  94. if err != nil {
  95. ExitWithError(ExitError, err)
  96. }
  97. return eps
  98. }