diff options
author | lukechampine <luke.champine@gmail.com> | 2016-03-31 01:13:43 -0400 |
---|---|---|
committer | lukechampine <luke.champine@gmail.com> | 2016-03-31 01:40:44 -0400 |
commit | a2aef6ba209dc8e4188ae1763b078bd1a9a68de6 (patch) | |
tree | 745d48fa069b4d723d7bfea443c0865b93c95c5b | |
parent | tweak example (diff) | |
download | stm-a2aef6ba209dc8e4188ae1763b078bd1a9a68de6.tar.gz stm-a2aef6ba209dc8e4188ae1763b078bd1a9a68de6.tar.xz |
replace RWMutex benchmarks with channel benchmarks
-rw-r--r-- | stm_test.go | 25 |
1 files changed, 10 insertions, 15 deletions
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() }() } |