Created
August 13, 2025 20:02
-
-
Save thrawn01/972b49543f9e181af1fe826342c158f9 to your computer and use it in GitHub Desktop.
Benchmark BloomFilters (Created by AI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "testing" | |
| bloom "github.com/bits-and-blooms/bloom/v3" | |
| ) | |
| func BenchmarkBloomFilterCheck(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark checking values | |
| for i := 0; i < b.N; i++ { | |
| key := []byte(fmt.Sprintf("element%d", i%tc.elements)) | |
| _ = filter.Test(key) | |
| } | |
| }) | |
| } | |
| } | |
| func BenchmarkBloomFilterCheckMisses(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark checking values that don't exist | |
| for i := 0; i < b.N; i++ { | |
| key := []byte(fmt.Sprintf("missing%d", i)) | |
| _ = filter.Test(key) | |
| } | |
| }) | |
| } | |
| } | |
| func BenchmarkBloomFilterSerialize(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark serialization | |
| for i := 0; i < b.N; i++ { | |
| _, _ = filter.GobEncode() | |
| } | |
| }) | |
| } | |
| } | |
| func BenchmarkBloomFilterDeserialize(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Serialize once | |
| data, _ := filter.GobEncode() | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark deserialization | |
| for i := 0; i < b.N; i++ { | |
| newFilter := &bloom.BloomFilter{} | |
| _ = newFilter.GobDecode(data) | |
| } | |
| }) | |
| } | |
| } | |
| func BenchmarkBloomFilterMarshalBinary(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark MarshalBinary | |
| for i := 0; i < b.N; i++ { | |
| _, _ = filter.MarshalBinary() | |
| } | |
| }) | |
| } | |
| } | |
| func BenchmarkBloomFilterUnmarshalBinary(b *testing.B) { | |
| tests := []struct { | |
| name string | |
| elements int | |
| fpRate float64 | |
| }{ | |
| {"elements1000_fp001", 1000, 0.01}, | |
| {"elements10000_fp001", 10000, 0.01}, | |
| {"elements100000_fp001", 100000, 0.01}, | |
| {"elements1000_fp0001", 1000, 0.001}, | |
| {"elements10000_fp0001", 10000, 0.001}, | |
| {"elements100000_fp0001", 100000, 0.001}, | |
| } | |
| for _, tc := range tests { | |
| b.Run(tc.name, func(b *testing.B) { | |
| filter := bloom.NewWithEstimates(uint(tc.elements), tc.fpRate) | |
| // Add elements to filter | |
| for i := 0; i < tc.elements; i++ { | |
| filter.Add([]byte(fmt.Sprintf("element%d", i))) | |
| } | |
| // Marshal once | |
| data, _ := filter.MarshalBinary() | |
| // Reset timer after setup | |
| b.ResetTimer() | |
| // Benchmark UnmarshalBinary | |
| for i := 0; i < b.N; i++ { | |
| newFilter := &bloom.BloomFilter{} | |
| _ = newFilter.UnmarshalBinary(data) | |
| } | |
| }) | |
| } | |
| } |
Author
thrawn01
commented
Aug 13, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment