Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cacheverify
- import (
- "sync/atomic"
- "testing"
- )
- type CountersUnpadded struct {
- requests uint64
- errors uint64
- latency uint64
- timeouts uint64
- }
- type CountersPadded struct {
- requests uint64
- _ [56]byte
- errors uint64
- _ [56]byte
- latency uint64
- _ [56]byte
- timeouts uint64
- _ [56]byte
- }
- func BenchmarkFalseSharing(b *testing.B) {
- b.Run("unpadded", func(b *testing.B) {
- c := &CountersUnpadded{}
- targets := []*uint64{&c.requests, &c.errors, &c.latency, &c.timeouts}
- var workerID uint32
- b.RunParallel(func(pb *testing.PB) {
- id := atomic.AddUint32(&workerID, 1) - 1
- slot := targets[id%uint32(len(targets))]
- for pb.Next() {
- atomic.AddUint64(slot, 1)
- }
- })
- })
- b.Run("padded", func(b *testing.B) {
- c := &CountersPadded{}
- targets := []*uint64{&c.requests, &c.errors, &c.latency, &c.timeouts}
- var workerID uint32
- b.RunParallel(func(pb *testing.PB) {
- id := atomic.AddUint32(&workerID, 1) - 1
- slot := targets[id%uint32(len(targets))]
- for pb.Next() {
- atomic.AddUint64(slot, 1)
- }
- })
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment