Skip to content

Commit a9cd780

Browse files
authored
Deprecate WithNuget (#2812)
* Deprecated .WithNuget. Added .WithMsBuildArguments. Updated IntroNuGet. * Simplify csproj version selection.
1 parent 7250970 commit a9cd780

File tree

15 files changed

+78
-24
lines changed

15 files changed

+78
-24
lines changed

docs/articles/samples/IntroNuGet.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
---
1+
---
22
uid: BenchmarkDotNet.Samples.IntroNuGet
33
---
44

55
## Sample: IntroNuGet
66

7-
You can set specific versions of NuGet dependencies for each job.
7+
You can set specific versions of NuGet dependencies for each job using MsBuild properties in your csproj.
88
It allows comparing different versions of the same package (if there are no breaking changes in API).
99

1010
### Source code
@@ -13,14 +13,11 @@ It allows comparing different versions of the same package (if there are no brea
1313

1414
### Output
1515

16-
| Method | Job | NuGetReferences | Mean | Error | StdDev |
17-
|------------------------- |------- |----------------------- |---------:|----------:|----------:|
18-
| SerializeAnonymousObject | 10.0.1 | Newtonsoft.Json 10.0.1 | 2.926 us | 0.0795 us | 0.0283 us |
19-
| SerializeAnonymousObject | 10.0.2 | Newtonsoft.Json 10.0.2 | 2.877 us | 0.5928 us | 0.2114 us |
20-
| SerializeAnonymousObject | 10.0.3 | Newtonsoft.Json 10.0.3 | 2.706 us | 0.1251 us | 0.0446 us |
21-
| SerializeAnonymousObject | 11.0.1 | Newtonsoft.Json 11.0.1 | 2.778 us | 0.5037 us | 0.1796 us |
22-
| SerializeAnonymousObject | 11.0.2 | Newtonsoft.Json 11.0.2 | 2.644 us | 0.0609 us | 0.0217 us |
23-
| SerializeAnonymousObject | 9.0.1 | Newtonsoft.Json 9.0.1 | 2.722 us | 0.3552 us | 0.1267 us |
16+
| Method | Job | Arguments | Mean | Error | StdDev |
17+
|-------------------------- |------- |-------------------- |---------:|----------:|----------:|
18+
| ToImmutableArrayBenchmark | v9.0.0 | /p:SciVersion=9.0.0 | 1.173 μs | 0.0057 μs | 0.0086 μs |
19+
| ToImmutableArrayBenchmark | v9.0.3 | /p:SciVersion=9.0.3 | 1.173 μs | 0.0038 μs | 0.0058 μs |
20+
| ToImmutableArrayBenchmark | v9.0.5 | /p:SciVersion=9.0.5 | 1.172 μs | 0.0107 μs | 0.0157 μs |
2421

2522
### Links
2623

samples/BenchmarkDotNet.Samples/BenchmarkDotNet.Samples.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
2020
<Reference Include="System.Reflection" />
2121
</ItemGroup>
22+
<PropertyGroup>
23+
<!-- Use 9.0.0 as baseline package for IntroNuGet -->
24+
<SciVersion Condition="'$(SciVersion)' == ''">9.0.0</SciVersion>
25+
</PropertyGroup>
2226
<ItemGroup>
23-
<!-- Use v9.0.0 as baseline package for WithNuGet tests -->
24-
<PackageReference Include="System.Collections.Immutable" Version="9.0.0" />
27+
<PackageReference Include="System.Collections.Immutable" Version="$(SciVersion)" />
2528
</ItemGroup>
2629
<ItemGroup>
2730
<PackageReference Include="System.Drawing.Common" Version="9.0.5" />

samples/BenchmarkDotNet.Samples/IntroNuGet.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,26 @@ namespace BenchmarkDotNet.Samples
1111
/// Benchmarks between various versions of a NuGet package
1212
/// </summary>
1313
/// <remarks>
14-
/// Only supported with the CsProjCoreToolchain toolchain
14+
/// Only supported with CsProj toolchains.
1515
/// </remarks>
1616
[Config(typeof(Config))]
1717
public class IntroNuGet
1818
{
19-
// Specify jobs with different versions of the same NuGet package to benchmark.
20-
// The NuGet versions referenced on these jobs must be greater or equal to the
21-
// same NuGet version referenced in this benchmark project.
22-
// Example: This benchmark project references Newtonsoft.Json 13.0.1
19+
// Setup your csproj like this:
20+
/*
21+
<PropertyGroup>
22+
<!-- Use 9.0.0 as default package version if not specified -->
23+
<SciVersion Condition="'$(SciVersion)' == ''">9.0.0</SciVersion>
24+
</PropertyGroup>
25+
<ItemGroup>
26+
<PackageReference Include="System.Collections.Immutable" Version="$(SciVersion)" />
27+
</ItemGroup>
28+
*/
29+
// All versions of the package must be source-compatible with your benchmark code.
2330
private class Config : ManualConfig
2431
{
2532
public Config()
2633
{
27-
var baseJob = Job.MediumRun;
28-
2934
string[] targetVersions = [
3035
"9.0.0",
3136
"9.0.3",
@@ -34,8 +39,10 @@ public Config()
3439

3540
foreach (var version in targetVersions)
3641
{
37-
AddJob(baseJob.WithNuGet("System.Collections.Immutable", version)
38-
.WithId($"v{version}"));
42+
AddJob(Job.MediumRun
43+
.WithMsBuildArguments($"/p:SciVersion={version}")
44+
.WithId($"v{version}")
45+
);
3946
}
4047
}
4148
}

src/BenchmarkDotNet/Environments/InfrastructureResolver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ private InfrastructureResolver()
1717
Register(InfrastructureMode.BuildConfigurationCharacteristic, () => InfrastructureMode.ReleaseConfigurationName);
1818

1919
Register(InfrastructureMode.ArgumentsCharacteristic, Array.Empty<Argument>);
20+
21+
#pragma warning disable CS0618 // Type or member is obsolete
2022
Register(InfrastructureMode.NuGetReferencesCharacteristic, Array.Empty<NuGetReference>);
23+
#pragma warning restore CS0618 // Type or member is obsolete
2124
}
2225
}
2326
}

src/BenchmarkDotNet/Jobs/InfrastructureMode.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
24
using System.Diagnostics.CodeAnalysis;
35
using BenchmarkDotNet.Characteristics;
46
using BenchmarkDotNet.Engines;
@@ -18,6 +20,9 @@ public sealed class InfrastructureMode : JobMode<InfrastructureMode>
1820
public static readonly Characteristic<IEngineFactory> EngineFactoryCharacteristic = CreateCharacteristic<IEngineFactory>(nameof(EngineFactory));
1921
public static readonly Characteristic<string> BuildConfigurationCharacteristic = CreateCharacteristic<string>(nameof(BuildConfiguration));
2022
public static readonly Characteristic<IReadOnlyList<Argument>> ArgumentsCharacteristic = CreateCharacteristic<IReadOnlyList<Argument>>(nameof(Arguments));
23+
24+
[EditorBrowsable(EditorBrowsableState.Never)]
25+
[Obsolete("This will soon be removed")]
2126
public static readonly Characteristic<IReadOnlyCollection<NuGetReference>> NuGetReferencesCharacteristic = CreateCharacteristic<IReadOnlyCollection<NuGetReference>>(nameof(NuGetReferences));
2227

2328
public static readonly InfrastructureMode InProcess = new InfrastructureMode(InProcessEmitToolchain.Instance);
@@ -64,6 +69,8 @@ public IReadOnlyList<Argument> Arguments
6469
set => ArgumentsCharacteristic[this] = value;
6570
}
6671

72+
[EditorBrowsable(EditorBrowsableState.Never)]
73+
[Obsolete("This will soon be removed")]
6774
public IReadOnlyCollection<NuGetReference> NuGetReferences
6875
{
6976
get => NuGetReferencesCharacteristic[this];

src/BenchmarkDotNet/Jobs/JobExtensions.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,8 @@ public static Job WithEnvironmentVariable(this Job job, string key, string value
339339
/// <param name="source">(optional)Indicate the URI of the NuGet package source to use during the restore operation.</param>
340340
/// <param name="prerelease">(optional)Allows prerelease packages to be installed.</param>
341341
/// <returns></returns>
342+
[EditorBrowsable(EditorBrowsableState.Never)]
343+
[Obsolete("This method will soon be removed, please start using .WithMsBuildArguments() instead.")]
342344
public static Job WithNuGet(this Job job, string packageName, string? packageVersion = null, Uri? source = null, bool prerelease = false) =>
343345
job.WithCore(j => j.Infrastructure.NuGetReferences =
344346
new NuGetReferenceList(j.Infrastructure.NuGetReferences ?? Array.Empty<NuGetReference>())
@@ -352,9 +354,14 @@ public static Job WithNuGet(this Job job, string packageName, string? packageVer
352354
/// <param name="job"></param>
353355
/// <param name="nuGetReferences">A collection of NuGet dependencies</param>
354356
/// <returns></returns>
357+
[EditorBrowsable(EditorBrowsableState.Never)]
358+
[Obsolete("This method will soon be removed, please start using .WithMsBuildArguments() instead.")]
355359
public static Job WithNuGet(this Job job, NuGetReferenceList nuGetReferences) =>
356360
job.WithCore(j => j.Infrastructure.NuGetReferences = nuGetReferences);
357361

362+
public static Job WithMsBuildArguments(this Job job, params string[] msBuildArguments)
363+
=> job.WithArguments([.. msBuildArguments.Select(a => new MsBuildArgument(a))]);
364+
358365
// Accuracy
359366
/// <summary>
360367
/// Maximum acceptable error for a benchmark (by default, BenchmarkDotNet continue iterations until the actual error is less than the specified error).
@@ -437,8 +444,10 @@ private static Job WithCore(this Job job, Action<Job> updateCallback)
437444
return newJob;
438445
}
439446

440-
internal static bool HasDynamicBuildCharacteristic(this Job job) =>
441-
job.HasValue(InfrastructureMode.NuGetReferencesCharacteristic)
447+
internal static bool HasDynamicBuildCharacteristic(this Job job)
448+
#pragma warning disable CS0618 // Type or member is obsolete
449+
=> job.HasValue(InfrastructureMode.NuGetReferencesCharacteristic)
450+
#pragma warning restore CS0618 // Type or member is obsolete
442451
|| job.HasValue(InfrastructureMode.BuildConfigurationCharacteristic)
443452
|| job.HasValue(InfrastructureMode.ArgumentsCharacteristic);
444453
}

src/BenchmarkDotNet/Jobs/NugetReference.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.ComponentModel;
23
using System.Text.RegularExpressions;
34

45
namespace BenchmarkDotNet.Jobs
56
{
7+
[EditorBrowsable(EditorBrowsableState.Never)]
8+
[Obsolete("This type will soon be removed")]
69
public class NuGetReference : IEquatable<NuGetReference>
710
{
811
public NuGetReference(string packageName, string packageVersion, Uri? source = null, bool prerelease = false)

src/BenchmarkDotNet/Jobs/NugetReferenceList.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.ComponentModel;
45

56
namespace BenchmarkDotNet.Jobs
67
{
78
/// <summary>
89
/// An ordered list of NuGet references. Does not allow duplicate references with the same PackageName.
910
/// </summary>
11+
[EditorBrowsable(EditorBrowsableState.Never)]
12+
[Obsolete("This type will soon be removed")]
1013
public class NuGetReferenceList : IReadOnlyCollection<NuGetReference>
1114
{
1215
private readonly List<NuGetReference> references = new List<NuGetReference>();

src/BenchmarkDotNet/Running/BenchmarkPartitioner.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ public bool Equals(BenchmarkCase x, BenchmarkCase y)
4747
return false;
4848
if (AreDifferent(jobX.Infrastructure.Arguments, jobY.Infrastructure.Arguments)) // arguments can be anything (Mono runtime settings or MsBuild parameters)
4949
return false;
50+
#pragma warning disable CS0618 // Type or member is obsolete
5051
if (AreDifferent(jobX.Infrastructure.NuGetReferences, jobY.Infrastructure.NuGetReferences))
5152
return false;
53+
#pragma warning restore CS0618 // Type or member is obsolete
5254
if (!jobX.Environment.Gc.Equals(jobY.Environment.Gc)) // GC settings are per .config/.csproj
5355
return false;
5456

@@ -81,8 +83,10 @@ public int GetHashCode(BenchmarkCase obj)
8183
hashCode.Add(job.Infrastructure.BuildConfiguration);
8284
foreach (var arg in job.Infrastructure.Arguments ?? Array.Empty<Argument>())
8385
hashCode.Add(arg);
86+
#pragma warning disable CS0618 // Type or member is obsolete
8487
foreach (var reference in job.Infrastructure.NuGetReferences ?? Array.Empty<NuGetReference>())
8588
hashCode.Add(reference);
89+
#pragma warning restore CS0618 // Type or member is obsolete
8690
return hashCode.ToHashCode();
8791
}
8892

src/BenchmarkDotNet/Toolchains/DotNetCli/DotNetCliCommand.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.ComponentModel;
34
using System.IO;
45
using System.Linq;
56
using System.Text;
@@ -57,7 +58,9 @@ public BuildResult RestoreThenBuild()
5758
{
5859
DotNetCliCommandExecutor.LogEnvVars(WithArguments(null));
5960

61+
#pragma warning disable CS0618 // Type or member is obsolete
6062
var packagesResult = AddPackages();
63+
#pragma warning restore CS0618 // Type or member is obsolete
6164
if (!packagesResult.IsSuccess)
6265
return BuildResult.Failure(GenerateResult, packagesResult.AllInformation);
6366

@@ -97,7 +100,9 @@ public BuildResult RestoreThenBuildThenPublish()
97100
{
98101
DotNetCliCommandExecutor.LogEnvVars(WithArguments(null));
99102

103+
#pragma warning disable CS0618 // Type or member is obsolete
100104
var packagesResult = AddPackages();
105+
#pragma warning restore CS0618 // Type or member is obsolete
101106
if (!packagesResult.IsSuccess)
102107
return BuildResult.Failure(GenerateResult, packagesResult.AllInformation);
103108

@@ -115,6 +120,8 @@ public BuildResult RestoreThenBuildThenPublish()
115120
return PublishNoRestore().ToBuildResult(GenerateResult);
116121
}
117122

123+
[EditorBrowsable(EditorBrowsableState.Never)]
124+
[Obsolete("This method will soon be removed")]
118125
public DotNetCliCommandResult AddPackages()
119126
{
120127
var executionTime = new TimeSpan(0);
@@ -150,6 +157,7 @@ public DotNetCliCommandResult PublishNoRestore()
150157
=> DotNetCliCommandExecutor.Execute(WithArguments(
151158
GetPublishCommand(GenerateResult.ArtifactsPaths, BuildPartition, $"{Arguments} --no-restore", "publish-no-restore")));
152159

160+
[Obsolete]
153161
internal static IEnumerable<string> GetAddPackagesCommands(BuildPartition buildPartition)
154162
=> GetNuGetAddPackageCommands(buildPartition.RepresentativeBenchmarkCase, buildPartition.Resolver);
155163

@@ -204,6 +212,7 @@ private static string GetCustomMsBuildArguments(BenchmarkCase benchmarkCase, IRe
204212
return string.Join(" ", msBuildArguments.Select(arg => arg.TextRepresentation));
205213
}
206214

215+
[Obsolete]
207216
private static IEnumerable<string> GetNuGetAddPackageCommands(BenchmarkCase benchmarkCase, IResolver resolver)
208217
{
209218
if (!benchmarkCase.Job.HasValue(InfrastructureMode.NuGetReferencesCharacteristic))
@@ -229,6 +238,7 @@ private static string GetMandatoryMsBuildSettings(string buildConfiguration)
229238
return $"{NoMsBuildZombieProcesses} {EnforceOptimizations}";
230239
}
231240

241+
[Obsolete]
232242
private static string BuildAddPackageCommand(NuGetReference reference)
233243
{
234244
var commandBuilder = new StringBuilder();

0 commit comments

Comments
 (0)