From a2aef6ba209dc8e4188ae1763b078bd1a9a68de6 Mon Sep 17 00:00:00 2001 From: lukechampine Date: Thu, 31 Mar 2016 01:13:43 -0400 Subject: replace RWMutex benchmarks with channel benchmarks --- stm_test.go | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'stm_test.go') diff --git a/stm_test.go b/stm_test.go index c48d704..5849fcf 100644 --- a/stm_test.go +++ b/stm_test.go @@ -140,24 +140,21 @@ func BenchmarkIncrementMutex(b *testing.B) { } } -func BenchmarkIncrementRWMutex(b *testing.B) { +func BenchmarkIncrementChannel(b *testing.B) { for i := 0; i < b.N; i++ { - var mu sync.RWMutex - x := 0 + c := make(chan int, 1) + c <- 0 for i := 0; i < 1000; i++ { go func() { - mu.Lock() - x++ - mu.Unlock() + c <- 1 + <-c }() } for { - mu.RLock() - read := x - mu.RUnlock() + read := <-c if read == 1000 { break } + c <- read } } } @@ -195,17 +192,15 @@ func BenchmarkReadVarMutex(b *testing.B) { } } -func BenchmarkReadVarRWMutex(b *testing.B) { +func BenchmarkReadVarChannel(b *testing.B) { for i := 0; i < b.N; i++ { - var mu sync.RWMutex var wg sync.WaitGroup wg.Add(1000) - x := 0 + c := make(chan int) + close(c) for i := 0; i < 1000; i++ { go func() { - mu.RLock() - _ = x - mu.RUnlock() + <-c wg.Done() }() } -- cgit v1.2.3