v2_stresser.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2016 The etcd Authors
  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. "context"
  17. "fmt"
  18. "math/rand"
  19. "net"
  20. "net/http"
  21. "sync"
  22. "sync/atomic"
  23. "time"
  24. "golang.org/x/time/rate"
  25. clientV2 "github.com/coreos/etcd/client"
  26. )
  27. type v2Stresser struct {
  28. Endpoint string
  29. keySize int
  30. keySuffixRange int
  31. N int
  32. rateLimiter *rate.Limiter
  33. wg sync.WaitGroup
  34. mu sync.Mutex
  35. atomicModifiedKey int64
  36. cancel func()
  37. }
  38. func (s *v2Stresser) Stress() error {
  39. cfg := clientV2.Config{
  40. Endpoints: []string{s.Endpoint},
  41. Transport: &http.Transport{
  42. Dial: (&net.Dialer{
  43. Timeout: time.Second,
  44. KeepAlive: 30 * time.Second,
  45. }).Dial,
  46. MaxIdleConnsPerHost: s.N,
  47. },
  48. }
  49. c, err := clientV2.New(cfg)
  50. if err != nil {
  51. return err
  52. }
  53. kv := clientV2.NewKeysAPI(c)
  54. ctx, cancel := context.WithCancel(context.Background())
  55. s.cancel = cancel
  56. s.wg.Add(s.N)
  57. for i := 0; i < s.N; i++ {
  58. go func() {
  59. defer s.wg.Done()
  60. s.run(ctx, kv)
  61. }()
  62. }
  63. return nil
  64. }
  65. func (s *v2Stresser) run(ctx context.Context, kv clientV2.KeysAPI) {
  66. for {
  67. if err := s.rateLimiter.Wait(ctx); err == context.Canceled {
  68. return
  69. }
  70. setctx, setcancel := context.WithTimeout(ctx, clientV2.DefaultRequestTimeout)
  71. key := fmt.Sprintf("foo%016x", rand.Intn(s.keySuffixRange))
  72. _, err := kv.Set(setctx, key, string(randBytes(s.keySize)), nil)
  73. if err == nil {
  74. atomic.AddInt64(&s.atomicModifiedKey, 1)
  75. }
  76. setcancel()
  77. if err == context.Canceled {
  78. return
  79. }
  80. }
  81. }
  82. func (s *v2Stresser) Cancel() {
  83. s.cancel()
  84. s.wg.Wait()
  85. }
  86. func (s *v2Stresser) ModifiedKeys() int64 {
  87. return atomic.LoadInt64(&s.atomicModifiedKey)
  88. }
  89. func (s *v2Stresser) Checker() Checker { return nil }
  90. func randBytes(size int) []byte {
  91. data := make([]byte, size)
  92. for i := 0; i < size; i++ {
  93. data[i] = byte(int('a') + rand.Intn(26))
  94. }
  95. return data
  96. }