//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="MatrixParallelTest.cs" // Project: StgSharp // AuthorGroup: Nitload Space // Copyright (c) Nitload Space. 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.Mathematics; using System; using System.Diagnostics; using System.Threading; namespace StgSharp { internal partial class Program { public static void TestMatrixParallel() { // 4. 线程调度基础功能测试 Console.WriteLine("=== 线程调度基础功能测试 ==="); TestSchedulingBasics(); Console.WriteLine(); // 5. 并行控制流测试 Console.WriteLine("=== 并行控制流测试 ==="); TestParallelControlFlow(); Console.WriteLine(); // 6. 重复操作稳定性测试 Console.WriteLine("=== 重复操作稳定性测试 ==="); TestRepeatedOperations(); Console.WriteLine(); // 7. 线程状态监控测试 Console.WriteLine("=== 线程状态监控测试 ==="); TestThreadStateMonitoring(); Console.WriteLine(); Console.WriteLine("? MatrixParallel 分层线程调度系统空跑测试完成"); } private static bool TestInitialization() { if (!MatrixParallel.SupportParallel) { Console.WriteLine("? 跳过初始化测试 - 系统不支持并行处理"); return false; } try { Console.WriteLine("正在初始化 MatrixParallel..."); MatrixParallel.Init(); Console.WriteLine("? MatrixParallel 初始化成功"); // 检查 LeaderHandle 是否正确设置 if (MatrixParallel.LeaderHandle != null) { Console.WriteLine($"? LeaderHandle 已创建,Wrap ID: {MatrixParallel.LeaderHandle.Wrap.ID}"); } else { Console.WriteLine("? LeaderHandle 未正确设置"); } return true; } catch (Exception ex) { Console.WriteLine($"? 初始化失败: {ex.Message}"); return false; } } private static void TestParallelControlFlow() { try { Console.WriteLine("测试并行控制流..."); for (int i = 0; i < 5; i++) { Console.WriteLine($" 第 {i + 1} 轮控制流测试:"); // 获取并行锁 Console.WriteLine(" 执行 LeadParallel()..."); MatrixParallel.LeadParallel(); try { // 启动并行处理 Console.WriteLine($" 执行 LaunchParallel({(SleepMode)(i % 3 + 1)})..."); MatrixParallel.LaunchParallel((SleepMode)(i % 3 + 1)); // 设置所有 Wrap MatrixParallel.SetAllWrap(); // 发布任务(空任务) MatrixParallel.PublishTask(); // 短暂等待,模拟实际工作 Thread.Sleep(10 + i * 5); Console.WriteLine(" ? 控制流执行正常"); } finally { // 释放并行锁 Console.WriteLine(" 执行 EndParallel()..."); MatrixParallel.EndParallel(); } } Console.WriteLine("? 并行控制流测试通过"); } catch (Exception ex) { Console.WriteLine($"? 并行控制流测试失败: {ex.Message}"); } } private static void TestRepeatedOperations() { try { Console.WriteLine("执行重复操作稳定性测试..."); Stopwatch stopwatch = Stopwatch.StartNew(); int iterations = 50; int successCount = 0; for (int i = 0; i < iterations; i++) { try { MatrixParallel.LeadParallel(); MatrixParallel.LaunchParallel(SleepMode.QuickWakeup); MatrixParallel.SetAllWrap(); MatrixParallel.PublishTask(); // 极短暂的等待 Thread.Sleep(1); successCount++; } catch (Exception ex) { Console.WriteLine($" 第 {i + 1} 次操作失败: {ex.Message}"); } finally { try { MatrixParallel.EndParallel(); } catch (Exception ex) { Console.WriteLine($" 第 {i + 1} 次 EndParallel 失败: {ex.Message}"); } } // 每10次操作显示一次进度 if ((i + 1) % 10 == 0) { Console.WriteLine($" 已完成 {i + 1}/{iterations} 次操作"); } } stopwatch.Stop(); Console.WriteLine($"? 重复操作测试完成:"); Console.WriteLine($" 总操作次数: {iterations}"); Console.WriteLine($" 成功次数: {successCount}"); Console.WriteLine($" 成功率: {(double)successCount / iterations * 100:F1}%"); Console.WriteLine($" 总耗时: {stopwatch.ElapsedMilliseconds} ms"); Console.WriteLine($" 平均耗时: {stopwatch.ElapsedMilliseconds / (double)iterations:F2} ms/次"); } catch (Exception ex) { Console.WriteLine($"? 重复操作测试失败: {ex.Message}"); } } private static void TestSchedulingBasics() { try { Console.WriteLine("测试基本调度操作..."); // 测试设置所有 Wrap Console.WriteLine(" 执行 SetAllWrap()..."); MatrixParallel.SetAllWrap(); Console.WriteLine(" ? SetAllWrap() 执行成功"); } catch (Exception ex) { Console.WriteLine($"? 基本调度操作测试失败: {ex.Message}"); } } } }