ssmith
ssmith

Reputation: 8962

Reading Environment Variables in BenchmarkDotNet

I'm trying to load values from environment variables when running a BenchmarkDotNet project from the command line. I'm passing my environment variables using --envVars key:value. However, they're not being picked up by the benchmark.

Here's some sample code:

using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
[ReturnValueValidator(failOnError: true)]
public class RedisBenchmark
{
  [GlobalSetup]
  public void GlobalSetup()
  {
    _redisService.Servers = System.Environment.GetEnvironmentVariable(Constants.REDIS_SERVERS) ?? "";
    if (string.IsNullOrEmpty(_redisService.Servers)) _redisService.Servers = "NO SERVERS SPECIFIED";
  }

  private static RedisService _redisService = new();

  [Benchmark(Description = "Write and Read to Redis")]
  public void WriteAndReadRedis() => _redisService.WriteAndRead();
}

Notice the "NO SERVERS SPECIFIED" string above. When my RedisService runs and outputs its connection string as the benchmark warms up, it outputs that string, so I know that GlobalSetup ran, but that it wasn't able to find or read the environment variable.

Is there something I'm missing?

Some background and discussion here as well: https://github.com/dotnet/BenchmarkDotNet/issues/2156

Upvotes: 1

Views: 280

Answers (1)

snowfrogdev
snowfrogdev

Reputation: 6873

As of this writing, there is a bug affecting the [ReturnValueValidator] and it looks like it skips running GlobalSetup. As a temporary fix you can remove the [ReturnValueValidator] and it should work.

Upvotes: 3

Related Questions