Guest User

Untitled

a guest
Oct 12th, 2025
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.11 KB | None | 0 0
  1. package cacheverify
  2.  
  3. import (
  4.     "sync/atomic"
  5.     "testing"
  6. )
  7.  
  8. type CountersUnpadded struct {
  9.     requests uint64
  10.     errors   uint64
  11.     latency  uint64
  12.     timeouts uint64
  13. }
  14.  
  15. type CountersPadded struct {
  16.     requests uint64
  17.     _        [56]byte
  18.     errors   uint64
  19.     _        [56]byte
  20.     latency  uint64
  21.     _        [56]byte
  22.     timeouts uint64
  23.     _        [56]byte
  24. }
  25.  
  26. func BenchmarkFalseSharing(b *testing.B) {
  27.     b.Run("unpadded", func(b *testing.B) {
  28.         c := &CountersUnpadded{}
  29.         targets := []*uint64{&c.requests, &c.errors, &c.latency, &c.timeouts}
  30.         var workerID uint32
  31.         b.RunParallel(func(pb *testing.PB) {
  32.             id := atomic.AddUint32(&workerID, 1) - 1
  33.             slot := targets[id%uint32(len(targets))]
  34.             for pb.Next() {
  35.                 atomic.AddUint64(slot, 1)
  36.             }
  37.         })
  38.     })
  39.  
  40.     b.Run("padded", func(b *testing.B) {
  41.         c := &CountersPadded{}
  42.         targets := []*uint64{&c.requests, &c.errors, &c.latency, &c.timeouts}
  43.         var workerID uint32
  44.         b.RunParallel(func(pb *testing.PB) {
  45.             id := atomic.AddUint32(&workerID, 1) - 1
  46.             slot := targets[id%uint32(len(targets))]
  47.             for pb.Next() {
  48.                 atomic.AddUint64(slot, 1)
  49.             }
  50.         })
  51.     })
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment