Skip to content

Commit 5db7284

Browse files
filzrevAndreyAkinshin
authored andcommitted
fix: auto-generate jobid between benchmark runs
1 parent ed5316b commit 5db7284

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/BenchmarkDotNet/Jobs/JobIdGenerator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ public static string GenerateRandomId(Job job)
1010
string presentation = CharacteristicSetPresenter.Display.ToPresentation(job);
1111
if (presentation == "")
1212
return "DefaultJob";
13-
int seed = presentation.GetHashCode();
13+
int seed = GetStableHashCode(presentation);
1414
var random = new Random(seed);
1515
string id = "";
1616
for (int i = 0; i < 6; i++)
17-
id += (char) ('A' + random.Next(26));
17+
id += (char)('A' + random.Next(26));
1818
return "Job-" + id;
1919
}
20+
21+
// Compute string hash value with DJB2 algorithm.
22+
private static int GetStableHashCode(string value)
23+
{
24+
uint hash = 5381;
25+
foreach (char c in value)
26+
{
27+
hash = ((hash << 5) + hash) + c; // hash * 32 + hash + c
28+
}
29+
return unchecked((int)hash);
30+
}
2031
}
2132
}

tests/BenchmarkDotNet.Tests/Configs/ImmutableConfigTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void WhenTwoConfigsAreAddedTheMutatorJobsAreAppliedToDefaultJobIfCustomDe
312312
Assert.Equal(warmupCount, mergedJob.Run.WarmupCount);
313313
Assert.False(mergedJob.Meta.IsDefault); // after the merge the "child" job becomes a standard job
314314
Assert.False(mergedJob.Meta.IsMutator); // after the merge the "child" job becomes a standard job
315-
Assert.Single(mergedJob.GetCharacteristicsWithValues(), changedCharacteristic => ReferenceEquals(changedCharacteristic, Jobs.RunMode.WarmupCountCharacteristic));
315+
Assert.Single(mergedJob.GetCharacteristicsWithValues(), changedCharacteristic => ReferenceEquals(changedCharacteristic, BenchmarkDotNet.Jobs.RunMode.WarmupCountCharacteristic));
316316
}
317317
}
318318

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using BenchmarkDotNet.Environments;
2+
using BenchmarkDotNet.Jobs;
3+
using BenchmarkDotNet.Toolchains.CsProj;
4+
using Xunit;
5+
6+
namespace BenchmarkDotNet.Tests.Jobs;
7+
8+
public class JobIdGeneratorTests
9+
{
10+
[Theory]
11+
[MemberData(nameof(GetTheoryData), DisableDiscoveryEnumeration = true)]
12+
public void AutoGenerateJobId(string expectedId, Job job)
13+
{
14+
// Act
15+
var result = job.ResolvedId;
16+
17+
// Assert
18+
Assert.Equal(expectedId, result);
19+
}
20+
21+
public static TheoryData<string, Job> GetTheoryData() => new TheoryData<string, Job>()
22+
{
23+
{"Job-OOTPKI", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80) },
24+
{"Job-QAODSR", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp90) },
25+
{"Job-KHMDUZ", Job.Default.WithToolchain(CsProjCoreToolchain.NetCoreApp80).WithRuntime(CoreRuntime.Core80) },
26+
};
27+
}

0 commit comments

Comments
 (0)