Skip to content

Commit

Permalink
better keeper concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
MiloszKrajewski committed May 16, 2023
1 parent d83fb45 commit 83cbb9f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.0.9 (2023/05/16)
* Added maximum concurrency setting

## 0.0.8 (2023/05/06)
* CHANGED: build process
* ADDED: GitHub actions
Expand Down
55 changes: 55 additions & 0 deletions src/Benchmarks/BatchBuilder1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Concurrent;
using BenchmarkDotNet.Attributes;
using K4os.Async.Toys;

namespace Benchmarks;

public class BatchBuilder1
{
private const int Concurrency = 64;
private readonly SemaphoreSlim _semaphore = new(Concurrency);
private const int OpCount = 10000;
private readonly ConcurrentBag<int> _bag = new();

private readonly IBatchBuilder<int, int> _batchBuilder;

public BatchBuilder1()
{
_batchBuilder = BatchBuilder.Create<int, int, int>(
request => request,
response => response,
NoOpN,
new BatchBuilderSettings { BatchSize = 10, Concurrency = Concurrency });
}

[Benchmark]
public void Straight()
{
Task.WhenAll(Enumerable.Range(0, OpCount).Select(NoOp1)).Wait();
}

[Benchmark]
public void AsBatch()
{
Task.WhenAll(Enumerable.Range(0, OpCount).Select(_batchBuilder.Request)).Wait();
}

private async Task<int[]> NoOpN(int[] requests)
{
await _semaphore.WaitAsync();
await Delay();
foreach (var request in requests) _bag.Add(request);
_semaphore.Release();
return requests;
}

private async Task NoOp1(int i)
{
await _semaphore.WaitAsync();
await Delay();
_bag.Add(i);
_semaphore.Release();
}

private static Task Delay() => Task.Delay(1);
}
18 changes: 18 additions & 0 deletions src/Benchmarks/Benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\K4os.Async.Toys\K4os.Async.Toys.csproj" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// See https://aka.ms/new-console-template for more information

using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
6 changes: 6 additions & 0 deletions src/K4os.Async.Toys.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K4os.Async.Toys.Tests", "K4
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "K4os.Async.Toys.App", "K4os.Async.Toys.App\K4os.Async.Toys.App.csproj", "{3E541988-CA33-477D-907E-DA43944AF400}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmarks", "Benchmarks\Benchmarks.csproj", "{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -53,6 +55,10 @@ Global
{3E541988-CA33-477D-907E-DA43944AF400}.Release|Any CPU.Build.0 = Release|Any CPU
{17C2C146-01F7-4C31-89FF-F20958A8C147}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17C2C146-01F7-4C31-89FF-F20958A8C147}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BA1AD6AB-F72E-4A0C-ACA7-AA328C70CD59}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 4 additions & 2 deletions src/K4os.Async.Toys/AliveKeeper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public AliveKeeper(
new BatchBuilderSettings {
BatchSize = settings.TouchBatchSize,
BatchDelay = settings.TouchBatchDelay,
Concurrency = 1,
Concurrency = settings.Concurrency,
},
log, time);

Expand All @@ -105,7 +105,7 @@ public AliveKeeper(
new BatchBuilderSettings {
BatchSize = settings.DeleteBatchSize,
BatchDelay = TimeSpan.Zero,
Concurrency = 1,
Concurrency = settings.Concurrency,
},
log, time);
}
Expand All @@ -118,6 +118,7 @@ private static IAliveKeeperSettings Validate(IAliveKeeperSettings settings) =>
DeleteBatchSize = settings.DeleteBatchSize.NotLessThan(1),
RetryInterval = settings.RetryInterval.NotLessThan(TimeSpan.Zero),
RetryLimit = settings.RetryLimit.NotLessThan(0),
Concurrency = settings.Concurrency.NotLessThan(1),
};

private static T Pass(T x) => x;
Expand Down Expand Up @@ -217,6 +218,7 @@ private async Task DeleteOne(string display, T item)
/// <param name="key">Key.</param>
/// <returns>Key in human readable format.</returns>
protected virtual string Display(T key) =>
// ReSharper disable once NullCoalescingConditionIsAlwaysNotNullAccordingToAPIContract
_keyToString?.Invoke(key) ?? key.ToString() ?? "<null>";

private async Task TouchOneLoop(T item, CancellationToken token)
Expand Down
6 changes: 6 additions & 0 deletions src/K4os.Async.Toys/AliveKeeperSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public interface IAliveKeeperSettings

/// <summary>Number of retries for deletion.</summary>
int RetryLimit { get; }

/// <summary>Level of concurrency for operations.</summary>
int Concurrency { get; }
}

/// <summary>
Expand All @@ -50,4 +53,7 @@ public class AliveKeeperSettings: IAliveKeeperSettings

/// <inheritdoc />
public int RetryLimit { get; set; }

/// <inheritdoc />
public int Concurrency { get; set; } = 1;
}

0 comments on commit 83cbb9f

Please sign in to comment.