forked from lionsoul2014/ip2region
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
54 lines (44 loc) · 1.36 KB
/
benchmark.py
File metadata and controls
54 lines (44 loc) · 1.36 KB
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
#-*- coding:utf-8 -*-
"""
" ip2region python seacher client module benchmark test file
"
" Autho: koma<komazhang@foxmail.com>
" Date : 2018-10-04
"""
import threading
import time, sys
from ip2Region import Ip2Region
class BenchmarkThread(threading.Thread):
__searcher = None
__lock = None
def __init__(self, searcher, lock):
self.__searcher = searcher
self.__lock = lock
threading.Thread.__init__(self)
def run(self):
self.__lock.acquire()
try:
sTime = time.time() * 1000
data = self.__searcher.memorySearch("49.220.138.233")
eTime = time.time() * 1000
# @Note uncomment the print to make it more like the product environment
print("%s|%s in %5f millseconds" % (data["city_id"], data["region"].decode('utf-8'), eTime - sTime))
finally:
self.__lock.release()
if __name__ == "__main__":
dbFile = "./data/ip2region.db"
if ( len(sys.argv) > 2 ):
dbFile = sys.argv[1];
threads = []
searcher = Ip2Region(dbFile)
lock = threading.Lock()
for i in range(10000):
t = BenchmarkThread(searcher, lock)
threads.append(t)
sTime = time.time() * 1000
for t in threads:
t.start()
for t in threads:
t.join()
eTime = time.time() * 1000
print("Benchmark done: %5f" % (eTime - sTime))