Files
serein-flow/Library/Utils/BenchmarkHelpers.cs
fengjiayi 48289dae11 1. 修改了FlowResult的创建方式,新增了IsSuccess与Message,为后续进行流程日志追踪准备。
2. 修复了删除节点时,没有正确消除与之相关的参数获取关系。
3. IFlowNode新增了StartFlowAsync方法。
4. Library项目中FlowNodeExtension拓展类转移到了NodeFlow项目中。
5.  修改了Script自定义参数保存,对参数类型、返回值类型进行保存。
6. Library.Utils.BenchmarkHelpter中添加了Task、Task<>的重载方法。
7. NodeFlow项目中,FlowEnvironment默认使用通过NewtonsoftJson实现的JSON门户类。
8. NodeFlow项目中,FlowControl缓存了 FlowWorkOptions 选项实体,并且对于 FlowWorkManagement 进行了池化管理(但后续的项目中考虑重构流程任务运行时)。
2025-07-30 11:29:12 +08:00

198 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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="action">需要执行的方法</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;
SereinEnv.WriteLine(InfoType.INFO, $"运行 {count} 次:");
SereinEnv.WriteLine(InfoType.INFO, $"总耗时 :{total} 毫秒:");
SereinEnv.WriteLine(InfoType.INFO, $"最大耗时:{max} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"最小耗时:{min} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"平均耗时:{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;
SereinEnv.WriteLine(InfoType.INFO, $"运行 {count} 次:");
SereinEnv.WriteLine(InfoType.INFO, $"总耗时 :{total} 毫秒:");
SereinEnv.WriteLine(InfoType.INFO, $"最大耗时:{max} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"最小耗时:{min} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"平均耗时:{avg} 毫秒");
}
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="task">需要执行的异步方法</param>
/// <param name="count">执行次数默认10000</param>
public static async Task BenchmarkAsync(Task task, 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 task;
sw.Stop();
double ms = sw.Elapsed.TotalMilliseconds;
if (ms > max) max = ms;
if (ms < min) min = ms;
total += ms;
}
double avg = total / count;
SereinEnv.WriteLine(InfoType.INFO, $"运行 {count} 次:");
SereinEnv.WriteLine(InfoType.INFO, $"总耗时 :{total} 毫秒:");
SereinEnv.WriteLine(InfoType.INFO, $"最大耗时:{max} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"最小耗时:{min} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"平均耗时:{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;
SereinEnv.WriteLine(InfoType.INFO, $"运行 {count} 次:");
SereinEnv.WriteLine(InfoType.INFO, $"总耗时 :{total} 毫秒:");
SereinEnv.WriteLine(InfoType.INFO, $"最大耗时:{max} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"最小耗时:{min} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"平均耗时:{avg} 毫秒");
return result;
}
/// <summary>
/// 运行指定异步方法多次并输出耗时的最大、最小和平均值。
/// </summary>
/// <param name="task">需要执行的异步方法</param>
/// <param name="count">执行次数默认10000</param>
public static async Task<TReult> BenchmarkAsync<TReult>(Task<TReult> task, 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 task;
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;
SereinEnv.WriteLine(InfoType.INFO, $"运行 {count} 次:");
SereinEnv.WriteLine(InfoType.INFO, $"总耗时 :{total} 毫秒:");
SereinEnv.WriteLine(InfoType.INFO, $"最大耗时:{max} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"最小耗时:{min} 毫秒");
SereinEnv.WriteLine(InfoType.INFO, $"平均耗时:{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;
var tips = $"运行耗时 :{total} 毫秒:";
SereinEnv.WriteLine(InfoType.INFO, tips);
return result;
}
}
}