stresser.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2015 CoreOS, Inc.
  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. "fmt"
  17. "io/ioutil"
  18. "log"
  19. "math/rand"
  20. "net"
  21. "net/http"
  22. "sync"
  23. "time"
  24. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  25. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  26. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc/grpclog"
  27. clientV2 "github.com/coreos/etcd/client"
  28. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  29. )
  30. func init() {
  31. grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
  32. }
  33. type Stresser interface {
  34. // Stress starts to stress the etcd cluster
  35. Stress() error
  36. // Cancel cancels the stress test on the etcd cluster
  37. Cancel()
  38. // Report reports the success and failure of the stress test
  39. Report() (success int, failure int)
  40. }
  41. type stresser struct {
  42. Endpoint string
  43. KeySize int
  44. KeySuffixRange int
  45. N int
  46. mu sync.Mutex
  47. wg *sync.WaitGroup
  48. cancel func()
  49. conn *grpc.ClientConn
  50. success int
  51. }
  52. func (s *stresser) Stress() error {
  53. conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure(), grpc.WithTimeout(5*time.Second))
  54. if err != nil {
  55. return fmt.Errorf("%v (%s)", err, s.Endpoint)
  56. }
  57. defer conn.Close()
  58. ctx, cancel := context.WithCancel(context.Background())
  59. wg := &sync.WaitGroup{}
  60. wg.Add(s.N)
  61. s.mu.Lock()
  62. s.conn = conn
  63. s.cancel = cancel
  64. s.wg = wg
  65. s.mu.Unlock()
  66. kvc := pb.NewKVClient(conn)
  67. for i := 0; i < s.N; i++ {
  68. go func(i int) {
  69. defer wg.Done()
  70. for {
  71. // TODO: 10-second is enough timeout to cover leader failure
  72. // and immediate leader election. Find out what other cases this
  73. // could be timed out.
  74. putctx, putcancel := context.WithTimeout(ctx, 10*time.Second)
  75. _, err := kvc.Put(putctx, &pb.PutRequest{
  76. Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))),
  77. Value: []byte(randStr(s.KeySize)),
  78. })
  79. putcancel()
  80. if err != nil {
  81. return
  82. }
  83. s.mu.Lock()
  84. s.success++
  85. s.mu.Unlock()
  86. }
  87. }(i)
  88. }
  89. <-ctx.Done()
  90. return nil
  91. }
  92. func (s *stresser) Cancel() {
  93. s.mu.Lock()
  94. cancel, conn, wg := s.cancel, s.conn, s.wg
  95. s.mu.Unlock()
  96. cancel()
  97. wg.Wait()
  98. conn.Close()
  99. }
  100. func (s *stresser) Report() (int, int) {
  101. s.mu.Lock()
  102. defer s.mu.Unlock()
  103. // TODO: find a better way to report v3 tests
  104. return s.success, -1
  105. }
  106. type stresserV2 struct {
  107. Endpoint string
  108. KeySize int
  109. KeySuffixRange int
  110. N int
  111. // TODO: not implemented
  112. Interval time.Duration
  113. mu sync.Mutex
  114. failure int
  115. success int
  116. cancel func()
  117. }
  118. func (s *stresserV2) Stress() error {
  119. cfg := clientV2.Config{
  120. Endpoints: []string{s.Endpoint},
  121. Transport: &http.Transport{
  122. Dial: (&net.Dialer{
  123. Timeout: time.Second,
  124. KeepAlive: 30 * time.Second,
  125. }).Dial,
  126. MaxIdleConnsPerHost: s.N,
  127. },
  128. }
  129. c, err := clientV2.New(cfg)
  130. if err != nil {
  131. return err
  132. }
  133. kv := clientV2.NewKeysAPI(c)
  134. ctx, cancel := context.WithCancel(context.Background())
  135. s.cancel = cancel
  136. for i := 0; i < s.N; i++ {
  137. go func() {
  138. for {
  139. setctx, setcancel := context.WithTimeout(ctx, clientV2.DefaultRequestTimeout)
  140. key := fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))
  141. _, err := kv.Set(setctx, key, randStr(s.KeySize), nil)
  142. setcancel()
  143. if err == context.Canceled {
  144. return
  145. }
  146. s.mu.Lock()
  147. if err != nil {
  148. s.failure++
  149. } else {
  150. s.success++
  151. }
  152. s.mu.Unlock()
  153. }
  154. }()
  155. }
  156. <-ctx.Done()
  157. return nil
  158. }
  159. func (s *stresserV2) Cancel() {
  160. s.cancel()
  161. }
  162. func (s *stresserV2) Report() (success int, failure int) {
  163. s.mu.Lock()
  164. defer s.mu.Unlock()
  165. return s.success, s.failure
  166. }
  167. func randStr(size int) string {
  168. data := make([]byte, size)
  169. for i := 0; i < size; i++ {
  170. data[i] = byte(int('a') + rand.Intn(26))
  171. }
  172. return string(data)
  173. }