You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置BenchmarkDotNet使其执行固定次数的迭代?

解决BenchmarkDotNet中两个基准测试方法执行次数不一致的问题

BenchmarkDotNet默认会根据方法执行时长动态调整迭代调用次数(比如更快的方法会被调用更多次以获取稳定统计数据),这会导致你两个上传接口的执行次数不一致。要强制让两个方法执行完全相同的上传次数,需要修改配置,禁用动态调整并固定调用次数。

关键配置修改

你需要调整Job的运行策略,固定迭代次数和每次迭代的调用次数,确保两个方法的总执行次数一致:

var config = new ManualConfig();

// 配置固定执行次数的Job
var fixedIterationJob = Job.Default
    .WithRunStrategy(RunStrategy.ColdStart) // 禁用动态调整,强制按配置执行
    .WithInvocationCount(1) // 每次迭代仅调用1次方法
    .WithIterationCount(50) // 总共执行50次迭代,即总上传50次
    .WithWarmupCount(0); // 可根据需求设置预热次数,0表示无预热

config.AddJob(fixedIterationJob);
// 保留默认的导出器、日志和列提供者
config.Add(DefaultConfig.Instance.GetExporters().ToArray());
config.Add(DefaultConfig.Instance.GetLoggers().ToArray());
config.Add(DefaultConfig.Instance.GetColumnProviders().ToArray());

BenchmarkRunner.Run<HttpClientBenchmark>(config);

代码优化建议

你的两个测试方法逻辑高度重复,建议提取通用上传逻辑,避免代码冗余,同时减少潜在的测试差异:

public class HttpClientBenchmark
{
    private HttpClient _httpClient;
    private readonly string _filePath = @"filepath for the file";
    private readonly string _serverUri = "http://localhost:5207";

    [GlobalSetup]
    public void GlobalSetup()
    {
        _httpClient = new HttpClient();
    }

    // 通用上传方法
    private async Task<string> UploadToEndpoint(string endpointSuffix)
    {
        using var stream = new FileStream(_filePath, FileMode.Open);
        var fileName = Path.GetFileName(_filePath);

        using var content = new MultipartFormDataContent();
        content.Add(new StreamContent(stream), "file", fileName);

        var response = await _httpClient.PostAsync($"{_serverUri}/upload/{endpointSuffix}", content);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    [Benchmark]
    public Task<string> PostWithAV() => UploadToEndpoint("WithAV");

    [Benchmark]
    public Task<string> PostWithoutAV() => UploadToEndpoint("WithoutAV");
}

说明

  • RunStrategy.ColdStart:强制BenchmarkDotNet严格按照配置的迭代次数执行,不会根据方法速度动态调整调用量
  • InvocationCount(1) + IterationCount(50):确保每个方法被精确调用50次(总上传次数50次)
  • 提取通用逻辑后,两个测试方法仅差异在于接口后缀,消除了代码重复带来的测试变量

内容的提问来源于stack exchange,提问作者Asadbek Sindarov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 18:08:11