Skip to content

Instantly share code, notes, and snippets.

@abursavich
Last active December 29, 2015 21:28
Show Gist options
  • Save abursavich/7729965 to your computer and use it in GitHub Desktop.
Save abursavich/7729965 to your computer and use it in GitHub Desktop.
/*
$ go test --bench .
PASS
Benchmark_TypeMap_Get-8 50000000 39.0 ns/op
Benchmark_RWMutex_RLock-8 50000000 48.4 ns/op
Benchmark_RWMutex_Lock-8 50000000 66.5 ns/op
Benchmark_Mutex_Lock-8 50000000 49.4 ns/op
Benchmark_Concurrent_RWMutex_RLock-8 50000000 39.5 ns/op
Benchmark_Concurrent_RWMutex_Lock-8 10000000 241 ns/op
Benchmark_Concurrent_Mutex_Lock-8 10000000 228 ns/op
ok github.com/abursavich/schema 17.593s
*/
package schema
import (
"reflect"
"runtime"
"sync"
"testing"
)
var (
c = runtime.NumCPU()
t = reflect.TypeOf((*struct{})(nil)).Elem()
m = map[reflect.Type]*structInfo{t: &structInfo{fields: make(map[string]*fieldInfo)}}
)
func init() {
runtime.GOMAXPROCS(c)
}
func Benchmark_TypeMap_Get(b *testing.B) {
for i := 0; i < b.N; i++ {
info := m[t]
_ = info // stop the compiler from complaining
}
}
func Benchmark_RWMutex_RLock(b *testing.B) {
var mu sync.RWMutex
for i := 0; i < b.N; i++ {
mu.RLock()
info := m[t]
_ = info // stop the compiler from complaining
mu.RUnlock()
}
}
func Benchmark_RWMutex_Lock(b *testing.B) {
var mu sync.RWMutex
for i := 0; i < b.N; i++ {
mu.Lock()
info := m[t]
_ = info // stop the compiler from complaining
mu.Unlock()
}
}
func Benchmark_Mutex_Lock(b *testing.B) {
var mu sync.Mutex
for i := 0; i < b.N; i++ {
mu.Lock()
info := m[t]
_ = info // stop the compiler from complaining
mu.Unlock()
}
}
func Benchmark_Concurrent_RWMutex_RLock(b *testing.B) {
var (
wait sync.WaitGroup
mu sync.RWMutex
n = b.N / c
)
wait.Add(c)
for x := 0; x < c; x++ {
go func() {
for i := 0; i < n; i++ {
mu.RLock()
info := m[t]
_ = info // stop the compiler from complaining
mu.RUnlock()
}
wait.Done()
}()
}
wait.Wait()
}
func Benchmark_Concurrent_RWMutex_Lock(b *testing.B) {
var (
wait sync.WaitGroup
mu sync.RWMutex
n = b.N / c
)
wait.Add(c)
for x := 0; x < c; x++ {
go func() {
for i := 0; i < n; i++ {
mu.Lock()
info := m[t]
_ = info // stop the compiler from complaining
mu.Unlock()
}
wait.Done()
}()
}
wait.Wait()
}
func Benchmark_Concurrent_Mutex_Lock(b *testing.B) {
var (
wait sync.WaitGroup
mu sync.Mutex
n = b.N / c
)
wait.Add(c)
for x := 0; x < c; x++ {
go func() {
for i := 0; i < n; i++ {
mu.Lock()
info := m[t]
_ = info // stop the compiler from complaining
mu.Unlock()
}
wait.Done()
}()
}
wait.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment