pekko/akka-bench-jmh/src/main/scala/akka/stream/io/FileSourcesBenchmark.scala

104 lines
2.8 KiB
Scala
Raw Normal View History

/**
* Copyright (C) 2014-2016 Lightbend Inc. <http://www.lightbend.com>
*/
package akka.stream.io
2016-01-17 16:37:45 +01:00
import java.io.{ FileInputStream, File }
import java.util.concurrent.TimeUnit
import akka.{ Done, NotUsed }
import akka.actor.ActorSystem
2016-01-17 16:37:45 +01:00
import akka.stream.{ Attributes, ActorMaterializer }
import akka.stream.scaladsl._
import akka.util.ByteString
import org.openjdk.jmh.annotations._
import scala.concurrent.duration._
2016-01-17 16:37:45 +01:00
import scala.concurrent.{ Promise, Await, Future }
import akka.stream.IOResult
/**
* Benchmark (bufSize) Mode Cnt Score Error Units
* FileSourcesBenchmark.fileChannel 2048 avgt 100 1140.192 ± 55.184 ms/op
*/
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Array(Mode.AverageTime))
class FileSourcesBenchmark {
implicit val system = ActorSystem("file-sources-benchmark")
implicit val materializer = ActorMaterializer()
val file: File = {
val line = ByteString("x" * 2048 + "\n")
val f = File.createTempFile(getClass.getName, ".bench.tmp")
f.deleteOnExit()
val ft = Source.fromIterator(() Iterator.continually(line))
.take(10 * 39062) // adjust as needed
.runWith(FileIO.toFile(f))
Await.result(ft, 30.seconds)
f
}
@Param(Array("2048"))
2016-03-05 22:34:57 -05:00
var bufSize = 0
var fileChannelSource: Source[ByteString, Future[IOResult]] = _
var fileInputStreamSource: Source[ByteString, Future[IOResult]] = _
var ioSourceLinesIterator: Source[ByteString, NotUsed] = _
@Setup
2016-03-05 22:34:57 -05:00
def setup():Unit = {
fileChannelSource = FileIO.fromFile(file, bufSize)
fileInputStreamSource = StreamConverters.fromInputStream(() new FileInputStream(file), bufSize)
ioSourceLinesIterator = Source.fromIterator(() scala.io.Source.fromFile(file).getLines()).map(ByteString(_))
}
@TearDown
def teardown(): Unit = {
file.delete()
}
@TearDown
2016-03-05 22:34:57 -05:00
def shutdown():Unit = {
2016-01-17 16:37:45 +01:00
Await.result(system.terminate(), Duration.Inf)
}
@Benchmark
2016-03-05 22:34:57 -05:00
def fileChannel():Unit = {
val h = fileChannelSource.to(Sink.ignore).run()
Await.result(h, 30.seconds)
}
@Benchmark
2016-03-05 22:34:57 -05:00
def fileChannel_noReadAhead():Unit = {
val h = fileChannelSource.withAttributes(Attributes.inputBuffer(1, 1)).to(Sink.ignore).run()
Await.result(h, 30.seconds)
}
@Benchmark
2016-03-05 22:34:57 -05:00
def inputStream():Unit = {
val h = fileInputStreamSource.to(Sink.ignore).run()
Await.result(h, 30.seconds)
}
/*
* The previous status quo was very slow:
* Benchmark Mode Cnt Score Error Units
* FileSourcesBenchmark.naive_ioSourceLinesIterator avgt 20 7067.944 ± 1341.847 ms/op
*/
@Benchmark
2016-03-05 22:34:57 -05:00
def naive_ioSourceLinesIterator():Unit = {
val p = Promise[Done]()
ioSourceLinesIterator.to(Sink.onComplete(p.complete(_))).run()
Await.result(p.future, 30.seconds)
}
}