akka.net ile çalışan bir kavram kanıtı almaya çalışıyorum. Eminim çok yanlış bir şey yapıyorum, ama ne olduğunu anlayamıyorum.Benim Akka.Net Demo inanılmaz derecede yavaş
Oyuncularımın düğümleri oluşturmasını istiyorum. Daha sonra bu iş objelerden karmaşık bir grafik olacak, ama şimdilik böyle basit bir doğrusal yapıya denemek istiyorum:
Ben 9 adım uzaklıktadır Komşuma bir düğüm sormak istiyorum. Bunu yinelemeli bir şekilde uygulamaya çalışıyorum. 9 adım uzaklıktaki bir komşu için 9 no'lu düğüme gidiyorum, sonra 8 adım ötedeki bir komşu için 8 no'lu node'a sorarım. Son olarak, bu cevap # 0 değerini bir cevap olarak döndürmelidir.
Kodum işe yarıyor, ancak yürütmek için 4 saniye'dan daha uzun sürüyor. Neden?
Bu benim tam kod listesidir:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Akka;
using Akka.Actor;
namespace AkkaTest
{
class Program
{
public static Stopwatch stopwatch = new Stopwatch();
static void Main(string[] args)
{
var system = ActorSystem.Create("MySystem");
IActorRef[] current = new IActorRef[0];
Console.WriteLine("Initializing actors...");
for (int i = 0; i < 10; i++)
{
var current1 = current;
var props = Props.Create<Obj>(() => new Obj(current1, Guid.NewGuid()));
var actorRef = system.ActorOf(props, i.ToString());
current = new[] { actorRef };
}
Console.WriteLine("actors initialized.");
FindNeighboursRequest r = new FindNeighboursRequest(9);
stopwatch.Start();
var response = current[0].Ask(r);
FindNeighboursResponse result = (FindNeighboursResponse)response.Result;
stopwatch.Stop();
foreach (var d in result.FoundNeighbours)
{
Console.WriteLine(d);
}
Console.WriteLine("Search took " + stopwatch.ElapsedMilliseconds + "ms.");
Console.ReadLine();
}
}
public class FindNeighboursRequest
{
public FindNeighboursRequest(int distance)
{
this.Distance = distance;
}
public int Distance { get; private set; }
}
public class FindNeighboursResponse
{
private IActorRef[] foundNeighbours;
public FindNeighboursResponse(IEnumerable<IActorRef> descendants)
{
this.foundNeighbours = descendants.ToArray();
}
public IActorRef[] FoundNeighbours
{
get { return this.foundNeighbours; }
}
}
public class Obj : ReceiveActor
{
private Guid objGuid;
readonly List<IActorRef> neighbours = new List<IActorRef>();
public Obj(IEnumerable<IActorRef> otherObjs, Guid objGuid)
{
this.neighbours.AddRange(otherObjs);
this.objGuid = objGuid;
Receive<FindNeighboursRequest>(r => handleFindNeighbourRequest(r));
}
public Obj()
{
}
private async void handleFindNeighbourRequest (FindNeighboursRequest r)
{
if (r.Distance == 0)
{
FindNeighboursResponse response = new FindNeighboursResponse(new IActorRef[] { Self });
Sender.Tell(response, Self);
return;
}
List<FindNeighboursResponse> responses = new List<FindNeighboursResponse>();
foreach (var actorRef in neighbours)
{
FindNeighboursRequest req = new FindNeighboursRequest(r.Distance - 1);
var response2 = actorRef.Ask(req);
responses.Add((FindNeighboursResponse)response2.Result);
}
FindNeighboursResponse response3 = new FindNeighboursResponse(responses.SelectMany(rx => rx.FoundNeighbours));
Sender.Tell(response3, Self);
}
}
}
ActorSystem'i oluşturduktan sonra kronometreyi başlatıp yeniden deneyebilir misiniz? –
Tam olarak bu kodun yaptığı şeydir - ya da en azından neyi amaçladığım. Kronometreyi _after_ 'for (int i = 0; i <10; i ++)' döngüsünü başlatıyorum. – user1691896
Oh evet yanlış okumuş. Horusiath'ın dediği gibi, aktörler engellememeli, bir açlık açlığı yaşıyorsunuz demektir. –