1

I neet to compare two implementations of the same functionality but done for two frameworks: .net 4.6.2 and .net 7

How do I define this benchmark?

Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
  • 1
    I'm probably wrong, but I think you should run two separate benchmarks... – Marco Jun 09 '23 at 12:36
  • @Marco yes, feels like that. I'm curious how I would match methods if there were many for each benchmark. – Pavel Voronin Jun 09 '23 at 12:39
  • Well, each benchmark can report a lot of _"functions"_ to be checked... if you call these functions the same way between the two benchmarks, I'd say it will be easy to match them. I beg your pardon, I'm just guessing... – Marco Jun 09 '23 at 12:43
  • don't quite understand your question, isn't that basically the default behaviour - you have two methods with the implementations you want to test - and just declare the attributes for the frameworks as needed eg. `[SimpleJob(RuntimeMoniker.Net472)] [SimpleJob(RuntimeMoniker.NetCoreApp30)]` ?!? - so basically the very first example on https://github.com/dotnet/BenchmarkDotNet - two methods and 4 frameworks – Rand Random Jun 09 '23 at 12:48
  • @RandRandom, well, while this would work, it will yeild 4 result 2 of which are irrelevant. I want each benchmark method run against one target framework. – Pavel Voronin Jun 09 '23 at 12:53
  • okay, than simply declare two classes, each with the desired attribute eg. as described here: https://stackoverflow.com/questions/70274948 – Rand Random Jun 09 '23 at 13:09

1 Answers1

3

I have created 2 library projects, one in .net 4.6.2 and one in .net 7 with the same class and function.

namespace ClassLibrary1
{
    public class Class1
    {
        public int CalcInt32(int OperationsPerInvoke)
        {
            int firstDigit = 135;
            int secondDigit = 145;

            for (int i = 0; i < OperationsPerInvoke; i++)
            {
                firstDigit += secondDigit;
            }
            return firstDigit;
        }
    }
}

After that I've created a new test project .net 7 that will do the tests by invoking the method from each assembly:

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Reflection;

BenchmarkRunner.Run<PerformanceTest>();
public class PerformanceTest
{
    const int OperationsPerInvoke = 4096;

    [Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
    public int TestClassLibrary7()
    {
        var myAssembly = Assembly.LoadFile("d:\\Teste\\.NetGeneral\\_Dlls\\ClassLibrary1\\bin\\Debug\\net7.0\\ClassLibrary1.dll");
        var myType = myAssembly.GetType("ClassLibrary1");
        if (myType == null)
            return 0;
        MethodInfo dllGetMethod = myType.GetMethod("CalcInt32", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

        object res = dllGetMethod?.Invoke(null, new object[] { OperationsPerInvoke });
        return (int)res;
    }


    [Benchmark(OperationsPerInvoke = OperationsPerInvoke)]
    public int TestClassLibrary462()
    {
        var myAssembly = Assembly.LoadFile("d:\\Teste\\.NetGeneral\\_Dlls\\ClassLibrary2\\bin\\Debug\\ClassLibrary2.dll");
        var myType = myAssembly.GetType("ClassLibrary1");
        if (myType == null)
            return 0;
        MethodInfo dllGetMethod = myType.GetMethod("CalcInt32", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);

        object res = dllGetMethod?.Invoke(null, new object[] { OperationsPerInvoke });
        return (int)res;
    }
}

and it's working. Here are some results for my test:

Method Mean Error StdDev
TestClassLibrary7 0.1374 ns 0.0017 ns 0.0015 ns
TestClassLibrary462 0.1308 ns 0.0019 ns 0.0017 ns
D A
  • 1,724
  • 1
  • 8
  • 19
  • I am surprised you could load .net 462 assembly from .net 7 – Pavel Voronin Jun 09 '23 at 15:38
  • This compiles and works, nothing to say. But honestly If I have to create two libraries and call them from another project, then why not using Benchmark within the two projects already existing (one for each framework)? I understand that in this way you have a single result easily comparable, but I'm not sure the result is correct. You're executing the code within both libraries in a different framework and in a different context, so I won't bet on such benchmarks. Naturally this is just my opinion and I perfectly know I could be wrong... – Marco Jun 09 '23 at 20:29