Files
git.stella-ops.org/src/__Libraries/StellaOps.HybridLogicalClock.Benchmarks/HlcTimestampBenchmarks.cs
2026-02-01 21:37:40 +02:00

133 lines
3.5 KiB
C#

// <copyright file="HlcTimestampBenchmarks.cs" company="StellaOps">
// Copyright (c) StellaOps. Licensed under BUSL-1.1.
// </copyright>
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using System.Text.Json;
namespace StellaOps.HybridLogicalClock.Benchmarks;
/// <summary>
/// Benchmarks for HlcTimestamp operations.
/// Measures parsing, serialization, and comparison performance.
/// </summary>
[MemoryDiagnoser]
[SimpleJob(RunStrategy.Throughput, iterationCount: 10)]
public class HlcTimestampBenchmarks
{
private HlcTimestamp _timestamp;
private string _sortableString = null!;
private string _jsonString = null!;
private HlcTimestamp[] _timestamps = null!;
private static readonly JsonSerializerOptions JsonOptions = new();
[GlobalSetup]
public void Setup()
{
_timestamp = new HlcTimestamp
{
PhysicalTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
NodeId = "scheduler-east-1",
LogicalCounter = 42
};
_sortableString = _timestamp.ToSortableString();
_jsonString = JsonSerializer.Serialize(_timestamp, JsonOptions);
// Generate array of timestamps for sorting benchmark
_timestamps = new HlcTimestamp[1000];
var random = new Random(42);
for (int i = 0; i < _timestamps.Length; i++)
{
_timestamps[i] = new HlcTimestamp
{
PhysicalTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + random.Next(-1000, 1000),
NodeId = $"node-{random.Next(1, 10)}",
LogicalCounter = random.Next(0, 1000)
};
}
}
/// <summary>
/// Benchmark ToSortableString serialization.
/// </summary>
[Benchmark]
public string ToSortableString()
{
return _timestamp.ToSortableString();
}
/// <summary>
/// Benchmark Parse from sortable string.
/// </summary>
[Benchmark]
public HlcTimestamp Parse()
{
return HlcTimestamp.Parse(_sortableString);
}
/// <summary>
/// Benchmark TryParse from sortable string.
/// </summary>
[Benchmark]
public bool TryParse()
{
return HlcTimestamp.TryParse(_sortableString, out _);
}
/// <summary>
/// Benchmark full round-trip: serialize then parse.
/// </summary>
[Benchmark]
public HlcTimestamp RoundTrip()
{
var str = _timestamp.ToSortableString();
return HlcTimestamp.Parse(str);
}
/// <summary>
/// Benchmark JSON serialization.
/// </summary>
[Benchmark]
public string JsonSerialize()
{
return JsonSerializer.Serialize(_timestamp, JsonOptions);
}
/// <summary>
/// Benchmark JSON deserialization.
/// </summary>
[Benchmark]
public HlcTimestamp JsonDeserialize()
{
return JsonSerializer.Deserialize<HlcTimestamp>(_jsonString, JsonOptions);
}
/// <summary>
/// Benchmark CompareTo operation.
/// </summary>
[Benchmark]
public int CompareTo()
{
var other = new HlcTimestamp
{
PhysicalTime = _timestamp.PhysicalTime + 1,
NodeId = _timestamp.NodeId,
LogicalCounter = 0
};
return _timestamp.CompareTo(other);
}
/// <summary>
/// Benchmark sorting 1000 timestamps.
/// </summary>
[Benchmark]
public void Sort1000Timestamps()
{
var copy = (HlcTimestamp[])_timestamps.Clone();
Array.Sort(copy);
}
}