Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/*
go test -bench Map -run Map -benchmem -benchtime 10s
BenchmarkMapNestedPopulate-4 50000000 493 ns/op 72 B/op 1 allocs/op
BenchmarkMapNestedQuery-4 50000000 336 ns/op 1 B/op 1 allocs/op
BenchmarkMapCompoundPopulate-4 30000000 723 ns/op 209 B/op 0 allocs/op
BenchmarkMapCompoundQuery-4 50000000 387 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkMapNestedPopulate(b *testing.B) {
m := make(map[string]map[int64]int64)
const tpCount = 20
const topic = "some-topic"
for i := 0; i < tpCount; i++ {
m[topic+":"+strconv.Itoa(i)] = make(map[int64]int64)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
m[topic+":"+strconv.Itoa(i%tpCount)][int64(i)] = int64(i * 2)
}
}
func BenchmarkMapNestedQuery(b *testing.B) {
m := make(map[string]map[int64]int64)
const tpCount = 20
const topic = "some-topic"
for i := 0; i < tpCount; i++ {
m[topic+":"+strconv.Itoa(i)] = make(map[int64]int64)
}
for i := 0; i < b.N; i++ {
m[topic+":"+strconv.Itoa(i%tpCount)][int64(i)] = int64(i * 2)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
m[topic+":"+strconv.Itoa(i%tpCount)][int64(i)]++
}
}
func BenchmarkMapCompoundPopulate(b *testing.B) {
type key struct {
topic string
partition int32
offset int64
}
const tpCount = 20
const topic = "some-topic"
m := make(map[key]int64)
b.ResetTimer()
for i := 0; i < b.N; i++ {
m[key{topic, int32(i % tpCount), int64(i)}] = int64(i * 2)
}
}
func BenchmarkMapCompoundQuery(b *testing.B) {
type key struct {
topic string
partition int32
offset int64
}
const tpCount = 20
const topic = "some-topic"
m := make(map[key]int64)
for i := 0; i < b.N; i++ {
m[key{topic, int32(i % tpCount), int64(i)}] = int64(i * 2)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
m[key{topic, int32(i % tpCount), int64(i)}]++
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment