Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6906de3

Browse files
committed
Use an auto-incremented id instead of guid.
Always use same ProgramName regardless of --keepFiles. Include benchmark assembly name in ProgramName.
1 parent 072337e commit 6906de3

File tree

6 files changed

+22
-18
lines changed

6 files changed

+22
-18
lines changed

src/BenchmarkDotNet/Running/BuildPartition.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using System.Reflection;
4+
using System.Threading;
45
using BenchmarkDotNet.Characteristics;
56
using BenchmarkDotNet.Configs;
67
using BenchmarkDotNet.Environments;
@@ -18,15 +19,20 @@ namespace BenchmarkDotNet.Running
1819
{
1920
public class BuildPartition
2021
{
22+
// We use an auto-increment global counter instead of Guid to guarantee uniqueness per benchmark run (Guid has a small chance to collide),
23+
// assuming there are fewer than 4 billion build partitions (a safe assumption).
24+
private static int s_partitionCounter;
25+
2126
public BuildPartition(BenchmarkBuildInfo[] benchmarks, IResolver resolver)
2227
{
2328
Resolver = resolver;
2429
RepresentativeBenchmarkCase = benchmarks[0].BenchmarkCase;
2530
Benchmarks = benchmarks;
26-
var keepBenchmarkFiles = benchmarks[0].Config.Options.IsSet(ConfigOptions.KeepBenchmarkFiles);
27-
var guid = Guid.NewGuid().ToString();
28-
ProgramName = keepBenchmarkFiles ? RepresentativeBenchmarkCase.Job.FolderInfo : guid;
29-
ProgramDirectory = keepBenchmarkFiles ? Path.Combine(RepresentativeBenchmarkCase.Job.FolderInfo, guid) : guid;
31+
// Combine the benchmark's assembly name, folder info, and build partition id.
32+
string benchmarkAssemblyName = RepresentativeBenchmarkCase.Descriptor.Type.Assembly.GetName().Name;
33+
string folderInfo = RepresentativeBenchmarkCase.Job.FolderInfo;
34+
int id = Interlocked.Increment(ref s_partitionCounter);
35+
ProgramName = $"{benchmarkAssemblyName}-{folderInfo}-{id}";
3036
LogBuildOutput = benchmarks[0].Config.Options.IsSet(ConfigOptions.LogBuildOutput);
3137
GenerateMSBuildBinLog = benchmarks[0].Config.Options.IsSet(ConfigOptions.GenerateMSBuildBinLog);
3238
}
@@ -35,8 +41,6 @@ public BuildPartition(BenchmarkBuildInfo[] benchmarks, IResolver resolver)
3541

3642
public string ProgramName { get; }
3743

38-
public string ProgramDirectory { get; }
39-
4044
/// <summary>
4145
/// the benchmarks are grouped by the build settings
4246
/// so you can use this benchmark to get the runtime settings

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@ protected DotNetCliGenerator(string targetFrameworkMoniker, string cliPath, stri
3636
/// we are limited by xprojs (by default compiles all .cs files in all subfolders, Program.cs could be doubled and fail the build)
3737
/// and also by NuGet internal implementation like looking for global.json file in parent folders
3838
/// </summary>
39-
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programDirectory)
39+
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
4040
{
4141
if (GetSolutionRootDirectory(out var directoryInfo))
4242
{
43-
return Path.Combine(directoryInfo.FullName, programDirectory);
43+
return Path.Combine(directoryInfo.FullName, programName);
4444
}
4545

4646
// we did not find global.json or any Visual Studio solution file?
4747
// let's return it in the old way and hope that it works ;)
4848
var parent = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent;
4949
if (parent == null)
5050
throw new DirectoryNotFoundException("Parent directory for current directory");
51-
return Path.Combine(parent.FullName, programDirectory);
51+
return Path.Combine(parent.FullName, programName);
5252
}
5353

5454
internal static bool GetSolutionRootDirectory(out DirectoryInfo directoryInfo)

src/BenchmarkDotNet/Toolchains/GeneratorBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public GenerateResult GenerateProject(BuildPartition buildPartition, ILogger log
4242
/// <summary>
4343
/// returns a path to the folder where auto-generated project and code are going to be placed
4444
/// </summary>
45-
[PublicAPI] protected abstract string GetBuildArtifactsDirectoryPath(BuildPartition assemblyLocation, string programDirectory);
45+
[PublicAPI] protected abstract string GetBuildArtifactsDirectoryPath(BuildPartition assemblyLocation, string programName);
4646

4747
/// <summary>
4848
/// returns a path where executable should be found after the build (usually \bin)
@@ -128,10 +128,10 @@ private ArtifactsPaths GetArtifactsPaths(BuildPartition buildPartition, string r
128128
// its not ".cs" in order to avoid VS from displaying and compiling it with xprojs/csprojs that include all *.cs by default
129129
const string codeFileExtension = ".notcs";
130130

131-
string buildArtifactsDirectoryPath = GetBuildArtifactsDirectoryPath(buildPartition, buildPartition.ProgramDirectory);
131+
string programName = buildPartition.ProgramName;
132+
string buildArtifactsDirectoryPath = GetBuildArtifactsDirectoryPath(buildPartition, programName);
132133
string binariesDirectoryPath = GetBinariesDirectoryPath(buildArtifactsDirectoryPath, buildPartition.BuildConfiguration);
133134

134-
string programName = buildPartition.ProgramName;
135135
string executablePath = GetExecutablePath(binariesDirectoryPath, programName);
136136

137137
return new ArtifactsPaths(

src/BenchmarkDotNet/Toolchains/NativeAot/Generator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ internal Generator(string ilCompilerVersion,
6161

6262
protected override string GetExecutableExtension() => OsDetector.ExecutableExtension;
6363

64-
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programDirectory)
64+
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
6565
=> useTempFolderForRestore
66-
? Path.Combine(Path.GetTempPath(), programDirectory) // store everything in temp to avoid collisions with IDE
67-
: base.GetBuildArtifactsDirectoryPath(buildPartition, programDirectory);
66+
? Path.Combine(Path.GetTempPath(), programName) // store everything in temp to avoid collisions with IDE
67+
: base.GetBuildArtifactsDirectoryPath(buildPartition, programName);
6868

6969
protected override string GetBinariesDirectoryPath(string buildArtifactsDirectoryPath, string configuration)
7070
=> Path.Combine(buildArtifactsDirectoryPath, "bin", configuration, TargetFrameworkMoniker, runtimeIdentifier, "publish");

src/BenchmarkDotNet/Toolchains/Roslyn/Generator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace BenchmarkDotNet.Toolchains.Roslyn
1313
[PublicAPI]
1414
public class Generator : GeneratorBase
1515
{
16-
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programDirectory)
16+
protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
1717
=> Path.GetDirectoryName(buildPartition.AssemblyLocation);
1818

1919
[PublicAPI]

tests/BenchmarkDotNet.Tests/CsProjGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,9 @@ public void TestAssemblyFilePathIsUsedWhenTheAssemblyLocationIsNotEmpty()
220220

221221
private class SteamLoadedBuildPartition : CsProjGenerator
222222
{
223-
internal string ResolvePathForBinaries(BuildPartition buildPartition, string programDirectory)
223+
internal string ResolvePathForBinaries(BuildPartition buildPartition, string programName)
224224
{
225-
return base.GetBuildArtifactsDirectoryPath(buildPartition, programDirectory);
225+
return base.GetBuildArtifactsDirectoryPath(buildPartition, programName);
226226
}
227227

228228
public SteamLoadedBuildPartition(string targetFrameworkMoniker, string cliPath, string packagesPath, string runtimeFrameworkVersion, bool isNetCore)

0 commit comments

Comments
 (0)