Skip to content

Commit

Permalink
引入 BenchmarkDotNet
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 11, 2024
1 parent 816f319 commit cc52acf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 15 deletions.
59 changes: 59 additions & 0 deletions Samples/Benchmark/BasicBenchmark.cs
Original file line number Diff line number Diff line change
@@ -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<String>(_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);
}
}
}
7 changes: 3 additions & 4 deletions Samples/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(VersionPrefix).*</AssemblyVersion>
<Deterministic>false</Deterministic>
<OutputPath>..\..\Bin\Console</OutputPath>
<OutputPath>..\..\Bin\Benchmark</OutputPath>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>latest</LangVersion>
Expand All @@ -25,7 +23,8 @@
<Using Include="NewLife.Reflection" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NewLife.Stardust" Version="3.1.2024.1004" />
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="NewLife.Stardust" Version="3.2.2024.1211" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NewLife.Redis\NewLife.Redis.csproj" />
Expand Down
16 changes: 5 additions & 11 deletions Samples/Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NewLife.Caching;
using Benchmark;
using BenchmarkDotNet.Running;
using NewLife.Caching;
using Stardust;

//!!! 标准后台服务项目模板,新生命团队强烈推荐
Expand All @@ -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<BasicBenchmark>();

var host = services.BuildHost();

// 异步阻塞,友好退出
await host.RunAsync();
Console.ReadLine();

0 comments on commit cc52acf

Please sign in to comment.