SearchTest

This commit is contained in:
Soar360 2022-07-18 09:47:22 +08:00
parent 0bd2c15c56
commit b44a71592d
6 changed files with 261 additions and 4 deletions

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>

View File

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{FF136AEA-D41F-4C14-842B-CE597BB97916}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>IP2Region.SearchTest</RootNamespace>
<AssemblyName>IP2Region.SearchTest</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\IP2Region\IP2Region.csproj">
<Project>{483575a5-ffb3-4131-a2dc-6d3ad3bb268f}</Project>
<Name>IP2Region</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,156 @@
using IP2Region.xdb;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IP2Region.SearchTest
{
internal class Program
{
public static void PrintHelp(String[] args)
{
Console.WriteLine("ip2region xdb searcher");
Console.WriteLine("SearchTest.exe [command] [command options]");
Console.WriteLine("Command: ");
Console.WriteLine(" search search input test");
Console.WriteLine(" bench search bench test");
}
public static Searcher CreateSearcher(String dbPath, String cachePolicy)
{
if ("file" == cachePolicy)
{
return Searcher.NewWithFileOnly(dbPath);
}
else if ("vectorIndex" == cachePolicy)
{
byte[] vIndex = Searcher.LoadVectorIndexFromFile(dbPath);
return Searcher.NewWithVectorIndex(dbPath, vIndex);
}
else if ("content" == cachePolicy)
{
byte[] cBuff = Searcher.LoadContentFromFile(dbPath);
return Searcher.NewWithBuffer(cBuff);
}
else
{
throw new Exception("invalid cache policy `" + cachePolicy + "`, options: file/vectorIndex/content");
}
}
public static void SearchTest(string[] args)
{
String dbPath = "", cachePolicy = "vectorIndex";
foreach (string r in args)
{
if (r.Length < 5)
{
continue;
}
if (r.IndexOf("--") != 0)
{
continue;
}
int sIdx = r.IndexOf('=');
if (sIdx < 0)
{
Console.WriteLine("missing = for args pair `{0}`", r);
return;
}
String key = r.Substring(2, sIdx - 2);
String val = r.Substring(sIdx + 1);
// System.out.printf("key=%s, val=%s\n", key, val);
if ("db" == key)
{
dbPath = val;
}
else if ("cache-policy" == key)
{
cachePolicy = val;
}
else
{
Console.WriteLine("undefined option `{0}`", r);
return;
}
}
if (dbPath.Length < 1)
{
Console.WriteLine("jSearchTest.exe search [command options]");
Console.WriteLine("options:");
Console.WriteLine(" --db string ip2region binary xdb file path");
Console.WriteLine(" --cache-policy string cache policy: file/vectorIndex/content");
return;
}
Searcher searcher = CreateSearcher(dbPath, cachePolicy);
Console.WriteLine("ip2region xdb searcher test program, cachePolicy: %s\ntype 'quit' to exit", cachePolicy);
while (true)
{
Console.Write("ip2region>> ");
var line = Console.ReadLine().Trim();
if (line.Length < 2) continue;
if (line == "quit") break;
try
{
var st = new Stopwatch();
st.Start();
var region = searcher.Search(line);
st.Stop();
var cost = st.ElapsedMilliseconds;
Console.WriteLine("{{region: {0}, ioCount: {1}, took: {2} ms}}", region, searcher.IOCount, cost);
}
catch (Exception e)
{
Console.WriteLine("{{err:{0}, ioCount: {1}}}", e, searcher.IOCount);
}
}
Console.WriteLine("searcher test program exited, thanks for trying");
}
public static void BenchTest(String[] args)
{
}
static void Main(string[] args)
{
if (args.Length < 1)
{
PrintHelp(args);
return;
}
switch (args[0])
{
case "search":
try
{
SearchTest(args);
}
catch (Exception e)
{
Console.WriteLine("failed running search test: {0}", e);
}
break;
case "bench":
try
{
BenchTest(args);
}
catch (Exception e)
{
Console.WriteLine("failed running bench test: {0}", e);
}
break;
default: PrintHelp(args); break;
}
}
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// 有关程序集的一般信息由以下
// 控制。更改这些特性值可修改
// 与程序集关联的信息。
[assembly: AssemblyTitle("IP2Region.SearchTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("IP2Region.SearchTest")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// 将 ComVisible 设置为 false 会使此程序集中的类型
//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
//请将此类型的 ComVisible 特性设置为 true。
[assembly: ComVisible(false)]
// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
[assembly: Guid("ff136aea-d41f-4c14-842b-ce597bb97916")]
// 程序集的版本信息由下列四个值组成:
//
// 主版本
// 次版本
// 生成号
// 修订号
//
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
</PropertyGroup>
</Project>

View File

@ -248,7 +248,7 @@ namespace IP2Region.xdb
public static long checkIP(String ip)
{
String[]
ps = ip.Split(".");
ps = ip.Split('.');
if (ps.Length != 4) throw new Exception("invalid ip address `" + ip + "`");
long ipDst = 0;