Last active
December 29, 2015 21:28
-
-
Save abursavich/7729965 to your computer and use it in GitHub Desktop.
benchmarking for https://github.com/gorilla/schema/pull/14
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
$ 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