stresser.go 3.8 KB

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