From ba2c4e87000db776d292fce68f14dd2dde4437be Mon Sep 17 00:00:00 2001 From: filzrev <103790468+filzrev@users.noreply.github.com> Date: Sat, 14 Jun 2025 21:01:36 +0900 Subject: [PATCH] fix: auto-generate jobid between benchmark runs --- src/BenchmarkDotNet/Jobs/JobIdGenerator.cs | 15 +++++++++-- .../Configs/ImmutableConfigTests.cs | 2 +- .../Jobs/JobIdGeneratorTests.cs | 27 +++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 tests/BenchmarkDotNet.Tests/Jobs/JobIdGeneratorTests.cs diff --git a/src/BenchmarkDotNet/Jobs/JobIdGenerator.cs b/src/BenchmarkDotNet/Jobs/JobIdGenerator.cs index fa8796d112..a208b3c9b7 100644 --- a/src/BenchmarkDotNet/Jobs/JobIdGenerator.cs +++ b/src/BenchmarkDotNet/Jobs/JobIdGenerator.cs @@ -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); + } } } \ No newline at end of file diff --git a/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs b/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs index d77c5202ed..c68134b7cd 100644 --- a/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs +++ b/tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs @@ -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)); } } diff --git a/tests/BenchmarkDotNet.Tests/Jobs/JobIdGeneratorTests.cs b/tests/BenchmarkDotNet.Tests/Jobs/JobIdGeneratorTests.cs new file mode 100644 index 0000000000..c0e845bb7a --- /dev/null +++ b/tests/BenchmarkDotNet.Tests/Jobs/JobIdGeneratorTests.cs @@ -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 GetTheoryData() => new TheoryData() + { + {"Job-OOTPKI", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80) }, + {"Job-QAODSR", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp90) }, + {"Job-KHMDUZ", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80).WithRuntime(CoreRuntime.Core80) }, + }; +}