github.com/TeaOSLab/EdgeNode@v1.3.8/internal/ttlcache/utils_test.go (about)

     1  package ttlcache_test
     2  
     3  import (
     4  	"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
     5  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     6  	"github.com/TeaOSLab/EdgeNode/internal/zero"
     7  	"github.com/cespare/xxhash/v2"
     8  	"runtime"
     9  	"strconv"
    10  	"testing"
    11  )
    12  
    13  func TestHashCollision(t *testing.T) {
    14  	var m = map[uint64]zero.Zero{}
    15  
    16  	var count = 1_000
    17  	if testutils.IsSingleTesting() {
    18  		count = 100_000_000
    19  	}
    20  
    21  	for i := 0; i < count; i++ {
    22  		var k = ttlcache.HashKeyString(strconv.Itoa(i))
    23  		_, ok := m[k]
    24  		if ok {
    25  			t.Fatal("collision at", i)
    26  		}
    27  		m[k] = zero.New()
    28  	}
    29  
    30  	t.Log(len(m), "elements")
    31  }
    32  
    33  func BenchmarkHashKey_Bytes(b *testing.B) {
    34  	runtime.GOMAXPROCS(1)
    35  	for i := 0; i < b.N; i++ {
    36  		ttlcache.HashKeyBytes([]byte("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD"))
    37  	}
    38  }
    39  
    40  func BenchmarkHashKey_String(b *testing.B) {
    41  	runtime.GOMAXPROCS(1)
    42  	for i := 0; i < b.N; i++ {
    43  		ttlcache.HashKeyString("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
    44  	}
    45  }
    46  
    47  func BenchmarkHashKey_XXHash(b *testing.B) {
    48  	runtime.GOMAXPROCS(1)
    49  	for i := 0; i < b.N; i++ {
    50  		xxhash.Sum64String("HELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLDHELLO,WORLD")
    51  	}
    52  }