feat: add tests for data caching

This commit is contained in:
Timothy Schenk 2023-11-08 15:34:09 +01:00
parent b7a33d0a3f
commit d90dcf696d
3 changed files with 124 additions and 7 deletions

View file

@ -2,16 +2,10 @@ namespace Benchmarks;
using System.Security.Cryptography;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Order;
[SimpleJob(RuntimeMoniker.Net80)]
[SimpleJob(RuntimeMoniker.Net70)]
[SimpleJob(RuntimeMoniker.Net60)]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[RankColumn]
[MemoryDiagnoser]
[ThreadingDiagnoser]
[Config(typeof(GenericConfig))]
public class BinaryConversionBenchmarks
{
private byte[] _data = null!;

View file

@ -0,0 +1,85 @@
using System.Collections.Concurrent;
using System.Collections.Immutable;
namespace Benchmarks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Order;
[Config(typeof(GenericConfig))]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
public class DataCacheBenchmark
{
[Params(1000, 100000, 1000000)] public int N;
private HashSet<int> _hashSet;
private Dictionary<int, int> _dictionary;
private ConcurrentDictionary<int, int> _concurrentDictionary;
private ImmutableHashSet<int> _immutableHashSet;
[GlobalSetup]
public void Setup()
{
_hashSet = new HashSet<int>();
_dictionary = new Dictionary<int, int>();
_concurrentDictionary = new ConcurrentDictionary<int, int>();
_immutableHashSet = ImmutableHashSet<int>.Empty;
_hashSet.Clear();
_dictionary.Clear();
_concurrentDictionary.Clear();
_immutableHashSet = _immutableHashSet.Clear();
_hashSet.EnsureCapacity(N);
_dictionary.EnsureCapacity(N);
for (var i = 0; i < N; i++)
{
_immutableHashSet = _immutableHashSet.Add(i);
_hashSet.Add(i);
_dictionary.Add(i, i);
_concurrentDictionary.TryAdd(i, i);
}
}
[Benchmark]
public void HashSetAdd()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Add(i));
}
[Benchmark]
public void DictionaryAdd()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.Add(N + i, i));
}
[Benchmark]
public void ConcurrentDictionaryAddOrUpdate()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i =>
_concurrentDictionary.AddOrUpdate(N + i, i, (key, oldValue) => oldValue + i));
}
[Benchmark]
public void ImmutableHashSetLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _immutableHashSet.Contains(i));
}
[Benchmark]
public void HashSetLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _hashSet.Contains(i));
}
[Benchmark]
public void DictionaryLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _dictionary.ContainsKey(i));
}
[Benchmark]
public void ConcurrentDictionaryLookup()
{
ParallelEnumerable.Range(0, N).AsParallel().ForAll(i => _concurrentDictionary.ContainsKey(i));
}
}

View file

@ -0,0 +1,38 @@
using BenchmarkDotNet.Analysers;
using BenchmarkDotNet.Columns;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnosers;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Exporters;
using BenchmarkDotNet.Exporters.Csv;
using BenchmarkDotNet.Jobs;
namespace Benchmarks;
public class GenericConfig : ManualConfig
{
public GenericConfig()
{
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core80))
.AddDiagnoser(ThreadingDiagnoser.Default, MemoryDiagnoser.Default,
EventPipeProfiler.Default)
.AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default,
RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default)
.AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default);
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core70))
.AddDiagnoser(ThreadingDiagnoser.Default, MemoryDiagnoser.Default,
EventPipeProfiler.Default)
.AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default,
RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default)
.AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default);
AddJob(Job.Default
.WithRuntime(CoreRuntime.Core60))
.AddDiagnoser(ThreadingDiagnoser.Default, MemoryDiagnoser.Default,
EventPipeProfiler.Default)
.AddAnalyser(MinIterationTimeAnalyser.Default, OutliersAnalyser.Default,
RuntimeErrorAnalyser.Default, EnvironmentAnalyser.Default)
.AddColumn(RankColumn.Arabic).AddExporter(CsvExporter.Default, MarkdownExporter.Default);
}
}