Files
EdgeNode/internal/utils/ttlcache/utils_test.go

53 lines
1.2 KiB
Go
Raw Normal View History

2024-04-18 18:25:33 +08:00
package ttlcache_test
import (
2024-04-18 18:25:33 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
2024-05-08 11:10:56 +08:00
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
2024-04-18 18:25:33 +08:00
"github.com/TeaOSLab/EdgeNode/internal/zero"
2024-04-17 17:18:11 +08:00
"github.com/cespare/xxhash/v2"
"runtime"
2024-04-18 18:25:33 +08:00
"strconv"
"testing"
)
2024-04-18 18:25:33 +08:00
func TestHashCollision(t *testing.T) {
var m = map[uint64]zero.Zero{}
var count = 1_000
if testutils.IsSingleTesting() {
count = 100_000_000
}
for i := 0; i < count; i++ {
var k = ttlcache.HashKeyString(strconv.Itoa(i))
_, ok := m[k]
if ok {
t.Fatal("collision at", i)
}
m[k] = zero.New()
}
t.Log(len(m), "elements")
}
func BenchmarkHashKey_Bytes(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
ttlcache.HashKeyBytes([]byte("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD"))
}
}
func BenchmarkHashKey_String(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
2024-04-18 18:25:33 +08:00
ttlcache.HashKeyString("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
}
}
2024-04-17 17:18:11 +08:00
2024-04-18 18:25:33 +08:00
func BenchmarkHashKey_XXHash(b *testing.B) {
2024-04-17 17:18:11 +08:00
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
xxhash.Sum64String("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
}
}