Skip to content

Commit

Permalink
some benchmarks might be using parameters that have locking finalizers
Browse files Browse the repository at this point in the history
so we need to dispose them after we are done running the benchmarks

see #1383 and dotnet/runtime#314 for more
  • Loading branch information
adamsitnik committed Oct 23, 2020
1 parent 503adcc commit 57bf3bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/BenchmarkDotNet/Parameters/ParameterInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace BenchmarkDotNet.Parameters
{
public class ParameterInstance
public class ParameterInstance : IDisposable
{
public const string NullParameterTextRepresentation = "?";

Expand All @@ -24,6 +24,8 @@ public ParameterInstance(ParameterDefinition definition, object value, SummarySt
maxParameterColumnWidth = summaryStyle?.MaxParameterColumnWidth ?? SummaryStyle.DefaultMaxParameterColumnWidth;
}

public void Dispose() => (Value as IDisposable)?.Dispose();

public string Name => Definition.Name;
public bool IsStatic => Definition.IsStatic;
public bool IsArgument => Definition.IsArgument;
Expand Down
13 changes: 11 additions & 2 deletions src/BenchmarkDotNet/Parameters/ParameterInstances.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Extensions;

namespace BenchmarkDotNet.Parameters
{
public class ParameterInstances
public class ParameterInstances : IDisposable
{
public IReadOnlyList<ParameterInstance> Items { get; }
public int Count => Items.Count;
Expand All @@ -18,6 +19,14 @@ public ParameterInstances(IReadOnlyList<ParameterInstance> items)
Items = items;
}

public void Dispose()
{
foreach (var parameterInstance in Items)
{
parameterInstance.Dispose();
}
}

public string FolderInfo => string.Join("_", Items.Select(p => $"{p.Name}-{p.ToDisplayText()}")).AsValidFileName();

public string DisplayInfo => Items.Any() ? "[" + string.Join(", ", Items.Select(p => $"{p.Name}={p.ToDisplayText()}")) + "]" : "";
Expand Down
4 changes: 3 additions & 1 deletion src/BenchmarkDotNet/Running/BenchmarkCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace BenchmarkDotNet.Running
{
public class BenchmarkCase : IComparable<BenchmarkCase>
public class BenchmarkCase : IComparable<BenchmarkCase>, IDisposable
{
public Descriptor Descriptor { get; }
public Job Job { get; }
Expand All @@ -26,6 +26,8 @@ private BenchmarkCase(Descriptor descriptor, Job job, ParameterInstances paramet
Config = config;
}

public void Dispose() => Parameters.Dispose();

public int CompareTo(BenchmarkCase other) => string.Compare(FolderInfo, other.FolderInfo, StringComparison.Ordinal);

public bool HasParameters => Parameters != null && Parameters.Items.Any();
Expand Down
10 changes: 9 additions & 1 deletion src/BenchmarkDotNet/Running/BenchmarkRunInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace BenchmarkDotNet.Running
{
public class BenchmarkRunInfo
public class BenchmarkRunInfo : IDisposable
{
public BenchmarkRunInfo(BenchmarkCase[] benchmarksCase, Type type, ImmutableConfig config)
{
Expand All @@ -12,6 +12,14 @@ public BenchmarkRunInfo(BenchmarkCase[] benchmarksCase, Type type, ImmutableConf
Config = config;
}

public void Dispose()
{
foreach (var benchmarkCase in BenchmarksCases)
{
benchmarkCase.Dispose();
}
}

public BenchmarkCase[] BenchmarksCases { get; }
public Type Type { get; }
public ImmutableConfig Config { get; }
Expand Down
8 changes: 8 additions & 0 deletions src/BenchmarkDotNet/Running/BenchmarkRunnerClean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ internal static Summary[] Run(BenchmarkRunInfo[] benchmarkRunInfos)
}
finally
{
// some benchmarks might be using parameters that have locking finalizers
// so we need to dispose them after we are done running the benchmarks
// see https://github.com/dotnet/BenchmarkDotNet/issues/1383 and https://github.com/dotnet/runtime/issues/314 for more
foreach (var benchmarkInfo in benchmarkRunInfos)
{
benchmarkInfo.Dispose();
}

compositeLogger.WriteLineHeader("// * Artifacts cleanup *");
Cleanup(new HashSet<string>(artifactsToCleanup.Distinct()));
compositeLogger.Flush();
Expand Down

0 comments on commit 57bf3bb

Please sign in to comment.