From 4ea2e7360da18a7f2d0ca79f56c152c7a48d2720 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Fri, 21 Nov 2025 12:25:24 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0=E5=8E=8B=E5=8A=9B?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../IP2Region.Net.BenchMark/Benmarks.cs | 50 +++++++++++++++++ .../csharp/IP2Region.Net.BenchMark/Program.cs | 56 +------------------ 2 files changed, 52 insertions(+), 54 deletions(-) create mode 100644 binding/csharp/IP2Region.Net.BenchMark/Benmarks.cs diff --git a/binding/csharp/IP2Region.Net.BenchMark/Benmarks.cs b/binding/csharp/IP2Region.Net.BenchMark/Benmarks.cs new file mode 100644 index 0000000..66dcb85 --- /dev/null +++ b/binding/csharp/IP2Region.Net.BenchMark/Benmarks.cs @@ -0,0 +1,50 @@ +// Copyright 2023 The Ip2Region Authors. All rights reserved. +// Use of this source code is governed by a Apache2.0-style +// license that can be found in the LICENSE file. +// @Author Alan +// @Date 2023/07/25 +// Updated by Argo Zhang at 2025/11/21 + +using BenchmarkDotNet.Attributes; +using IP2Region.Net.XDB; + +namespace IP2Region.Net.BenchMark; + +public class Benchmarks +{ + 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 static readonly Searcher _contentV4Searcher = new Searcher(CachePolicy.Content, XdbPathV4); + private static readonly Searcher _vectorV4Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV4); + private static readonly Searcher _fileV4Searcher = new Searcher(CachePolicy.File, XdbPathV4); + private static readonly Searcher _contentV6Searcher = new Searcher(CachePolicy.Content, XdbPathV6); + private static readonly Searcher _vectorV6Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV6); + private static readonly Searcher _fileV6Searcher = new Searcher(CachePolicy.File, XdbPathV6); + + 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 ContentIpV4() => _contentV4Searcher.Search(_testIpV4Address); + + //[Benchmark] + [BenchmarkCategory(nameof(CachePolicy.VectorIndex))] + public void VectorIndexIpV4() => _vectorV4Searcher.Search(_testIpV4Address); + + [Benchmark] + [BenchmarkCategory(nameof(CachePolicy.File))] + 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); +} diff --git a/binding/csharp/IP2Region.Net.BenchMark/Program.cs b/binding/csharp/IP2Region.Net.BenchMark/Program.cs index 9f7d0b0..5ca00ae 100644 --- a/binding/csharp/IP2Region.Net.BenchMark/Program.cs +++ b/binding/csharp/IP2Region.Net.BenchMark/Program.cs @@ -1,56 +1,4 @@ -using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; -using IP2Region.Net.XDB; +using IP2Region.Net.BenchMark; -BenchmarkRunner.Run(typeof(Program).Assembly); - -public class CachePolicyCompare -{ - 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 Searcher _contentV4Searcher = new Searcher(CachePolicy.Content, XdbPathV4); - private readonly Searcher _vectorV4Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV4); - private readonly Searcher _fileV4Searcher = new Searcher(CachePolicy.File, XdbPathV4); - private readonly Searcher _contentV6Searcher = new Searcher(CachePolicy.Content, XdbPathV6); - private readonly Searcher _vectorV6Searcher = new Searcher(CachePolicy.VectorIndex, XdbPathV6); - private readonly Searcher _fileV6Searcher = new Searcher(CachePolicy.File, XdbPathV6); - - private readonly string _testIpV4Address = "114.114.114.114"; - private readonly string _testIpV6Address = "240e:3b7:3272:d8d0:db09:c067:8d59:539e"; - - public CachePolicyCompare() - { - _contentV4Searcher.Search(_testIpV4Address); - _vectorV4Searcher.Search(_testIpV4Address); - _fileV4Searcher.Search(_testIpV4Address); - _contentV6Searcher.Search(_testIpV6Address); - _vectorV6Searcher.Search(_testIpV6Address); - _fileV6Searcher.Search(_testIpV6Address); - } - - [Benchmark] - [BenchmarkCategory(nameof(CachePolicy.Content))] - public void ContentIpV4() => _contentV4Searcher.Search(_testIpV4Address); - - [Benchmark] - [BenchmarkCategory(nameof(CachePolicy.VectorIndex))] - public void VectorIndexIpV4() => _vectorV4Searcher.Search(_testIpV4Address); - - - [Benchmark] - [BenchmarkCategory(nameof(CachePolicy.File))] - 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); -} +BenchmarkRunner.Run();