github.com/moontrade/nogc@v0.1.7/collections/wormhole/wormhole_cgo_unsafe_test.go (about) 1 package wormhole 2 3 import ( 4 "fmt" 5 "github.com/moontrade/nogc" 6 "sync" 7 "sync/atomic" 8 "testing" 9 "unsafe" 10 ) 11 12 func TestNewQSBR(t *testing.T) { 13 q := NewQSBR() 14 println(q) 15 16 go func() { 17 var ref QSBRRef 18 _ = q.Register(&ref) 19 q.Park(&ref) 20 }() 21 22 go func() { 23 var ref QSBRRef 24 _ = q.Register(&ref) 25 q.Park(&ref) 26 q.Resume(&ref) 27 q.Wait(1) 28 }() 29 30 var r1 QSBRRef 31 fmt.Println(r1) 32 33 success := q.Register(&r1) 34 q.Park(&r1) 35 fmt.Println("Register:", success) 36 37 q.Resume(&r1) 38 q.Park(&r1) 39 40 fmt.Println(r1) 41 42 _ = q.Close() 43 println(unsafe.Sizeof(QSBRRef{})) 44 } 45 46 func TestNewWormhole(t *testing.T) { 47 wh := NewWormhole() 48 ref := wh.Ref() 49 println(ref) 50 51 var k int64 = 100 52 var key = nogc.FatPointerOfInt64(&k) 53 54 wh.Put(ref, key, key) 55 56 var val int64 = 0 57 var value = nogc.FatPointerOfInt64(&val) 58 59 wh.Get(ref, key, value) 60 61 fmt.Println(val) 62 } 63 64 func BenchmarkQSBR_Park(b *testing.B) { 65 b.Run("QSBR", func(b *testing.B) { 66 q := NewQSBR() 67 var ref QSBRRef 68 _ = q.Register(&ref) 69 q.Park(&ref) 70 71 b.ReportAllocs() 72 b.ResetTimer() 73 for i := 0; i < b.N; i++ { 74 q.Resume(&ref) 75 q.Park(&ref) 76 } 77 }) 78 79 b.Run("Go RWMutex.RLock", func(b *testing.B) { 80 var m sync.RWMutex 81 82 b.ReportAllocs() 83 b.ResetTimer() 84 for i := 0; i < b.N; i++ { 85 m.RLock() 86 m.RUnlock() 87 } 88 }) 89 90 b.Run("Go RWMutex.Lock", func(b *testing.B) { 91 var m sync.RWMutex 92 93 b.ReportAllocs() 94 b.ResetTimer() 95 for i := 0; i < b.N; i++ { 96 m.Lock() 97 m.Unlock() 98 } 99 }) 100 101 b.Run("Go Atomic", func(b *testing.B) { 102 var l int64 103 104 b.ReportAllocs() 105 b.ResetTimer() 106 for i := 0; i < b.N; i++ { 107 atomic.AddInt64(&l, 1) 108 atomic.AddInt64(&l, -1) 109 } 110 }) 111 } 112 113 func BenchmarkWormhole_Put(b *testing.B) { 114 wh := NewWormhole() 115 ref := wh.Ref() 116 117 b.ReportAllocs() 118 b.ResetTimer() 119 for i := 0; i < b.N; i++ { 120 var k = int64(i) 121 var key = nogc.FatPointerOfInt64(&k) 122 123 wh.Put(ref, key, key) 124 } 125 } 126 127 func BenchmarkWormhole_Get(b *testing.B) { 128 wh := NewWormhole() 129 ref := wh.Ref() 130 131 for i := 0; i < b.N; i++ { 132 var k = int64(i) 133 var key = nogc.FatPointerOfInt64(&k) 134 135 wh.Put(ref, key, key) 136 } 137 138 var k = int64(0) 139 var key = nogc.FatPointerOfInt64(&k) 140 141 var val int64 = 0 142 var value = nogc.FatPointerOfInt64(&val) 143 144 b.ReportAllocs() 145 b.ResetTimer() 146 for i := 0; i < b.N; i++ { 147 k = int64(i) 148 wh.Get(ref, key, value) 149 } 150 } 151 152 func BenchmarkUnsafe_Put(b *testing.B) { 153 wh := NewUnsafe() 154 155 b.ReportAllocs() 156 b.ResetTimer() 157 for i := 0; i < b.N; i++ { 158 var k = int64(i) 159 var key = nogc.FatPointerOfInt64(&k) 160 161 wh.Put(key, key) 162 } 163 } 164 165 func BenchmarkUnsafe_Get(b *testing.B) { 166 wh := NewUnsafe() 167 168 for i := 0; i < b.N; i++ { 169 var k = int64(i) 170 var key = nogc.FatPointerOfInt64(&k) 171 172 wh.Put(key, key) 173 } 174 175 var k = int64(0) 176 var key = nogc.FatPointerOfInt64(&k) 177 178 b.ReportAllocs() 179 b.ResetTimer() 180 for i := 0; i < b.N; i++ { 181 k = int64(i) 182 value := wh.Get(key) 183 _ = value 184 } 185 }