stresser.go 3.6 KB

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