Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何创建最小Akka演员?_Scala_Akka_Actor - Fatal编程技术网

Scala 如何创建最小Akka演员?

Scala 如何创建最小Akka演员?,scala,akka,actor,Scala,Akka,Actor,在我的笔记本上,100万Akka演员的实例化耗时约20秒: 我应该在哪里挖掘以加快它们的创建,在中还是在中?这里有一个小基准,展示了如何在层次结构中创建参与者。对于大约1000000个参与者,我也可以得到20秒的时间,但是这是在一个非常慢的笔记本CPU上,并且没有对JVM选项(比如基准测试中的useNUMA)进行调整 我试图从github运行您的基准测试,但它甚至无法在我的机器上启动,因为它需要的内存超过了我现有的4GB。看来你的笔记本比我的强大多了 以下是我在两级层次结构中创建参与者时得到的结

在我的笔记本上,100万Akka演员的实例化耗时约20秒:


我应该在哪里挖掘以加快它们的创建,在中还是在中?

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

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

以下是我在两级层次结构中创建参与者时得到的结果(1000组,每组1000名工人)

下面是我在根actor的正下方创建1000000个actor时得到的结果:

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()
    }
  }
}
case类CreateChildren(n:Int,内部:Option[CreateChildren]=None)
类TestActor扩展了Actor{
Main.newActorCreated()
def接收={
案例CreateChildren(n,内部)=>
val children=(0到n).map(i=>context.actorf(Props[TestActor],“child_uu+i))

对于(msgFirst打开详细的GC日志记录,看看这是否是问题所在。你想在20微秒内创建一个
Actor
实例吗?另一个问题是你需要1Mactor做什么?你的Actor层次结构看起来如何?这些都是同一个Actor系统的顶级Actor吗?这可能是性能瓶颈。试试看创建一个包含1000名顶级演员的层次结构,每个顶级演员都有1000个孩子。另外,给他们起名字可能是一个好主意,这样演员系统就不必为他们生成唯一的名字。创建顶级演员是一种性能惩罚,因此在层次结构中至少引入一个级别的演员是有意义的(这也是一个好做法).@endre varga是否在文档中用示例进行了解释?如果这是最佳实践,那么为什么默认情况下不由参与者系统执行?最佳实践是将参与者组织在层次结构中,而不是平面结构中。默认情况下,参与者系统无法执行此操作,因为“最佳”层次结构依赖于应用程序(这是一个设计决策)。
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()
    }
  }
}