|
| 1 | +/* |
| 2 | + * Copyright 2019 Spotify AB. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an |
| 12 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 13 | + * KIND, either express or implied. See the License for the |
| 14 | + * specific language governing permissions and limitations |
| 15 | + * under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.spotify.scio.jmh |
| 19 | + |
| 20 | +import com.spotify.scio.util.{BloomFilter, BloomFilterAggregator, MutableBF} |
| 21 | +import org.openjdk.jmh.annotations._ |
| 22 | + |
| 23 | +import scala.util.Random |
| 24 | + |
| 25 | +/** |
| 26 | + * Benchmarks for com.spotify.scio.util.BloomFilter |
| 27 | + * |
| 28 | + * Creating a BF from a collection |
| 29 | + */ |
| 30 | +object BloomFilterCreateBenchmark { |
| 31 | + def createRandomString(nbrOfStrings: Int, lengthOfStrings: Int): Seq[String] = |
| 32 | + Seq.fill(nbrOfStrings)(Random.nextString(lengthOfStrings)) |
| 33 | + |
| 34 | + @State(Scope.Benchmark) |
| 35 | + class BloomFilterState { |
| 36 | + @Param(Array("100", "1000", "10000")) |
| 37 | + var nbrOfElements: Int = 0 |
| 38 | + |
| 39 | + @Param(Array("0.01", "0.001")) |
| 40 | + var falsePositiveRate: Double = 0 |
| 41 | + |
| 42 | + var randomStrings: Seq[String] = _ |
| 43 | + |
| 44 | + @Setup(Level.Trial) |
| 45 | + def setup(): Unit = |
| 46 | + randomStrings = createRandomString(nbrOfElements, 10) |
| 47 | + |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +@State(Scope.Benchmark) |
| 52 | +class BloomFilterCreateBenchmark { |
| 53 | + |
| 54 | + import BloomFilterCreateBenchmark._ |
| 55 | + |
| 56 | + /** |
| 57 | + * Create a bloom filter by aggregating on a monoid. |
| 58 | + * This is the most efficient way to create the bloom filter. |
| 59 | + */ |
| 60 | + @Benchmark |
| 61 | + def scioMutableBF(bloomFilterState: BloomFilterState): MutableBF[String] = { |
| 62 | + val bfMonoid = |
| 63 | + BloomFilter[String](bloomFilterState.nbrOfElements, bloomFilterState.falsePositiveRate) |
| 64 | + val bfAggregator = BloomFilterAggregator(bfMonoid) |
| 65 | + |
| 66 | + val sBf = bloomFilterState.randomStrings.aggregate(bfAggregator.monoid.zero)(_ += _, _ ++= _) |
| 67 | + sBf |
| 68 | + } |
| 69 | + |
| 70 | +} |
0 commit comments