2

100 万个 Akka 演员的实例化在我的笔记本上花费了大约 20 秒:http: //goo.gl/OwQht

我应该在哪里挖掘以加快它们的创建速度,在配置中还是在代码中?

4

1 回答 1

5

这是一个小基准,展示了如何在层次结构中创建参与者。对于约 1000000 个演员,我也有大约 20 秒的时间,但这是在一个非常慢的笔记本 CPU 上,并且没有调整基准测试中的 useNUMA 等 JVM 选项。

我试图从 github 运行你的基准测试,它甚至无法在我的机器上启动,因为它需要的内存超过了我所拥有的 4GB。所以看起来你的笔记本比我的要强大得多。

这是我在两级层次结构中创建演员时得到的结果(每组 1000 个工人,每组 1000 个)

Created 1001001 actors
20.752500989 s
48235.198279503136 actors per second

以下是我在根演员正下方创建 1000000 个演员时得到的结果:

Created 1000001 actors
56.118988558 s
17819.29834616461 actors per second

所以很明显,即使在像我这样相对较慢的机器上,在层次结构中创建演员也有很大的性能优势。在具有更多内核的机器上,差异会更大。

编辑:我现在在我的主要开发机器上运行测试,这是一个具有 4 核的 i7,因此速度明显更快:

两级层次结构:

Created 1001001 actors
2.358266323 s
424464.78170735424 actors per second

平面层次结构

Created 1000001 actors
6.032559898 s
165767.27241971265 actors per second

尝试运行它并报告你得到的结果。您可以尝试不同的层次结构深度,看看它是否有所作为。确保添加您的 CPU 和系统规格。

case class CreateChildren(n:Int, inner:Option[CreateChildren] = None)

class TestActor extends Actor {

  Main.newActorCreated()

  def receive = {
    case CreateChildren(n, inner) =>
      val children = (0 until n).map(i => context.actorOf(Props[TestActor], "child_"+i))
      for(msg<-inner; child<-children)
        child ! msg
  }
}

object Main extends App {

  val count = new java.util.concurrent.atomic.AtomicInteger(0)

  val system = ActorSystem.create("test")
  val root = system.actorOf(Props[TestActor])
  val t0 = System.nanoTime()
  root ! CreateChildren(1000, Some(CreateChildren(1000)))
  val total = 1001001

  def newActorCreated() {
    if(Main.count.incrementAndGet()==total) {
      val dt = (System.nanoTime() - t0)/1e9
      val per = total/dt
      println(s"Created $total actors")
      println(s"$dt s")
      println(s"$per actors per second")
      system.shutdown()
    }
  }
}
于 2013-07-14T09:26:34.487 回答