mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-07 18:50:27 +08:00
优化ttlcache相关代码
This commit is contained in:
52
internal/utils/ttlcache/utils_test.go
Normal file
52
internal/utils/ttlcache/utils_test.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package ttlcache_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils/ttlcache"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/zero"
|
||||
"github.com/cespare/xxhash/v2"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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++ {
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user