v2_stresser.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. atomicModifiedKey int64
  35. cancel func()
  36. }
  37. func (s *v2Stresser) Stress() error {
  38. cfg := clientV2.Config{
  39. Endpoints: []string{s.Endpoint},
  40. Transport: &http.Transport{
  41. Dial: (&net.Dialer{
  42. Timeout: time.Second,
  43. KeepAlive: 30 * time.Second,
  44. }).Dial,
  45. MaxIdleConnsPerHost: s.N,
  46. },
  47. }
  48. c, err := clientV2.New(cfg)
  49. if err != nil {
  50. return err
  51. }
  52. kv := clientV2.NewKeysAPI(c)
  53. ctx, cancel := context.WithCancel(context.Background())
  54. s.cancel = cancel
  55. s.wg.Add(s.N)
  56. for i := 0; i < s.N; i++ {
  57. go func() {
  58. defer s.wg.Done()
  59. s.run(ctx, kv)
  60. }()
  61. }
  62. return nil
  63. }
  64. func (s *v2Stresser) run(ctx context.Context, kv clientV2.KeysAPI) {
  65. for {
  66. if err := s.rateLimiter.Wait(ctx); err == context.Canceled {
  67. return
  68. }
  69. setctx, setcancel := context.WithTimeout(ctx, clientV2.DefaultRequestTimeout)
  70. key := fmt.Sprintf("foo%016x", rand.Intn(s.keySuffixRange))
  71. _, err := kv.Set(setctx, key, string(randBytes(s.keySize)), nil)
  72. if err == nil {
  73. atomic.AddInt64(&s.atomicModifiedKey, 1)
  74. }
  75. setcancel()
  76. if err == context.Canceled {
  77. return
  78. }
  79. }
  80. }
  81. func (s *v2Stresser) Pause() {
  82. s.cancel()
  83. s.wg.Wait()
  84. }
  85. func (s *v2Stresser) Close() {
  86. s.Pause()
  87. }
  88. func (s *v2Stresser) ModifiedKeys() int64 {
  89. return atomic.LoadInt64(&s.atomicModifiedKey)
  90. }
  91. func (s *v2Stresser) Checker() Checker { return nil }
  92. func randBytes(size int) []byte {
  93. data := make([]byte, size)
  94. for i := 0; i < size; i++ {
  95. data[i] = byte(int('a') + rand.Intn(26))
  96. }
  97. return data
  98. }