How can I force BenchmarkDotnet to do exact number of iterations?

46 Views Asked by At

I was trying to do some benchmarking on my api upload endpoints with Antivirus scan and without antivirus scan. I've used BenchmarkDotnet package which has a good reputation in the field. I've created a small console application and created benchmark class which has 2 methods on it called PostWithAV and PostWitoutAV (AV -> Antivirus scan). But I could not figure out how can I configure it in a way that it will do exact number of uploads for both of the endpoints, currently it is doing different number of uploads using the endpoints I want it to do exact number of uploads for both of the upload endpoints. Here is the code I'me trying to run:

var config = new ManualConfig();

config.AddJob(Job.Default.WithIterationCount(50));
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;
    
    [GlobalSetup]
    public void GlobalSetup()
    {
        _httpClient = new HttpClient();
    }

    [Benchmark]
    public async Task<string> PostWithAV()
    {
        var filePath = @"filepath for the file";
        using var stream = new FileStream(filePath, FileMode.Open);
        var fileName = Path.GetFileName(filePath);
    
        var serverUri = "http://localhost:5207";
    
        using var content = new MultipartFormDataContent();
        
        content.Add(new StreamContent(stream), "file", fileName);
        
        var response = await _httpClient.PostAsync($"{serverUri}/upload/WithAV", content);
    
        response.EnsureSuccessStatusCode();
    
        var result =  await response.Content.ReadAsStringAsync();
    
        return result;
    }
    
    [Benchmark]
    public async Task<string> PostWithoutAV()
    {
        var filePath = @"filepath for the file";
        using var stream = new FileStream(filePath, FileMode.Open);
        var fileName = Path.GetFileName(filePath);

        var serverUri = "http://localhost:5207";

        using var content = new MultipartFormDataContent();
        
        content.Add(new StreamContent(stream), "file", fileName);
        
        var response = await _httpClient.PostAsync($"{serverUri}/upload/WithoutAV", content);

        response.EnsureSuccessStatusCode();

        var result =  await response.Content.ReadAsStringAsync();

        return result;
    }
}
0

There are 0 best solutions below