Files
stgsharp/test/StgSharpDebug/SlabStressTest.cs
T

63 lines
1.9 KiB
C#

// -----------------------------------------------------------------------------
// file="SlabStressTest"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using Nitload.Common.HighPerformance.Memory;
using System;
using System.Diagnostics;
namespace Nitload
{
internal static class SlabStressTest
{
// Exercise the Nonconcurrent Chunked SLAB used by HLSF (allocation/free only)
public static unsafe void Run()
{
Console.WriteLine("=== SLAB Allocator (Chunked) Single-Thread Stress Test ===");
// Use SLAB with element size of pointer (nuint) to simulate Entry handles
SlabAllocator<BunchOfBytes> slab = SlabAllocator.Create<BunchOfBytes>(64, SlabBufferLayout.Chunked, false);
int cycles = 10;
int allocsPerCycle = 200_000; // large enough to force internal growth
Stopwatch sw = new Stopwatch();
for (int c = 0; c < cycles; c++)
{
Console.WriteLine($"--- Cycle {c+1} ---");
nuint[] ptrs = new nuint[allocsPerCycle];
sw.Restart();
for (int i = 0; i < allocsPerCycle; i++) {
ptrs[i] = slab.Allocate();
}
sw.Stop();
Console.WriteLine($"Allocate: {allocsPerCycle} in {sw.Elapsed.TotalSeconds:0.00}s");
sw.Restart();
for (int i = 0; i < allocsPerCycle; i++) {
slab.Free(ptrs[i]);
}
sw.Stop();
Console.WriteLine($"Free: {allocsPerCycle} in {sw.Elapsed.TotalSeconds:0.00}s");
}
slab.Dispose();
Console.WriteLine("=== SLAB Stress Complete ===");
}
internal unsafe struct BunchOfBytes
{
public fixed byte Data[128];
}
}
}