2015-09-03 15:26:49 +03:00
|
|
|
/**
|
2016-02-23 12:58:39 +01:00
|
|
|
* Copyright (C) 2014-2016 Lightbend Inc. <http://www.lightbend.com>
|
2015-09-03 15:26:49 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package akka.stream.io
|
|
|
|
|
|
2016-01-17 16:37:45 +01:00
|
|
|
import java.io.{ FileInputStream, File }
|
2015-09-03 15:26:49 +03:00
|
|
|
import java.util.concurrent.TimeUnit
|
2016-02-16 16:56:06 +01:00
|
|
|
import akka.{ Done, NotUsed }
|
2015-09-03 15:26:49 +03:00
|
|
|
import akka.actor.ActorSystem
|
2016-01-17 16:37:45 +01:00
|
|
|
import akka.stream.{ Attributes, ActorMaterializer }
|
2015-09-03 15:26:49 +03:00
|
|
|
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 }
|
2016-02-16 16:56:06 +01:00
|
|
|
import akka.stream.IOResult
|
2015-09-03 15:26:49 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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")
|
2015-12-11 14:45:24 +01:00
|
|
|
implicit val materializer = ActorMaterializer()
|
2015-09-03 15:26:49 +03:00
|
|
|
|
|
|
|
|
val file: File = {
|
|
|
|
|
val line = ByteString("x" * 2048 + "\n")
|
|
|
|
|
|
|
|
|
|
val f = File.createTempFile(getClass.getName, ".bench.tmp")
|
|
|
|
|
f.deleteOnExit()
|
|
|
|
|
|
2015-12-17 11:48:30 +02:00
|
|
|
val ft = Source.fromIterator(() ⇒ Iterator.continually(line))
|
2015-09-03 15:26:49 +03:00
|
|
|
.take(10 * 39062) // adjust as needed
|
2015-12-08 18:47:58 +01:00
|
|
|
.runWith(FileIO.toFile(f))
|
2015-09-03 15:26:49 +03:00
|
|
|
Await.result(ft, 30.seconds)
|
|
|
|
|
|
|
|
|
|
f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Param(Array("2048"))
|
2016-03-05 22:34:57 -05:00
|
|
|
var bufSize = 0
|
2015-09-03 15:26:49 +03:00
|
|
|
|
2016-01-21 18:06:42 +02:00
|
|
|
var fileChannelSource: Source[ByteString, Future[IOResult]] = _
|
|
|
|
|
var fileInputStreamSource: Source[ByteString, Future[IOResult]] = _
|
2016-01-20 10:00:37 +02:00
|
|
|
var ioSourceLinesIterator: Source[ByteString, NotUsed] = _
|
2015-09-03 15:26:49 +03:00
|
|
|
|
|
|
|
|
@Setup
|
2016-03-05 22:34:57 -05:00
|
|
|
def setup():Unit = {
|
2015-12-08 18:47:58 +01:00
|
|
|
fileChannelSource = FileIO.fromFile(file, bufSize)
|
|
|
|
|
fileInputStreamSource = StreamConverters.fromInputStream(() ⇒ new FileInputStream(file), bufSize)
|
2015-12-17 11:48:30 +02:00
|
|
|
ioSourceLinesIterator = Source.fromIterator(() ⇒ scala.io.Source.fromFile(file).getLines()).map(ByteString(_))
|
2015-09-03 15:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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)
|
2015-09-03 15:26:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Benchmark
|
2016-03-05 22:34:57 -05:00
|
|
|
def fileChannel():Unit = {
|
2015-09-03 15:26:49 +03:00
|
|
|
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 = {
|
2015-09-03 15:26:49 +03:00
|
|
|
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 = {
|
2015-09-03 15:26:49 +03:00
|
|
|
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 = {
|
2016-01-20 10:00:37 +02:00
|
|
|
val p = Promise[Done]()
|
2015-09-03 15:26:49 +03:00
|
|
|
ioSourceLinesIterator.to(Sink.onComplete(p.complete(_))).run()
|
|
|
|
|
|
|
|
|
|
Await.result(p.future, 30.seconds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|