chore: 更新 BenchMark 工程

This commit is contained in:
Argo Zhang 2025-11-21 08:15:23 +08:00
parent d7d06e2185
commit f1d0e1bee0
2 changed files with 34 additions and 12 deletions

View File

@ -2,14 +2,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet.Annotations" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
<PackageReference Include="BenchmarkDotNet.Annotations" Version="0.15.6" />
</ItemGroup>
<ItemGroup>
@ -21,6 +21,10 @@
<Link>IP2Region/ip2region_v4.xdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Content Include="..\..\..\data\ip2region_v6.xdb">
<Link>IP2Region/ip2region_v6.xdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@ -7,23 +7,41 @@ BenchmarkRunner.Run(typeof(Program).Assembly);
public class CachePolicyCompare
{
private static readonly string XdbPath = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region_v4.xdb");
private readonly ISearcher _contentSearcher = new Searcher(CachePolicy.Content, XdbPath);
private readonly ISearcher _vectorSearcher = new Searcher(CachePolicy.VectorIndex,XdbPath);
private readonly ISearcher _fileSearcher = new Searcher(CachePolicy.File,XdbPath);
private static readonly string XdbPathV4 = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region_v4.xdb");
private static readonly string XdbPathV6 = Path.Combine(AppContext.BaseDirectory, "IP2Region", "ip2region_v6.xdb");
private readonly ISearcher _contentV4Searcher = new Searcher(CachePolicy.Content, XdbPathV4);
private readonly ISearcher _vectorV4Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV4);
private readonly ISearcher _fileV4Searcher = new Searcher(CachePolicy.File, XdbPathV4);
private readonly ISearcher _contentV6Searcher = new Searcher(CachePolicy.Content, XdbPathV6);
private readonly ISearcher _vectorV6Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV6);
private readonly ISearcher _fileV6Searcher = new Searcher(CachePolicy.File, XdbPathV6);
private readonly string _testIpAddress = "114.114.114.114";
private readonly string _testIpV4Address = "114.114.114.114";
private readonly string _testIpV6Address = "240e:3b7:3272:d8d0:db09:c067:8d59:539e";
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.Content))]
public void CachePolicy_Content() => _contentSearcher.Search(_testIpAddress);
public void ContentIpV4() => _contentV4Searcher.Search(_testIpV4Address);
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.VectorIndex))]
public void CachePolicy_VectorIndex() => _vectorSearcher.Search(_testIpAddress);
public void VectorIndexIpV4() => _vectorV4Searcher.Search(_testIpV4Address);
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.File))]
public void CachePolicy_File() => _fileSearcher.Search(_testIpAddress);
public void FileIpV4() => _fileV4Searcher.Search(_testIpV4Address);
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.Content))]
public void ContentIpV6() => _contentV6Searcher.Search(_testIpV6Address);
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.VectorIndex))]
public void VectorIndexIpV6() => _vectorV6Searcher.Search(_testIpV6Address);
[Benchmark]
[BenchmarkCategory(nameof(CachePolicy.File))]
public void FileIpV6() => _fileV6Searcher.Search(_testIpV6Address);
}