ソースを参照

ExpressionMapper跑分测试

懒得勤快 6 年 前
コミット
511a8eafd6

+ 16 - 0
Masuit.Tools.ExpressionMapperBenchmark/Masuit.Tools.ExpressionMapperBenchmark.csproj

@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.2</TargetFramework>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="AutoMapper" Version="8.0.0" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\Masuit.Tools.Core\Masuit.Tools.Core.csproj" />
+  </ItemGroup>
+
+</Project>

+ 205 - 0
Masuit.Tools.ExpressionMapperBenchmark/Program.cs

@@ -0,0 +1,205 @@
+using AutoMapper;
+using Masuit.Tools.Mapping;
+using Masuit.Tools.Systems;
+using System;
+using System.Collections.Generic;
+
+namespace Masuit.Tools.ExpressionMapperBenchmark
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            #region 配置automapper
+
+            Mapper.Initialize(e =>
+            {
+                e.CreateMap<TestClassA, TestClassB>().ReverseMap();
+                e.CreateMap<TestClassC, TestClassD>().ReverseMap();
+            });
+
+            #endregion
+
+            #region 配置ExpressionMapper
+
+            ExpressionMapper.CreateMap<TestClassA, TestClassB>().ReverseMap();
+            ExpressionMapper.CreateMap<TestClassC, TestClassD>().ReverseMap();
+
+            #endregion
+
+            #region 造一个大对象
+
+            var a = new TestClassA()
+            {
+                MyProperty = "ssssssssssssssssssssss",
+                DateTime = DateTime.Now,
+                Double = 123.33,
+                Int = 100,
+                TestClassC = new TestClassC()
+                {
+                    MyProperty = "ccccccccccccccccccccccccccc",
+                    DateTime = DateTime.Now,
+                    Double = 2345.555,
+                    Int = 10100,
+                    Obj = new TestClassD()
+                    {
+                        MyProperty = "ddddddddddddddddddddddddd",
+                        Obj = new TestClassC()
+                        {
+                            MyProperty = "cccccc",
+                            DateTime = DateTime.Now,
+                            Double = 23458894.555,
+                            Int = 10100000,
+                            Obj = new TestClassD()
+                        }
+                    }
+                },
+                List = new List<TestClassC>()
+                {
+                    new TestClassC()
+                    {
+                        MyProperty = "cccccc",
+                        DateTime = DateTime.Now,
+                        Double = 2345.555,
+                        Int = 10100,
+                        Obj = new TestClassD()
+                        {
+                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
+                            DateTime = DateTime.Now,
+                            Double = 2345.555,
+                            Int = 10100,
+                            Obj = new TestClassC()
+                            {
+                                MyProperty = "cccccccccccccccccccccccccccccc",
+                                DateTime = DateTime.Now,
+                                Double = 2345.555,
+                                Int = 10100,
+                                Obj = new TestClassD()
+                            }
+                        }
+                    },
+                    new TestClassC()
+                    {
+                        MyProperty = "cccccc",
+                        DateTime = DateTime.Now,
+                        Double = 2345.555,
+                        Int = 10100,
+                        Obj = new TestClassD()
+                        {
+                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
+                            DateTime = DateTime.Now,
+                            Double = 2345.555,
+                            Int = 10100,
+                            Obj = new TestClassC()
+                            {
+                                MyProperty = "cccccccccccccccccccccccccccccc",
+                                DateTime = DateTime.Now,
+                                Double = 2345.555,
+                                Int = 10100,
+                                Obj = new TestClassD()
+                            }
+                        }
+                    },
+                    new TestClassC()
+                    {
+                        MyProperty = "cccccc",
+                        DateTime = DateTime.Now,
+                        Double = 2345.555,
+                        Int = 10100,
+                        Obj = new TestClassD()
+                        {
+                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
+                            DateTime = DateTime.Now,
+                            Double = 2345.555,
+                            Int = 10100,
+                            Obj = new TestClassC()
+                            {
+                                MyProperty = "cccccccccccccccccccccccccccccc",
+                                DateTime = DateTime.Now,
+                                Double = 2345.555,
+                                Int = 10100,
+                                Obj = new TestClassD()
+                            }
+                        }
+                    },
+                    new TestClassC()
+                    {
+                        MyProperty = "cccccc",
+                        DateTime = DateTime.Now,
+                        Double = 2345.555,
+                        Int = 10100,
+                        Obj = new TestClassD()
+                        {
+                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
+                            DateTime = DateTime.Now,
+                            Double = 2345.555,
+                            Int = 10100,
+                            Obj = new TestClassC()
+                            {
+                                MyProperty = "cccccccccccccccccccccccccccccc",
+                                DateTime = DateTime.Now,
+                                Double = 2345.555,
+                                Int = 10100,
+                                Obj = new TestClassD()
+                            }
+                        }
+                    },
+                    new TestClassC()
+                    {
+                        MyProperty = "cccccc",
+                        DateTime = DateTime.Now,
+                        Double = 2345.555,
+                        Int = 10100,
+                        Obj = new TestClassD()
+                        {
+                            MyProperty = "ddddddddddddddddddddddddddddddddddd",
+                            DateTime = DateTime.Now,
+                            Double = 2345.555,
+                            Int = 10100,
+                            Obj = new TestClassC()
+                            {
+                                MyProperty = "cccccccccccccccccccccccccccccc",
+                                DateTime = DateTime.Now,
+                                Double = 2345.555,
+                                Int = 10100,
+                                Obj = new TestClassD()
+                            }
+                        }
+                    },
+                }
+            };
+
+            #endregion
+
+            var time = HiPerfTimer.Execute(() =>
+            {
+                a.Map<TestClassA, TestClassB>();
+                a.Map<TestClassA, TestClassB>();
+            });
+            Console.WriteLine($"ExpressionMapper映射2次耗时:{time}s");
+            time = HiPerfTimer.Execute(() =>
+            {
+                for (int i = 0; i < 100000; i++)
+                {
+                    var b = a.Map<TestClassA, TestClassB>();
+                }
+            });
+            Console.WriteLine($"ExpressionMapper映射100000次耗时:{time}s");
+
+            time = HiPerfTimer.Execute(() =>
+            {
+                Mapper.Map<TestClassB>(a);
+                Mapper.Map<TestClassB>(a);
+            });
+            Console.WriteLine($"AutoMapper映射2次耗时:{time}s");
+            time = HiPerfTimer.Execute(() =>
+            {
+                for (int i = 0; i < 100000; i++)
+                {
+                    var b = Mapper.Map<TestClassB>(a);
+                }
+            });
+            Console.WriteLine($"AutoMapper映射100000次耗时:{time}s");
+        }
+    }
+}

+ 18 - 4
Masuit.Tools.UnitTest/TestClasses/TestClassA.cs → Masuit.Tools.ExpressionMapperBenchmark/TestClassA.cs

@@ -1,34 +1,48 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 
-namespace Masuit.Tools.UnitTest.TestClasses
+namespace Masuit.Tools.ExpressionMapperBenchmark
 {
     public class TestClassA
     {
         public string MyProperty { get; set; }
+        public int Int { get; set; }
+        public double Double { get; set; }
+        public DateTime DateTime { get; set; }
 
         public TestClassC TestClassC { get; set; }
         public List<TestClassC> List { get; set; }
-        public TestClassC[] Array { get; set; }
     }
 
     public class TestClassB
     {
         public string MyProperty { get; set; }
+        public int Int { get; set; }
+        public double Double { get; set; }
+        public DateTime DateTime { get; set; }
+
 
         public TestClassC TestClassC { get; set; }
         public List<TestClassD> List { get; set; }
-        public TestClassD[] Array { get; set; }
     }
 
     public class TestClassC
     {
         public string MyProperty { get; set; }
+        public int Int { get; set; }
+        public double Double { get; set; }
+        public DateTime DateTime { get; set; }
+
         public TestClassD Obj { get; set; }
     }
 
     public class TestClassD
     {
         public string MyProperty { get; set; }
+        public int Int { get; set; }
+        public double Double { get; set; }
+        public DateTime DateTime { get; set; }
+
         public TestClassC Obj { get; set; }
     }
 }

+ 0 - 1
Masuit.Tools.UnitTest/Masuit.Tools.UnitTest.csproj

@@ -136,7 +136,6 @@
     <Compile Include="NumberFormaterTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="TemplateTest.cs" />
-    <Compile Include="TestClasses\TestClassA.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />

+ 8 - 1
Masuit.Tools.sln

@@ -25,7 +25,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test.csproj",
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCoreTest", "NetCoreTest\NetCoreTest.csproj", "{ABEC3F65-6556-44FD-BECE-33F7927FB3AE}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.AspNetCore.ResumeFileResults.WebTest", "Masuit.Tools.AspNetCore.ResumeFileResults.WebTest\Masuit.Tools.AspNetCore.ResumeFileResults.WebTest.csproj", "{49E16577-01CD-4104-B73E-449B2960D956}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masuit.Tools.AspNetCore.ResumeFileResults.WebTest", "Masuit.Tools.AspNetCore.ResumeFileResults.WebTest\Masuit.Tools.AspNetCore.ResumeFileResults.WebTest.csproj", "{49E16577-01CD-4104-B73E-449B2960D956}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Masuit.Tools.ExpressionMapperBenchmark", "Masuit.Tools.ExpressionMapperBenchmark\Masuit.Tools.ExpressionMapperBenchmark.csproj", "{1D51DC49-6626-41AF-B1B5-085C83307FF2}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -77,6 +79,10 @@ Global
 		{49E16577-01CD-4104-B73E-449B2960D956}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{49E16577-01CD-4104-B73E-449B2960D956}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{49E16577-01CD-4104-B73E-449B2960D956}.Release|Any CPU.Build.0 = Release|Any CPU
+		{1D51DC49-6626-41AF-B1B5-085C83307FF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{1D51DC49-6626-41AF-B1B5-085C83307FF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{1D51DC49-6626-41AF-B1B5-085C83307FF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{1D51DC49-6626-41AF-B1B5-085C83307FF2}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
@@ -87,6 +93,7 @@ Global
 		{6CA28B75-2F2D-401A-BDF7-EE314C14C95F} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 		{9D2E3A00-9E94-4C3D-AAAE-70B0DA236FE6} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 		{49E16577-01CD-4104-B73E-449B2960D956} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
+		{1D51DC49-6626-41AF-B1B5-085C83307FF2} = {E0B8FBD1-A28A-4420-9DE2-6BD06035CBAC}
 	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {B57FDA8F-95CF-478B-A0A8-7FF0F01CCFAB}