Skip to content

Commit

Permalink
Skynet benchmark for ActorSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
mhelleborg committed May 26, 2021
1 parent ba2b46b commit 0260265
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions ProtoActor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ Global
{DF4464D4-B411-42E2-B4DC-16EBE5ABB0EF}.Release|x64.ActiveCfg = Release|Any CPU
{DF4464D4-B411-42E2-B4DC-16EBE5ABB0EF}.Release|x86.ActiveCfg = Release|Any CPU
{DF4464D4-B411-42E2-B4DC-16EBE5ABB0EF}.Release|x86.Build.0 = Release|Any CPU
{DF4464D4-B411-42E2-B4DC-16EBE5ABB0EF}.Release|x64.Build.0 = Release|Any CPU
{8849DD9F-CEB4-40D5-9DC0-EC6EDCA09E65}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8849DD9F-CEB4-40D5-9DC0-EC6EDCA09E65}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8849DD9F-CEB4-40D5-9DC0-EC6EDCA09E65}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/ProtoActorBenchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace ProtoActorBenchmarks
{
public class Program
public static class Program
{
public static void Main(string[] args) => BenchmarkRunner.Run<ShortBenchmark>();
public static void Main() => BenchmarkRunner.Run<SkyNetBenchmark>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>9</LangVersion>
<ServerGarbageCollection>true</ServerGarbageCollection>
<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
</PropertyGroup>
Expand Down
97 changes: 97 additions & 0 deletions benchmarks/ProtoActorBenchmarks/SkyNetBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// -----------------------------------------------------------------------
// <copyright file="Skynet.cs" company="Asynkron AB">
// Copyright (C) 2015-2021 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System.Threading;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using Proto;

namespace ProtoActorBenchmarks
{
[InProcess]
public class SkyNetBenchmark
{
private RootContext _context;
private ActorSystem _actorSystem;

[Params(0, 5000)]
public int SharedFutures { get; set; }

[GlobalSetup]
public void Setup()
{
var config = ActorSystemConfig.Setup();

if (SharedFutures > 0)
{
config = config.WithSharedFutures(SharedFutures);
}

_actorSystem = new ActorSystem(config);
_context = _actorSystem.Root;
}

[GlobalCleanup]
public void Cleanup() => _actorSystem.ShutdownAsync();

[Benchmark]
public Task SkyNetTest()
{
var pid = _context.Spawn(SkyNetActor.Props);
return _context.RequestAsync<long>(pid, new Calculate(1_000_000, 0), CancellationToken.None);
}

private class SkyNetActor : IActor
{
public static readonly Props Props = Props.FromProducer(() => new SkyNetActor());

public async Task ReceiveAsync(IContext context)
{
if (context.Message is Calculate calc)
{
if (calc.Count <= 1)
{
context.Respond((long) calc.From);
return;
}

var tasks = new Task<long>[10];
var each = calc.Count / 10;
var to = calc.From + calc.Count;

for (var i = 0; i < 10; i++)
{
var pid = context.Spawn(Props);
tasks[i] = context.RequestAsync<long>(pid, new Calculate(each, calc.From + i * each)
);
}

await Task.WhenAll(tasks);
var tot = 0L;

for (var i = 0; i < tasks.Length; i++)
{
tot += tasks[i].Result;
}

context.Respond(tot);
context.Stop(context.Self);
}
}
}

readonly struct Calculate
{
public Calculate(int count, int from)
{
Count = count;
From = from;
}

public int Count { get; }
public int From { get; }
}
}
}

0 comments on commit 0260265

Please sign in to comment.