reset hlsf to single thread

This commit is contained in:
Youmu Liu
2025-08-23 22:05:47 +08:00
parent 15bd35fbfe
commit 11b630ffa2
6 changed files with 441 additions and 938 deletions
+137 -36
View File
@@ -63,62 +63,163 @@ namespace StgSharp
// HLSF test arena
Console.WriteLine(
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Multi-threaded Test Arena ===\n");
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024 * 4);
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n");
int threadCount = 4;
int blocksPerThread = 1024;
int blockSize = 1024;
Thread[] threads = new Thread[threadCount];
for (int t = 0; t < threadCount; t++)
// 使用适中的内存池大小
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(16 * 1024 * 1024);
Console.WriteLine("开始单线程压力测试...");
// 单线程压力测试:适度的分配和释放操作
int totalCycles = 5;
int allocationsPerCycle = 1024 * 64; // 65536 次分配
for (int cycle = 0; cycle < totalCycles; cycle++)
{
threads[t] = new Thread(() => {
int threadIndex = Environment.CurrentManagedThreadId;
Stack<HybridLayerSegregatedFitAllocationHandle> handles = new();
Console.WriteLine($"\n=== 第 {cycle + 1} 轮压力测试 ===");
// Allocate blocks
for (int i = 0; i < blocksPerThread; i++)
// 测试不同大小的分配
List<HybridLayerSegregatedFitAllocationHandle> handles = new List<HybridLayerSegregatedFitAllocationHandle>();
Random random = new Random(cycle); // 使用固定种子以确保可重现性
// 分配阶段
Console.WriteLine($"分配阶段:分配 {allocationsPerCycle} 个内存块...");
Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < allocationsPerCycle; i++)
{
// 随机大小:64字节到1KB
uint size = (uint)(64 + random.Next(1024 - 64 + 1));
handles.Add(hlsfAllocator.Alloc(size));
if (i % 5000 == 0 && i > 0) {
Console.WriteLine($" 已分配 {i} 个块");
}
}
stopwatch.Stop();
Console.WriteLine($"分配完成,总计分配 {handles.Count} 个块");
Console.WriteLine($"分配耗时: {stopwatch.ElapsedMilliseconds} ms");
if (stopwatch.ElapsedMilliseconds > 0) {
Console.WriteLine($"分配速度: {allocationsPerCycle * 1000 / stopwatch.ElapsedMilliseconds} allocs/sec");
}
// 随机释放阶段
Console.WriteLine("随机释放阶段...");
stopwatch.Restart();
// 随机打乱释放顺序
for (int i = handles.Count - 1; i > 0; i--)
{
int j = random.Next(i + 1);
(handles[i], handles[j]) = (handles[j], handles[i]);
}
for (int i = 0; i < handles.Count; i++)
{
hlsfAllocator.Free(handles[i]);
if (i % 5000 == 0 && i > 0) {
Console.WriteLine($" 已释放 {i} 个块");
}
}
stopwatch.Stop();
Console.WriteLine($"释放完成,总计释放 {handles.Count} 个块");
Console.WriteLine($"释放耗时: {stopwatch.ElapsedMilliseconds} ms");
if (stopwatch.ElapsedMilliseconds > 0) {
Console.WriteLine($"释放速度: {handles.Count * 1000 / stopwatch.ElapsedMilliseconds} frees/sec");
}
handles.Clear();
// 碎片化测试
Console.WriteLine("碎片化测试:交替分配释放...");
List<HybridLayerSegregatedFitAllocationHandle> fragmentationHandles = new List<HybridLayerSegregatedFitAllocationHandle>();
for (int i = 0; i < 500; i++) // 减少碎片化测试次数
{
// 分配5个块
for (int j = 0; j < 5; j++)
{
Console.WriteLine($"Thread {threadIndex}: Allocating block {i}.");
handles.Push(hlsfAllocator.Alloc((uint)blockSize));
uint size = (uint)(64 << (i % 4)); // 64, 128, 256, 512
fragmentationHandles.Add(hlsfAllocator.Alloc(size));
}
// Free blocks
while (handles.TryPop(out HybridLayerSegregatedFitAllocationHandle handle))
// 释放前3个块,保留后2个
for (int j = 0; j < 3 && fragmentationHandles.Count > 0; j++)
{
Console.WriteLine($"Thread {threadIndex}: Freeing a block.");
hlsfAllocator.Free(handle);
hlsfAllocator.Free(fragmentationHandles[0]);
fragmentationHandles.RemoveAt(0);
}
}
Console.WriteLine($"Thread {threadIndex}: All blocks allocated and freed.");
});
// 释放剩余的块
foreach (HybridLayerSegregatedFitAllocationHandle handle in fragmentationHandles)
{
hlsfAllocator.Free(handle);
}
Console.WriteLine($"第 {cycle + 1} 轮测试完成");
}
// Start all threads
foreach (Thread thread in threads)
Console.WriteLine("\n=== 边界测试 ===");
// 测试各种边界情况
Console.WriteLine("测试最小分配 (64字节)...");
List<HybridLayerSegregatedFitAllocationHandle> minHandles = new List<HybridLayerSegregatedFitAllocationHandle>();
for (int i = 0; i < 1000; i++) {
minHandles.Add(hlsfAllocator.Alloc(64));
}
foreach (HybridLayerSegregatedFitAllocationHandle handle in minHandles) {
hlsfAllocator.Free(handle);
}
Console.WriteLine("最小分配测试完成");
Console.WriteLine("测试最大分配 (1024字节)...");
List<HybridLayerSegregatedFitAllocationHandle> maxHandles = new List<HybridLayerSegregatedFitAllocationHandle>();
for (int i = 0; i < 1000; i++) {
maxHandles.Add(hlsfAllocator.Alloc(1024));
}
foreach (HybridLayerSegregatedFitAllocationHandle handle in maxHandles) {
hlsfAllocator.Free(handle);
}
Console.WriteLine("最大分配测试完成");
Console.WriteLine("测试混合大小分配...");
List<HybridLayerSegregatedFitAllocationHandle> mixedHandles = new List<HybridLayerSegregatedFitAllocationHandle>();
uint[] testSizes = {
64, 128, 256, 512, 1024
};
for (int i = 0; i < 2000; i++)
{
thread.Start();
uint size = testSizes[i % testSizes.Length];
mixedHandles.Add(hlsfAllocator.Alloc(size));
}
// Wait for all threads to complete
foreach (Thread thread in threads)
{
thread.Join();
foreach (HybridLayerSegregatedFitAllocationHandle handle in mixedHandles) {
hlsfAllocator.Free(handle);
}
Console.WriteLine("混合大小分配测试完成");
Console.WriteLine("All threads completed successfully.");
Console.WriteLine("\n所有测试完成!HLSF分配器表现良好。");
Console.WriteLine("按任意键退出...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex);
/*
World.WriteLog($"{ex}", LogType.Error);
if( true ) { }
Dialogue.PostError( ex );
/**/
Console.WriteLine($"测试过程中发生错误: {ex}");
Console.WriteLine("按任意键退出...");
Console.ReadKey();
World.Terminate(-1);
/**/
}
finally
{