Skip to content

Commit

Permalink
Improve templates (#2338)
Browse files Browse the repository at this point in the history
* Fix batch file

* Update Program.cs

* Fix type of framework parameter

* Fix docs

* Set consoleApp=true by default

* Remove outdated info. The parameter was changed in #1632

* Update docs
  • Loading branch information
YegorStepanov authored Jul 1, 2023
1 parent 17df128 commit dc3cefd
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 29 deletions.
17 changes: 8 additions & 9 deletions docs/articles/guides/dotnet-new-templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: BenchmarkDotNet templates

# BenchmarkDotNet templates

BenchmarkDotNet provides project templates to setup your benchmarks easily
BenchmarkDotNet provides project templates to setup your benchmarks easily.
The template exists for each major .NET language ([C#](https://learn.microsoft.com/dotnet/csharp/), [F#](https://learn.microsoft.com/dotnet/fsharp/) and [VB](https://learn.microsoft.com/dotnet/visual-basic/)) with equivalent features and structure.

## How to install the templates
Expand Down Expand Up @@ -43,22 +43,21 @@ dotnet new benchmark -lang VB

The template projects has five additional options - all of them are optional.

By default a class library project targeting netstandard2.0 is created.
You can specify `-f` or `--frameworks` to change targeting to one or more frameworks:
By default a console app project targeting `net6.0` is created.
This lets you run the benchmarks from console (`dotnet run`) or from your favorite IDE.

The option `-f` or `--framework` changes the target framework:

```log
dotnet new benchmark -f netstandard2.0;net472
dotnet new benchmark -f net472
```

The option `--console-app` creates a console app project targeting `netcoreapp3.0` with an entry point:
You can specify `--console-app=false` to create a class library project targeting `netstandard2.0` by default:

```log
dotnet new benchmark --console-app
dotnet new benchmark --console-app=false
```

This lets you run the benchmarks from console (`dotnet run`) or from your favorite IDE.
**Note:** option `-f` or `--frameworks` will be ignored when `--console-app` is set.

The option `-b` or `--benchmarkName` sets the name of the benchmark class:

```log
Expand Down
18 changes: 14 additions & 4 deletions templates/install-from-source.bat
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
dotnet build -c Release BenchmarkDotNet.Templates.csproj
dotnet pack -c Release BenchmarkDotNet.Templates.csproj
dotnet new -u BenchmarkDotNet.Templates
dotnet new -i BenchmarkDotNet.Templates::0.0.0-* --nuget-source .\bin\Release\
:: Run only from the folder where the batch file is located!

dotnet build BenchmarkDotNet.Templates.csproj -c Release
dotnet pack BenchmarkDotNet.Templates.csproj -c Release

:: If we install the templates via a folder path, then it will have a different ID (ID=folder path).
:: It will conflict with BDN templates from nuget.
:: We need to install the templates via a FILE path in order to update the template from nuget.
::
:: https://stackoverflow.com/questions/47450531/batch-write-output-of-dir-to-a-variable
for /f "delims=" %%a in ('dir /s /b BenchmarkDotNet.Templates*.nupkg') do set "nupkg_path=%%a"

dotnet new --uninstall "BenchmarkDotNet.Templates"
dotnet new --install "%nupkg_path%"
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,54 @@
},
"framework": {
"type": "parameter",
"datatype": "string",
"description": "The target framework for the project (e.g. netstandard2.0). Default \"net5.0\" if \"--console-app\" is true, \"netstandard2.0\" if \"--console-app\" is false",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "net8.0",
"description": ".NET 8"
},
{
"choice": "net7.0",
"description": ".NET 7"
},
{
"choice": "net6.0",
"description": ".NET 6"
},
{
"choice": "netstandard2.1",
"description": ".NET Standard 2.1"
},
{
"choice": "netstandard2.0",
"description": ".NET Standard 2.0"
},
{
"choice": "net481",
"description": ".NET Framework 4.8.1"
},
{
"choice": "net48",
"description": ".NET Framework 4.8"
},
{
"choice": "net472",
"description": ".NET Framework 4.7.2"
},
{
"choice": "net471",
"description": ".NET Framework 4.7.1"
},
{
"choice": "net47",
"description": ".NET Framework 4.7"
},
{
"choice": "net462",
"description": ".NET Framework 4.6.2"
}
],
"defaultValue": ""
},
"frameworkDefault": {
Expand All @@ -45,7 +91,7 @@
"cases": [
{
"condition": "(framework == '' && consoleApp == true)",
"value": "net5.0"
"value": "net6.0"
},
{
"condition": "(framework == '' && consoleApp == false)",
Expand Down Expand Up @@ -92,13 +138,13 @@
"type": "parameter",
"datatype": "bool",
"description": "If specified, the project is set up as console app.",
"defaultValue": "false"
"defaultValue": "true"
},
"version": {
"type": "parameter",
"datatype": "string",
"description": "Version of BenchmarkDotNet that will be referenced.",
"defaultValue": "0.12.1",
"defaultValue": "0.13.5",
"replaces": "$(BenchmarkDotNetVersion)"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

namespace _BenchmarkProjectName_
Expand All @@ -6,7 +7,11 @@ public class Program
{
public static void Main(string[] args)
{
var summary = BenchmarkRunner.Run<$(BenchmarkName)>();
var config = DefaultConfig.Instance;
var summary = BenchmarkRunner.Run<$(BenchmarkName)>(config, args);

// Use this to select benchmarks from the console:
// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,54 @@
},
"framework": {
"type": "parameter",
"datatype": "string",
"description": "The target framework for the project (e.g. netstandard2.0). Default \"net5.0\" if \"--console-app\" is true, \"netstandard2.0\" if \"--console-app\" is false",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "net8.0",
"description": ".NET 8"
},
{
"choice": "net7.0",
"description": ".NET 7"
},
{
"choice": "net6.0",
"description": ".NET 6"
},
{
"choice": "netstandard2.1",
"description": ".NET Standard 2.1"
},
{
"choice": "netstandard2.0",
"description": ".NET Standard 2.0"
},
{
"choice": "net481",
"description": ".NET Framework 4.8.1"
},
{
"choice": "net48",
"description": ".NET Framework 4.8"
},
{
"choice": "net472",
"description": ".NET Framework 4.7.2"
},
{
"choice": "net471",
"description": ".NET Framework 4.7.1"
},
{
"choice": "net47",
"description": ".NET Framework 4.7"
},
{
"choice": "net462",
"description": ".NET Framework 4.6.2"
}
],
"defaultValue": ""
},
"frameworkDefault": {
Expand All @@ -45,7 +91,7 @@
"cases": [
{
"condition": "(framework == '' && consoleApp == true)",
"value": "net5.0"
"value": "net6.0"
},
{
"condition": "(framework == '' && consoleApp == false)",
Expand Down Expand Up @@ -92,13 +138,13 @@
"type": "parameter",
"datatype": "bool",
"description": "If specified, the project is set up as console app.",
"defaultValue": "false"
"defaultValue": "true"
},
"version": {
"type": "parameter",
"datatype": "string",
"description": "Version of BenchmarkDotNet that will be referenced.",
"defaultValue": "0.12.1",
"defaultValue": "0.13.5",
"replaces": "$(BenchmarkDotNetVersion)"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,54 @@
},
"framework": {
"type": "parameter",
"datatype": "string",
"description": "The target framework for the project (e.g. netstandard2.0). Default \"net5.0\" if \"--console-app\" is true, \"netstandard2.0\" if \"--console-app\" is false",
"description": "The target framework for the project.",
"datatype": "choice",
"choices": [
{
"choice": "net8.0",
"description": ".NET 8"
},
{
"choice": "net7.0",
"description": ".NET 7"
},
{
"choice": "net6.0",
"description": ".NET 6"
},
{
"choice": "netstandard2.1",
"description": ".NET Standard 2.1"
},
{
"choice": "netstandard2.0",
"description": ".NET Standard 2.0"
},
{
"choice": "net481",
"description": ".NET Framework 4.8.1"
},
{
"choice": "net48",
"description": ".NET Framework 4.8"
},
{
"choice": "net472",
"description": ".NET Framework 4.7.2"
},
{
"choice": "net471",
"description": ".NET Framework 4.7.1"
},
{
"choice": "net47",
"description": ".NET Framework 4.7"
},
{
"choice": "net462",
"description": ".NET Framework 4.6.2"
}
],
"defaultValue": ""
},
"frameworkDefault": {
Expand All @@ -45,7 +91,7 @@
"cases": [
{
"condition": "(framework == '' && consoleApp == true)",
"value": "net5.0"
"value": "net6.0"
},
{
"condition": "(framework == '' && consoleApp == false)",
Expand Down Expand Up @@ -92,13 +138,13 @@
"type": "parameter",
"datatype": "bool",
"description": "If specified, the project is set up as console app.",
"defaultValue": "false"
"defaultValue": "true"
},
"version": {
"type": "parameter",
"datatype": "string",
"description": "Version of BenchmarkDotNet that will be referenced.",
"defaultValue": "0.12.1",
"defaultValue": "0.13.5",
"replaces": "$(BenchmarkDotNetVersion)"
}
},
Expand Down

0 comments on commit dc3cefd

Please sign in to comment.