global.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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
  25. totalClientConnections int
  26. )
  27. // GlobalFlags are flags that defined globally
  28. // and are inherited to all sub-commands.
  29. type GlobalFlags struct {
  30. Endpoints []string
  31. DialTimeout time.Duration
  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) {
  51. var mu sync.Mutex
  52. var wg sync.WaitGroup
  53. wg.Add(len(rcs))
  54. finished := make(chan struct{}, 0)
  55. for i := range rcs {
  56. go func(rc *roundClient) {
  57. defer wg.Done()
  58. for rc.progress < rounds {
  59. for rc.acquire() != nil { /* spin */
  60. }
  61. mu.Lock()
  62. if err := rc.validate(); err != nil {
  63. log.Fatal(err)
  64. }
  65. mu.Unlock()
  66. time.Sleep(10 * time.Millisecond)
  67. rc.progress++
  68. finished <- struct{}{}
  69. mu.Lock()
  70. for rc.release() != nil {
  71. mu.Unlock()
  72. mu.Lock()
  73. }
  74. mu.Unlock()
  75. }
  76. }(&rcs[i])
  77. }
  78. start := time.Now()
  79. for i := 1; i < len(rcs)*rounds+1; i++ {
  80. select {
  81. case <-finished:
  82. if i%100 == 0 {
  83. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  84. start = time.Now()
  85. }
  86. case <-time.After(time.Minute):
  87. log.Panic("no progress after 1 minute!")
  88. }
  89. }
  90. wg.Wait()
  91. for _, rc := range rcs {
  92. rc.c.Close()
  93. }
  94. }
  95. func endpointsFromFlag(cmd *cobra.Command) []string {
  96. endpoints, err := cmd.Flags().GetStringSlice("endpoints")
  97. if err != nil {
  98. ExitWithError(ExitError, err)
  99. }
  100. return endpoints
  101. }
  102. func dialTimeoutFromCmd(cmd *cobra.Command) time.Duration {
  103. dialTimeout, err := cmd.Flags().GetDuration("dial-timeout")
  104. if err != nil {
  105. ExitWithError(ExitError, err)
  106. }
  107. return dialTimeout
  108. }