|
| 1 | +package org.fastfilter; |
| 2 | + |
| 3 | +import org.openjdk.jmh.annotations.*; |
| 4 | +import org.openjdk.jmh.infra.Blackhole; |
| 5 | +import org.openjdk.jmh.runner.Runner; |
| 6 | +import org.openjdk.jmh.runner.RunnerException; |
| 7 | +import org.openjdk.jmh.runner.options.Options; |
| 8 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 9 | + |
| 10 | +import org.fastfilter.Filter; |
| 11 | +import org.fastfilter.xor.Xor8; |
| 12 | +import org.fastfilter.xor.Xor16; |
| 13 | +import org.fastfilter.xor.XorBinaryFuse8; |
| 14 | +import org.fastfilter.xor.XorBinaryFuse16; |
| 15 | + |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +@BenchmarkMode(Mode.AverageTime) |
| 19 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 20 | +@Warmup(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS) |
| 21 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 22 | +@Fork(1) |
| 23 | +@State(Scope.Benchmark) |
| 24 | +public class FilterBenchmark { |
| 25 | + |
| 26 | + @Param({"XOR_8", "XOR_16", "XOR_BINARY_FUSE_8", "XOR_BINARY_FUSE_16"}) |
| 27 | + public String filterType; |
| 28 | + |
| 29 | + private Filter filter; |
| 30 | + private long[] testKeys; |
| 31 | + private final int NUM_KEYS = 1_000_000; |
| 32 | + |
| 33 | + @Setup |
| 34 | + public void setup() { |
| 35 | + // Create 1,000,000 keys (even numbers) |
| 36 | + testKeys = new long[NUM_KEYS]; |
| 37 | + for (int i = 0; i < testKeys.length; i++) { |
| 38 | + testKeys[i] = (long) i * 2L; // even numbers |
| 39 | + } |
| 40 | + |
| 41 | + try { |
| 42 | + switch (filterType) { |
| 43 | + case "XOR_8": |
| 44 | + filter = Xor8.construct(testKeys); |
| 45 | + break; |
| 46 | + case "XOR_16": |
| 47 | + filter = Xor16.construct(testKeys); |
| 48 | + break; |
| 49 | + case "XOR_BINARY_FUSE_8": |
| 50 | + filter = XorBinaryFuse8.construct(testKeys); |
| 51 | + break; |
| 52 | + case "XOR_BINARY_FUSE_16": |
| 53 | + filter = XorBinaryFuse16.construct(testKeys); |
| 54 | + break; |
| 55 | + default: |
| 56 | + throw new IllegalArgumentException("Unknown filter type: " + filterType); |
| 57 | + } |
| 58 | + } catch (Throwable e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @TearDown |
| 64 | + public void tearDown() { |
| 65 | + filter = null; |
| 66 | + testKeys = null; |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + @OperationsPerInvocation(NUM_KEYS) |
| 71 | + public void benchmarkContainsExisting(Blackhole blackhole) throws Throwable { |
| 72 | + for (long key : testKeys) { |
| 73 | + if (!filter.mayContain(key)) { |
| 74 | + throw new RuntimeException("Key should exist: " + key); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Benchmark |
| 80 | + @OperationsPerInvocation(NUM_KEYS) |
| 81 | + public void benchmarkContainsNonExisting(Blackhole blackhole) throws Throwable { |
| 82 | + int fp = 0; |
| 83 | + for (int i = 0; i < testKeys.length; i++) { |
| 84 | + long key = (long) i * 2L + 1L; // odd numbers |
| 85 | + if (filter.mayContain(key)) { |
| 86 | + fp++; |
| 87 | + } |
| 88 | + } |
| 89 | + if (fp > 10000) { |
| 90 | + throw new RuntimeException("Too many false positives: " + fp); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Benchmark |
| 95 | + @BenchmarkMode(Mode.Throughput) |
| 96 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 97 | + @OperationsPerInvocation(NUM_KEYS) |
| 98 | + public void benchmarkContainsExistingThroughput(Blackhole blackhole) throws Throwable { |
| 99 | + for (long key : testKeys) { |
| 100 | + if (!filter.mayContain(key)) { |
| 101 | + throw new RuntimeException("Key should exist: " + key); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Benchmark |
| 107 | + @BenchmarkMode(Mode.Throughput) |
| 108 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 109 | + @OperationsPerInvocation(NUM_KEYS) |
| 110 | + public void benchmarkContainsNonExistingThroughput(Blackhole blackhole) throws Throwable { |
| 111 | + int fp = 0; |
| 112 | + for (int i = 0; i < testKeys.length; i++) { |
| 113 | + long key = (long) i * 2L + 1L; // odd numbers |
| 114 | + if (filter.mayContain(key)) { |
| 115 | + fp++; |
| 116 | + } |
| 117 | + } |
| 118 | + if (fp > 10000) { |
| 119 | + throw new RuntimeException("Too many false positives: " + fp); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String[] args) throws RunnerException { |
| 124 | + Options opt = new OptionsBuilder() |
| 125 | + .include(FilterBenchmark.class.getSimpleName()) |
| 126 | + .build(); |
| 127 | + |
| 128 | + new Runner(opt).run(); |
| 129 | + } |
| 130 | +} |
0 commit comments