0

I have a benchmark class with code similar to the following:

[GlobalSetup]
public async Task Setup()
{
    await Task.Delay(1000 * 10);

    Console.WriteLine("Got here...");
}

[Benchmark]
public async Task Benchmark()
{
   await OtherCode();
}

When I run this, OtherCode is ran before the GlobalSetup finishes entirely. Is this a bug in BenchmarkDotNet, or am I likely doing something incorrectly?

I've attempted to search online, but most of my results brought up the same handful of articles that didn't have any useful info.

Edit:

https://github.com/dotnet/BenchmarkDotNet/issues/1738

This link shows a similar problem and it doesn't look like it ever got solved. Has anyone dealt with a problem similar to this, and if so, what was your work-around / solution?

Edit 2:

With my specific setup, I was able to get it working, though it does seem a bit hacky. The code that I had in the GlobalSetup I instead calculated beforehand and stored in a static field. Then I referenced that in the benchmark class.

It's also important to note that I'm running my benchmark class from an xunit test and with InProcessEmitToolchain in the job's config for the runner.

That being said, I'm still curious as to why GlobalSetup doesn't get awaited properly. Is this in the works to be fixed, or is it intentional?

Knight Steele
  • 169
  • 4
  • 14
  • post he code as answer. still i fear you didn't understant await asynchrony in its entirety, you start 2 threads one halt for ten seconds and the seconds does what it does in less than 10 seconds. – nbk Aug 21 '23 at 21:16
  • @nbk As far as I understand it, GlobalSetup is suppose to run entirely before any of the benchmarks get hit. However, the code in the benchmark gets called before Setup ever gets to "Got here...". The time either of the async tasks take to complete should be irrelevant, as no matter what the task in GlobalSetup should finish completely before OtherCode gets ran. – Knight Steele Aug 21 '23 at 21:28
  • the samoles don't have any await in them, but it does open a new thread and run, you can grab the starttime and end time of the second other ´code and see how long it actually takes – nbk Aug 21 '23 at 21:34
  • Not sure what you mean. Both methods are async and have awaits in them. Do you mean on Benchmark's side of things GlobablSetup doesn't get awaited? – Knight Steele Aug 21 '23 at 22:00
  • no, like i explained from the start, both processes start only the globalsetup is slower, this can you verify buy getting the start tme and end time of other methid – nbk Aug 21 '23 at 22:18
  • If that's true then I definitely misunderstood the purpose of GlobalSetup. I thought it was to setup the data for your benchmarks (as the name implies), then run your benchmarks. If they run simultaneously I don't see the purpose of GlobalSetup. – Knight Steele Aug 21 '23 at 22:24

0 Answers0