gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/pkg/atomicbitops/atomicbitops_benchmark_test.go (about)

     1  // Copyright 2018 The gVisor 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 atomicbitops
    16  
    17  import (
    18  	"testing"
    19  )
    20  
    21  func BenchmarkAndUint32(b *testing.B) {
    22  	val32 := FromUint32(0xfffffff)
    23  	for i := 0; i < b.N; i++ {
    24  		AndUint32(&val32, uint32(i))
    25  	}
    26  }
    27  
    28  func BenchmarkAndUint64(b *testing.B) {
    29  	val64 := FromUint64(0xfffffffffffffff)
    30  	for i := 0; i < b.N; i++ {
    31  		AndUint64(&val64, uint64(i))
    32  	}
    33  }
    34  
    35  func BenchmarkAndUint32Parallel(b *testing.B) {
    36  	val32 := FromUint32(0xfffffff)
    37  	b.RunParallel(func(pb *testing.PB) {
    38  		i := uint32(0)
    39  		for pb.Next() {
    40  			AndUint32(&val32, i)
    41  			i++
    42  		}
    43  	})
    44  }
    45  
    46  func BenchmarkAndUint64Parallel(b *testing.B) {
    47  	val64 := FromUint64(0xfffffffffffffff)
    48  	b.RunParallel(func(pb *testing.PB) {
    49  		i := uint64(0)
    50  		for pb.Next() {
    51  			AndUint64(&val64, i)
    52  			i++
    53  		}
    54  	})
    55  }
    56  
    57  func BenchmarkOrUint32(b *testing.B) {
    58  	val32 := FromUint32(0xfffffff)
    59  	for i := 0; i < b.N; i++ {
    60  		OrUint32(&val32, uint32(i))
    61  	}
    62  }
    63  
    64  func BenchmarkOrUint64(b *testing.B) {
    65  	val64 := FromUint64(0xfffffffffffffff)
    66  	for i := 0; i < b.N; i++ {
    67  		OrUint64(&val64, uint64(i))
    68  	}
    69  }
    70  
    71  func BenchmarkOrUint32Parallel(b *testing.B) {
    72  	val32 := FromUint32(0xfffffff)
    73  	b.RunParallel(func(pb *testing.PB) {
    74  		i := uint32(0)
    75  		for pb.Next() {
    76  			OrUint32(&val32, i)
    77  			i++
    78  		}
    79  	})
    80  }
    81  
    82  func BenchmarkOrUint64Parallel(b *testing.B) {
    83  	val64 := FromUint64(0xfffffffffffffff)
    84  	b.RunParallel(func(pb *testing.PB) {
    85  		i := uint64(0)
    86  		for pb.Next() {
    87  			OrUint64(&val64, i)
    88  			i++
    89  		}
    90  	})
    91  }
    92  
    93  func BenchmarkXorUint32(b *testing.B) {
    94  	val32 := FromUint32(0xfffffff)
    95  	for i := 0; i < b.N; i++ {
    96  		XorUint32(&val32, uint32(i))
    97  	}
    98  }
    99  
   100  func BenchmarkXorUint64(b *testing.B) {
   101  	val64 := FromUint64(0xfffffffffffffff)
   102  	for i := 0; i < b.N; i++ {
   103  		XorUint64(&val64, uint64(i))
   104  	}
   105  }
   106  
   107  func BenchmarkXorUint32Parallel(b *testing.B) {
   108  	val32 := FromUint32(0xfffffff)
   109  	b.RunParallel(func(pb *testing.PB) {
   110  		i := uint32(0)
   111  		for pb.Next() {
   112  			XorUint32(&val32, i)
   113  			i++
   114  		}
   115  	})
   116  }
   117  
   118  func BenchmarkXorUint64Parallel(b *testing.B) {
   119  	val64 := FromUint64(0xfffffffffffffff)
   120  	b.RunParallel(func(pb *testing.PB) {
   121  		i := uint64(0)
   122  		for pb.Next() {
   123  			XorUint64(&val64, i)
   124  			i++
   125  		}
   126  	})
   127  }
   128  
   129  func BenchmarkCompareAndSwapUint32(b *testing.B) {
   130  	x := FromUint32(1)
   131  	ptr := &x
   132  	b.RunParallel(func(pb *testing.PB) {
   133  		for pb.Next() {
   134  			CompareAndSwapUint32(ptr, 1, 0)
   135  			CompareAndSwapUint32(ptr, 0, 1)
   136  		}
   137  	})
   138  }
   139  
   140  func BenchmarkCompareAndSwapUint64(b *testing.B) {
   141  	x := FromUint64(1)
   142  	ptr := &x
   143  	b.RunParallel(func(pb *testing.PB) {
   144  		for pb.Next() {
   145  			CompareAndSwapUint64(ptr, 1, 0)
   146  			CompareAndSwapUint64(ptr, 0, 1)
   147  		}
   148  	})
   149  }
   150  
   151  func BenchmarkCompareAndSwapUint32Parallel(b *testing.B) {
   152  	x := FromUint32(1)
   153  	ptr := &x
   154  	b.RunParallel(func(pb *testing.PB) {
   155  		for pb.Next() {
   156  			CompareAndSwapUint32(ptr, 1, 0)
   157  			CompareAndSwapUint32(ptr, 0, 1)
   158  		}
   159  	})
   160  }
   161  
   162  func BenchmarkCompareAndSwapUint64Parallel(b *testing.B) {
   163  	x := FromUint64(1)
   164  	ptr := &x
   165  	b.RunParallel(func(pb *testing.PB) {
   166  		for pb.Next() {
   167  			CompareAndSwapUint64(ptr, 1, 0)
   168  			CompareAndSwapUint64(ptr, 0, 1)
   169  		}
   170  	})
   171  }