private static async Task Main() { const int messageCount = 1000000; const int batchSize = 100; int[] clientCounts = { 8, 16, 32 }; var tps = new[] { 50, 100, 200, 400, 800 }; foreach (var t in tps) { var d = new ThreadPoolDispatcher { Throughput = t }; foreach (var clientCount in clientCounts) { var sys = new ActorSystem(); Console.WriteLine("Starting test " + clientCount); var pingActors = new PID[clientCount]; var pongActors = new PID[clientCount]; for (var i = 0; i < clientCount; i++) { pingActors[i] = sys.Root.Spawn( PingActor .Props(messageCount, batchSize) .WithDispatcher(d) ); pongActors[i] = sys.Root.Spawn( PongActor .Props .WithDispatcher(d) ); } Console.WriteLine("Actors created"); var tasks = new Task[clientCount]; var sw = Stopwatch.StartNew(); for (var i = 0; i < clientCount; i++) { var pingActor = pingActors[i]; var pongActor = pongActors[i]; tasks[i] = sys.Root.RequestAsync <bool>(pingActor, new PingActor.Start(pongActor)); } Console.WriteLine("Waiting for actors"); await Task.WhenAll(tasks); sw.Stop(); var totalMessages = messageCount * 2 * clientCount; var x = (int)(totalMessages / (double)sw.ElapsedMilliseconds * 1000.0d); Console.WriteLine(); Console.WriteLine($"{clientCount}\t\t{sw.ElapsedMilliseconds}\t\t{x:n0}"); } } }
public void BasicThreadPoolQueue() { ThreadPoolDispatcher dispatcher = new ThreadPoolDispatcher(); // test CreateQueue - succeeds IActionQueue queue = dispatcher.CreateQueue(); Assert.IsNotNull(queue); // test queuing an action counter = 0; queue.Enqueue(() => { IncrementCounter(Thread.CurrentThread.ManagedThreadId); }); queue.Enqueue(() => { IncrementCounter(Thread.CurrentThread.ManagedThreadId); }); // spin until counter updates while (counter < 2) { Thread.Sleep(0); } // check if a thread failed Assert.IsFalse(failed); // close things out dispatcher.Dispose(); }
static void Main(string[] args) { var context = new RootContext(); Console.WriteLine($"Is Server GC {GCSettings.IsServerGC}"); const int messageCount = 1000000; const int batchSize = 100; Console.WriteLine("Dispatcher\t\tElapsed\t\tMsg/sec"); var tps = new[] { 300, 400, 500, 600, 700, 800, 900 }; foreach (var t in tps) { var d = new ThreadPoolDispatcher { Throughput = t }; var clientCount = Environment.ProcessorCount * 1; var clients = new PID[clientCount]; var echos = new PID[clientCount]; var completions = new TaskCompletionSource <bool> [clientCount]; var echoProps = Props.FromProducer(() => new EchoActor()) .WithDispatcher(d) .WithMailbox(() => BoundedMailbox.Create(2048)); for (var i = 0; i < clientCount; i++) { var tsc = new TaskCompletionSource <bool>(); completions[i] = tsc; var clientProps = Props.FromProducer(() => new PingActor(tsc, messageCount, batchSize)) .WithDispatcher(d) .WithMailbox(() => BoundedMailbox.Create(2048)); clients[i] = context.Spawn(clientProps); echos[i] = context.Spawn(echoProps); } var tasks = completions.Select(tsc => tsc.Task).ToArray(); var sw = Stopwatch.StartNew(); for (var i = 0; i < clientCount; i++) { var client = clients[i]; var echo = echos[i]; context.Send(client, new Start(echo)); } Task.WaitAll(tasks); sw.Stop(); var totalMessages = messageCount * 2 * clientCount; var x = (int)(totalMessages / (double)sw.ElapsedMilliseconds * 1000.0d); Console.WriteLine($"{t}\t\t\t{sw.ElapsedMilliseconds}\t\t{x}"); Thread.Sleep(2000); } Console.ReadLine(); }
public Task InProcessPingPong() { var d = new ThreadPoolDispatcher { Throughput = Tps }; var clientCount = Environment.ProcessorCount * 1; var clients = new PID[clientCount]; var echos = new PID[clientCount]; var completions = new TaskCompletionSource <bool> [clientCount]; var echoProps = Props.FromProducer(() => new EchoActor()) .WithDispatcher(d) .WithMailbox(() => BoundedMailbox.Create(2048)); for (var i = 0; i < clientCount; i++) { var tsc = new TaskCompletionSource <bool>(); completions[i] = tsc; var clientProps = Props.FromProducer(() => new PingActor(tsc, MessageCount, BatchSize)) .WithDispatcher(d) .WithMailbox(() => BoundedMailbox.Create(2048)); clients[i] = _context.Spawn(clientProps); echos[i] = _context.Spawn(echoProps); } var tasks = completions.Select(tsc => tsc.Task).ToArray(); for (var i = 0; i < clientCount; i++) { var client = clients[i]; var echo = echos[i]; _context.Send(client, new Start(echo)); } return(Task.WhenAll(tasks)); }
private static async Task <BenchmarkResult> Benchmark(int throughput, int numberOfClients, long numberOfRepeats, PrintStats printStats, long bestThroughput, int redCount, long repeat) { var totalMessagesReceived = GetTotalMessagesReceived(numberOfRepeats); //times 2 since the client and the destination both send messages var repeatsPerClient = numberOfRepeats / numberOfClients; var totalWatch = Stopwatch.StartNew(); var countdown = new CountdownEvent(numberOfClients * 2); var waitForStartsActorProps = Actor.FromProducer(() => new WaitForStarts(countdown)); var waitForStartsActor = Actor.SpawnNamed(waitForStartsActorProps, $"wait-for-starts-{throughput}-{repeat}"); var clients = new List <PID>(); var destinations = new List <PID>(); var tasks = new List <Task>(); var started = new Messages.Started { Sender = waitForStartsActor }; var d = new ThreadPoolDispatcher { Throughput = throughput }; for (var i = 0; i < numberOfClients; i++) { var destinationProps = Actor.FromProducer(() => new Destination()).WithDispatcher(d); var destination = Actor.SpawnNamed(destinationProps, $"destination-{i}-{throughput}-{repeat}"); destinations.Add(destination); var ts = new TaskCompletionSource <bool>(); tasks.Add(ts.Task); var clientProps = Actor.FromProducer(() => new ClientActor(destination, repeatsPerClient, ts)).WithDispatcher(d); var client = Actor.SpawnNamed(clientProps, $"client-{i}-{throughput}-{repeat}"); clients.Add(client); client.Tell(started); destination.Tell(started); } if (!countdown.Wait(TimeSpan.FromSeconds(10))) { Console.WriteLine("The system did not start in 10 seconds. Aborting."); return(new BenchmarkResult { BestThroughput = bestThroughput, RedCount = redCount }); } var setupTime = totalWatch.Elapsed; var sw = Stopwatch.StartNew(); clients.ForEach(c => { var run = new Messages.Run { Sender = c }; c.Tell(run); }); await Task.WhenAll(tasks.ToArray()); sw.Stop(); totalWatch.Stop(); var elapsedMilliseconds = sw.ElapsedMilliseconds; var throughputResult = elapsedMilliseconds == 0 ? -1 : totalMessagesReceived / elapsedMilliseconds * 1000; var foregroundColor = Console.ForegroundColor; if (throughputResult >= bestThroughput) { Console.ForegroundColor = ConsoleColor.Green; bestThroughput = throughputResult; redCount = 0; } else { redCount++; Console.ForegroundColor = ConsoleColor.Red; } if (printStats.HasFlag(PrintStats.StartTimeOnly)) { Console.Write("{0,5}", setupTime.TotalMilliseconds.ToString("F2", CultureInfo.InvariantCulture)); } else { if (printStats.HasFlag(PrintStats.LineStart)) { Console.Write("{0,10}, ", throughput); } if (printStats.HasFlag(PrintStats.Stats)) { Console.Write("{0,8}, {1,10}, {2,10}", throughputResult, setupTime.TotalMilliseconds.ToString("F2", CultureInfo.InvariantCulture), totalWatch.Elapsed.TotalMilliseconds.ToString("F2", CultureInfo.InvariantCulture)); } } Console.ForegroundColor = foregroundColor; return(new BenchmarkResult { BestThroughput = bestThroughput, RedCount = redCount }); }
private static void Main(string[] args) { var benchmarkSettings = Configuration.GetConfiguration <InprocBenchmarkSettings>("InprocBenchmarkSettings"); Console.WriteLine($"Is Server GC {GCSettings.IsServerGC}"); int messageCount = benchmarkSettings.MessageCount; int batchSize = benchmarkSettings.BatchSize; Console.WriteLine("Dispatcher\t\tElapsed\t\tMsg/sec"); var tps = benchmarkSettings.Throughputs; var msgSecs = new List <int>(); foreach (var t in tps) { var d = new ThreadPoolDispatcher { Throughput = t }; var clientCount = Environment.ProcessorCount * 1; var clients = new PID[clientCount]; var echos = new PID[clientCount]; var completions = new TaskCompletionSource <bool> [clientCount]; var echoProps = Actor.FromProducer(() => new EchoActor()) .WithDispatcher(d) .WithMailbox(() => benchmarkSettings.MailboxType == "bounded-mailbox" ? BoundedMailbox.Create(2048) : UnboundedMailbox.Create()); for (var i = 0; i < clientCount; i++) { var tsc = new TaskCompletionSource <bool>(); completions[i] = tsc; var clientProps = Actor.FromProducer(() => new PingActor(tsc, messageCount, batchSize)) .WithDispatcher(d) .WithMailbox(() => benchmarkSettings.MailboxType == "bounded-mailbox" ? BoundedMailbox.Create(2048) : UnboundedMailbox.Create()); clients[i] = Actor.Spawn(clientProps); echos[i] = Actor.Spawn(echoProps); } var tasks = completions.Select(tsc => tsc.Task).ToArray(); var sw = Stopwatch.StartNew(); for (var i = 0; i < clientCount; i++) { var client = clients[i]; var echo = echos[i]; client.Tell(new Start(echo)); } Task.WaitAll(tasks); sw.Stop(); var totalMessages = messageCount * 2 * clientCount; var x = (int)(totalMessages / (double)sw.ElapsedMilliseconds * 1000.0d); Console.WriteLine($"{t}\t\t\t{sw.ElapsedMilliseconds}\t\t{x}"); msgSecs.Add(x); Thread.Sleep(2000); } Console.WriteLine($"Avg Msg/sec : {msgSecs.Average()}"); Console.ReadLine(); }
private static async Task Main() { var context = new RootContext(new ActorSystem()); Console.WriteLine($"Is Server GC {GCSettings.IsServerGC}"); const int messageCount = 1_000_000; const int batchSize = 100; Console.WriteLine("ClientCount\t\tDispatcher\t\tElapsed\t\tMsg/sec"); var tps = new[] { 50, 100, 200, 400, 800 }; int[] clientCounts = { 4, 8, 16, 32 }; foreach (var t in tps) { var d = new ThreadPoolDispatcher { Throughput = t }; foreach (var clientCount in clientCounts) { var pingActor = new PID[clientCount]; var pongActor = new PID[clientCount]; var completions = new TaskCompletionSource <bool> [clientCount]; var pongProps = Props.FromProducer(() => new PongActor()) .WithDispatcher(d); for (var i = 0; i < clientCount; i++) { var tsc = new TaskCompletionSource <bool>(); completions[i] = tsc; var pingProps = Props.FromProducer(() => new PingActor(tsc, messageCount, batchSize)) .WithDispatcher(d); pingActor[i] = context.Spawn(pingProps); pongActor[i] = context.Spawn(pongProps); } var tasks = completions.Select(tsc => tsc.Task).ToArray(); var sw = Stopwatch.StartNew(); for (var i = 0; i < clientCount; i++) { var client = pingActor[i]; var echo = pongActor[i]; context.Send(client, new Start(echo)); } await Task.WhenAll(tasks); sw.Stop(); var totalMessages = messageCount * 2 * clientCount; var x = ((int)(totalMessages / (double)sw.ElapsedMilliseconds * 1000.0d)).ToString("#,##0,,M", CultureInfo.InvariantCulture ); Console.WriteLine($"{clientCount}\t\t\t{t}\t\t\t{sw.ElapsedMilliseconds} ms\t\t{x}"); await Task.Delay(2000); } } }