global.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. "fmt"
  17. "log"
  18. "sync"
  19. "time"
  20. "github.com/coreos/etcd/clientv3"
  21. "github.com/spf13/cobra"
  22. )
  23. var (
  24. rounds int // total number of rounds the operation needs to be performed
  25. totalClientConnections int // total number of client connections to be made with server
  26. noOfPrefixes int // total number of prefixes which will be watched upon
  27. watchPerPrefix int // number of watchers per prefix
  28. reqRate int // put request per second
  29. totalKeys int // total number of keys for operation
  30. runningTime time.Duration // time for which operation should be performed
  31. )
  32. // GlobalFlags are flags that defined globally
  33. // and are inherited to all sub-commands.
  34. type GlobalFlags struct {
  35. Endpoints []string
  36. DialTimeout time.Duration
  37. }
  38. type roundClient struct {
  39. c *clientv3.Client
  40. progress int
  41. acquire func() error
  42. validate func() error
  43. release func() error
  44. }
  45. func newClient(eps []string, timeout time.Duration) *clientv3.Client {
  46. c, err := clientv3.New(clientv3.Config{
  47. Endpoints: eps,
  48. DialTimeout: time.Duration(timeout) * time.Second,
  49. })
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. return c
  54. }
  55. func doRounds(rcs []roundClient, rounds int) {
  56. var mu sync.Mutex
  57. var wg sync.WaitGroup
  58. wg.Add(len(rcs))
  59. finished := make(chan struct{})
  60. for i := range rcs {
  61. go func(rc *roundClient) {
  62. defer wg.Done()
  63. for rc.progress < rounds {
  64. for rc.acquire() != nil { /* spin */
  65. }
  66. mu.Lock()
  67. if err := rc.validate(); err != nil {
  68. log.Fatal(err)
  69. }
  70. mu.Unlock()
  71. time.Sleep(10 * time.Millisecond)
  72. rc.progress++
  73. finished <- struct{}{}
  74. mu.Lock()
  75. for rc.release() != nil {
  76. mu.Unlock()
  77. mu.Lock()
  78. }
  79. mu.Unlock()
  80. }
  81. }(&rcs[i])
  82. }
  83. start := time.Now()
  84. for i := 1; i < len(rcs)*rounds+1; i++ {
  85. select {
  86. case <-finished:
  87. if i%100 == 0 {
  88. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  89. start = time.Now()
  90. }
  91. case <-time.After(time.Minute):
  92. log.Panic("no progress after 1 minute!")
  93. }
  94. }
  95. wg.Wait()
  96. for _, rc := range rcs {
  97. rc.c.Close()
  98. }
  99. }
  100. func endpointsFromFlag(cmd *cobra.Command) []string {
  101. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  102. if err != nil {
  103. ExitWithError(ExitError, err)
  104. }
  105. return endpoints
  106. }
  107. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  108. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  109. if err != nil {
  110. ExitWithError(ExitError, err)
  111. }
  112. return dialTimeout
  113. }