Skip to content

Strange performance degradation in .net core 3.1 #32973

Closed
@B-Esmaili

Description

@B-Esmaili

I was benchmarking AutoMapper in .net core 3.1 and found out a strange performance degradation in .net core 3.1 with respect to .net core 2.1 here is the screenshot from my benchmark :

.NET CORE 3.1
image

.NET CORE 2.0
netcore21

and following is code for my benchmark if anyone interested :

using AutoMapper;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApp5
{


    public class Model1
    {
        public int number { get; set; }

        public double dlb;

        public DateTime dateTime;

        public string str;
    }

    public class Model2
    {
        public int number { get; set; }

        public double dlb;

        public DateTime dateTime;

        public string str;
    }
    [MemoryDiagnoser]
    public class Test
    {
        [Benchmark]
        public void AutoMap()
        {
            var configuration = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap<Model1, Model2>();
            });

            Random rnd = new Random();
            Model1 m1 = new Model1();
            List<Model2> m2List = new List<Model2>();
            var mapper = configuration.CreateMapper();

            for (int i = 0; i < 1000000; i++)
            {
                m1.number = rnd.Next(0, 5000);
                m1.dlb = rnd.NextDouble() * 50000;
                m1.dateTime = DateTime.Now;
                m1.str = "SOME TEXT TO RUN BENCHMARK";

                Model2 m2 = new Model2();
                mapper.Map<Model1, Model2>(m1, m2);
                m2List.Add(m2);
            }
        }
        [Benchmark]
        public void MapByHand()
        {
            Random rnd = new Random();
            Model1 m1 = new Model1();
            List<Model2> m2List = new List<Model2>();

            for (int i = 0; i < 1000000; i++)
            {
                m1.number = rnd.Next(0, 5000);
                m1.dlb = rnd.NextDouble() * 50000;
                m1.dateTime = DateTime.Now;
                m1.str = "SOME TEXT TO RUN BENCHMARK";

                Model2 m2 = new Model2();
                m2.dateTime = m1.dateTime;
                m2.dlb = m1.dlb;
                m2.number = m1.number;
                m2.str = m1.str;

                m2List.Add(m2);
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            BenchmarkRunner.Run<Test>();                       
        }
    }
}

not only benchmark result is better is .net core 2.1 but the benchmark itself toke less time to run with respect to .net core 3.1. could anyone from .net team describe about the situation?
thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    tenet-performancePerformance related issueuntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions