46 lines
1.4 KiB
C#
46 lines
1.4 KiB
C#
using System.Data.Common;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
|
|
namespace BuildFlow.Workflow.Tests.Performance;
|
|
|
|
/// <summary>
|
|
/// 只统计查询命令的 EF 拦截器,用于稳定验证查询次数不随输入规模增长。
|
|
/// </summary>
|
|
public sealed class CountingCommandInterceptor : DbCommandInterceptor
|
|
{
|
|
/// <summary>自最近一次重置后的 SELECT 命令数。</summary>
|
|
public int SelectCount { get; private set; }
|
|
|
|
/// <summary>清零命令计数,排除测试建数阶段。</summary>
|
|
public void Reset() => SelectCount = 0;
|
|
|
|
public override InterceptionResult<DbDataReader> ReaderExecuting(
|
|
DbCommand command,
|
|
CommandEventData eventData,
|
|
InterceptionResult<DbDataReader> result)
|
|
{
|
|
Count(command);
|
|
return result;
|
|
}
|
|
|
|
public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
|
|
DbCommand command,
|
|
CommandEventData eventData,
|
|
InterceptionResult<DbDataReader> result,
|
|
CancellationToken cancellationToken = default)
|
|
{
|
|
Count(command);
|
|
return ValueTask.FromResult(result);
|
|
}
|
|
|
|
private void Count(DbCommand command)
|
|
{
|
|
if (command.CommandText.TrimStart().StartsWith(
|
|
"SELECT",
|
|
StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
SelectCount++;
|
|
}
|
|
}
|
|
}
|