优化xxhash和fnv相关代码

This commit is contained in:
GoEdgeLab
2024-04-18 18:25:33 +08:00
parent d2725fc713
commit 57c1ec5d19
12 changed files with 61 additions and 18 deletions

View File

@@ -1,19 +1,50 @@
package ttlcache
package ttlcache_test
import (
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
"github.com/TeaOSLab/EdgeNode/internal/zero"
"github.com/cespare/xxhash/v2"
"runtime"
"strconv"
"testing"
)
func BenchmarkHashKey(b *testing.B) {
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++ {
HashKey([]byte("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD"))
ttlcache.HashKeyBytes([]byte("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD"))
}
}
func BenchmarkHashKey2(b *testing.B) {
func BenchmarkHashKey_String(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
ttlcache.HashKeyString("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
}
}
func BenchmarkHashKey_XXHash(b *testing.B) {
runtime.GOMAXPROCS(1)
for i := 0; i < b.N; i++ {
xxhash.Sum64String("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")