stresser.go 4.1 KB

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