完善节点图的代码生成

This commit is contained in:
fengjiayi
2025-07-07 20:40:24 +08:00
parent b25fd9c83c
commit 678b01f2fe
33 changed files with 1219 additions and 214 deletions

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Serein.Library.Utils
{
/// <summary>
/// 代码计时工具类
/// </summary>
public static class BenchmarkHelpers
{
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="func">需要执行的异步方法</param>
/// <param name="count">执行次数默认10000</param>
public static void Benchmark(Action action, int count = 10000)
{
double max = double.MinValue;
double min = double.MaxValue;
double total = 0;
for (int i = 0; i < count; i++)
{
var sw = Stopwatch.StartNew();
action();
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
if (ms > max) max = ms;
if (ms < min) min = ms;
total += ms;
}
double avg = total / count;
Console.WriteLine($"运行 {count} 次:");
Console.WriteLine($"最大耗时:{max} 毫秒");
Console.WriteLine($"最小耗时:{min} 毫秒");
Console.WriteLine($"平均耗时:{avg} 毫秒");
}
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="func">需要执行的异步方法</param>
/// <param name="count">执行次数默认10000</param>
public static async Task BenchmarkAsync(Func<Task> func, int count = 10000)
{
double max = double.MinValue;
double min = double.MaxValue;
double total = 0;
for (int i = 0; i < count; i++)
{
var sw = Stopwatch.StartNew();
await func();
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
if (ms > max) max = ms;
if (ms < min) min = ms;
total += ms;
}
double avg = total / count;
Console.WriteLine($"运行 {count} 次:");
Console.WriteLine($"总耗时 :{total} 毫秒:");
Console.WriteLine($"最大耗时:{max} 毫秒");
Console.WriteLine($"最小耗时:{min} 毫秒");
Console.WriteLine($"平均耗时:{avg} 毫秒");
}
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="func">需要执行的异步方法</param>
/// <param name="count">执行次数默认10000</param>
public static async Task<TReult> BenchmarkAsync<TReult>(Func<Task<TReult>> func, int count = 10000)
{
double max = double.MinValue;
double min = double.MaxValue;
double total = 0;
TReult result = default;
for (int i = 0; i < count; i++)
{
var sw = Stopwatch.StartNew();
result = await func();
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
if (ms > max) max = ms;
if (ms < min) min = ms;
total += ms;
//Console.WriteLine($"第{count}次: 耗时 {ms} ms");
}
double avg = total / count;
Console.WriteLine($"运行 {count} 次:");
Console.WriteLine($"总耗时 :{total} 毫秒:");
Console.WriteLine($"最大耗时:{max} 毫秒");
Console.WriteLine($"最小耗时:{min} 毫秒");
Console.WriteLine($"平均耗时:{avg} 毫秒");
return result;
}
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="func">需要执行的异步方法</param>
public static async Task<TReult> BenchmarkAsync<TReult>(Func<Task<TReult>> func)
{
double max = double.MinValue;
double min = double.MaxValue;
double total = 0;
TReult result = default;
var sw = Stopwatch.StartNew();
result = await func();
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
if (ms > max) max = ms;
if (ms < min) min = ms;
total += ms;
Console.WriteLine($"运行1次耗时 :{total} 毫秒:");
return result;
}
}
}

View File

@@ -31,6 +31,7 @@ namespace Serein.Library.Utils
/// </summary>
private readonly ConcurrentDictionary<string, Func<object>> _registerCallback;
/// <summary>
/// 未完成注入的实例集合。
/// 键:需要的类型名称
@@ -83,7 +84,6 @@ namespace Serein.Library.Utils
/// 向容器注册类型,并指定其实例成员
/// </summary>
/// <typeparam name="T">需要注册的类型</typeparam>
/// <param name="getInstance">获取实例的回调函数</param>
/// <returns></returns>
public ISereinIOC Register<T>()
{
@@ -117,6 +117,7 @@ namespace Serein.Library.Utils
public ISereinIOC Register<TService, TImplementation>(Func<TService> getInstance)
where TImplementation : TService
{
RegisterType(typeof(TService).FullName, typeof(TImplementation), () => getInstance.Invoke());
return this;
}
@@ -130,6 +131,7 @@ namespace Serein.Library.Utils
public ISereinIOC Register<TService, TImplementation>()
where TImplementation : TService
{
RegisterType(typeof(TService).FullName, typeof(TImplementation));
return this;
}
@@ -137,7 +139,7 @@ namespace Serein.Library.Utils
#endregion
#region
#region
/// <summary>
/// 用于临时实例的创建不登记到IOC容器中依赖项注入失败时也不记录。
/// </summary>
@@ -335,7 +337,8 @@ namespace Serein.Library.Utils
{
if (!dependencyMap[IOC_MAIN].Contains(type.FullName))
{
dependencyMap[IOC_MAIN].Add(type.FullName);
//dependencyMap[IOC_MAIN].Add(type.FullName);
dependencyMap[IOC_MAIN].Add(typeFullName);
}
continue;
}
@@ -459,6 +462,7 @@ namespace Serein.Library.Utils
/// <returns></returns>
private object CreateInstance(string typeName)
{
if (!_typeMappings.TryGetValue(typeName, out var type)) // 获取类型
{
return null;
@@ -535,8 +539,8 @@ namespace Serein.Library.Utils
/// <returns></returns>
public ISereinIOC Build()
{
var dependencyTree = BuildDependencyTree(); // 生成类型依赖关系
var creationOrder = GetCreationOrder(dependencyTree); // 生成创建顺序
Dictionary<string, List<string>> dependencyTree = BuildDependencyTree(); // 生成类型依赖关系
List<string> creationOrder = GetCreationOrder(dependencyTree); // 生成创建顺序
// 输出创建顺序
Debug.WriteLine("创建顺序: " + string.Join($"{Environment.NewLine}↓ {Environment.NewLine}", creationOrder));
@@ -544,7 +548,10 @@ namespace Serein.Library.Utils
// 创建对象
foreach (var typeName in creationOrder)
{
if (typeName.Equals("Serein.Library.LightweightFlowEnvironment"))
{
}
if (_dependencies.ContainsKey(typeName))
{
continue;
@@ -554,13 +561,15 @@ namespace Serein.Library.Utils
continue;
}
var value = CreateInstance(typeName);
if(value is null)
if (value is null)
{
SereinEnv.WriteLine(InfoType.ERROR, $"IOC容器无法创建对象{typeName}");
continue;
}
_dependencies[typeName] = value;
OnIOCMembersChanged?.Invoke(new IOCMembersChangedEventArgs(typeName, value));
}
_typeMappings.Clear();
return this;