stresser.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. putctx, putcancel := context.WithTimeout(ctx, 5*time.Second)
  72. _, err := kvc.Put(putctx, &pb.PutRequest{
  73. Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))),
  74. Value: []byte(randStr(s.KeySize)),
  75. })
  76. putcancel()
  77. if err != nil {
  78. return
  79. }
  80. s.mu.Lock()
  81. s.success++
  82. s.mu.Unlock()
  83. }
  84. }(i)
  85. }
  86. <-ctx.Done()
  87. return nil
  88. }
  89. func (s *stresser) Cancel() {
  90. s.mu.Lock()
  91. cancel, conn, wg := s.cancel, s.conn, s.wg
  92. s.mu.Unlock()
  93. cancel()
  94. wg.Wait()
  95. conn.Close()
  96. }
  97. func (s *stresser) Report() (int, int) {
  98. s.mu.Lock()
  99. defer s.mu.Unlock()
  100. // TODO: find a better way to report v3 tests
  101. return s.success, -1
  102. }
  103. type stresserV2 struct {
  104. Endpoint string
  105. KeySize int
  106. KeySuffixRange int
  107. N int
  108. // TODO: not implemented
  109. Interval time.Duration
  110. mu sync.Mutex
  111. failure int
  112. success int
  113. cancel func()
  114. }
  115. func (s *stresserV2) Stress() error {
  116. cfg := clientV2.Config{
  117. Endpoints: []string{s.Endpoint},
  118. Transport: &http.Transport{
  119. Dial: (&net.Dialer{
  120. Timeout: time.Second,
  121. KeepAlive: 30 * time.Second,
  122. }).Dial,
  123. MaxIdleConnsPerHost: s.N,
  124. },
  125. }
  126. c, err := clientV2.New(cfg)
  127. if err != nil {
  128. return err
  129. }
  130. kv := clientV2.NewKeysAPI(c)
  131. ctx, cancel := context.WithCancel(context.Background())
  132. s.cancel = cancel
  133. for i := 0; i < s.N; i++ {
  134. go func() {
  135. for {
  136. setctx, setcancel := context.WithTimeout(ctx, clientV2.DefaultRequestTimeout)
  137. key := fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))
  138. _, err := kv.Set(setctx, key, randStr(s.KeySize), nil)
  139. setcancel()
  140. if err == context.Canceled {
  141. return
  142. }
  143. s.mu.Lock()
  144. if err != nil {
  145. s.failure++
  146. } else {
  147. s.success++
  148. }
  149. s.mu.Unlock()
  150. }
  151. }()
  152. }
  153. <-ctx.Done()
  154. return nil
  155. }
  156. func (s *stresserV2) Cancel() {
  157. s.cancel()
  158. }
  159. func (s *stresserV2) Report() (success int, failure int) {
  160. s.mu.Lock()
  161. defer s.mu.Unlock()
  162. return s.success, s.failure
  163. }
  164. func randStr(size int) string {
  165. data := make([]byte, size)
  166. for i := 0; i < size; i++ {
  167. data[i] = byte(int('a') + rand.Intn(26))
  168. }
  169. return string(data)
  170. }