Skip to content

Commit dc3cefd

Browse files
Improve templates (#2338)
* 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
1 parent 17df128 commit dc3cefd

File tree

6 files changed

+181
-29
lines changed

6 files changed

+181
-29
lines changed

docs/articles/guides/dotnet-new-templates.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: BenchmarkDotNet templates
55

66
# BenchmarkDotNet templates
77

8-
BenchmarkDotNet provides project templates to setup your benchmarks easily
8+
BenchmarkDotNet provides project templates to setup your benchmarks easily.
99
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.
1010

1111
## How to install the templates
@@ -43,22 +43,21 @@ dotnet new benchmark -lang VB
4343

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

46-
By default a class library project targeting netstandard2.0 is created.
47-
You can specify `-f` or `--frameworks` to change targeting to one or more frameworks:
46+
By default a console app project targeting `net6.0` is created.
47+
This lets you run the benchmarks from console (`dotnet run`) or from your favorite IDE.
48+
49+
The option `-f` or `--framework` changes the target framework:
4850

4951
```log
50-
dotnet new benchmark -f netstandard2.0;net472
52+
dotnet new benchmark -f net472
5153
```
5254

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

5557
```log
56-
dotnet new benchmark --console-app
58+
dotnet new benchmark --console-app=false
5759
```
5860

59-
This lets you run the benchmarks from console (`dotnet run`) or from your favorite IDE.
60-
**Note:** option `-f` or `--frameworks` will be ignored when `--console-app` is set.
61-
6261
The option `-b` or `--benchmarkName` sets the name of the benchmark class:
6362

6463
```log

templates/install-from-source.bat

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
dotnet build -c Release BenchmarkDotNet.Templates.csproj
2-
dotnet pack -c Release BenchmarkDotNet.Templates.csproj
3-
dotnet new -u BenchmarkDotNet.Templates
4-
dotnet new -i BenchmarkDotNet.Templates::0.0.0-* --nuget-source .\bin\Release\
1+
:: Run only from the folder where the batch file is located!
2+
3+
dotnet build BenchmarkDotNet.Templates.csproj -c Release
4+
dotnet pack BenchmarkDotNet.Templates.csproj -c Release
5+
6+
:: If we install the templates via a folder path, then it will have a different ID (ID=folder path).
7+
:: It will conflict with BDN templates from nuget.
8+
:: We need to install the templates via a FILE path in order to update the template from nuget.
9+
::
10+
:: https://stackoverflow.com/questions/47450531/batch-write-output-of-dir-to-a-variable
11+
for /f "delims=" %%a in ('dir /s /b BenchmarkDotNet.Templates*.nupkg') do set "nupkg_path=%%a"
12+
13+
dotnet new --uninstall "BenchmarkDotNet.Templates"
14+
dotnet new --install "%nupkg_path%"

templates/templates/BenchmarkDotNet.BenchmarkProjectTemplate.CSharp/.template.config/template.json

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,54 @@
3131
},
3232
"framework": {
3333
"type": "parameter",
34-
"datatype": "string",
35-
"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",
34+
"description": "The target framework for the project.",
35+
"datatype": "choice",
36+
"choices": [
37+
{
38+
"choice": "net8.0",
39+
"description": ".NET 8"
40+
},
41+
{
42+
"choice": "net7.0",
43+
"description": ".NET 7"
44+
},
45+
{
46+
"choice": "net6.0",
47+
"description": ".NET 6"
48+
},
49+
{
50+
"choice": "netstandard2.1",
51+
"description": ".NET Standard 2.1"
52+
},
53+
{
54+
"choice": "netstandard2.0",
55+
"description": ".NET Standard 2.0"
56+
},
57+
{
58+
"choice": "net481",
59+
"description": ".NET Framework 4.8.1"
60+
},
61+
{
62+
"choice": "net48",
63+
"description": ".NET Framework 4.8"
64+
},
65+
{
66+
"choice": "net472",
67+
"description": ".NET Framework 4.7.2"
68+
},
69+
{
70+
"choice": "net471",
71+
"description": ".NET Framework 4.7.1"
72+
},
73+
{
74+
"choice": "net47",
75+
"description": ".NET Framework 4.7"
76+
},
77+
{
78+
"choice": "net462",
79+
"description": ".NET Framework 4.6.2"
80+
}
81+
],
3682
"defaultValue": ""
3783
},
3884
"frameworkDefault": {
@@ -45,7 +91,7 @@
4591
"cases": [
4692
{
4793
"condition": "(framework == '' && consoleApp == true)",
48-
"value": "net5.0"
94+
"value": "net6.0"
4995
},
5096
{
5197
"condition": "(framework == '' && consoleApp == false)",
@@ -92,13 +138,13 @@
92138
"type": "parameter",
93139
"datatype": "bool",
94140
"description": "If specified, the project is set up as console app.",
95-
"defaultValue": "false"
141+
"defaultValue": "true"
96142
},
97143
"version": {
98144
"type": "parameter",
99145
"datatype": "string",
100146
"description": "Version of BenchmarkDotNet that will be referenced.",
101-
"defaultValue": "0.12.1",
147+
"defaultValue": "0.13.5",
102148
"replaces": "$(BenchmarkDotNetVersion)"
103149
}
104150
},
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using BenchmarkDotNet.Configs;
12
using BenchmarkDotNet.Running;
23

34
namespace _BenchmarkProjectName_
@@ -6,7 +7,11 @@ public class Program
67
{
78
public static void Main(string[] args)
89
{
9-
var summary = BenchmarkRunner.Run<$(BenchmarkName)>();
10+
var config = DefaultConfig.Instance;
11+
var summary = BenchmarkRunner.Run<$(BenchmarkName)>(config, args);
12+
13+
// Use this to select benchmarks from the console:
14+
// var summaries = BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
1015
}
1116
}
1217
}

templates/templates/BenchmarkDotNet.BenchmarkProjectTemplate.FSharp/.template.config/template.json

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,54 @@
3131
},
3232
"framework": {
3333
"type": "parameter",
34-
"datatype": "string",
35-
"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",
34+
"description": "The target framework for the project.",
35+
"datatype": "choice",
36+
"choices": [
37+
{
38+
"choice": "net8.0",
39+
"description": ".NET 8"
40+
},
41+
{
42+
"choice": "net7.0",
43+
"description": ".NET 7"
44+
},
45+
{
46+
"choice": "net6.0",
47+
"description": ".NET 6"
48+
},
49+
{
50+
"choice": "netstandard2.1",
51+
"description": ".NET Standard 2.1"
52+
},
53+
{
54+
"choice": "netstandard2.0",
55+
"description": ".NET Standard 2.0"
56+
},
57+
{
58+
"choice": "net481",
59+
"description": ".NET Framework 4.8.1"
60+
},
61+
{
62+
"choice": "net48",
63+
"description": ".NET Framework 4.8"
64+
},
65+
{
66+
"choice": "net472",
67+
"description": ".NET Framework 4.7.2"
68+
},
69+
{
70+
"choice": "net471",
71+
"description": ".NET Framework 4.7.1"
72+
},
73+
{
74+
"choice": "net47",
75+
"description": ".NET Framework 4.7"
76+
},
77+
{
78+
"choice": "net462",
79+
"description": ".NET Framework 4.6.2"
80+
}
81+
],
3682
"defaultValue": ""
3783
},
3884
"frameworkDefault": {
@@ -45,7 +91,7 @@
4591
"cases": [
4692
{
4793
"condition": "(framework == '' && consoleApp == true)",
48-
"value": "net5.0"
94+
"value": "net6.0"
4995
},
5096
{
5197
"condition": "(framework == '' && consoleApp == false)",
@@ -92,13 +138,13 @@
92138
"type": "parameter",
93139
"datatype": "bool",
94140
"description": "If specified, the project is set up as console app.",
95-
"defaultValue": "false"
141+
"defaultValue": "true"
96142
},
97143
"version": {
98144
"type": "parameter",
99145
"datatype": "string",
100146
"description": "Version of BenchmarkDotNet that will be referenced.",
101-
"defaultValue": "0.12.1",
147+
"defaultValue": "0.13.5",
102148
"replaces": "$(BenchmarkDotNetVersion)"
103149
}
104150
},

templates/templates/BenchmarkDotNet.BenchmarkProjectTemplate.VB/.template.config/template.json

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,54 @@
3131
},
3232
"framework": {
3333
"type": "parameter",
34-
"datatype": "string",
35-
"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",
34+
"description": "The target framework for the project.",
35+
"datatype": "choice",
36+
"choices": [
37+
{
38+
"choice": "net8.0",
39+
"description": ".NET 8"
40+
},
41+
{
42+
"choice": "net7.0",
43+
"description": ".NET 7"
44+
},
45+
{
46+
"choice": "net6.0",
47+
"description": ".NET 6"
48+
},
49+
{
50+
"choice": "netstandard2.1",
51+
"description": ".NET Standard 2.1"
52+
},
53+
{
54+
"choice": "netstandard2.0",
55+
"description": ".NET Standard 2.0"
56+
},
57+
{
58+
"choice": "net481",
59+
"description": ".NET Framework 4.8.1"
60+
},
61+
{
62+
"choice": "net48",
63+
"description": ".NET Framework 4.8"
64+
},
65+
{
66+
"choice": "net472",
67+
"description": ".NET Framework 4.7.2"
68+
},
69+
{
70+
"choice": "net471",
71+
"description": ".NET Framework 4.7.1"
72+
},
73+
{
74+
"choice": "net47",
75+
"description": ".NET Framework 4.7"
76+
},
77+
{
78+
"choice": "net462",
79+
"description": ".NET Framework 4.6.2"
80+
}
81+
],
3682
"defaultValue": ""
3783
},
3884
"frameworkDefault": {
@@ -45,7 +91,7 @@
4591
"cases": [
4692
{
4793
"condition": "(framework == '' && consoleApp == true)",
48-
"value": "net5.0"
94+
"value": "net6.0"
4995
},
5096
{
5197
"condition": "(framework == '' && consoleApp == false)",
@@ -92,13 +138,13 @@
92138
"type": "parameter",
93139
"datatype": "bool",
94140
"description": "If specified, the project is set up as console app.",
95-
"defaultValue": "false"
141+
"defaultValue": "true"
96142
},
97143
"version": {
98144
"type": "parameter",
99145
"datatype": "string",
100146
"description": "Version of BenchmarkDotNet that will be referenced.",
101-
"defaultValue": "0.12.1",
147+
"defaultValue": "0.13.5",
102148
"replaces": "$(BenchmarkDotNetVersion)"
103149
}
104150
},

0 commit comments

Comments
 (0)