This commit is contained in:
Nitload
2025-08-25 09:50:30 +08:00
parent bd0e31e922
commit 1359ff4ecc
1630 changed files with 738486 additions and 2399 deletions
+39 -110
View File
@@ -45,7 +45,6 @@ using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Threading;
using System.Xml.XPath;
namespace StgSharp
@@ -57,131 +56,61 @@ namespace StgSharp
internal static unsafe void Main(string[] args)
{
/*
try
{
/**/
World.Init();
World.Init();
Console.WriteLine("StgSharp 内存分配器综合性能评估系统");
Console.WriteLine("===============================================\n");
// HLSF test arena
Console.WriteLine(
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Test Arena ===\n");
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024);
Stack<HybridLayerSegregatedFitAllocationHandle> handles = [];
// HLSF test arena (保留原始测试)
Console.WriteLine($"\n{new string('=', 80)}");
Console.WriteLine(
"=== HLSF (Hybrid Layer Segregated Fit Allocator) Original Stress Test ===\n");
hlsfAllocator.DisplayEntryLayout(false);
for (int c = 0; c < 1; c++)
{
int i = 0;
for (; i < 1024 * 1024 / 1024; i++)
{
Console.WriteLine($"{c} cycles: Allocating the {i}th block.");
handles.Push(hlsfAllocator.Alloc(1024));
hlsfAllocator.DisplayEntryLayout(true);
// 使用适中的内存池大小
HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(16 * 1024 * 1024);
Console.WriteLine("开始单线程压力测试...\n");
// 单线程压力测试:适度的分配和释放操作
int totalCycles = 5;
int allocationsPerCycle = 1024 * 64; // 65536 次分配
for (int cycle = 0; cycle < totalCycles; cycle++)
{
Console.WriteLine($"\n=== 第 {cycle + 1} 轮压力测试 ===");
// 预生成随机数
uint[] randomSizes = new uint[allocationsPerCycle];
Random random = new Random(cycle);
for (int i = 0; i < allocationsPerCycle; i++) {
randomSizes[i] = (uint)(64 + random.Next(1024 - 64 + 1));
Console.ReadKey();
}
Console.WriteLine($"{i} allocs called, {handles.Count} handles collected.");
Console.ReadKey();
while (handles.TryPop(out HybridLayerSegregatedFitAllocationHandle h))
{
i--;
hlsfAllocator.Free(h);
Console.WriteLine($"{c} cycles: The {i}th handle is freed");
}
if (handles.Count != 0)
{
Console.WriteLine($"[警告] 第{c}轮后Stack未清空,剩余{handles.Count}个handle未释放!");
} else
{
Console.WriteLine("All handles freed.");
}
Console.ReadKey();
}
// 分配阶段
List<HybridLayerSegregatedFitAllocationHandle> handles = new List<HybridLayerSegregatedFitAllocationHandle>(allocationsPerCycle);
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < allocationsPerCycle; i++) {
handles.Add(hlsfAllocator.Alloc(randomSizes[i]));
}
stopwatch.Stop();
Console.WriteLine($"分配完成,总计分配 {handles.Count} 个块");
Console.WriteLine($"分配耗时: {stopwatch.ElapsedMilliseconds} ms");
if (stopwatch.ElapsedMilliseconds > 0) {
Console.WriteLine($"分配速度: {allocationsPerCycle * 1000 / stopwatch.ElapsedMilliseconds} allocs/sec");
}
// 随机释放阶段
stopwatch.Restart();
for (int i = handles.Count - 1; i >= 0; i--) {
hlsfAllocator.Free(handles[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("\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++)
{
uint size = testSizes[i % testSizes.Length];
mixedHandles.Add(hlsfAllocator.Alloc(size));
}
foreach (HybridLayerSegregatedFitAllocationHandle handle in mixedHandles) {
hlsfAllocator.Free(handle);
}
Console.WriteLine("混合大小分配测试完成");
Console.WriteLine("\n所有测试完成!HLSF分配器表现良好。");
Console.WriteLine("按任意键退出...");
Console.ReadKey();
/*
}
catch (Exception ex)
{
Console.WriteLine(ex);
/*
World.WriteLog($"{ex}", LogType.Error);
if( true ) { }
Dialogue.PostError( ex );
/**/
World.Terminate(-1);
/**/
}
finally
{
World.Terminate(0);
}
/**/
}
}