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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/BenchmarkDotNet/Jobs/JobIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ public static string GenerateRandomId(Job job)
string presentation = CharacteristicSetPresenter.Display.ToPresentation(job);
if (presentation == "")
return "DefaultJob";
int seed = presentation.GetHashCode();
int seed = GetStableHashCode(presentation);
var random = new Random(seed);
string id = "";
for (int i = 0; i < 6; i++)
id += (char) ('A' + random.Next(26));
id += (char)('A' + random.Next(26));
return "Job-" + id;
}

// Compute string hash value with DJB2 algorithm.
private static int GetStableHashCode(string value)
{
uint hash = 5381;
foreach (char c in value)
{
hash = ((hash << 5) + hash) + c; // hash * 32 + hash + c
}
return unchecked((int)hash);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public void WhenTwoConfigsAreAddedTheMutatorJobsAreAppliedToDefaultJobIfCustomDe
Assert.Equal(warmupCount, mergedJob.Run.WarmupCount);
Assert.False(mergedJob.Meta.IsDefault); // after the merge the "child" job becomes a standard job
Assert.False(mergedJob.Meta.IsMutator); // after the merge the "child" job becomes a standard job
Assert.Single(mergedJob.GetCharacteristicsWithValues(), changedCharacteristic => ReferenceEquals(changedCharacteristic, Jobs.RunMode.WarmupCountCharacteristic));
Assert.Single(mergedJob.GetCharacteristicsWithValues(), changedCharacteristic => ReferenceEquals(changedCharacteristic, BenchmarkDotNet.Jobs.RunMode.WarmupCountCharacteristic));
}
}

Expand Down
27 changes: 27 additions & 0 deletions tests/BenchmarkDotNet.Tests/Jobs/JobIdGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.CsProj;
using Xunit;

namespace BenchmarkDotNet.Tests.Jobs;

public class JobIdGeneratorTests
{
[Theory]
[MemberData(nameof(GetTheoryData), DisableDiscoveryEnumeration = true)]
public void AutoGenerateJobId(string expectedId, Job job)
{
// Act
var result = job.ResolvedId;

// Assert
Assert.Equal(expectedId, result);
}

public static TheoryData<string, Job> GetTheoryData() => new TheoryData<string, Job>()
{
{"Job-OOTPKI", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80) },
{"Job-QAODSR", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp90) },
{"Job-KHMDUZ", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80).WithRuntime(CoreRuntime.Core80) },
};
}