namespace DTLib.XXHash.PerformanceTest { using System; using System.IO; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; [RPlotExporter, RankColumn] [MinColumn, MaxColumn] [MemoryDiagnoser] // [DisassemblyDiagnoser(printAsm: true, printSource: true)] public class xxHashBenchmark { const int KB = 1024; const int MB = 1024 * KB; const int GB = 1024 * MB; private byte[] data; private MemoryStream stream; [Params(KB, MB, GB)] public int N; [GlobalSetup] public void Setup() { Random rand = new Random(42); data = new byte[N]; rand.NextBytes(data); stream = new MemoryStream(data); } [Benchmark] public uint Hash32_Array() { return xxHash32.ComputeHash(data, data.Length); } #if NET6_0_OR_GREATER [Benchmark] public uint Hash32_Span() { Span span = new Span(data); return xxHash32.ComputeHash(span, span.Length); } [Benchmark] public uint Hash32_ReadOnlySpan() { ReadOnlySpan span = new ReadOnlySpan(data); return xxHash32.ComputeHash(span, span.Length); } #endif [Benchmark] public uint Hash32_Stream() { stream.Seek(0, SeekOrigin.Begin); return xxHash32.ComputeHash(stream); } [Benchmark] public async Task Hash32_StreamAsync() { stream.Seek(0, SeekOrigin.Begin); return await xxHash32.ComputeHashAsync(stream); } [Benchmark] public ulong Hash64_Array() { return xxHash64.ComputeHash(data, data.Length); } #if NET6_0_OR_GREATER [Benchmark] public ulong Hash64_Span() { Span span = new Span(data); return xxHash64.ComputeHash(span, span.Length); } [Benchmark] public ulong Hash64_ReadOnlySpan() { ReadOnlySpan span = new ReadOnlySpan(data); return xxHash64.ComputeHash(span, span.Length); } #endif [Benchmark] public ulong Hash64_Stream() { stream.Seek(0, SeekOrigin.Begin); return xxHash64.ComputeHash(stream); } [Benchmark] public async Task Hash64_StreamAsync() { stream.Seek(0, SeekOrigin.Begin); return await xxHash64.ComputeHashAsync(stream); } } }