BenchTest

This commit is contained in:
Soar360 2022-07-18 10:01:17 +08:00
parent b44a71592d
commit 7a6a6fc515

View File

@ -1,10 +1,7 @@
using IP2Region.xdb; using IP2Region.xdb;
using System; using System;
using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace IP2Region.SearchTest namespace IP2Region.SearchTest
{ {
@ -65,7 +62,7 @@ namespace IP2Region.SearchTest
String key = r.Substring(2, sIdx - 2); String key = r.Substring(2, sIdx - 2);
String val = r.Substring(sIdx + 1); String val = r.Substring(sIdx + 1);
// System.out.printf("key=%s, val=%s\n", key, val); // Console.WriteLinef("key=%s, val=%s\n", key, val);
if ("db" == key) if ("db" == key)
{ {
dbPath = val; dbPath = val;
@ -83,7 +80,7 @@ namespace IP2Region.SearchTest
if (dbPath.Length < 1) if (dbPath.Length < 1)
{ {
Console.WriteLine("jSearchTest.exe search [command options]"); Console.WriteLine("SearchTest.exe search [command options]");
Console.WriteLine("options:"); Console.WriteLine("options:");
Console.WriteLine(" --db string ip2region binary xdb file path"); Console.WriteLine(" --db string ip2region binary xdb file path");
Console.WriteLine(" --cache-policy string cache policy: file/vectorIndex/content"); Console.WriteLine(" --cache-policy string cache policy: file/vectorIndex/content");
@ -118,7 +115,117 @@ namespace IP2Region.SearchTest
} }
public static void BenchTest(String[] args) public static void BenchTest(String[] args)
{ {
String dbPath = "", srcPath = "", 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);
if ("db" == key)
{
dbPath = val;
}
else if ("src" == key)
{
srcPath = val;
}
else if ("cache-policy" == key)
{
cachePolicy = val;
}
else
{
Console.WriteLine("undefined option `{0}`", r);
return;
}
}
if (dbPath.Length < 1 || srcPath.Length < 1)
{
Console.WriteLine("SearchTest.exe bench [command options]");
Console.WriteLine("options:");
Console.WriteLine(" --db string ip2region binary xdb file path");
Console.WriteLine(" --src string source ip text file path");
Console.WriteLine(" --cache-policy string cache policy: file/vectorIndex/content");
return;
}
Searcher searcher = CreateSearcher(dbPath, cachePolicy);
long count = 0;
var sw = new Stopwatch();
var lines = File.ReadAllLines(srcPath);
foreach (var line in lines)
{
String l = line.Trim();
String[] ps = l.Split(new[] { '|' }, 3);
if (ps.Length != 3)
{
Console.WriteLine("invalid ip segment `{0}`", l);
return;
}
long sip;
try
{
sip = Searcher.checkIP(ps[0]);
}
catch (Exception e)
{
Console.WriteLine("check start ip `{0}`: {1}", ps[0], e);
return;
}
long eip;
try
{
eip = Searcher.checkIP(ps[1]);
}
catch (Exception e)
{
Console.WriteLine("check end ip `{0}`: {1}", ps[1], e);
return;
}
if (sip > eip)
{
Console.WriteLine("start ip({0}) should not be greater than end ip({1})", ps[0], ps[1]);
return;
}
long mip = (sip + eip) >> 1;
foreach (var ip in new long[] { sip, (sip + mip) >> 1, mip, (mip + eip) >> 1, eip })
{
sw.Start();
String region = searcher.Search(ip);
sw.Stop();
// check the region info
if (ps[2] != (region))
{
Console.WriteLine("failed search({0}) with ({1} != {2})\n", Searcher.Long2ip(ip), region, ps[2]);
return;
}
count++;
}
}
Console.WriteLine("Bench finished, {{cachePolicy: {0}, total: {1}, took: {2}, cost: {3} ms/op}}",
cachePolicy, count, sw.Elapsed,
count == 0 ? 0 : sw.ElapsedMilliseconds / count);
} }
static void Main(string[] args) static void Main(string[] args)
{ {