File: benchmark.py

package info (click to toggle)
python-maxminddb 2.8.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,580 kB
  • sloc: ansic: 7,251; python: 1,520; perl: 987; makefile: 273; sh: 191
file content (35 lines) | stat: -rwxr-xr-x 922 bytes parent folder | download
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
#!/usr/bin/python
"""Basic benchmark for maxminddb."""

import argparse
import random
import socket
import struct
import timeit

import maxminddb

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()

random.seed(0)
reader = maxminddb.open_database(args.file, args.mode)


def lookup_ip_address() -> None:
    """Look up the IP."""
    ip = socket.inet_ntoa(struct.pack("!L", random.getrandbits(32)))
    reader.get(str(ip))


elapsed = timeit.timeit(
    "lookup_ip_address()",
    setup="from __main__ import lookup_ip_address",
    number=args.count,
)

print(f"{int(args.count / elapsed):,}", "lookups per second")  # noqa: T201