Skip to content

Commit

Permalink
Added benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Emil Koutanov committed Jan 29, 2024
1 parent fe13fdc commit bf0eea2
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ jobs:
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
- name: Run
run: dotnet run --project Examples
- name: Bench
run: dotnet run --property:Configuration=Release --project Actors.Benchmarks
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,6 @@ FakesAssemblies/

# Visual Studio 6 workspace options file
*.opt

.idea
BenchmarkDotNet.Artifacts
34 changes: 34 additions & 0 deletions Actors.Benchmarks/AckDelivery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;

namespace Actors.Benchmarks;

public class AckDelivery
{
class WorkItem
{
public TaskCompletionSource Completion { get; } = new();
}

class AckDeliveryWorker : Actor<WorkItem>
{
protected override Task Perform(Inbox inbox)
{
var workItem = inbox.Receive();
workItem.Completion.SetResult();
return Task.CompletedTask;
}
}

private readonly AckDeliveryWorker worker = new();

[Benchmark]
public async Task SendAndAwaitAck()
{
var workItem = new WorkItem();
worker.Send(workItem);
await workItem.Completion.Task;
}
}
23 changes: 23 additions & 0 deletions Actors.Benchmarks/Actors.Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.13.12" Condition="'$(OS)' == 'Windows_NT'"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Actors\Actors.csproj" />
</ItemGroup>
</Project>
33 changes: 33 additions & 0 deletions Actors.Benchmarks/FireAndForget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Threading.Tasks;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;

namespace Actors.Benchmarks;

public class FireAndForget
{
class FireAndForgetWorker : Actor<int>
{
protected override Task Perform(Inbox inbox)
{
inbox.Receive();
return Task.CompletedTask;
}
}

private readonly FireAndForgetWorker worker = new();

[Params(1_000)]
public int numMessages;

[Benchmark]
public async Task SendAndDrain()
{
for (var i = 0; i < numMessages; i++)
{
worker.Send(i);
}
await worker.Drain();
}
}
18 changes: 18 additions & 0 deletions Actors.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

namespace Actors.Benchmarks
{
public class Program
{
public static void Main(string[] args)
{
var config = DefaultConfig.Instance;
BenchmarkRunner.Run<FireAndForget>(config, args);
BenchmarkRunner.Run<AckDelivery>(config, args);

// Use this to select benchmarks from the console:
// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
}
2 changes: 1 addition & 1 deletion Actors/Actor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private async Task RunAsync()
/// <summary>
/// Represents the actor's dedicated inbox.
/// </summary>
public class Inbox
protected class Inbox
{
private readonly Actor<M> actor;

Expand Down
4 changes: 2 additions & 2 deletions Examples/Throttle/Throttle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class Parent : Actor
private const int Radix = 10;

/// <summary>
/// The maximum number of works that may coexist.
/// The maximum number of workers that may coexist.
/// </summary>
private const int MaxChildren = 8;

/// <summary>
/// The worker children. (Child labour.)
/// The child worker actors. (Child labour.)
/// </summary>
private readonly Child?[] children = new Child[Radix];

Expand Down
6 changes: 6 additions & 0 deletions actors.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Actors", "Actors\Actors.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Actors.Tests", "Actors.Tests\Actors.Tests.csproj", "{608221E0-A26E-40C5-AF55-286292D042A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Actors.Benchmarks", "Actors.Benchmarks\Actors.Benchmarks.csproj", "{786D9D14-EC7B-4574-9C4E-38DA97FA2077}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,5 +32,9 @@ Global
{608221E0-A26E-40C5-AF55-286292D042A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{608221E0-A26E-40C5-AF55-286292D042A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{608221E0-A26E-40C5-AF55-286292D042A4}.Release|Any CPU.Build.0 = Release|Any CPU
{786D9D14-EC7B-4574-9C4E-38DA97FA2077}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{786D9D14-EC7B-4574-9C4E-38DA97FA2077}.Debug|Any CPU.Build.0 = Debug|Any CPU
{786D9D14-EC7B-4574-9C4E-38DA97FA2077}.Release|Any CPU.ActiveCfg = Release|Any CPU
{786D9D14-EC7B-4574-9C4E-38DA97FA2077}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
6 changes: 5 additions & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ _help:

# runs the example app
run *ARGS:
dotnet run --project Examples/Examples.csproj {{ARGS}}
dotnet run --project Examples {{ARGS}}

# runs the test cases
test:
Expand All @@ -12,3 +12,7 @@ test:
# loops through test cases
loop:
while [ true ]; do dotnet test; done

# runs the benchmarks
bench:
dotnet run --property:Configuration=Release --project Actors.Benchmarks

0 comments on commit bf0eea2

Please sign in to comment.