-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathbenchmark.py
More file actions
executable file
·37 lines (27 loc) · 1016 Bytes
/
benchmark.py
File metadata and controls
executable file
·37 lines (27 loc) · 1016 Bytes
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
#!/usr/bin/python
"""Simple benchmarking script."""
import argparse
import contextlib
import random
import socket
import struct
import timeit
import geoip2.database
import geoip2.errors
parser = argparse.ArgumentParser(description="Benchmark maxminddb.")
parser.add_argument("--count", default=250000, type=int, help="number of lookups")
parser.add_argument("--mode", default=0, type=int, help="reader mode to use")
parser.add_argument("--file", default="GeoIP2-City.mmdb", help="path to mmdb file")
args = parser.parse_args()
reader = geoip2.database.Reader(args.file, mode=args.mode)
def lookup_ip_address() -> None:
"""Look up IP address."""
ip = socket.inet_ntoa(struct.pack("!L", random.getrandbits(32)))
with contextlib.suppress(geoip2.errors.AddressNotFoundError):
reader.city(str(ip))
elapsed = timeit.timeit(
"lookup_ip_address()",
setup="from __main__ import lookup_ip_address",
number=args.count,
)
print(args.count / elapsed, "lookups per second") # noqa: T201