Files
stgsharp/StgSharpDebug/hlsf-test.cs
T
2026-05-12 18:25:48 +08:00

135 lines
5.1 KiB
C#

//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="hlsf-test"
// Project: StgSharp
// AuthorGroup: Nitload
// Copyright (c) Nitload. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using StgSharp.HighPerformance.Memory;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tlsf = StgSharp.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace StgSharp
{
internal partial class Program
{
public static void TestHlsf()
{
unchecked
{
nuint size = (nuint)(4UL * 1024UL * 1024UL * 1024UL);
// HLSF test arena
Console.WriteLine(
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n");
TwoLayerSegregatedFitAllocator hlsfAllocator = new(size);
// === 简洁交替测试:100次alloc与100次free交替进行,直到触发异常或达到最大循环 ===
Console.WriteLine("\n=== 简洁交替测试:100次alloc + 100次free 交替 ===");
int maxRounds = 10000; // 可调整
int maxCount = 10000;
uint[] sizes = [
64u,
128u,
256u,
512u,
1024u,
4096u,
16384u,
65536u,
262144u,
1048576u
];
int[] offset = [
-4,
-3,
-2,
-1,
0,
1,
2,
3,
4
];
Random rnd = new Random(42);
for (int round = 1; round <= maxRounds; round++)
{
List<Tlsf.Handle> batch = new(maxCount);
for (int i = 0; i < maxCount; i++)
{
try
{
uint sz = sizes[rnd.Next(sizes.Length)];
batch.Add(hlsfAllocator.Alloc(sz));
}
catch (Exception e)
{
Console.WriteLine($"alloced:{round},{i}");
Console.WriteLine(e);
throw;
}
}
for (int i = 5; i < maxCount; i += 10)
{
Tlsf.Handle origin = batch[i];
int index = i + offset[rnd.Next(offset.Length)];
batch[i] = batch[index];
batch[index] = origin;
}
for (int i = batch.Count - 1; i >= 0; i--)
{
try
{
hlsfAllocator.Free(batch[i]);
}
catch (Exception e)
{
Console.WriteLine($"freed:{round},{i}");
Console.WriteLine(e);
throw;
}
}
if (round % 100 == 0)
{
Console.WriteLine($"[交替测试] 已完成 {round} 轮 {maxCount}alloc+{maxCount}free");
// hlsfAllocator.DumpPositionChain(false, 10);
// hlsfAllocator.Collect();
// hlsfAllocator.DumpPositionChain(false, 10);
}
}
Console.WriteLine("[交替测试] 正常结束,无异常触发");
}
}
}
}