github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/functional/tester/stresser_composite.go (about)

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