stress_composite.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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() {
  33. var wg sync.WaitGroup
  34. wg.Add(len(cs.stressers))
  35. for i := range cs.stressers {
  36. go func(s Stresser) {
  37. defer wg.Done()
  38. s.Pause()
  39. }(cs.stressers[i])
  40. }
  41. wg.Wait()
  42. }
  43. func (cs *compositeStresser) Close() {
  44. var wg sync.WaitGroup
  45. wg.Add(len(cs.stressers))
  46. for i := range cs.stressers {
  47. go func(s Stresser) {
  48. defer wg.Done()
  49. s.Close()
  50. }(cs.stressers[i])
  51. }
  52. wg.Wait()
  53. }
  54. func (cs *compositeStresser) ModifiedKeys() (modifiedKey int64) {
  55. for _, stress := range cs.stressers {
  56. modifiedKey += stress.ModifiedKeys()
  57. }
  58. return modifiedKey
  59. }
  60. func (cs *compositeStresser) Checker() Checker {
  61. var chks []Checker
  62. for _, s := range cs.stressers {
  63. if chk := s.Checker(); chk != nil {
  64. chks = append(chks, chk)
  65. }
  66. }
  67. if len(chks) == 0 {
  68. return nil
  69. }
  70. return newCompositeChecker(chks)
  71. }