Skip to content

Commit e7c53e7

Browse files
committed
Added a benchmark
Signed-off-by: Alun Evans <[email protected]>
1 parent ddbc439 commit e7c53e7

File tree

1 file changed

+46
-10
lines changed

1 file changed

+46
-10
lines changed

map_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func TestBatchPerCPUHash(t *testing.T) {
106106
}
107107
defer m.Close()
108108

109-
possibleCPUs, err := internal.PossibleCPUs()
109+
possibleCPUs, err := PossibleCPU()
110110
if err != nil {
111111
t.Fatal(err)
112112
}
@@ -1982,38 +1982,74 @@ func BenchmarkMarshaling(b *testing.B) {
19821982
}
19831983

19841984
func BenchmarkPerCPUMarshalling(b *testing.B) {
1985+
batch := 1024
19851986
newMap := func(valueSize uint32) *Map {
19861987
m, err := NewMap(&MapSpec{
19871988
Type: PerCPUHash,
19881989
KeySize: 8,
19891990
ValueSize: valueSize,
1990-
MaxEntries: 1,
1991+
MaxEntries: uint32(batch),
19911992
})
19921993
if err != nil {
19931994
b.Fatal(err)
19941995
}
19951996
return m
19961997
}
19971998

1998-
key := uint64(1)
1999-
val := []uint64{1, 2, 3, 4, 5, 6, 7, 8}
1999+
possibleCPUs, err := PossibleCPU()
2000+
if err != nil {
2001+
b.Fatal(err)
2002+
}
2003+
2004+
val := make([]uint64, possibleCPUs)
2005+
for i := range val {
2006+
val[i] = uint64(i + 1)
2007+
}
20002008

20012009
m := newMap(8)
2002-
if err := m.Put(key, val[0:]); err != nil {
2003-
b.Fatal(err)
2010+
for i := 0; i < batch; i++ {
2011+
key := uint64(i + 1)
2012+
if err := m.Put(key, val); err != nil {
2013+
b.Fatal(err)
2014+
}
20042015
}
20052016
b.Cleanup(func() { m.Close() })
20062017

2007-
b.Run("reflection", func(b *testing.B) {
2018+
b.Run("Lookup", func(b *testing.B) {
20082019
b.ReportAllocs()
20092020
b.ResetTimer()
20102021

20112022
var value []uint64
20122023

20132024
for i := 0; i < b.N; i++ {
2014-
err := m.Lookup(unsafe.Pointer(&key), &value)
2015-
if err != nil {
2016-
b.Fatal("Can't get key:", err)
2025+
for j := 0; j < batch; j++ {
2026+
key := uint64(j + 1)
2027+
err := m.Lookup(unsafe.Pointer(&key), &value)
2028+
if err != nil {
2029+
b.Fatal("Can't get key:", err)
2030+
}
2031+
}
2032+
}
2033+
})
2034+
b.Run("BatchLookup", func(b *testing.B) {
2035+
b.ReportAllocs()
2036+
b.ResetTimer()
2037+
2038+
var (
2039+
keys = make([]uint64, batch)
2040+
values = make([]uint64, batch*possibleCPUs)
2041+
)
2042+
2043+
for i := 0; i < b.N; i++ {
2044+
var key uint64
2045+
count, err := m.BatchLookup(nil, unsafe.Pointer(&key), keys, values, &BatchOptions{})
2046+
if count <= 0 {
2047+
if err != nil {
2048+
b.Fatal("Can't get batch:", err)
2049+
}
2050+
}
2051+
if count != batch {
2052+
b.Fatalf("Can't get batch: %d", count)
20172053
}
20182054
}
20192055
})

0 commit comments

Comments
 (0)