package cmap

import (
	"fmt"
	"go-admin/pkg/utility"
	"hash/crc32"
	"testing"
)

// 离散性测试
func Test_fnv32(t *testing.T) {
	st := make(map[uint32]int)
	for i := 0; i < 1000000; i++ {
		fnv := crc32.ChecksumIEEE([]byte(utility.GenerateRandString(8)))
		k := fnv & 15
		count, ok := st[k]
		if !ok {
			st[k] = 1
		}
		st[k] = count + 1
	}
	for k, v := range st {
		fmt.Println(k, "\t", float64(v)/1000000)
	}
}

// go test -bench=_QE_ -benchmem -run=^$
// -benchtime 默认为1秒  -benchmem 获得内存分配的统计数据
// Benchmark_QE_1-6          146641              8192 ns/op              32 B/op          3 allocs/op
// Benchmark_QE_2-6          143118              8246 ns/op              40 B/op          4 allocs/op
//
// Benchmark_QE_1-6          146289              8212 ns/op              32 B/op          3 allocs/op
// Benchmark_QE_2-6          144918              8239 ns/op              40 B/op          4 allocs/op
func Benchmark_QE_1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		fnv32(utility.GenerateRandString(8))
	}
}

func Benchmark_QE_2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		crc32.ChecksumIEEE([]byte(utility.GenerateRandString(8)))
	}
}

// go test -bench=_QE2_ -benchmem -benchtime=5s -run=^$
// -benchtime 默认为1秒  -benchmem 获得内存分配的统计数据
// Benchmark_QE2_1-6       1000000000               0.2623 ns/op          0 B/op          0 allocs/op
// Benchmark_QE2_2-6       1000000000               0.2631 ns/op          0 B/op          0 allocs/op
func Benchmark_QE2_1(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = i & 31
	}
}

func Benchmark_QE2_2(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = i % 31
	}
}
