ExecutionContext.Implicits.global で先物を使用してブロッキング IO を実行する簡単なプログラムがあります。この IO を 100 回実行したときのパフォーマンスをベンチマークしたいのですが、future がすべて完了する前にループが終了してしまいます。ExecutionContext にプッシュされたすべてのタスクが完了したときにのみ終了する方法はありますか?
1 に答える
In the absence of any code, I'll provide a simple version but give the caveat that this is an inaccurate way of measuring and not great for things that take a small amount of time. Only really useful if the operation your measuring actually takes a decent amount of time so most of the time is spent in the Future and not in the test code.
import scala.concurrent._
import scala.concurrent.duration._
import ExecutionContext.Implicits.global
val myList = (1 to 10)
val start = System.currentTimeMillis
val seqFutures = for (elem <- myList) yield {
Future {
Thread.sleep(5000) // blocking operation
}
}
val result = Future.sequence(seqFutures)
Await.result(result, 30.seconds)
val end = System.currentTimeMillis
println(s"${end-start} seconds")
If you are going to have a large number of blocking IO calls, it can be worth configuring a separate execution context that has a larger thread pool just for the blocking IO to avoid the blocking IO consuming all your threads.