Skip to content

fixed OutOfMemoryError in RxScala #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class ComputationFollowedByAsyncPublishing {
@Benchmark def bmParallelFutures(): Unit = futures benchmark 1 // <= 44 threads ("Live peak" in JVisualVM)
@Benchmark def bmBlocking(): Unit = blocking benchmark 64 // <= 549 threads ("Live peak" in JVisualVM)
@Benchmark def bmStreams(): Unit = streams benchmark 1 // <= 52 threads ("Live peak" in JVisualVM)

//@Benchmark def bmRxScala(): Unit = rx benchmark // blows up with OutOfMemoryError :(
@Benchmark def bmRxScala(): Unit = rx benchmark 1 // <= 50 threads

@TearDown def tearDown(): Unit = system.terminate()
}
30 changes: 19 additions & 11 deletions src/main/scala/org/zalando/benchmarks/RxScala.scala
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package org.zalando.benchmarks

import java.util.concurrent.{ExecutorService, Executors}

import akka.actor.ActorSystem
import rx.lang.scala.Observable
import rx.lang.scala.schedulers.{ComputationScheduler, ExecutionContextScheduler}

import scala.concurrent.{ExecutionContext, Future}

class RxScala(system: ActorSystem) {
import ComputationFollowedByAsyncPublishing._

def benchmark: Unit = {
// looks nice, not sure if correct, blows up the heap
Observable
.from(1 to numTasks map Job)
.subscribeOn(ComputationScheduler())
.map(Computer compute)
.subscribeOn(ExecutionContextScheduler(system dispatcher))
.flatMap(1024, r => Observable.from(Publisher publish (r, system))(system dispatcher))
.foldLeft(0) { case (s, r) => s + computeResult(r) }
.foreach(println)
def benchmark(coreFactor: Int): Unit = {
val executor: ExecutorService = Executors.newCachedThreadPool()
implicit val ec = ExecutionContext.fromExecutor(executor)
try {
Observable
.from(1 to numTasks)
.map(Job)
.flatMap(numWorkers(coreFactor), job => Observable.from(Future(Computer compute job)))
.flatMap(r => Observable.from(Publisher publish (r, system))(system dispatcher))
.foldLeft(0) { case (s, r) => s + computeResult(r) }
.toBlocking
.foreach(println)
} finally {
executor.shutdown()
}
}
}