BenchmarkDotNet icon indicating copy to clipboard operation
BenchmarkDotNet copied to clipboard

Add new template for "dotnet new benchmark"

Open adamsitnik opened this issue 7 years ago • 19 comments

I think that we should add a new template for dotnet cli that would create following app when used:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

namespace BenchmarkApp
{
    class Program
    {
        static void Main(string[] args) => BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
    }

    public class TypeWithBenchmarks
    {
        [Benchmark]
        public int YourBenchmark()
        {
            // implement your benchmark here
            return 0;
        }
    }
}

@AndreyAkinshin what do you think?

adamsitnik avatar Jan 28 '19 08:01 adamsitnik

@adamsitnik I like this idea!

AndreyAkinshin avatar Jan 28 '19 09:01 AndreyAkinshin

btw the .csproj should be:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>$whatUserHasAskedFor</TargetFramework>
    <OutputType>Exe</OutputType>
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <DebugSymbols>true</DebugSymbols>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    <Optimize>true</Optimize>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="BenchmarkDotNet" Version="0.1*" />
    <PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.1*" />
  </ItemGroup>

</Project>

adamsitnik avatar Jan 29 '19 18:01 adamsitnik

I would like to do this :)

CodeTherapist avatar Feb 01 '19 12:02 CodeTherapist

@CodeTherapist awesome! please let me know if you need anything

adamsitnik avatar Feb 01 '19 18:02 adamsitnik

Would it be possible to have template for F# and with more code (i.e. template with examples) like next https://github.com/TheAngryByrd/BenchmarkDotNet.Template/issues/3 ?

dzmitry-lahoda avatar Feb 10 '19 07:02 dzmitry-lahoda

I'd be happy to just port the code I have in that repo to something under here as it will get more love and attention.

TheAngryByrd avatar Mar 14 '19 18:03 TheAngryByrd

I've written a template or two before and would love to help.

Before I get started could you provide some basic information?

  1. Where would you prefer the template content to be stored within the repository? Alternatively would a separate repository be better and ownership transferred.
  2. Templating supports file system or Nuget installation. File system is a low bar of entry but Nuget is ultimately more flexible and easier for consumers albeit requires CI/CD to build the package. What's your preference?

JamieKeeling avatar May 03 '19 15:05 JamieKeeling

How can I be of help here?

Kavignon avatar Aug 22 '19 22:08 Kavignon

@Kavignon we already have a pull request by @CodeTherapist with basic template support: https://github.com/dotnet/BenchmarkDotNet/pull/1044 However, I raised some concerns about the current PR status: https://github.com/dotnet/BenchmarkDotNet/pull/1044#pullrequestreview-261539007 . I think it will be better to continue discussion there and finish the PR. @CodeTherapist do you need any help with the PR?

AndreyAkinshin avatar Aug 30 '19 11:08 AndreyAkinshin

@AndreyAkinshin No help needed, thanks - I will finish it this week(end).

CodeTherapist avatar Aug 30 '19 13:08 CodeTherapist

@CodeTherapist awesome, thanks!

AndreyAkinshin avatar Aug 30 '19 13:08 AndreyAkinshin

@AndreyAkinshin, when would the official template package made available on nuget.org?

e.g.

  • https://www.nuget.org/packages/NUnit3.DotNetNew.Template/
  • https://www.nuget.org/packages/giraffe-template/

and unofficial packages of BenchmarkDotNet:

  • https://www.nuget.org/packages/AtleX.BenchmarkDotNet.Template.CSharp/
  • https://www.nuget.org/packages/TheAngryByrd.BenchmarkDotNet.Templates/

am11 avatar Oct 11 '19 16:10 am11

@am11 official template was published a few days ago: https://www.nuget.org/packages/BenchmarkDotNet.Templates/

AndreyAkinshin avatar Oct 28 '19 17:10 AndreyAkinshin

I just marked my github repo as archived and my nuget package as deprecated.

TheAngryByrd avatar Oct 28 '19 17:10 TheAngryByrd

@TheAngryByrd great, thanks!

AndreyAkinshin avatar Oct 28 '19 17:10 AndreyAkinshin

Thank you @AndreyAkinshin! 🎉

[maybe not a huge deal but..] IMHO, the template should be in a runnable state with these additions:

Click to expand
--- a/Benchmarks.cs
+++ b/Benchmarks.cs
using System;
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
+using BenchmarkDotNet.Running;
 
 namespace Sample
 {
+    class Program
+    {
+        static void Main() => BenchmarkRunner.Run<Benchmarks>();
+    }
+
     public class Benchmarks
     {
         [Benchmark]

--- a/Sample.csproj
+++ b/Sample.csproj
 <Project Sdk="Microsoft.NET.Sdk">
   <PropertyGroup>
-    <TargetFrameworks>netstandard2.0</TargetFrameworks>
+    <TargetFramework>netcoreapp3.0</TargetFramework>
+    <OutputType>Exe</OutputType>
   </PropertyGroup>
   <PropertyGroup>
     <PlatformTarget>AnyCPU</PlatformTarget>

So the getting started steps would look like:

dotnet new benchmark --name Sample
cd Sample
dotnet run

am11 avatar Oct 28 '19 18:10 am11

there's a CLI flag for that with the template --console-app

dotnet new benchmark --console-app

I know I missed it the first time. I could argue for it being the default too :)

TheAngryByrd avatar Oct 28 '19 18:10 TheAngryByrd

Thanks @am11 and @TheAngryByrd for your feedback and question. I'm happy to help. The template has this "runnale state" as an option when you create the new project (docs):

dotnet new benchmark --name Sample --console-app
cd Sample
dotnet run

Alternatively, you could install and use the the global BenchmarkDotNet tool and invoke the benchmarks from the CLI (docs).

BenchmarkDotnet will strengthen it's integration with leading IDE's in the future to provide better user experience and workflow - similiar to unit tests. Neverless, any feedback is appreciated and will be considered for further releases.

CodeTherapist avatar Oct 28 '19 19:10 CodeTherapist

Great! Thanks for your help and documentation. 👍

am11 avatar Oct 28 '19 19:10 am11