stresser_composite.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2018 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 tester
  15. import "sync"
  16. // compositeStresser implements a Stresser that runs a slice of
  17. // stressing clients concurrently.
  18. type compositeStresser struct {
  19. stressers []Stresser
  20. }
  21. func (cs *compositeStresser) Stress() error {
  22. for i, s := range cs.stressers {
  23. if err := s.Stress(); err != nil {
  24. for j := 0; j < i; j++ {
  25. cs.stressers[j].Close()
  26. }
  27. return err
  28. }
  29. }
  30. return nil
  31. }
  32. func (cs *compositeStresser) Pause() (ems map[string]int) {
  33. var emu sync.Mutex
  34. ems = make(map[string]int)
  35. var wg sync.WaitGroup
  36. wg.Add(len(cs.stressers))
  37. for i := range cs.stressers {
  38. go func(s Stresser) {
  39. defer wg.Done()
  40. errs := s.Pause()
  41. for k, v := range errs {
  42. emu.Lock()
  43. ems[k] += v
  44. emu.Unlock()
  45. }
  46. }(cs.stressers[i])
  47. }
  48. wg.Wait()
  49. return ems
  50. }
  51. func (cs *compositeStresser) Close() (ems map[string]int) {
  52. var emu sync.Mutex
  53. ems = make(map[string]int)
  54. var wg sync.WaitGroup
  55. wg.Add(len(cs.stressers))
  56. for i := range cs.stressers {
  57. go func(s Stresser) {
  58. defer wg.Done()
  59. errs := s.Close()
  60. for k, v := range errs {
  61. emu.Lock()
  62. ems[k] += v
  63. emu.Unlock()
  64. }
  65. }(cs.stressers[i])
  66. }
  67. wg.Wait()
  68. return ems
  69. }
  70. func (cs *compositeStresser) ModifiedKeys() (modifiedKey int64) {
  71. for _, stress := range cs.stressers {
  72. modifiedKey += stress.ModifiedKeys()
  73. }
  74. return modifiedKey
  75. }