Skip to content

Commit ccee3e8

Browse files
authored
Merge pull request #1033 from dotnet/tools
BenchmarkDotNet as global tool (#1006), fixes #213
2 parents f9c8cc5 + aa1ded5 commit ccee3e8

File tree

8 files changed

+195
-5
lines changed

8 files changed

+195
-5
lines changed

BenchmarkDotNet.sln

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.Diagnostics
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.IntegrationTests.CustomPaths", "tests\BenchmarkDotNet.IntegrationTests.CustomPaths\BenchmarkDotNet.IntegrationTests.CustomPaths.csproj", "{0031728E-A5D4-47C1-9C1A-6C859A765C9D}"
1717
EndProject
18-
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BenchmarkDotNet.Samples.FSharp", "samples\BenchmarkDotNet.Samples.FSharp\BenchmarkDotNet.Samples.FSharp.fsproj", "{A329F00E-4B9D-4BC6-B688-92698D773CBF}"
18+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "BenchmarkDotNet.Samples.FSharp", "samples\BenchmarkDotNet.Samples.FSharp\BenchmarkDotNet.Samples.FSharp.fsproj", "{A329F00E-4B9D-4BC6-B688-92698D773CBF}"
1919
EndProject
20-
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BenchmarkDotNet.IntegrationTests.FSharp", "tests\BenchmarkDotNet.IntegrationTests.FSharp\BenchmarkDotNet.IntegrationTests.FSharp.fsproj", "{367FAFE1-A1C8-4AA1-9334-F4762E128DBB}"
20+
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "BenchmarkDotNet.IntegrationTests.FSharp", "tests\BenchmarkDotNet.IntegrationTests.FSharp\BenchmarkDotNet.IntegrationTests.FSharp.fsproj", "{367FAFE1-A1C8-4AA1-9334-F4762E128DBB}"
2121
EndProject
22-
Project("{F184B08F-C81C-45F6-A57F-5ABD9991F28F}") = "BenchmarkDotNet.IntegrationTests.VisualBasic", "tests\BenchmarkDotNet.IntegrationTests.VisualBasic\BenchmarkDotNet.IntegrationTests.VisualBasic.vbproj", "{D8803ECA-4ABF-45B8-BD1D-7C637416C448}"
22+
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "BenchmarkDotNet.IntegrationTests.VisualBasic", "tests\BenchmarkDotNet.IntegrationTests.VisualBasic\BenchmarkDotNet.IntegrationTests.VisualBasic.vbproj", "{D8803ECA-4ABF-45B8-BD1D-7C637416C448}"
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.IntegrationTests.DisabledOptimizations", "tests\BenchmarkDotNet.IntegrationTests.DisabledOptimizations\BenchmarkDotNet.IntegrationTests.DisabledOptimizations.csproj", "{AC2188E5-A140-43E2-8A76-4BDABFE30ABA}"
2525
EndProject
@@ -37,7 +37,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.Disassemble
3737
EndProject
3838
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.Disassembler.x86", "src\BenchmarkDotNet.Disassembler.x86\BenchmarkDotNet.Disassembler.x86.csproj", "{D189AAB3-46B4-4437-8E9C-72F021AB2B6E}"
3939
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.IntegrationTests.ManualRunning", "tests\BenchmarkDotNet.IntegrationTests.ManualRunning\BenchmarkDotNet.IntegrationTests.ManualRunning.csproj", "{9816D316-95C4-42E6-9E7B-A256C7E5D4BF}"
40+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BenchmarkDotNet.IntegrationTests.ManualRunning", "tests\BenchmarkDotNet.IntegrationTests.ManualRunning\BenchmarkDotNet.IntegrationTests.ManualRunning.csproj", "{9816D316-95C4-42E6-9E7B-A256C7E5D4BF}"
41+
EndProject
42+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.Tool", "src\BenchmarkDotNet.Tool\BenchmarkDotNet.Tool.csproj", "{1336CAFF-5321-495E-BAFA-AB4DC6BF791C}"
4143
EndProject
4244
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BenchmarkDotNet.IntegrationTests.Static", "tests\BenchmarkDotNet.IntegrationTests.Static\BenchmarkDotNet.IntegrationTests.Static.csproj", "{B4405781-40D3-42B8-B168-00E711FABA15}"
4345
EndProject
@@ -107,6 +109,10 @@ Global
107109
{9816D316-95C4-42E6-9E7B-A256C7E5D4BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
108110
{9816D316-95C4-42E6-9E7B-A256C7E5D4BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
109111
{9816D316-95C4-42E6-9E7B-A256C7E5D4BF}.Release|Any CPU.Build.0 = Release|Any CPU
112+
{1336CAFF-5321-495E-BAFA-AB4DC6BF791C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
113+
{1336CAFF-5321-495E-BAFA-AB4DC6BF791C}.Debug|Any CPU.Build.0 = Debug|Any CPU
114+
{1336CAFF-5321-495E-BAFA-AB4DC6BF791C}.Release|Any CPU.ActiveCfg = Release|Any CPU
115+
{1336CAFF-5321-495E-BAFA-AB4DC6BF791C}.Release|Any CPU.Build.0 = Release|Any CPU
110116
{B4405781-40D3-42B8-B168-00E711FABA15}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111117
{B4405781-40D3-42B8-B168-00E711FABA15}.Debug|Any CPU.Build.0 = Debug|Any CPU
112118
{B4405781-40D3-42B8-B168-00E711FABA15}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -131,6 +137,7 @@ Global
131137
{E5A0833C-B633-4D62-B645-A927CEBFEEBB} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
132138
{D189AAB3-46B4-4437-8E9C-72F021AB2B6E} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
133139
{9816D316-95C4-42E6-9E7B-A256C7E5D4BF} = {14195214-591A-45B7-851A-19D3BA2413F9}
140+
{1336CAFF-5321-495E-BAFA-AB4DC6BF791C} = {D6597E3A-6892-4A68-8E14-042FC941FDA2}
134141
{B4405781-40D3-42B8-B168-00E711FABA15} = {14195214-591A-45B7-851A-19D3BA2413F9}
135142
EndGlobalSection
136143
GlobalSection(ExtensibilityGlobals) = postSolution
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
---
2+
uid: docs.dotnet-benchmarkdotnet
3+
name: The global BenchmarkDotNet tool
4+
---
5+
6+
# BenchmarkDotNet as global dotnet tool
7+
8+
BenchmarkDotNet is also available as a global dotnet tool and provides a convenient way to execute your benchmark(s) from the command line interface.
9+
10+
## How to install the tool
11+
12+
Download and install the [.NET Core 2.1 SDK](https://www.microsoft.com/net/download) or newer. Once installed, run the following command:
13+
14+
```log
15+
dotnet tool install BenchmarkDotNet.Tool -g
16+
```
17+
18+
If you already have a previous version of installed, you can upgrade to the latest version using the following command:
19+
20+
```log
21+
dotnet tool update BenchmarkDotNet.Tool -g
22+
```
23+
24+
## Usage
25+
26+
The basic usage syntax is:
27+
28+
```log
29+
dotnet benchmarkdotnet [arguments] [options]
30+
```
31+
32+
### Arguments
33+
34+
* The **first** argument in `[arguments]` **must** be the path to an assembly file with your benchmarks.
35+
* Further arguments are passed to the the `BenchmarkSwitcher`.
36+
37+
### Options
38+
39+
| Option | Description |
40+
| ------ | ----------- |
41+
|--version|Show version information|
42+
|-?, -h or --help|Show help information|
43+
44+
```log
45+
dotnet benchmarkdotnet -?
46+
```
47+
48+
**Note**: This shows also all valid arguments for `BenchmarkSwitcher`.
49+
50+
## Examples
51+
52+
The following example scans the `MyAssemblyWithBenchmarks.dll` for benchmarks and lets you select which benchmark(s) to execute:
53+
54+
```log
55+
dotnet benchmarkdotnet MyAssemblyWithBenchmarks.dll
56+
```
57+
58+
To execute all benchmarks use `--filter *`:
59+
60+
```log
61+
dotnet benchmarkdotnet MyAssemblyWithBenchmarks.dll --filter *
62+
```
63+
64+
**Note**: For further arguments for the `BenchmarkSwticher` see also [Console Arguments](console-args.md).

docs/articles/guides/how-to-run.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ var summary = BenchmarkRunner.Run<MyBenchmarkClass>();
99
var summary = BenchmarkRunner.Run(typeof(MyBenchmarkClass));
1010
```
1111

12+
## Global dotnet tool
13+
14+
```log
15+
dotnet tool install BenchmarkDotNet.Tool -g
16+
```
17+
18+
```log
19+
dotnet benchmarkdotnet MyAssemblyWithBenchmarks.dll --filter *
20+
```
21+
1222
## Url
1323

1424
You can also run a benchmark directly from the internet:

docs/articles/guides/nuget.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ We have the following set of NuGet packages (you can install it directly from `n
1111

1212
* `BenchmarkDotNet`: Basic BenchmarkDotNet infrastructure and logic. This is all you need to run benchmarks.
1313
* `BenchmarkDotNet.Diagnostics.Windows`: an additional optional package that provides a set of Windows diagnosers.
14+
* `BenchmarkDotNet.Tool`: A dotnet tool to execute benchmarks built with BenchmarkDotNet.
1415

1516
## Versioning system and feeds
1617
We have 3 kinds of versions: *stable*, *nightly*, and *develop*.

docs/articles/guides/toc.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
- name: Console Arguments
1616
href: console-args.md
1717
- name: Troubleshooting
18-
href: troubleshooting.md
18+
href: troubleshooting.md
19+
- name: BenchmarkDotNet as global dotnet tool
20+
href: global-dotnet-tool.md
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="..\..\build\common.props" />
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
<IsPackable>true</IsPackable>
7+
<PackAsTool>true</PackAsTool>
8+
<ToolCommandName>dotnet-BenchmarkDotNet</ToolCommandName>
9+
<Description>A dotnet tool to execute BenchmarkDotNet benchmarks.</Description>
10+
<PackageTags>benchmark;benchmarking;performance;tool</PackageTags>
11+
<PackageId>BenchmarkDotNetTool</PackageId>
12+
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
13+
</PropertyGroup>
14+
<ItemGroup>
15+
<ProjectReference Include="..\BenchmarkDotNet\BenchmarkDotNet.csproj" />
16+
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.3.0" />
17+
</ItemGroup>
18+
</Project>

src/BenchmarkDotNet.Tool/Program.cs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using BenchmarkDotNet.ConsoleArguments;
2+
using BenchmarkDotNet.Running;
3+
using CommandLine;
4+
using McMaster.Extensions.CommandLineUtils;
5+
using System;
6+
using System.ComponentModel.DataAnnotations;
7+
using System.IO;
8+
using System.Reflection;
9+
using System.Text;
10+
11+
namespace BenchmarkDotNet.Tool
12+
{
13+
[Command(
14+
Name = "BenchmarkDotNet",
15+
Description = "A dotnet tool to execute benchmarks built with BenchmarkDotNet.")]
16+
[HelpOption()]
17+
[VersionOptionFromMember(MemberName = nameof(GetVersion))]
18+
public sealed class Program
19+
{
20+
public static int Main(string[] args)
21+
{
22+
using (CommandLineApplication<Program> app = new CommandLineApplication<Program>())
23+
{
24+
app.Conventions.UseDefaultConventions();
25+
app.ThrowOnUnexpectedArgument = false;
26+
app.ExtendedHelpText = GenerateExtendedHelpText();
27+
return app.Execute(args);
28+
}
29+
}
30+
31+
private IConsole Console => PhysicalConsole.Singleton;
32+
33+
[Argument(0, Description = "The assembly with the benchmarks (required).")]
34+
[Required]
35+
[FileExists]
36+
public string AssemblyFile { get; set; }
37+
38+
public string[] RemainingArguments { get; set; }
39+
40+
public int OnExecute()
41+
{
42+
Assembly assembly;
43+
try
44+
{
45+
assembly = Assembly.LoadFrom(AssemblyFile);
46+
}
47+
catch (Exception ex)
48+
{
49+
Console.Error.WriteLine($"Couldn't load the assembly {AssemblyFile}.");
50+
Console.Error.WriteLine(ex.ToString());
51+
return 1;
52+
}
53+
54+
BenchmarkSwitcher benchmarkSwitcher = BenchmarkSwitcher.FromAssembly(assembly);
55+
benchmarkSwitcher.Run(RemainingArguments);
56+
return 0;
57+
}
58+
59+
private static string GetVersion()
60+
{
61+
return typeof(Program).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion;
62+
}
63+
64+
private static string GenerateExtendedHelpText()
65+
{
66+
StringBuilder sb = new StringBuilder()
67+
.AppendLine()
68+
.AppendLine("The first argument in [arguments] is the benchmark assembly and every following argument is passed to the BenchmarkSwitcher.")
69+
.AppendLine("BenchmarkSwitcher arguments:")
70+
.AppendLine();
71+
using (StringWriter sw = new StringWriter(sb))
72+
{
73+
using (Parser p = new Parser((ps) => { ps.HelpWriter = sw; }))
74+
{
75+
p.ParseArguments<CommandLineOptions>(new string[] { "--help" });
76+
}
77+
}
78+
return sb.ToString();
79+
}
80+
}
81+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"profiles": {
3+
"BenchmarkDotNet.Tool": {
4+
"commandName": "Project"
5+
}
6+
}
7+
}

0 commit comments

Comments
 (0)