Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions .bench-harness-current/BenchHarness.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\src\FastCloner\FastCloner.csproj" />
</ItemGroup>
</Project>
34 changes: 34 additions & 0 deletions .bench-harness-current/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Diagnostics;
using FastCloner;

var results = new List<(string Name, double Ms, double NsPerOp)>();
Measure("SmallObject x100000", CreateSmallObject(), 2000, 100000, static x => FastCloner.FastCloner.DeepClone((SmallObject)x)!);
Measure("StringArray1000 x20000", CreateStringArray(1000), 500, 20000, static x => FastCloner.FastCloner.DeepClone((string[])x)!);
Measure("Dictionary50 x5000", CreateDictionary(50), 200, 5000, static x => FastCloner.FastCloner.DeepClone((Dictionary<string, SmallObject>)x)!);
foreach (var (name, ms, nsPerOp) in results)
Console.WriteLine($"{name}|{ms:F2}|{nsPerOp:F1}");

void Measure(string name, object value, int warmup, int iterations, Func<object, object> clone)
{
for (var i = 0; i < warmup; i++) _ = clone(value);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
var sw = Stopwatch.StartNew();
for (var i = 0; i < iterations; i++) _ = clone(value);
sw.Stop();
results.Add((name, sw.Elapsed.TotalMilliseconds, sw.Elapsed.TotalMilliseconds * 1_000_000d / iterations));
}

static SmallObject CreateSmallObject() => new() { Id = 123, Name = "small-object-name", CreatedAt = new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc), IsActive = true, Score = 42.5 };
static string[] CreateStringArray(int count) { var arr = new string[count]; for (var i = 0; i < count; i++) arr[i] = $"value-{i}"; return arr; }
static Dictionary<string, SmallObject> CreateDictionary(int count) { var dict = new Dictionary<string, SmallObject>(count); for (var i = 0; i < count; i++) dict[$"key-{i}"] = new SmallObject { Id = i, Name = $"item-{i}", CreatedAt = new DateTime(2025, 1, 1).AddMinutes(i), IsActive = (i & 1) == 0, Score = i * 1.25 }; return dict; }

public sealed class SmallObject
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; }
public bool IsActive { get; set; }
public double Score { get; set; }
}
6 changes: 3 additions & 3 deletions src/FastCloner.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

namespace FastCloner.Benchmark;

class Program
internal class Program
{
static void Main(string[] args)
{
// this is used only for package "FastDeepCloner" which is not properly packed; todo: fork it and pack for a fair bench
// this is used only for package "FastDeepCloner" which is not properly packed
ManualConfig config = DefaultConfig.Instance
.WithOptions(ConfigOptions.DisableOptimizationsValidator);

Expand Down Expand Up @@ -49,7 +49,7 @@ private static IBenchmarkIdea[] GetIdeas()
{
return typeof(Program).Assembly
.GetTypes()
.Where(t => !t.IsAbstract && !t.IsInterface && typeof(IBenchmarkIdea).IsAssignableFrom(t))
.Where(t => t is { IsAbstract: false, IsInterface: false } && typeof(IBenchmarkIdea).IsAssignableFrom(t))
.Select(t => (IBenchmarkIdea)Activator.CreateInstance(t)!)
.OrderBy(x => x.Id, StringComparer.OrdinalIgnoreCase)
.ToArray();
Expand Down
Loading
Loading