stresser.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/client"
  24. )
  25. type Stresser interface {
  26. // Stress starts to stress the etcd cluster
  27. Stress() error
  28. // Cancel cancels the stress test on the etcd cluster
  29. Cancel()
  30. // Report reports the success and failure of the stress test
  31. Report() (success int, failure int)
  32. }
  33. type stresser struct {
  34. Endpoint string
  35. KeySize int
  36. KeySuffixRange int
  37. N int
  38. // TODO: not implemented
  39. Interval time.Duration
  40. mu sync.Mutex
  41. failure int
  42. success int
  43. cancel func()
  44. }
  45. func (s *stresser) Stress() error {
  46. cfg := client.Config{
  47. Endpoints: []string{s.Endpoint},
  48. Transport: &http.Transport{
  49. Dial: (&net.Dialer{
  50. Timeout: time.Second,
  51. KeepAlive: 30 * time.Second,
  52. }).Dial,
  53. MaxIdleConnsPerHost: s.N,
  54. },
  55. }
  56. c, err := client.New(cfg)
  57. if err != nil {
  58. return err
  59. }
  60. kv := client.NewKeysAPI(c)
  61. ctx, cancel := context.WithCancel(context.Background())
  62. s.cancel = cancel
  63. for i := 0; i < s.N; i++ {
  64. go func() {
  65. for {
  66. setctx, setcancel := context.WithTimeout(ctx, client.DefaultRequestTimeout)
  67. key := fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))
  68. _, err := kv.Set(setctx, key, randStr(s.KeySize), nil)
  69. setcancel()
  70. if err == context.Canceled {
  71. return
  72. }
  73. s.mu.Lock()
  74. if err != nil {
  75. s.failure++
  76. } else {
  77. s.success++
  78. }
  79. s.mu.Unlock()
  80. }
  81. }()
  82. }
  83. <-ctx.Done()
  84. return nil
  85. }
  86. func (s *stresser) Cancel() {
  87. s.cancel()
  88. }
  89. func (s *stresser) Report() (success int, failure int) {
  90. s.mu.Lock()
  91. defer s.mu.Unlock()
  92. return s.success, s.failure
  93. }
  94. func randStr(size int) string {
  95. data := make([]byte, size)
  96. for i := 0; i < size; i++ {
  97. data[i] = byte(int('a') + rand.Intn(26))
  98. }
  99. return string(data)
  100. }