go.mway.dev/x@v0.0.0-20240520034138-950aede9a3fb/sync/waitgroup_benchmark_test.go (about)

     1  // Copyright (c) 2022 Matt Way
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to
     5  // deal in the Software without restriction, including without limitation the
     6  // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
     7  // sell copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    18  // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    19  // IN THE THE SOFTWARE.
    20  
    21  package sync_test
    22  
    23  import (
    24  	"math"
    25  	"sync"
    26  	"testing"
    27  
    28  	xsync "go.mway.dev/x/sync"
    29  )
    30  
    31  func BenchmarkWaitGroupAdd(b *testing.B) {
    32  	b.Run("sync", func(b *testing.B) {
    33  		b.ReportAllocs()
    34  		b.ResetTimer()
    35  
    36  		b.RunParallel(func(pb *testing.PB) {
    37  			wg := &sync.WaitGroup{}
    38  			for pb.Next() {
    39  				wg.Add(1)
    40  			}
    41  		})
    42  	})
    43  
    44  	b.Run("xsync", func(b *testing.B) {
    45  		b.ReportAllocs()
    46  		b.ResetTimer()
    47  
    48  		b.RunParallel(func(pb *testing.PB) {
    49  			wg := &xsync.WaitGroup{}
    50  			for pb.Next() {
    51  				wg.Add(1)
    52  			}
    53  		})
    54  	})
    55  }
    56  
    57  func BenchmarkWaitGroupDone(b *testing.B) {
    58  	var wg xsync.WaitGroup
    59  	wg.Add(math.MaxInt32)
    60  
    61  	b.ReportAllocs()
    62  	b.ResetTimer()
    63  
    64  	for i := 0; i < b.N; i++ {
    65  		wg.Done()
    66  	}
    67  }