mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 08:32:40 +08:00
添加存储
This commit is contained in:
@ -2,17 +2,21 @@ package bloom_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/charlienet/go-mixed/bloom"
|
||||
"github.com/charlienet/go-mixed/rand"
|
||||
"github.com/charlienet/go-mixed/sys"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const ()
|
||||
|
||||
func TestBloom(t *testing.T) {
|
||||
b := bloom.NewBloomFilter()
|
||||
b := bloom.NewBloomFilter(1000, 0.03)
|
||||
|
||||
for i := 0; i < 1000000; i++ {
|
||||
b.Add(strconv.Itoa(i))
|
||||
@ -20,51 +24,94 @@ func TestBloom(t *testing.T) {
|
||||
|
||||
v := "6943553521463296-1635402930"
|
||||
|
||||
t.Log(b.Contains(v))
|
||||
t.Log(b.ExistString(v))
|
||||
b.Add(v)
|
||||
t.Log(b.Contains(v))
|
||||
t.Log(b.ExistString(v))
|
||||
|
||||
fmt.Println("过滤器中包含值:", b.Contains(strconv.Itoa(9999)))
|
||||
fmt.Println("过滤器中未包含:", b.Contains("ss"))
|
||||
isSet, err := b.ExistString(strconv.Itoa(9999))
|
||||
fmt.Println("过滤器中包含值:", isSet, err)
|
||||
|
||||
isSet, err = b.ExistString("ss")
|
||||
fmt.Println("过滤器中未包含:", isSet, err)
|
||||
|
||||
t.Log(sys.ShowMemUsage())
|
||||
}
|
||||
|
||||
func TestSize(t *testing.T) {
|
||||
bloom.NewBloomFilter(bloom.WithSize(1 << 2))
|
||||
func TestOptimize(t *testing.T) {
|
||||
|
||||
expectedInsertions := 1000000 // 期望存储数据量
|
||||
falseProbability := 0.00002 // 预期误差
|
||||
bits := uint(float64(-expectedInsertions) * math.Log(falseProbability) / (math.Log(2) * math.Log(2)))
|
||||
hashSize := uint(math.Round(float64(bits) / float64(expectedInsertions) * math.Log(2)))
|
||||
|
||||
t.Log(bits)
|
||||
t.Log(hashSize)
|
||||
}
|
||||
|
||||
func TestRedis(t *testing.T) {
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: "192.168.2.222:6379",
|
||||
Password: "123456",
|
||||
})
|
||||
|
||||
bf := bloom.NewBloomFilter(10000, 0.03, bloom.WithRedis(client, "bloom:test"))
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
bf.Add(strconv.Itoa(i))
|
||||
}
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
isSet, err := bf.ExistString(strconv.Itoa(i))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if !isSet {
|
||||
t.Log(i, isSet)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 101; i < 200; i++ {
|
||||
isSet, err := bf.ExistString(strconv.Itoa(i))
|
||||
t.Log(isSet, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClear(t *testing.T) {
|
||||
bf := bloom.NewBloomFilter()
|
||||
bf := bloom.NewBloomFilter(1000, 0.03)
|
||||
|
||||
v := "abc"
|
||||
bf.Add(v)
|
||||
assert.True(t, bf.Contains(v))
|
||||
isSet, _ := bf.ExistString(v)
|
||||
assert.True(t, isSet)
|
||||
|
||||
bf.Clear()
|
||||
assert.False(t, bf.Contains(v))
|
||||
isSet, _ = bf.ExistString(v)
|
||||
assert.False(t, isSet)
|
||||
}
|
||||
|
||||
func TestParallel(t *testing.T) {
|
||||
f := bloom.NewBloomFilter()
|
||||
f := bloom.NewBloomFilter(1000, 0.03)
|
||||
|
||||
for i := 0; i < 10000; i++ {
|
||||
v := rand.Hex.Generate(10)
|
||||
|
||||
f.Add(v)
|
||||
assert.True(t, f.Contains(v))
|
||||
isSet, _ := f.ExistString(v)
|
||||
|
||||
assert.True(t, isSet)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFilter(b *testing.B) {
|
||||
f := bloom.NewBloomFilter()
|
||||
f := bloom.NewBloomFilter(1000, 0.03)
|
||||
|
||||
b.RunParallel(func(p *testing.PB) {
|
||||
for p.Next() {
|
||||
v := rand.Hex.Generate(10)
|
||||
f.Add(v)
|
||||
|
||||
f.Contains(v)
|
||||
f.ExistString(v)
|
||||
|
||||
// assert.True(b, f.Contains(v))
|
||||
|
||||
|
Reference in New Issue
Block a user