Skip to content

Commit fea9c38

Browse files
committed
add benchmark(bad)
1 parent f1fd10b commit fea9c38

File tree

4 files changed

+85
-34
lines changed

4 files changed

+85
-34
lines changed

bf/bfbits/bits.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ func (bf *bloomFilter) Add(item []byte) error {
6363
}
6464

6565
idx := int(h.Sum64() % uint64(bf.size))
66+
// fmt.Printf("%b", bf.bits[idx/64])
6667
bf.bits[idx/64] |= 1 << (idx % 64)
68+
// fmt.Printf("%b", bf.bits[idx/64])
6769
}
6870

6971
return nil

bf/bfbits/bits_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,70 @@
11
package bfbits
22

33
import (
4+
"crypto/rand"
5+
"encoding/hex"
6+
"runtime"
7+
"sync"
48
"testing"
59
)
610

11+
func BenchmarkBloomFilter_Contains(b *testing.B) {
12+
filter := NewBloomFilter(100_000_000, WithHashNum(3))
13+
wCh := make(chan []byte, 1)
14+
rCh := make(chan []byte, 1)
15+
wChGroup := sync.WaitGroup{}
16+
wChGroup.Add(runtime.NumCPU())
17+
18+
buf := make([]byte, 16)
19+
for i := 0; i < runtime.NumCPU(); i++ {
20+
go func() {
21+
defer wChGroup.Done()
22+
for i := 0; i < 10_000_000/runtime.NumCPU(); i++ {
23+
read, err := rand.Read(buf)
24+
if err != nil {
25+
b.Error(err)
26+
return
27+
}
28+
29+
chunk := make([]byte, hex.EncodedLen(16))
30+
n := hex.Encode(chunk, buf[:read])
31+
wCh <- chunk[:n]
32+
rCh <- chunk[:n]
33+
}
34+
}()
35+
}
36+
37+
go func() {
38+
wChGroup.Wait()
39+
close(wCh)
40+
close(rCh)
41+
}()
42+
43+
wg := sync.WaitGroup{}
44+
wg.Add(2)
45+
go func() {
46+
defer wg.Done()
47+
for bytes := range wCh {
48+
if err := filter.Add(bytes); err != nil {
49+
b.Error(err)
50+
return
51+
}
52+
}
53+
}()
54+
55+
go func() {
56+
wg.Done()
57+
for bytes := range rCh {
58+
if _, err := filter.Contains(bytes); err != nil {
59+
b.Error(err)
60+
return
61+
}
62+
}
63+
}()
64+
65+
wg.Wait()
66+
}
67+
768
func TestBloomFilter_Add(t *testing.T) {
869
bf := NewBloomFilter(10000, WithHashNum(1))
970

cmd/go-bf/main.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
package main
22

3-
import (
4-
"crypto/rand"
5-
"encoding/hex"
6-
"fmt"
7-
"log"
8-
9-
"github.com/robotomize/go-bf/bf/bfbits"
10-
)
11-
123
func main() {
13-
filter := bfbits.NewBloomFilter(100_000_000, bfbits.WithHashNum(3))
14-
15-
buf := make([]byte, 8)
16-
b := make([][]byte, 5)
17-
18-
for i := 0; i < 70_000_000; i++ {
19-
read, err := rand.Read(buf)
20-
if err != nil {
21-
return
22-
}
23-
key := []byte(hex.EncodeToString(buf[:read]))
24-
if err := filter.Add(key); err != nil {
25-
log.Fatal(err)
26-
}
27-
if i < len(b) {
28-
b[i] = key
29-
}
30-
}
314

32-
for i := 0; i < len(b); i++ {
33-
contains, err := filter.Contains(b[i])
34-
if err != nil {
35-
log.Fatal(err)
36-
}
37-
fmt.Println(contains)
38-
}
395
}

examples/main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/robotomize/go-bf/bf/bfbits"
7+
)
8+
9+
func main() {
10+
bf := bfbits.NewBloomFilter(10000, bfbits.WithHashNum(1))
11+
12+
if err := bf.Add([]byte("apple")); err != nil {
13+
fmt.Printf("Add() error = %v", err)
14+
}
15+
16+
items := []string{"banana", "cherry", "date"}
17+
for _, item := range items {
18+
if err := bf.Add([]byte(item)); err != nil {
19+
fmt.Printf("Add() error = %v", err)
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)