-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from valencik/benchmarks
Add Benchmarks
- Loading branch information
Showing
5 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
77 changes: 77 additions & 0 deletions
77
benchmarks/src/main/scala/textmogrify/LuceneStreamingBenchmark.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2022 Pig.io | ||
* | ||
* 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 textmogrify | ||
package benchmarks | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import fs2.Stream | ||
import fs2.io.file.{Files, Path} | ||
|
||
import java.util.concurrent.TimeUnit | ||
import org.openjdk.jmh.annotations._ | ||
import textmogrify.lucene.AnalyzerPipe | ||
import textmogrify.lucene.AnalyzerBuilder | ||
|
||
/** To run the benchmark from within sbt: | ||
* | ||
* jmh:run -i 10 -wi 10 -f 2 -t 1 textmogrify.benchmarks.LuceneStreamingBenchmark | ||
* | ||
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that | ||
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but | ||
* more is better. | ||
*/ | ||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class LuceneStreamingBenchmark { | ||
|
||
var asciiBytes: Array[Byte] = _ | ||
@Setup | ||
def setup(): Unit = | ||
asciiBytes = Files[IO] | ||
.readAll(Path("../LICENSE")) | ||
.compile | ||
.to(Array) | ||
.unsafeRunSync() | ||
|
||
@Benchmark | ||
def tokenizeBytesTokenN1(): String = { | ||
val analyzer = AnalyzerBuilder.default.withLowerCasing.build[IO] | ||
val pipe = AnalyzerPipe.fromResource(analyzer) | ||
val bytes: Stream[IO, Byte] = Stream.emits(asciiBytes) | ||
pipe | ||
.tokenizeBytes(bytes, 1) | ||
.compile | ||
.last | ||
.unsafeRunSync() | ||
.get | ||
} | ||
|
||
@Benchmark | ||
def tokenizeBytesTokenN128(): String = { | ||
val analyzer = AnalyzerBuilder.default.withLowerCasing.build[IO] | ||
val pipe = AnalyzerPipe.fromResource(analyzer) | ||
val bytes: Stream[IO, Byte] = Stream.emits(asciiBytes) | ||
pipe | ||
.tokenizeBytes(bytes, 128) | ||
.compile | ||
.last | ||
.unsafeRunSync() | ||
.get | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
benchmarks/src/main/scala/textmogrify/LuceneTokenizationBenchmark.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2022 Pig.io | ||
* | ||
* 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 textmogrify | ||
package benchmarks | ||
|
||
import cats.syntax.all._ | ||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import fs2.text | ||
import fs2.io.file.{Files, Path} | ||
|
||
import java.util.concurrent.TimeUnit | ||
import org.openjdk.jmh.annotations._ | ||
import textmogrify.lucene.AnalyzerBuilder | ||
|
||
/** To run the benchmark from within sbt: | ||
* | ||
* jmh:run -i 10 -wi 10 -f 2 -t 1 textmogrify.benchmarks.LuceneTokenizationBenchmark | ||
* | ||
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that | ||
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but | ||
* more is better. | ||
*/ | ||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class LuceneTokenizationBenchmark { | ||
|
||
var lines: Vector[String] = _ | ||
@Setup | ||
def setup(): Unit = | ||
lines = Files[IO] | ||
.readAll(Path("../LICENSE")) | ||
.through(text.utf8.decode) | ||
.through(text.lines) | ||
.compile | ||
.toVector | ||
.unsafeRunSync() | ||
|
||
@Benchmark | ||
def doNothing(): Vector[String] = { | ||
val tokenizer = AnalyzerBuilder.default.withLowerCasing.tokenizer[IO] | ||
tokenizer | ||
.use(_ => lines.traverse(x => IO.pure(Vector(x)))) | ||
.unsafeRunSync() | ||
.last | ||
} | ||
|
||
@Benchmark | ||
def manualToLowerCaseAndSplit(): Vector[String] = { | ||
val tokenizer = AnalyzerBuilder.default.withLowerCasing.tokenizer[IO] | ||
tokenizer | ||
.use(_ => lines.traverse(x => IO.pure(x.toLowerCase.split(" ").toVector))) | ||
.unsafeRunSync() | ||
.last | ||
} | ||
|
||
@Benchmark | ||
def tokenizeAndLowerCase(): Vector[String] = { | ||
val tokenizer = AnalyzerBuilder.default.withLowerCasing.tokenizer[IO] | ||
tokenizer | ||
.use(f => lines.traverse(f)) | ||
.unsafeRunSync() | ||
.last | ||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
addSbtPlugin("org.typelevel" % "sbt-typelevel" % "0.5.0-M5") | ||
addSbtPlugin("org.typelevel" % "sbt-typelevel-site" % "0.5.0-M5") | ||
addSbtPlugin("pl.project13.scala" % "sbt-jmh" % "0.4.3") |