|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main}; |
| 19 | +use parquet::bloom_filter::Sbbf; |
| 20 | + |
| 21 | +/// Build a bloom filter sized for `initial_ndv` at `fpp`, insert `num_values` distinct values, |
| 22 | +/// and return it ready for folding. |
| 23 | +fn build_filter(initial_ndv: u64, fpp: f64, num_values: u64) -> Sbbf { |
| 24 | + let mut sbbf = Sbbf::new_with_ndv_fpp(initial_ndv, fpp).unwrap(); |
| 25 | + for i in 0..num_values { |
| 26 | + sbbf.insert(&i); |
| 27 | + } |
| 28 | + sbbf |
| 29 | +} |
| 30 | + |
| 31 | +fn bench_fold_to_target_fpp(c: &mut Criterion) { |
| 32 | + let mut group = c.benchmark_group("fold_to_target_fpp"); |
| 33 | + |
| 34 | + // Realistic scenario: filter sized for 1M NDV, varying actual distinct values |
| 35 | + let initial_ndv = 1_000_000u64; |
| 36 | + let fpp = 0.05; |
| 37 | + |
| 38 | + for num_values in [1_000u64, 10_000, 100_000] { |
| 39 | + let filter = build_filter(initial_ndv, fpp, num_values); |
| 40 | + let num_blocks = filter.num_blocks(); |
| 41 | + group.throughput(Throughput::Elements(num_blocks as u64)); |
| 42 | + group.bench_with_input(BenchmarkId::new("ndv", num_values), &filter, |b, filter| { |
| 43 | + b.iter_batched( |
| 44 | + || filter.clone(), |
| 45 | + |mut f| { |
| 46 | + f.fold_to_target_fpp(fpp); |
| 47 | + f |
| 48 | + }, |
| 49 | + criterion::BatchSize::SmallInput, |
| 50 | + ); |
| 51 | + }); |
| 52 | + } |
| 53 | + group.finish(); |
| 54 | +} |
| 55 | + |
| 56 | +fn bench_insert_and_fold(c: &mut Criterion) { |
| 57 | + let mut group = c.benchmark_group("insert_and_fold"); |
| 58 | + |
| 59 | + let initial_ndv = 1_000_000u64; |
| 60 | + let fpp = 0.05; |
| 61 | + |
| 62 | + for num_values in [1_000u64, 10_000, 100_000] { |
| 63 | + group.throughput(Throughput::Elements(num_values)); |
| 64 | + group.bench_with_input( |
| 65 | + BenchmarkId::new("values", num_values), |
| 66 | + &num_values, |
| 67 | + |b, &num_values| { |
| 68 | + b.iter(|| { |
| 69 | + let mut sbbf = Sbbf::new_with_ndv_fpp(initial_ndv, fpp).unwrap(); |
| 70 | + for i in 0..num_values { |
| 71 | + sbbf.insert(&i); |
| 72 | + } |
| 73 | + sbbf.fold_to_target_fpp(fpp); |
| 74 | + sbbf |
| 75 | + }); |
| 76 | + }, |
| 77 | + ); |
| 78 | + } |
| 79 | + group.finish(); |
| 80 | +} |
| 81 | + |
| 82 | +fn bench_insert_only(c: &mut Criterion) { |
| 83 | + let mut group = c.benchmark_group("insert_only"); |
| 84 | + |
| 85 | + let initial_ndv = 1_000_000u64; |
| 86 | + let fpp = 0.05; |
| 87 | + |
| 88 | + for num_values in [1_000u64, 10_000, 100_000] { |
| 89 | + group.throughput(Throughput::Elements(num_values)); |
| 90 | + group.bench_with_input( |
| 91 | + BenchmarkId::new("values", num_values), |
| 92 | + &num_values, |
| 93 | + |b, &num_values| { |
| 94 | + b.iter(|| { |
| 95 | + let mut sbbf = Sbbf::new_with_ndv_fpp(initial_ndv, fpp).unwrap(); |
| 96 | + for i in 0..num_values { |
| 97 | + sbbf.insert(&i); |
| 98 | + } |
| 99 | + sbbf |
| 100 | + }); |
| 101 | + }, |
| 102 | + ); |
| 103 | + } |
| 104 | + group.finish(); |
| 105 | +} |
| 106 | + |
| 107 | +criterion_group!( |
| 108 | + benches, |
| 109 | + bench_fold_to_target_fpp, |
| 110 | + bench_insert_and_fold, |
| 111 | + bench_insert_only |
| 112 | +); |
| 113 | +criterion_main!(benches); |
0 commit comments