-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
bench_test.py
120 lines (104 loc) · 3.98 KB
/
bench_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright 2022 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.
#
# Created by luckydog on 2022/7/1.
# Copyright © 2022年 luckydog. All rights reserved.
#
# from ast import main
import io
from xdbSearcher import XdbSearcher
import argparse
import time
import sys
def printHelp():
print("python bench_test.py [command options]")
print("options: ")
print(" --db string ip2region binary xdb file path")
print(" --src string source ip text file path")
print(" --cache-policy string cache policy: file/vectorIndex/content")
def trim(string):
if string[:1] != ' ' and string[-1:] != ' ':
return string
elif string[:1] == ' ':
return trim(string[1:])
else:
return trim(string[:-1])
def start_bench(dbFile="", srcFile="", cachePolicy="vectorIndex"):
if cachePolicy == "file":
try:
searcher = XdbSearcher(dbfile=dbFile)
except Exception as err:
print(err)
return
elif cachePolicy == "vectorIndex":
try:
vi = XdbSearcher.loadVectorIndexFromFile(dbfile=dbFile)
if vi is None:
print(f"failed to load vector index from {dbFile}\n")
searcher = XdbSearcher(dbfile=dbFile, vectorIndex=vi)
except Exception as err:
print(err)
return
else:
try:
cb = XdbSearcher.loadContentFromFile(dbfile=dbFile)
if cb is None:
print(f"failed to load xdb content from {dbFile}\n")
searcher = XdbSearcher(contentBuff=cb)
except Exception as err:
print(err)
return
# do the bench test
try:
count = 0
costs = 0
sTime = time.time()
f = io.open(srcFile, "rb")
while True:
line = trim(f.readline(1024)).decode("utf-8").replace("\n", "")
if len(line) < 1:
break
ps = line.split("|",2)
if len(ps) != 3:
print(f"invalid ip segment line :{line}")
return
sip = XdbSearcher.ip2long(None, ps[0])
eip = XdbSearcher.ip2long(None, ps[1])
if sip > eip:
print(f"start ip({ps[0]}) should not be greater than end ip({ps[1]})")
return
mip = (sip + eip) >> 1
for ip in [sip, (sip + mip) >> 1, mip, (mip + eip) >> 1, eip]:
try:
cTime = time.time()
region = searcher.search(ip)
costs = costs + (time.time() - cTime)
except Exception as error:
print(f"failed to search ip :{ip}")
return
if region is None:
print(f"failed to search ip :{ip}")
return
if region != ps[2]:
print(f"failed search({ip}) with ({region} != {ps[2]})")
return
count = count + 1
# close the searcher at last
f.close()
searcher.close()
print(f"Bench finished, {{cachePolicy: {cachePolicy}, total: {count}, took: {round(time.time() - sTime, 2)} s, cost: {round(costs/count*1000, 4)} ms/op}}")
except Exception as err:
print(f"failed to open source text file :{err}")
return
if __name__ == '__main__':
if len(sys.argv) < 2:
printHelp()
exit(0)
parse = argparse.ArgumentParser()
parse.add_argument("--db", help="ip2region binary xdb file path")
parse.add_argument("--src", help="source ip text file path")
parse.add_argument("--cache-policy", choices=["file", "vectorIndex", "content"],
help="cache policy: file/vectorIndex/content")
args = parse.parse_args()
start_bench(dbFile=args.db, srcFile=args.src, cachePolicy=args.cache_policy)