mirror of
https://gitee.com/langsisi_admin/serein-flow
synced 2026-03-03 00:00:49 +08:00
完善节点图的代码生成
This commit is contained in:
133
Library/Utils/BenchmarkHelpers.cs
Normal file
133
Library/Utils/BenchmarkHelpers.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user