From b7e919999c1f44337f7b3e894d06cc479b725170 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Thu, 21 Sep 2017 13:52:52 +0800 Subject: [PATCH] Add performance test project --- Src/Asp.Net/PerformanceTest/App.config | 6 ++ Src/Asp.Net/PerformanceTest/Dabase/test.sql | 54 ++++++++++++ .../PerformanceTest/Items/PerHelper.cs | 33 +++++++ Src/Asp.Net/PerformanceTest/Items/PubConst.cs | 12 +++ .../PerformanceTest/Items/SelectBigData.cs | 65 ++++++++++++++ Src/Asp.Net/PerformanceTest/Items/WarmUp.cs | 22 +++++ .../PerformanceTest/Models/TestEntity.cs | 26 ++++++ .../PerformanceTest/PerformanceTest.csproj | 87 +++++++++++++++++++ Src/Asp.Net/PerformanceTest/Program.cs | 25 ++++++ .../Properties/AssemblyInfo.cs | 36 ++++++++ Src/Asp.Net/PerformanceTest/packages.config | 6 ++ Src/Asp.Net/SqlSugar.sln | 6 ++ 12 files changed, 378 insertions(+) create mode 100644 Src/Asp.Net/PerformanceTest/App.config create mode 100644 Src/Asp.Net/PerformanceTest/Dabase/test.sql create mode 100644 Src/Asp.Net/PerformanceTest/Items/PerHelper.cs create mode 100644 Src/Asp.Net/PerformanceTest/Items/PubConst.cs create mode 100644 Src/Asp.Net/PerformanceTest/Items/SelectBigData.cs create mode 100644 Src/Asp.Net/PerformanceTest/Items/WarmUp.cs create mode 100644 Src/Asp.Net/PerformanceTest/Models/TestEntity.cs create mode 100644 Src/Asp.Net/PerformanceTest/PerformanceTest.csproj create mode 100644 Src/Asp.Net/PerformanceTest/Program.cs create mode 100644 Src/Asp.Net/PerformanceTest/Properties/AssemblyInfo.cs create mode 100644 Src/Asp.Net/PerformanceTest/packages.config diff --git a/Src/Asp.Net/PerformanceTest/App.config b/Src/Asp.Net/PerformanceTest/App.config new file mode 100644 index 000000000..88fa4027b --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Src/Asp.Net/PerformanceTest/Dabase/test.sql b/Src/Asp.Net/PerformanceTest/Dabase/test.sql new file mode 100644 index 000000000..dbc6c239b --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Dabase/test.sql @@ -0,0 +1,54 @@ +CREATE TABLE [dbo].[Test]( + [Id] [int] IDENTITY(1,1) NOT NULL, + [F_Byte] [tinyint] NULL, + [F_Int16] [smallint] NULL, + [F_Int32] [int] NULL, + [F_Int64] [bigint] NULL, + [F_Double] [float] NULL, + [F_Float] [real] NULL, + [F_Decimal] [decimal](18, 0) NULL, + [F_Bool] [bit] NULL, + [F_DateTime] [datetime] NULL, + [F_Guid] [uniqueidentifier] NULL, + [F_String] [nvarchar](100) NULL, + CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED +( + [Id] ASC +)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] +) ON [PRIMARY] + +GO + +declare @i int = 0; + +begin tran; +while(@i<=1000000) +begin +INSERT INTO [dbo].[Test] + ([F_Byte] + ,[F_Int16] + ,[F_Int32] + ,[F_Int64] + ,[F_Double] + ,[F_Float] + ,[F_Decimal] + ,[F_Bool] + ,[F_DateTime] + ,[F_Guid] + ,[F_String]) + VALUES + (1 + ,2 + ,@i + ,@i + ,@i + ,@i + ,@i + ,@i%2 + ,GETDATE() + ,NEWID() + ,'Chloe' + CAST(@i AS nvarchar(1000)) + ) +set @i=@i+1; +end +commit; \ No newline at end of file diff --git a/Src/Asp.Net/PerformanceTest/Items/PerHelper.cs b/Src/Asp.Net/PerformanceTest/Items/PerHelper.cs new file mode 100644 index 000000000..9db30d291 --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Items/PerHelper.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; + +namespace PerformanceTest.Items +{ + /// + /// 性能测试类,用于循环执行代码并统计时间 + /// + public class PerHelper + { + public static void Execute(int count, string title, Action fun) + { + SyntacticSugar.PerformanceTest ptef = new SyntacticSugar.PerformanceTest(); + ptef.SetCount(count);//执行count次 + ptef.Execute( + i => + { + fun(); + + }, + res => + { + Console.WriteLine(string.Format("执行{0}次,{1}{2}", count, title, res)); + }); + + } + + } +} diff --git a/Src/Asp.Net/PerformanceTest/Items/PubConst.cs b/Src/Asp.Net/PerformanceTest/Items/PubConst.cs new file mode 100644 index 000000000..8d4d6fe40 --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Items/PubConst.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace PerformanceTest.Items +{ + public class PubConst + { + public static string connectionString = "server=.;uid=sa;pwd=sasa;database=SqlSugarTest"; + } +} diff --git a/Src/Asp.Net/PerformanceTest/Items/SelectBigData.cs b/Src/Asp.Net/PerformanceTest/Items/SelectBigData.cs new file mode 100644 index 000000000..0ef65ccad --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Items/SelectBigData.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Data.SqlClient; +using Dapper; +using SqlSugar; +using Dapper.Contrib.Extensions; + +namespace PerformanceTest.Items +{ + public class SelectBigData + { + /// + /// 测试一次读取100万条数据的速度 + /// + public void Init() + { + Console.WriteLine("测试一次读取100万条数据的速度"); + var eachCount = 1000; + + /*******************车轮战是性能评估最准确的一种方式***********************/ + for (int i = 0; i < 10; i++) + { + + //dapper + Dapper(eachCount); + + //sqlSugar + SqlSugar(eachCount); + } + + } + + private static void SqlSugar(int eachCount) + { + GC.Collect();//回收资源 + System.Threading.Thread.Sleep(1);//休息2秒 + + PerHelper.Execute(eachCount, "SqlSugar", () => + { + using (SqlSugarClient conn = new SqlSugarClient(new ConnectionConfig() { InitKeyType=InitKeyType.SystemTable, ConnectionString= PubConst.connectionString, DbType=DbType.SqlServer })) + { + // var list = conn.Ado.SqlQuery("select * from test where id=1"); + var list2 = conn.Queryable().InSingle(1); + } + }); + } + + private static void Dapper(int eachCount) + { + GC.Collect();//回收资源 + System.Threading.Thread.Sleep(1);//休息2秒 + + //正试比拼 + PerHelper.Execute(eachCount, "Dapper", () => + { + using (SqlConnection conn = new SqlConnection(PubConst.connectionString)) + { + var list = conn.Get(1); + } + }); + } + } +} diff --git a/Src/Asp.Net/PerformanceTest/Items/WarmUp.cs b/Src/Asp.Net/PerformanceTest/Items/WarmUp.cs new file mode 100644 index 000000000..c8b46ab7f --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Items/WarmUp.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Data.SqlClient; +using SqlSugar; +using Dapper; +using Dapper.Contrib; +using Dapper.Contrib.Extensions; +namespace PerformanceTest.Items +{ + public class WarmUp + { + public WarmUp() + { + Console.WriteLine("开启预热"); + + Console.WriteLine("预热完毕"); + Console.WriteLine("----------------比赛开始-------------------"); + } + } +} diff --git a/Src/Asp.Net/PerformanceTest/Models/TestEntity.cs b/Src/Asp.Net/PerformanceTest/Models/TestEntity.cs new file mode 100644 index 000000000..936e2b82b --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Models/TestEntity.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PerformanceTest +{ + [Dapper.Contrib.Extensions.Table("Test")] + public class Test + { + [Dapper.Contrib.Extensions.Key] + public int Id { get; set; } + public byte? F_Byte { get; set; } + public Int16? F_Int16 { get; set; } + public int? F_Int32 { get; set; } + public long? F_Int64 { get; set; } + public double? F_Double { get; set; } + public float? F_Float { get; set; } + public decimal? F_Decimal { get; set; } + public bool? F_Bool { get; set; } + public DateTime? F_DateTime { get; set; } + public Guid? F_Guid { get; set; } + public string F_String { get; set; } + } +} diff --git a/Src/Asp.Net/PerformanceTest/PerformanceTest.csproj b/Src/Asp.Net/PerformanceTest/PerformanceTest.csproj new file mode 100644 index 000000000..0251158ae --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/PerformanceTest.csproj @@ -0,0 +1,87 @@ + + + + + Debug + AnyCPU + {60D6AA62-93ED-4D02-80E4-6BEB81766D3E} + Exe + Properties + PerformanceTest + PerformanceTest + v4.5.2 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Dapper.1.50.4-alpha1-00070\lib\net451\Dapper.dll + True + + + ..\packages\Dapper.Contrib.1.50.0\lib\net45\Dapper.Contrib.dll + True + + + ..\SqliteTest\OtherDll\SyntacticSugar.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {489BB790-226C-4FAD-8D1E-51D72A7FF8E5} + SqlSugar + + + + + \ No newline at end of file diff --git a/Src/Asp.Net/PerformanceTest/Program.cs b/Src/Asp.Net/PerformanceTest/Program.cs new file mode 100644 index 000000000..a41733632 --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using PerformanceTest.Items; + +namespace PerformanceTest +{ + class Program + { + + /// + /// SqlSugar与Dapper的性能比较 + /// + /// + static void Main(string[] args) + { + WarmUp wu = new WarmUp();//预热处理 + + new SelectBigData().Init(); + + Console.ReadKey(); + } + } +} diff --git a/Src/Asp.Net/PerformanceTest/Properties/AssemblyInfo.cs b/Src/Asp.Net/PerformanceTest/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..a61cd9ce6 --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("PerformanceTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PerformanceTest")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +//将 ComVisible 设置为 false 将使此程序集中的类型 +//对 COM 组件不可见。 如果需要从 COM 访问此程序集中的类型, +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("60d6aa62-93ed-4d02-80e4-6beb81766d3e")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值, +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/Src/Asp.Net/PerformanceTest/packages.config b/Src/Asp.Net/PerformanceTest/packages.config new file mode 100644 index 000000000..400e96a06 --- /dev/null +++ b/Src/Asp.Net/PerformanceTest/packages.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Src/Asp.Net/SqlSugar.sln b/Src/Asp.Net/SqlSugar.sln index e68eff82b..05b69427a 100644 --- a/Src/Asp.Net/SqlSugar.sln +++ b/Src/Asp.Net/SqlSugar.sln @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteTest", "SqliteTest\Sq EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OracleTest", "OracleTest\OracleTest.csproj", "{4177D054-D113-4F8A-9E01-642E072F9C87}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceTest", "PerformanceTest\PerformanceTest.csproj", "{60D6AA62-93ED-4D02-80E4-6BEB81766D3E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +41,10 @@ Global {4177D054-D113-4F8A-9E01-642E072F9C87}.Debug|Any CPU.Build.0 = Debug|Any CPU {4177D054-D113-4F8A-9E01-642E072F9C87}.Release|Any CPU.ActiveCfg = Release|Any CPU {4177D054-D113-4F8A-9E01-642E072F9C87}.Release|Any CPU.Build.0 = Release|Any CPU + {60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60D6AA62-93ED-4D02-80E4-6BEB81766D3E}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE