Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark to test SingleRequestResponseToRemoteEntity with a local proxy #5232

Merged
merged 3 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/benchmark/Akka.Cluster.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Reflection;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

namespace Akka.Cluster.Benchmarks
Expand All @@ -8,7 +9,12 @@ class Program
{
static void Main(string[] args)
{
#if (DEBUG)
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args, new DebugInProcessConfig());
#else
BenchmarkSwitcher.FromAssembly(Assembly.GetExecutingAssembly()).Run(args);
#endif

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Akka.Actor;
using Akka.Benchmarks.Configurations;
using Akka.Cluster.Sharding;
using Akka.Routing;
using BenchmarkDotNet.Attributes;
using static Akka.Cluster.Benchmarks.Sharding.ShardingHelper;

Expand All @@ -33,6 +34,7 @@ public class ShardMessageRoutingBenchmarks

private IActorRef _shardRegion1;
private IActorRef _shardRegion2;
private IActorRef _localRouter;

private string _entityOnSys1;
private string _entityOnSys2;
Expand All @@ -43,6 +45,50 @@ public class ShardMessageRoutingBenchmarks
private IActorRef _batchActor;
private Task _batchComplete;

#if (DEBUG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was using this to debug the functionality of the benchmark (be able to attach breakpoints etc.) as per https://benchmarkdotnet.org/articles/guides/troubleshooting.html - if another approach is usually taken, happy to remove or change as needed

Thanks

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything different about code in this block vs. the #else block other than async Task vs. void? Was trying to spot some but none jumped out at me. The other statement, with the debugging config added, makes sense to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct I was running into an issue with async code in debug mode - the benchmark was executing before the task completion. I can refactor for a cleaner approach if it’s better (still would need two methods I believe due to async signature)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Thank you!

Nope, that's ok - just adding a comment to explain why it's there would be sufficient. We can do that in a subsequent PR.

Thank you for your contribution!

[GlobalSetup]
public void Setup()
{
var config = StateMode switch
{
StateStoreMode.Persistence => CreatePersistenceConfig(),
StateStoreMode.DData => CreateDDataConfig(),
_ => null
};

_sys1 = ActorSystem.Create("BenchSys", config);
_sys2 = ActorSystem.Create("BenchSys", config);

var c1 = Cluster.Get(_sys1);
var c2 = Cluster.Get(_sys2);

c1.JoinAsync(c1.SelfAddress).Wait();
c2.JoinAsync(c1.SelfAddress).Wait();

_shardRegion1 = StartShardRegion(_sys1);
_shardRegion2 = StartShardRegion(_sys2);

_localRouter = _sys1.ActorOf(Props.Create<ShardedProxyEntityActor>(_shardRegion1).WithRouter(new RoundRobinPool(50)));

var s1Asks = new List<Task<ShardedEntityActor.ResolveResp>>(20);
var s2Asks = new List<Task<ShardedEntityActor.ResolveResp>>(20);

foreach (var i in Enumerable.Range(0, 20))
{
s1Asks.Add(_shardRegion1.Ask<ShardedEntityActor.ResolveResp>(new ShardingEnvelope(i.ToString(), ShardedEntityActor.Resolve.Instance), TimeSpan.FromSeconds(3)));
s2Asks.Add(_shardRegion2.Ask<ShardedEntityActor.ResolveResp>(new ShardingEnvelope(i.ToString(), ShardedEntityActor.Resolve.Instance), TimeSpan.FromSeconds(3)));
}

// wait for all Ask operations to complete
Task.WhenAll(s1Asks.Concat(s2Asks)).Wait();

_entityOnSys2 = s1Asks.First(x => x.Result.Addr.Equals(c2.SelfAddress)).Result.EntityId;
_entityOnSys1 = s2Asks.First(x => x.Result.Addr.Equals(c1.SelfAddress)).Result.EntityId;

_messageToSys1 = new ShardedMessage(_entityOnSys1, 10);
_messageToSys2 = new ShardedMessage(_entityOnSys2, 10);
}
#else
[GlobalSetup]
public async Task Setup()
{
Expand All @@ -65,6 +111,8 @@ public async Task Setup()
_shardRegion1 = StartShardRegion(_sys1);
_shardRegion2 = StartShardRegion(_sys2);

_localRouter = _sys1.ActorOf(Props.Create<ShardedProxyEntityActor>(_shardRegion1).WithRouter(new RoundRobinPool(1000)));

var s1Asks = new List<Task<ShardedEntityActor.ResolveResp>>(20);
var s2Asks = new List<Task<ShardedEntityActor.ResolveResp>>(20);

Expand All @@ -83,6 +131,7 @@ public async Task Setup()
_messageToSys1 = new ShardedMessage(_entityOnSys1, 10);
_messageToSys2 = new ShardedMessage(_entityOnSys2, 10);
}
#endif

[IterationSetup]
public void PerIteration()
Expand All @@ -98,21 +147,29 @@ public async Task SingleRequestResponseToLocalEntity()
for (var i = 0; i < MsgCount; i++)
await _shardRegion1.Ask<ShardedMessage>(_messageToSys1);
}

[Benchmark]
public async Task StreamingToLocalEntity()
{
_batchActor.Tell(new BulkSendActor.BeginSend(_messageToSys1, _shardRegion1, BatchSize));
await _batchComplete;
}

[Benchmark]
public async Task SingleRequestResponseToRemoteEntity()
{
for (var i = 0; i < MsgCount; i++)
await _shardRegion1.Ask<ShardedMessage>(_messageToSys2);
}



[Benchmark]
public async Task SingleRequestResponseToRemoteEntityWithLocalProxy()
{
for (var i = 0; i < MsgCount; i++)
await _localRouter.Ask<ShardedMessage>(new SendShardedMessage(_messageToSys2.EntityId, _messageToSys2));
}

[Benchmark]
public async Task StreamingToRemoteEntity()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,63 @@ public ShardedEntityActor()
}
}



public sealed class ShardedProxyEntityActor : ReceiveActor, IWithUnboundedStash
{
private IActorRef _shardRegion;
private IActorRef _sender;





public ShardedProxyEntityActor(IActorRef shardRegion)
{
_shardRegion = shardRegion;
WaitRequest();
}

public void WaitRequest()
{

Receive<SendShardedMessage>(e =>
{
_sender = Sender;
_shardRegion.Tell(e.Message);
Become(WaitResult);

});

ReceiveAny(x => {
Sender.Tell(x);
});


}


public void WaitResult()
{
Receive<ShardedMessage>((msg) => {
_sender.Tell(msg);
Stash.UnstashAll();
Become(WaitRequest);
});

ReceiveAny(msg =>
Stash.Stash()
);


}

public IStash Stash { get; set; }

}



public sealed class BulkSendActor : ReceiveActor
{
public sealed class BeginSend
Expand Down Expand Up @@ -108,6 +165,22 @@ public ShardedMessage(string entityId, int message)
public int Message { get; }
}



public sealed class SendShardedMessage
{
public SendShardedMessage(string entityId, ShardedMessage message)
{
EntityId = entityId;
Message = message;
}

public string EntityId { get; }

public ShardedMessage Message { get; }
}


/// <summary>
/// Use a default <see cref="IMessageExtractor"/> even though it takes extra work to setup the benchmark
/// </summary>
Expand Down