|
| 1 | +using lib <io>; |
| 2 | +using ns std; |
| 3 | + |
| 4 | +// 🚀 CodeNothing v0.6.11 线程本地内存池性能测试 |
| 5 | + |
| 6 | +fn main(): int { |
| 7 | + std::println("🧮 线程本地内存池性能测试开始"); |
| 8 | + |
| 9 | + // 测试1: 基础线程本地内存分配 |
| 10 | + std::println("测试1: 基础线程本地内存分配"); |
| 11 | + test_basic_local_allocation(); |
| 12 | + |
| 13 | + // 测试2: 大量小对象分配 |
| 14 | + std::println("测试2: 大量小对象分配"); |
| 15 | + test_small_object_allocation(); |
| 16 | + |
| 17 | + // 测试3: 内存池扩展测试 |
| 18 | + std::println("测试3: 内存池扩展测试"); |
| 19 | + test_memory_pool_expansion(); |
| 20 | + |
| 21 | + // 测试4: 智能内存分配策略 |
| 22 | + std::println("测试4: 智能内存分配策略"); |
| 23 | + test_smart_allocation_strategy(); |
| 24 | + |
| 25 | + // 测试5: 性能基准测试 |
| 26 | + std::println("测试5: 性能基准测试"); |
| 27 | + test_performance_benchmark(); |
| 28 | + |
| 29 | + std::println("🎉 线程本地内存池测试完成!"); |
| 30 | + |
| 31 | + return 0; |
| 32 | +}; |
| 33 | + |
| 34 | +fn test_basic_local_allocation(): int { |
| 35 | + std::println(" - 基础分配测试"); |
| 36 | + |
| 37 | + // 分配一些基础类型 |
| 38 | + i : int = 1; |
| 39 | + while (i <= 10) { |
| 40 | + temp_int : int = i * 100; |
| 41 | + temp_str : string = "test_object"; |
| 42 | + temp_bool : bool = (i % 2 == 0); |
| 43 | + |
| 44 | + std::println(" 分配对象完成"); |
| 45 | + i = i + 1; |
| 46 | + }; |
| 47 | + |
| 48 | + std::println(" ✅ 基础分配测试完成"); |
| 49 | + return 0; |
| 50 | +}; |
| 51 | + |
| 52 | +fn test_small_object_allocation(): int { |
| 53 | + std::println(" - 小对象大量分配测试"); |
| 54 | + |
| 55 | + total_allocated : int = 0; |
| 56 | + i : int = 1; |
| 57 | + while (i <= 100) { |
| 58 | + // 分配小对象 |
| 59 | + small_int : int = i; |
| 60 | + small_float : float = 1.5; |
| 61 | + small_string : string = "obj_test"; |
| 62 | + |
| 63 | + total_allocated = total_allocated + 3; // 每次分配3个对象 |
| 64 | + |
| 65 | + if (i % 20 == 0) { |
| 66 | + std::println(" 已分配小对象"); |
| 67 | + }; |
| 68 | + |
| 69 | + i = i + 1; |
| 70 | + }; |
| 71 | + |
| 72 | + std::println(" ✅ 小对象分配测试完成"); |
| 73 | + return total_allocated; |
| 74 | +}; |
| 75 | + |
| 76 | +fn test_memory_pool_expansion(): int { |
| 77 | + std::println(" - 内存池扩展测试"); |
| 78 | + |
| 79 | + // 分配大量对象以触发池扩展 |
| 80 | + expansion_count : int = 0; |
| 81 | + i : int = 1; |
| 82 | + while (i <= 50) { |
| 83 | + // 创建较大的字符串以快速消耗内存池 |
| 84 | + large_string : string = "large_object_with_extra_data_to_consume_memory_pool_space"; |
| 85 | + array_data : array<int> = [1, 2, 3, 4, 5]; |
| 86 | + |
| 87 | + expansion_count = expansion_count + 1; |
| 88 | + |
| 89 | + if (i % 10 == 0) { |
| 90 | + std::println(" 扩展测试进度"); |
| 91 | + }; |
| 92 | + |
| 93 | + i = i + 1; |
| 94 | + }; |
| 95 | + |
| 96 | + std::println(" ✅ 内存池扩展测试完成"); |
| 97 | + return expansion_count; |
| 98 | +}; |
| 99 | + |
| 100 | +fn test_smart_allocation_strategy(): int { |
| 101 | + std::println(" - 智能分配策略测试"); |
| 102 | + |
| 103 | + strategy_tests : int = 0; |
| 104 | + |
| 105 | + // 测试不同类型的智能分配 |
| 106 | + i : int = 1; |
| 107 | + while (i <= 20) { |
| 108 | + // 小型临时值 |
| 109 | + small_temp : int = i; |
| 110 | + |
| 111 | + // 中等大小字符串 |
| 112 | + medium_string : string = "medium_size_string_for_testing_allocation_strategy"; |
| 113 | + |
| 114 | + // 小数组 |
| 115 | + small_array : array<int> = [1, 2, 3]; |
| 116 | + |
| 117 | + strategy_tests = strategy_tests + 3; |
| 118 | + |
| 119 | + if (i % 5 == 0) { |
| 120 | + std::println(" 智能分配测试进行中"); |
| 121 | + }; |
| 122 | + |
| 123 | + i = i + 1; |
| 124 | + }; |
| 125 | + |
| 126 | + std::println(" ✅ 智能分配策略测试完成"); |
| 127 | + return strategy_tests; |
| 128 | +}; |
| 129 | + |
| 130 | +fn test_performance_benchmark(): int { |
| 131 | + std::println(" - 性能基准测试"); |
| 132 | + |
| 133 | + // 高频分配测试 |
| 134 | + benchmark_iterations : int = 200; |
| 135 | + total_operations : int = 0; |
| 136 | + |
| 137 | + std::println(" 开始高频内存操作基准测试..."); |
| 138 | + |
| 139 | + i : int = 1; |
| 140 | + while (i <= benchmark_iterations) { |
| 141 | + // 模拟真实应用场景的内存操作 |
| 142 | + temp_calc : int = i * i; |
| 143 | + temp_result : float = 3.14159; |
| 144 | + temp_message : string = "benchmark_result"; |
| 145 | + |
| 146 | + // 嵌套分配 |
| 147 | + j : int = 1; |
| 148 | + while (j <= 3) { |
| 149 | + nested_value : int = i * j; |
| 150 | + nested_string : string = "nested_value"; |
| 151 | + j = j + 1; |
| 152 | + total_operations = total_operations + 2; |
| 153 | + }; |
| 154 | + |
| 155 | + total_operations = total_operations + 3; |
| 156 | + |
| 157 | + if (i % 50 == 0) { |
| 158 | + std::println(" 基准测试进度"); |
| 159 | + }; |
| 160 | + |
| 161 | + i = i + 1; |
| 162 | + }; |
| 163 | + |
| 164 | + std::println(" ✅ 性能基准测试完成"); |
| 165 | + std::println(" 总迭代次数完成"); |
| 166 | + std::println(" 总内存操作数完成"); |
| 167 | + std::println(" 平均每次迭代操作数完成"); |
| 168 | + |
| 169 | + return total_operations; |
| 170 | +}; |
0 commit comments