From a0efcf04563a81b447789e7f5f6e14e1bafe6262 Mon Sep 17 00:00:00 2001 From: Lion Date: Fri, 24 Jun 2022 11:52:33 +0800 Subject: [PATCH] impl the bench test --- .gitignore | 1 + .../org/lionsoul/ip2region/SearchTest.java | 111 ++++++++++++++++-- .../org/lionsoul/ip2region/xdb/Searcher.java | 2 +- 3 files changed, 105 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1704759..acdba36 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ *.la *.so *.xdb +*.iml META-INF/ # Binary Files # diff --git a/binding/java/src/main/java/org/lionsoul/ip2region/SearchTest.java b/binding/java/src/main/java/org/lionsoul/ip2region/SearchTest.java index 77a66ae..8f3c819 100644 --- a/binding/java/src/main/java/org/lionsoul/ip2region/SearchTest.java +++ b/binding/java/src/main/java/org/lionsoul/ip2region/SearchTest.java @@ -8,9 +8,7 @@ package org.lionsoul.ip2region; import org.lionsoul.ip2region.xdb.Searcher; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.util.concurrent.TimeUnit; public class SearchTest { @@ -56,13 +54,13 @@ public class SearchTest { String key = r.substring(2, sIdx); String val = r.substring(sIdx + 1); - System.out.printf("key=%s, val=%s\n", key, val); + // System.out.printf("key=%s, val=%s\n", key, val); if ("db".equals(key)) { dbPath = val; } else if ("cache-policy".equals(key)) { cachePolicy = val; } else { - System.out.printf("undefined option `%s`", r); + System.out.printf("undefined option `%s`\n", r); return; } } @@ -104,8 +102,101 @@ public class SearchTest { System.out.println("searcher test program exited, thanks for trying"); } - public static void benchTest(String[] args) { + public static void benchTest(String[] args) throws IOException { + String dbPath = "", srcPath = "", cachePolicy = "vectorIndex"; + for (final String r : args) { + if (r.length() < 5) { + continue; + } + if (r.indexOf("--") != 0) { + continue; + } + + int sIdx = r.indexOf('='); + if (sIdx < 0) { + System.out.printf("missing = for args pair `%s`\n", r); + return; + } + + String key = r.substring(2, sIdx); + String val = r.substring(sIdx + 1); + if ("db".equals(key)) { + dbPath = val; + } else if ("src".equals(key)) { + srcPath = val; + } else if ("cache-policy".equals(key)) { + cachePolicy = val; + } else { + System.out.printf("undefined option `%s`\n", r); + return; + } + } + + if (dbPath.length() < 1 || srcPath.length() < 1) { + System.out.print("java -jar ip2region-{version}.jar bench [command options]\n"); + System.out.print("options:\n"); + System.out.print(" --db string ip2region binary xdb file path\n"); + System.out.print(" --src string source ip text file path\n"); + System.out.print(" --cache-policy string cache policy: file/vectorIndex/content\n"); + return; + } + + Searcher searcher = createSearcher(dbPath, cachePolicy); + long count = 0, costs = 0, tStart = System.nanoTime(); + String line; + final BufferedReader reader = new BufferedReader(new FileReader(srcPath)); + while ((line = reader.readLine()) != null) { + String l = line.trim(); + String[] ps = l.split("\\|", 3); + if (ps.length != 3) { + System.out.printf("invalid ip segment `%s`\n", l); + return; + } + + long sip; + try { + sip = Searcher.checkIpAddr(ps[0]); + } catch (Exception e) { + System.out.printf("check start ip `%s`: %s\n", ps[0], e); + return; + } + + long eip; + try { + eip = Searcher.checkIpAddr(ps[1]); + } catch (Exception e) { + System.out.printf("check end ip `%s`: %s\n", ps[1], e); + return; + } + + if (sip > eip) { + System.out.printf("start ip(%s) should not be greater than end ip(%s)\n", ps[0], ps[1]); + return; + } + + long mip = (sip + eip) >> 1; + for (final long ip : new long[]{sip, (sip + mip) >> 1, mip, (mip + eip) >> 1, eip}) { + long sTime = System.nanoTime(); + String region = searcher.search(ip); + costs += System.nanoTime() - sTime; + + // check the region info + if (!ps[2].equals(region)) { + System.out.printf("failed search(%s) with (%s != %s)\n", Searcher.long2ip(ip), region, ps[2]); + return; + } + + count++; + } + } + + reader.close(); + searcher.close(); + long took = System.nanoTime() - tStart; + System.out.printf("Bench finished, {cachePolicy: %s, total: %d, took: %ds, cost: %d μs/op}\n", + cachePolicy, count, TimeUnit.NANOSECONDS.toSeconds(took), + count == 0 ? 0 : TimeUnit.NANOSECONDS.toMicros(costs/count)); } public static void main(String[] args) { @@ -118,10 +209,14 @@ public class SearchTest { try { searchTest(args); } catch (IOException e) { - System.out.printf("failed running search test: %s", e); + System.out.printf("failed running search test: %s\n", e); } } else if ("bench".equals(args[0])) { - benchTest(args); + try { + benchTest(args); + } catch (IOException e) { + System.out.printf("failed running bench test: %s\n", e); + } } else { printHelp(args); } diff --git a/binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java b/binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java index f99198b..900942b 100644 --- a/binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java +++ b/binding/java/src/main/java/org/lionsoul/ip2region/xdb/Searcher.java @@ -48,7 +48,7 @@ public class Searcher { return new Searcher(null, null, cBuff); } - // --- + // --- End of creator public Searcher(String dbFile, byte[] vectorIndex, byte[] cBuff) throws IOException { if (cBuff != null) {