primitives_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. *
  3. * Copyright 2017 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package primitives_test contains benchmarks for various synchronization primitives
  19. // available in Go.
  20. package primitives_test
  21. import (
  22. "fmt"
  23. "sync"
  24. "sync/atomic"
  25. "testing"
  26. "time"
  27. "unsafe"
  28. )
  29. func BenchmarkSelectClosed(b *testing.B) {
  30. c := make(chan struct{})
  31. close(c)
  32. x := 0
  33. b.ResetTimer()
  34. for i := 0; i < b.N; i++ {
  35. select {
  36. case <-c:
  37. x++
  38. default:
  39. }
  40. }
  41. b.StopTimer()
  42. if x != b.N {
  43. b.Fatal("error")
  44. }
  45. }
  46. func BenchmarkSelectOpen(b *testing.B) {
  47. c := make(chan struct{})
  48. x := 0
  49. b.ResetTimer()
  50. for i := 0; i < b.N; i++ {
  51. select {
  52. case <-c:
  53. default:
  54. x++
  55. }
  56. }
  57. b.StopTimer()
  58. if x != b.N {
  59. b.Fatal("error")
  60. }
  61. }
  62. func BenchmarkAtomicBool(b *testing.B) {
  63. c := int32(0)
  64. x := 0
  65. b.ResetTimer()
  66. for i := 0; i < b.N; i++ {
  67. if atomic.LoadInt32(&c) == 0 {
  68. x++
  69. }
  70. }
  71. b.StopTimer()
  72. if x != b.N {
  73. b.Fatal("error")
  74. }
  75. }
  76. func BenchmarkAtomicValueLoad(b *testing.B) {
  77. c := atomic.Value{}
  78. c.Store(0)
  79. x := 0
  80. b.ResetTimer()
  81. for i := 0; i < b.N; i++ {
  82. if c.Load().(int) == 0 {
  83. x++
  84. }
  85. }
  86. b.StopTimer()
  87. if x != b.N {
  88. b.Fatal("error")
  89. }
  90. }
  91. func BenchmarkAtomicValueStore(b *testing.B) {
  92. c := atomic.Value{}
  93. v := 123
  94. b.ResetTimer()
  95. for i := 0; i < b.N; i++ {
  96. c.Store(v)
  97. }
  98. b.StopTimer()
  99. }
  100. func BenchmarkMutex(b *testing.B) {
  101. c := sync.Mutex{}
  102. x := 0
  103. b.ResetTimer()
  104. for i := 0; i < b.N; i++ {
  105. c.Lock()
  106. x++
  107. c.Unlock()
  108. }
  109. b.StopTimer()
  110. if x != b.N {
  111. b.Fatal("error")
  112. }
  113. }
  114. func BenchmarkRWMutex(b *testing.B) {
  115. c := sync.RWMutex{}
  116. x := 0
  117. b.ResetTimer()
  118. for i := 0; i < b.N; i++ {
  119. c.RLock()
  120. x++
  121. c.RUnlock()
  122. }
  123. b.StopTimer()
  124. if x != b.N {
  125. b.Fatal("error")
  126. }
  127. }
  128. func BenchmarkRWMutexW(b *testing.B) {
  129. c := sync.RWMutex{}
  130. x := 0
  131. b.ResetTimer()
  132. for i := 0; i < b.N; i++ {
  133. c.Lock()
  134. x++
  135. c.Unlock()
  136. }
  137. b.StopTimer()
  138. if x != b.N {
  139. b.Fatal("error")
  140. }
  141. }
  142. func BenchmarkMutexWithDefer(b *testing.B) {
  143. c := sync.Mutex{}
  144. x := 0
  145. b.ResetTimer()
  146. for i := 0; i < b.N; i++ {
  147. func() {
  148. c.Lock()
  149. defer c.Unlock()
  150. x++
  151. }()
  152. }
  153. b.StopTimer()
  154. if x != b.N {
  155. b.Fatal("error")
  156. }
  157. }
  158. func BenchmarkMutexWithClosureDefer(b *testing.B) {
  159. c := sync.Mutex{}
  160. x := 0
  161. b.ResetTimer()
  162. for i := 0; i < b.N; i++ {
  163. func() {
  164. c.Lock()
  165. defer func() { c.Unlock() }()
  166. x++
  167. }()
  168. }
  169. b.StopTimer()
  170. if x != b.N {
  171. b.Fatal("error")
  172. }
  173. }
  174. func BenchmarkMutexWithoutDefer(b *testing.B) {
  175. c := sync.Mutex{}
  176. x := 0
  177. b.ResetTimer()
  178. for i := 0; i < b.N; i++ {
  179. func() {
  180. c.Lock()
  181. x++
  182. c.Unlock()
  183. }()
  184. }
  185. b.StopTimer()
  186. if x != b.N {
  187. b.Fatal("error")
  188. }
  189. }
  190. func BenchmarkAtomicAddInt64(b *testing.B) {
  191. var c int64
  192. b.ResetTimer()
  193. for i := 0; i < b.N; i++ {
  194. atomic.AddInt64(&c, 1)
  195. }
  196. b.StopTimer()
  197. if c != int64(b.N) {
  198. b.Fatal("error")
  199. }
  200. }
  201. func BenchmarkAtomicTimeValueStore(b *testing.B) {
  202. var c atomic.Value
  203. t := time.Now()
  204. b.ResetTimer()
  205. for i := 0; i < b.N; i++ {
  206. c.Store(t)
  207. }
  208. b.StopTimer()
  209. }
  210. func BenchmarkAtomic16BValueStore(b *testing.B) {
  211. var c atomic.Value
  212. t := struct {
  213. a int64
  214. b int64
  215. }{
  216. 123, 123,
  217. }
  218. b.ResetTimer()
  219. for i := 0; i < b.N; i++ {
  220. c.Store(t)
  221. }
  222. b.StopTimer()
  223. }
  224. func BenchmarkAtomic32BValueStore(b *testing.B) {
  225. var c atomic.Value
  226. t := struct {
  227. a int64
  228. b int64
  229. c int64
  230. d int64
  231. }{
  232. 123, 123, 123, 123,
  233. }
  234. b.ResetTimer()
  235. for i := 0; i < b.N; i++ {
  236. c.Store(t)
  237. }
  238. b.StopTimer()
  239. }
  240. func BenchmarkAtomicPointerStore(b *testing.B) {
  241. t := 123
  242. var up unsafe.Pointer
  243. b.ResetTimer()
  244. for i := 0; i < b.N; i++ {
  245. atomic.StorePointer(&up, unsafe.Pointer(&t))
  246. }
  247. b.StopTimer()
  248. }
  249. func BenchmarkAtomicTimePointerStore(b *testing.B) {
  250. t := time.Now()
  251. var up unsafe.Pointer
  252. b.ResetTimer()
  253. for i := 0; i < b.N; i++ {
  254. atomic.StorePointer(&up, unsafe.Pointer(&t))
  255. }
  256. b.StopTimer()
  257. }
  258. func BenchmarkStoreContentionWithAtomic(b *testing.B) {
  259. t := 123
  260. var c unsafe.Pointer
  261. b.RunParallel(func(pb *testing.PB) {
  262. for pb.Next() {
  263. atomic.StorePointer(&c, unsafe.Pointer(&t))
  264. }
  265. })
  266. }
  267. func BenchmarkStoreContentionWithMutex(b *testing.B) {
  268. t := 123
  269. var mu sync.Mutex
  270. var c int
  271. b.RunParallel(func(pb *testing.PB) {
  272. for pb.Next() {
  273. mu.Lock()
  274. c = t
  275. mu.Unlock()
  276. }
  277. })
  278. _ = c
  279. }
  280. type dummyStruct struct {
  281. a int64
  282. b time.Time
  283. }
  284. func BenchmarkStructStoreContention(b *testing.B) {
  285. d := dummyStruct{}
  286. dp := unsafe.Pointer(&d)
  287. t := time.Now()
  288. for _, j := range []int{100000000, 10000, 0} {
  289. for _, i := range []int{100000, 10} {
  290. b.Run(fmt.Sprintf("CAS/%v/%v", j, i), func(b *testing.B) {
  291. b.SetParallelism(i)
  292. b.RunParallel(func(pb *testing.PB) {
  293. n := &dummyStruct{
  294. b: t,
  295. }
  296. for pb.Next() {
  297. for y := 0; y < j; y++ {
  298. }
  299. for {
  300. v := (*dummyStruct)(atomic.LoadPointer(&dp))
  301. n.a = v.a + 1
  302. if atomic.CompareAndSwapPointer(&dp, unsafe.Pointer(v), unsafe.Pointer(n)) {
  303. n = v
  304. break
  305. }
  306. }
  307. }
  308. })
  309. })
  310. }
  311. }
  312. var mu sync.Mutex
  313. for _, j := range []int{100000000, 10000, 0} {
  314. for _, i := range []int{100000, 10} {
  315. b.Run(fmt.Sprintf("Mutex/%v/%v", j, i), func(b *testing.B) {
  316. b.SetParallelism(i)
  317. b.RunParallel(func(pb *testing.PB) {
  318. for pb.Next() {
  319. for y := 0; y < j; y++ {
  320. }
  321. mu.Lock()
  322. d.a++
  323. d.b = t
  324. mu.Unlock()
  325. }
  326. })
  327. })
  328. }
  329. }
  330. }
  331. type myFooer struct{}
  332. func (myFooer) Foo() {}
  333. type fooer interface {
  334. Foo()
  335. }
  336. func BenchmarkInterfaceTypeAssertion(b *testing.B) {
  337. // Call a separate function to avoid compiler optimizations.
  338. runInterfaceTypeAssertion(b, myFooer{})
  339. }
  340. func runInterfaceTypeAssertion(b *testing.B, fer interface{}) {
  341. x := 0
  342. b.ResetTimer()
  343. for i := 0; i < b.N; i++ {
  344. if _, ok := fer.(fooer); ok {
  345. x++
  346. }
  347. }
  348. b.StopTimer()
  349. if x != b.N {
  350. b.Fatal("error")
  351. }
  352. }
  353. func BenchmarkStructTypeAssertion(b *testing.B) {
  354. // Call a separate function to avoid compiler optimizations.
  355. runStructTypeAssertion(b, myFooer{})
  356. }
  357. func runStructTypeAssertion(b *testing.B, fer interface{}) {
  358. x := 0
  359. b.ResetTimer()
  360. for i := 0; i < b.N; i++ {
  361. if _, ok := fer.(myFooer); ok {
  362. x++
  363. }
  364. }
  365. b.StopTimer()
  366. if x != b.N {
  367. b.Fatal("error")
  368. }
  369. }