Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions scio-jmh/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# JMH benchmarks

JMH based benchmarks for certain specific components in Scio.

Getting started and running JMH benchmarks via sbt-shell:
```
$ sbt
...
sbt:scio> project scio-jmh
sbt:scio-jmh> jmh:run -f1 -wi 2 -i 3 .*BloomFilter.*Benchmark.*
```

The options for `jmh:run`
- `-f1` Run with 1 fork
- `-wi 2` Run 2 warm up iterations
- `-i 3` Run 3 iterations
- `.*BloomFilter.*Benchmark.*` RegExp for Benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2019 Spotify AB.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.spotify.scio.jmh

import com.spotify.scio.util.{BloomFilter, BloomFilterAggregator, MutableBF}
import org.openjdk.jmh.annotations._

import scala.util.Random

/**
* Benchmarks for com.spotify.scio.util.BloomFilter
*
* Creating a BF from a collection
*/
object BloomFilterCreateBenchmark {
def createRandomString(nbrOfStrings: Int, lengthOfStrings: Int): Seq[String] =
Seq.fill(nbrOfStrings)(Random.nextString(lengthOfStrings))

@State(Scope.Benchmark)
class BloomFilterState {
@Param(Array("100", "1000", "10000"))
var nbrOfElements: Int = 0

@Param(Array("0.01", "0.001"))
var falsePositiveRate: Double = 0

var randomStrings: Seq[String] = _

@Setup(Level.Trial)
def setup(): Unit =
randomStrings = createRandomString(nbrOfElements, 10)

}
}

@State(Scope.Benchmark)
class BloomFilterCreateBenchmark {

import BloomFilterCreateBenchmark._

/**
* Create a bloom filter by aggregating on a monoid.
* This is the most efficient way to create the bloom filter.
*/
@Benchmark
def scioMutableBF(bloomFilterState: BloomFilterState): MutableBF[String] = {
val bfMonoid =
BloomFilter[String](bloomFilterState.nbrOfElements, bloomFilterState.falsePositiveRate)
val bfAggregator = BloomFilterAggregator(bfMonoid)

val sBf = bloomFilterState.randomStrings.aggregate(bfAggregator.monoid.zero)(_ += _, _ ++= _)
sBf
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Spotify AB.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package com.spotify.scio.jmh

import com.spotify.scio.util.{BloomFilter, MutableBF}
import com.twitter.algebird.ApproximateBoolean
import org.openjdk.jmh.annotations._

/**
* Benchmarks for com.spotify.scio.util.BloomFilter
*
* Querying for elements from a BloomFilter.
*/
object BloomFilterQueryBenchmark {

@State(Scope.Benchmark)
class BloomFilterState {

@Param(Array("100", "1000", "10000"))
var nbrOfElements: Int = 0

@Param(Array("0.001", "0.01"))
var falsePositiveRate: Double = 0

private[scio] var bf: MutableBF[String] = _

@Setup(Level.Trial)
def setup(): Unit = {
val randomStrings =
BloomFilterCreateBenchmark.createRandomString(nbrOfElements, 10)
bf = BloomFilter[String](nbrOfElements, falsePositiveRate)
.create(randomStrings: _*)
}
}
}

@State(Scope.Benchmark)
class BloomFilterQueryBenchmark {
import BloomFilterQueryBenchmark._

@Benchmark
def scioBloomFilter(bloomFilterState: BloomFilterState): ApproximateBoolean =
bloomFilterState.bf.contains("1")
}