Skip to content

Commit

Permalink
added Benchmark for Channel and Ply
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Nov 21, 2022
1 parent c5c0961 commit 2ae14df
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 5 deletions.
6 changes: 6 additions & 0 deletions NBomber.Performance.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Microsoft Visual Studio Solution File, Format Version 12.00
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "NBomber.Benchmarks", "performance\NBomber.Benchmarks\NBomber.Benchmarks.fsproj", "{60140186-F81E-4C62-ACD9-1A327D297399}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBomber.CSharpImpl", "performance\NBomber.CSharpImpl\NBomber.CSharpImpl.csproj", "{C4632132-9C00-4A87-A235-F7A830F5853E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -12,5 +14,9 @@ Global
{60140186-F81E-4C62-ACD9-1A327D297399}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60140186-F81E-4C62-ACD9-1A327D297399}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60140186-F81E-4C62-ACD9-1A327D297399}.Release|Any CPU.Build.0 = Release|Any CPU
{C4632132-9C00-4A87-A235-F7A830F5853E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C4632132-9C00-4A87-A235-F7A830F5853E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C4632132-9C00-4A87-A235-F7A830F5853E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C4632132-9C00-4A87-A235-F7A830F5853E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "7.0.0",
"rollForward": "latestMajor",
"allowPrerelease": true
}
}
42 changes: 39 additions & 3 deletions performance/NBomber.Benchmarks/Actors/Benchmark.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace NBomber.Benchmarks.Actors
open System.Threading.Tasks
open System.Threading.Tasks.Dataflow
open BenchmarkDotNet.Attributes
open NBomber.CSharpImpl

type ActorMessage =
| Inc
Expand All @@ -26,7 +27,7 @@ type ActorState =
type ActorsBenchmark() as this =

//[<Params(100_000, 1_000_000, 10_000_000)>]
[<Params(1_00_000)>]
[<Params(50_000)>]
member val ThreadIteration = 0 with get, set

[<Params(10)>]
Expand All @@ -49,6 +50,38 @@ type ActorsBenchmark() as this =
tcs.Task.Wait()

[<Benchmark>]
member _.ChannelActorPly() =
let actor = ChannelActorPly<ActorState, ActorMessage>(ActorState.init(), ActorState.handle)

[| 1..this.ThreadCount |]
|> Array.Parallel.iter (fun _ ->
for i = 0 to this.ThreadIteration do
let isAdd = i % 2 = 0
if isAdd then actor.Publish(ActorMessage.Inc)
else actor.Publish(ActorMessage.Dec)
)

let tcs = TaskCompletionSource<int>()
actor.Publish(ActorMessage.GetValue tcs)
tcs.Task.Wait()

[<Benchmark>]
member _.ChannelActorCSharp() =
let actor = ChannelActorCSharp<ActorState, ActorMessage>(ActorState.init(), ActorState.handle)

[| 1..this.ThreadCount |]
|> Array.Parallel.iter (fun _ ->
for i = 0 to this.ThreadIteration do
let isAdd = i % 2 = 0
if isAdd then actor.Publish(ActorMessage.Inc)
else actor.Publish(ActorMessage.Dec)
)

let tcs = TaskCompletionSource<int>()
actor.Publish(ActorMessage.GetValue tcs)
tcs.Task.Wait()

//[<Benchmark>]
member _.FastActor() =
let actor = Actor.FastActor<ActorState, ActorMessage>(ActorState.init(), ActorState.handle)

Expand All @@ -69,7 +102,10 @@ type ActorsBenchmark() as this =
let mutable state = ActorState.init()

let actor = ActionBlock(fun msg ->
state <- ActorState.handle state msg
backgroundTask {
state <- ActorState.handle state msg
}
:> Task
)

[| 1..this.ThreadCount |]
Expand All @@ -84,7 +120,7 @@ type ActorsBenchmark() as this =
actor.Post(ActorMessage.GetValue tcs) |> ignore
tcs.Task.Wait()

[<Benchmark>]
//[<Benchmark>]
member _.MailBox() =
let mutable state = ActorState.init()

Expand Down
21 changes: 20 additions & 1 deletion performance/NBomber.Benchmarks/Actors/ChannelActor.fs
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
namespace NBomber.Benchmarks.Actors

open System
open System.Threading.Channels
open FSharp.Control.Tasks.NonAffine

type ChannelActor<'TState,'TMessage>(initialState: 'TState, handler: 'TState -> 'TMessage -> 'TState) =

let _channel = Channel.CreateUnbounded<'TMessage>()
let mutable _currentState = initialState
let mutable _stop = false

let loop () = task {
let loop () = backgroundTask {
while not _stop do
let! msg = _channel.Reader.ReadAsync()
_currentState <- handler _currentState msg
}

do loop() |> ignore

member _.Publish(message: 'TMessage) =
_channel.Writer.TryWrite(message) |> ignore

type ChannelActorPly<'TState,'TMessage>(initialState: 'TState, handler: 'TState -> 'TMessage -> 'TState) =

let _channel = Channel.CreateUnbounded<'TMessage>()
let mutable _currentState = initialState
let mutable _stop = false

let loop () = vtask {
while not _stop do
let! msg = _channel.Reader.ReadAsync()
_currentState <- handler _currentState msg
Expand Down
8 changes: 7 additions & 1 deletion performance/NBomber.Benchmarks/NBomber.Benchmarks.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand All @@ -16,7 +16,13 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="FSharpx.Collections" Version="2.1.2" />
<PackageReference Include="Ply" Version="0.3.1" />
<PackageReference Include="Streams" Version="0.5.0" />
<PackageReference Update="FSharp.Core" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NBomber.CSharpImpl\NBomber.CSharpImpl.csproj" />
</ItemGroup>

</Project>
30 changes: 30 additions & 0 deletions performance/NBomber.CSharpImpl/ChannelActorCSharp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading.Channels;

namespace NBomber.CSharpImpl;

public class ChannelActorCSharp<TState,TMessage>
{
TState _currentState;
private bool stop = false;
private readonly Func<TState, TMessage, TState> _handler;
private readonly Channel<TMessage> _channel = Channel.CreateUnbounded<TMessage>();

public ChannelActorCSharp(TState initialState, Func<TState, TMessage, TState> handler)
{
_currentState = initialState;
_handler = handler;

Loop();
}

async Task Loop()
{
while (!stop)
{
var msg = await _channel.Reader.ReadAsync();
_currentState = _handler(_currentState, msg);
}
}

public void Publish(TMessage message) => _channel.Writer.TryWrite(message);
}
9 changes: 9 additions & 0 deletions performance/NBomber.CSharpImpl/NBomber.CSharpImpl.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

0 comments on commit 2ae14df

Please sign in to comment.