From 816f3196f9c8b5729daf9e644b9fabafbf3fc361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 11 Dec 2024 09:42:32 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=95=B4=E7=90=86Samples=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=8E=8B=E6=B5=8B=E9=A1=B9=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Redis.sln | 21 ++++++++---- NewLife.Redis/NewLife.Redis.csproj | 2 +- Samples/Benchmark/Benchmark.csproj | 34 +++++++++++++++++++ Samples/Benchmark/Program.cs | 26 ++++++++++++++ Samples/Benchmark/appsettings.json | 22 ++++++++++++ {QueueDemo => Samples/QueueDemo}/AckQueue.cs | 0 {QueueDemo => Samples/QueueDemo}/Area.cs | 0 .../QueueDemo}/DelayQueue.cs | 0 {QueueDemo => Samples/QueueDemo}/EasyQueue.cs | 0 {QueueDemo => Samples/QueueDemo}/FullQueue.cs | 0 .../QueueDemo}/MemoryQueue.cs | 0 .../QueueDemo}/MultipleConsumer.cs | 0 {QueueDemo => Samples/QueueDemo}/Program.cs | 0 .../QueueDemo}/QueueDemo.csproj | 4 +-- Test/Test.csproj | 2 +- XUnitTest/XUnitTest.csproj | 2 +- 16 files changed, 101 insertions(+), 12 deletions(-) create mode 100644 Samples/Benchmark/Benchmark.csproj create mode 100644 Samples/Benchmark/Program.cs create mode 100644 Samples/Benchmark/appsettings.json rename {QueueDemo => Samples/QueueDemo}/AckQueue.cs (100%) rename {QueueDemo => Samples/QueueDemo}/Area.cs (100%) rename {QueueDemo => Samples/QueueDemo}/DelayQueue.cs (100%) rename {QueueDemo => Samples/QueueDemo}/EasyQueue.cs (100%) rename {QueueDemo => Samples/QueueDemo}/FullQueue.cs (100%) rename {QueueDemo => Samples/QueueDemo}/MemoryQueue.cs (100%) rename {QueueDemo => Samples/QueueDemo}/MultipleConsumer.cs (100%) rename {QueueDemo => Samples/QueueDemo}/Program.cs (100%) rename {QueueDemo => Samples/QueueDemo}/QueueDemo.csproj (78%) diff --git a/NewLife.Redis.sln b/NewLife.Redis.sln index b232a23..c0251ba 100644 --- a/NewLife.Redis.sln +++ b/NewLife.Redis.sln @@ -20,10 +20,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Samples", "Samples", "{E136AE06-927B-4D10-BB83-B607ED0B1FD4}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QueueDemo", "QueueDemo\QueueDemo.csproj", "{5829188A-DC4A-4F00-AD95-5873E4057BDE}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NewLife.Redis.Extensions", "NewLife.Redis.Extensions\NewLife.Redis.Extensions.csproj", "{499A9E8E-050C-40CD-90AF-FE4E499121D1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueueDemo", "Samples\QueueDemo\QueueDemo.csproj", "{AC75EE22-B722-418F-A2A7-66020E29FE4C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Samples\Benchmark\Benchmark.csproj", "{FAC752B8-087D-44A9-88E8-3B759A72F936}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -42,20 +44,25 @@ Global {08A39462-0531-45AB-ACBB-03F62AF4400F}.Debug|Any CPU.Build.0 = Debug|Any CPU {08A39462-0531-45AB-ACBB-03F62AF4400F}.Release|Any CPU.ActiveCfg = Release|Any CPU {08A39462-0531-45AB-ACBB-03F62AF4400F}.Release|Any CPU.Build.0 = Release|Any CPU - {5829188A-DC4A-4F00-AD95-5873E4057BDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5829188A-DC4A-4F00-AD95-5873E4057BDE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5829188A-DC4A-4F00-AD95-5873E4057BDE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5829188A-DC4A-4F00-AD95-5873E4057BDE}.Release|Any CPU.Build.0 = Release|Any CPU {499A9E8E-050C-40CD-90AF-FE4E499121D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {499A9E8E-050C-40CD-90AF-FE4E499121D1}.Debug|Any CPU.Build.0 = Debug|Any CPU {499A9E8E-050C-40CD-90AF-FE4E499121D1}.Release|Any CPU.ActiveCfg = Release|Any CPU {499A9E8E-050C-40CD-90AF-FE4E499121D1}.Release|Any CPU.Build.0 = Release|Any CPU + {AC75EE22-B722-418F-A2A7-66020E29FE4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC75EE22-B722-418F-A2A7-66020E29FE4C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC75EE22-B722-418F-A2A7-66020E29FE4C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC75EE22-B722-418F-A2A7-66020E29FE4C}.Release|Any CPU.Build.0 = Release|Any CPU + {FAC752B8-087D-44A9-88E8-3B759A72F936}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAC752B8-087D-44A9-88E8-3B759A72F936}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAC752B8-087D-44A9-88E8-3B759A72F936}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAC752B8-087D-44A9-88E8-3B759A72F936}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution - {5829188A-DC4A-4F00-AD95-5873E4057BDE} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4} + {AC75EE22-B722-418F-A2A7-66020E29FE4C} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4} + {FAC752B8-087D-44A9-88E8-3B759A72F936} = {E136AE06-927B-4D10-BB83-B607ED0B1FD4} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {323831A1-A95B-40AB-B9AD-36A0BC10C2CB} diff --git a/NewLife.Redis/NewLife.Redis.csproj b/NewLife.Redis/NewLife.Redis.csproj index c776b91..c2ddf5b 100644 --- a/NewLife.Redis/NewLife.Redis.csproj +++ b/NewLife.Redis/NewLife.Redis.csproj @@ -56,7 +56,7 @@ - + diff --git a/Samples/Benchmark/Benchmark.csproj b/Samples/Benchmark/Benchmark.csproj new file mode 100644 index 0000000..014e59b --- /dev/null +++ b/Samples/Benchmark/Benchmark.csproj @@ -0,0 +1,34 @@ + + + + Exe + net8.0 + 应用后台任务 + 数据处理、定时任务、MQ生产消费、系统监控等超长独立工作的后台任务 + 新生命开发团队 + ©2002-2024 NewLife + 1.0 + $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) + $(VersionPrefix).$(VersionSuffix) + $(Version) + $(VersionPrefix).* + false + ..\..\Bin\Console + false + enable + latest + + + + + + + + + + + + + + + diff --git a/Samples/Benchmark/Program.cs b/Samples/Benchmark/Program.cs new file mode 100644 index 0000000..cacc34c --- /dev/null +++ b/Samples/Benchmark/Program.cs @@ -0,0 +1,26 @@ +using NewLife.Caching; +using Stardust; + +//!!! 标准后台服务项目模板,新生命团队强烈推荐 + +// 启用控制台日志,拦截所有异常 +XTrace.UseConsole(); + +// 初始化对象容器,提供依赖注入能力 +var services = ObjectContainer.Current; +services.AddSingleton(XTrace.Log); + +// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址 +var star = services.AddStardust(); + +var rds = new FullRedis +{ + Tracer = star.Tracer, + Log = XTrace.Log, +}; +rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); + +var host = services.BuildHost(); + +// 异步阻塞,友好退出 +await host.RunAsync(); diff --git a/Samples/Benchmark/appsettings.json b/Samples/Benchmark/appsettings.json new file mode 100644 index 0000000..19250ac --- /dev/null +++ b/Samples/Benchmark/appsettings.json @@ -0,0 +1,22 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*", + //"StarServer": "http://s.newlifex.com:6600", + //"RedisCache": "server=127.0.0.1:6379;password=;db=3", + //"RedisQueue": "server=127.0.0.1:6379;password=;db=5", + "ConnectionStrings": { + "Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite" + + // 各种数据库连接字符串模版,连接名Zero对应Zero.Data/Projects/Model.xml中的ConnName + //"Zero": "Server=.;Port=3306;Database=zero;Uid=root;Pwd=root;Provider=MySql", + //"Zero": "Data Source=.;Initial Catalog=zero;user=sa;password=sa;Provider=SqlServer", + //"Zero": "Server=.;Database=zero;Uid=root;Pwd=root;Provider=PostgreSql", + //"Zero": "Data Source=Tcp://127.0.0.1/ORCL;User Id=scott;Password=tiger;Provider=Oracle" + } +} diff --git a/QueueDemo/AckQueue.cs b/Samples/QueueDemo/AckQueue.cs similarity index 100% rename from QueueDemo/AckQueue.cs rename to Samples/QueueDemo/AckQueue.cs diff --git a/QueueDemo/Area.cs b/Samples/QueueDemo/Area.cs similarity index 100% rename from QueueDemo/Area.cs rename to Samples/QueueDemo/Area.cs diff --git a/QueueDemo/DelayQueue.cs b/Samples/QueueDemo/DelayQueue.cs similarity index 100% rename from QueueDemo/DelayQueue.cs rename to Samples/QueueDemo/DelayQueue.cs diff --git a/QueueDemo/EasyQueue.cs b/Samples/QueueDemo/EasyQueue.cs similarity index 100% rename from QueueDemo/EasyQueue.cs rename to Samples/QueueDemo/EasyQueue.cs diff --git a/QueueDemo/FullQueue.cs b/Samples/QueueDemo/FullQueue.cs similarity index 100% rename from QueueDemo/FullQueue.cs rename to Samples/QueueDemo/FullQueue.cs diff --git a/QueueDemo/MemoryQueue.cs b/Samples/QueueDemo/MemoryQueue.cs similarity index 100% rename from QueueDemo/MemoryQueue.cs rename to Samples/QueueDemo/MemoryQueue.cs diff --git a/QueueDemo/MultipleConsumer.cs b/Samples/QueueDemo/MultipleConsumer.cs similarity index 100% rename from QueueDemo/MultipleConsumer.cs rename to Samples/QueueDemo/MultipleConsumer.cs diff --git a/QueueDemo/Program.cs b/Samples/QueueDemo/Program.cs similarity index 100% rename from QueueDemo/Program.cs rename to Samples/QueueDemo/Program.cs diff --git a/QueueDemo/QueueDemo.csproj b/Samples/QueueDemo/QueueDemo.csproj similarity index 78% rename from QueueDemo/QueueDemo.csproj rename to Samples/QueueDemo/QueueDemo.csproj index dafcd8d..13e5122 100644 --- a/QueueDemo/QueueDemo.csproj +++ b/Samples/QueueDemo/QueueDemo.csproj @@ -5,14 +5,14 @@ net8.0 1.0.* false - ..\Bin\QueueDemo + ..\..\Bin\QueueDemo false enable latest - + diff --git a/Test/Test.csproj b/Test/Test.csproj index 2ba2c77..4b40d34 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index e8f24dd..6cc2a43 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -10,7 +10,7 @@ - + From cc52acf2f6d79b35407d76bcc541cf144d51720c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 11 Dec 2024 10:21:42 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=95=E5=85=A5=20BenchmarkDotNet?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Samples/Benchmark/BasicBenchmark.cs | 59 +++++++++++++++++++++++++++++ Samples/Benchmark/Benchmark.csproj | 7 ++-- Samples/Benchmark/Program.cs | 16 +++----- 3 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 Samples/Benchmark/BasicBenchmark.cs diff --git a/Samples/Benchmark/BasicBenchmark.cs b/Samples/Benchmark/BasicBenchmark.cs new file mode 100644 index 0000000..b84be6c --- /dev/null +++ b/Samples/Benchmark/BasicBenchmark.cs @@ -0,0 +1,59 @@ +using System.Security.Cryptography; +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Engines; +using NewLife.Caching; +using NewLife.Security; + +namespace Benchmark; + +[SimpleJob(RunStrategy.ColdStart, iterationCount: 1)] +[MemoryDiagnoser] +public class BasicBenchmark +{ + public FullRedis Redis { get; set; } + + private String[] _keys; + + [GlobalSetup] + public void Setup() + { + var rds = new FullRedis + { + Tracer = DefaultTracer.Instance, + Log = XTrace.Log, + }; + rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); + + Redis = rds; + + var ks = new String[100_000]; + for (var i = 0; i < ks.Length; i++) + { + ks[i] = Rand.NextString(16); + } + _keys = ks; + } + + [Benchmark] + public void GetTest() + { + var rds = Redis; + + for (var i = 0; i < _keys.Length; i++) + { + var value = rds.Get(_keys[i]); + } + } + + [Benchmark] + public void SetTest() + { + var rds = Redis; + var value = Rand.NextString(16); + + for (var i = 0; i < _keys.Length; i++) + { + rds.Set(_keys[i], value); + } + } +} diff --git a/Samples/Benchmark/Benchmark.csproj b/Samples/Benchmark/Benchmark.csproj index 014e59b..fece7f5 100644 --- a/Samples/Benchmark/Benchmark.csproj +++ b/Samples/Benchmark/Benchmark.csproj @@ -11,9 +11,7 @@ $([System.DateTime]::Now.ToString(`yyyy.MMdd`)) $(VersionPrefix).$(VersionSuffix) $(Version) - $(VersionPrefix).* - false - ..\..\Bin\Console + ..\..\Bin\Benchmark false enable latest @@ -25,7 +23,8 @@ - + + diff --git a/Samples/Benchmark/Program.cs b/Samples/Benchmark/Program.cs index cacc34c..f641917 100644 --- a/Samples/Benchmark/Program.cs +++ b/Samples/Benchmark/Program.cs @@ -1,4 +1,6 @@ -using NewLife.Caching; +using Benchmark; +using BenchmarkDotNet.Running; +using NewLife.Caching; using Stardust; //!!! 标准后台服务项目模板,新生命团队强烈推荐 @@ -13,14 +15,6 @@ // 配置星尘。自动读取配置文件 config/star.config 中的服务器地址 var star = services.AddStardust(); -var rds = new FullRedis -{ - Tracer = star.Tracer, - Log = XTrace.Log, -}; -rds.Init("server=127.0.0.1:6379;password=;db=3;timeout=5000"); +var summary = BenchmarkRunner.Run(); -var host = services.BuildHost(); - -// 异步阻塞,友好退出 -await host.RunAsync(); +Console.ReadLine(); \ No newline at end of file