main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 main
  15. import (
  16. "flag"
  17. "fmt"
  18. "log"
  19. "math/rand"
  20. "os"
  21. "strings"
  22. "sync"
  23. "time"
  24. "github.com/coreos/etcd/clientv3"
  25. )
  26. func init() {
  27. rand.Seed(time.Now().UTC().UnixNano())
  28. }
  29. func main() {
  30. log.SetFlags(log.Lmicroseconds)
  31. endpointStr := flag.String("endpoints", "localhost:2379", "endpoints of etcd cluster")
  32. mode := flag.String("mode", "watcher", "test mode (election, lock-racer, lease-renewer, watcher)")
  33. round := flag.Int("rounds", 100, "number of rounds to run")
  34. clientTimeout := flag.Int("client-timeout", 60, "max timeout seconds for a client to get connection")
  35. flag.Parse()
  36. eps := strings.Split(*endpointStr, ",")
  37. getClient := func() *clientv3.Client { return newClient(eps, *clientTimeout) }
  38. switch *mode {
  39. case "election":
  40. runElection(getClient, *round)
  41. case "lock-racer":
  42. runRacer(getClient, *round)
  43. case "lease-renewer":
  44. runLeaseRenewer(getClient)
  45. case "watcher":
  46. runWatcher(getClient, *round)
  47. default:
  48. fmt.Fprintf(os.Stderr, "unsupported mode %v\n", *mode)
  49. }
  50. }
  51. type getClientFunc func() *clientv3.Client
  52. func newClient(eps []string, timeout int) *clientv3.Client {
  53. c, err := clientv3.New(clientv3.Config{
  54. Endpoints: eps,
  55. DialTimeout: time.Duration(timeout) * time.Second,
  56. })
  57. if err != nil {
  58. log.Fatal(err)
  59. }
  60. return c
  61. }
  62. type roundClient struct {
  63. c *clientv3.Client
  64. progress int
  65. acquire func() error
  66. validate func() error
  67. release func() error
  68. }
  69. func doRounds(rcs []roundClient, rounds int) {
  70. var mu sync.Mutex
  71. var wg sync.WaitGroup
  72. wg.Add(len(rcs))
  73. finished := make(chan struct{}, 0)
  74. for i := range rcs {
  75. go func(rc *roundClient) {
  76. defer wg.Done()
  77. for rc.progress < rounds {
  78. for rc.acquire() != nil { /* spin */
  79. }
  80. mu.Lock()
  81. if err := rc.validate(); err != nil {
  82. log.Fatal(err)
  83. }
  84. mu.Unlock()
  85. time.Sleep(10 * time.Millisecond)
  86. rc.progress++
  87. finished <- struct{}{}
  88. mu.Lock()
  89. for rc.release() != nil {
  90. mu.Unlock()
  91. mu.Lock()
  92. }
  93. mu.Unlock()
  94. }
  95. }(&rcs[i])
  96. }
  97. start := time.Now()
  98. for i := 1; i < len(rcs)*rounds+1; i++ {
  99. select {
  100. case <-finished:
  101. if i%100 == 0 {
  102. fmt.Printf("finished %d, took %v\n", i, time.Since(start))
  103. start = time.Now()
  104. }
  105. case <-time.After(time.Minute):
  106. log.Panic("no progress after 1 minute!")
  107. }
  108. }
  109. wg.Wait()
  110. for _, rc := range rcs {
  111. rc.c.Close()
  112. }
  113. }