benchmark: add primivites benchmark for Unlocking via defer vs. inline (#1534)
diff --git a/benchmark/primitives/primitives_test.go b/benchmark/primitives/primitives_test.go
index 1570c87..30e5c33 100644
--- a/benchmark/primitives/primitives_test.go
+++ b/benchmark/primitives/primitives_test.go
@@ -136,3 +136,54 @@
b.Fatal("error")
}
}
+
+func BenchmarkMutexWithDefer(b *testing.B) {
+ c := sync.Mutex{}
+ x := 0
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ func() {
+ c.Lock()
+ defer c.Unlock()
+ x++
+ }()
+ }
+ b.StopTimer()
+ if x != b.N {
+ b.Fatal("error")
+ }
+}
+
+func BenchmarkMutexWithClosureDefer(b *testing.B) {
+ c := sync.Mutex{}
+ x := 0
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ func() {
+ c.Lock()
+ defer func() { c.Unlock() }()
+ x++
+ }()
+ }
+ b.StopTimer()
+ if x != b.N {
+ b.Fatal("error")
+ }
+}
+
+func BenchmarkMutexWithoutDefer(b *testing.B) {
+ c := sync.Mutex{}
+ x := 0
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ func() {
+ c.Lock()
+ x++
+ c.Unlock()
+ }()
+ }
+ b.StopTimer()
+ if x != b.N {
+ b.Fatal("error")
+ }
+}