Skip to main content

A Python package to benchmark query performance and comparison on PostgreSQL Database

Project description

pgbenchmark

codecov PyPI Version PyPI Downloads

Python package to benchmark query performance on a PostgreSQL database. It allows you to measure the execution time of queries over multiple runs, providing detailed metrics about each run's performance.



Installation

pip install pgbenchmark

Example

import psycopg2
from pgbenchmark import Benchmark

conn = psycopg2.connect(
    "<< YOUR CONNECTION >>"
)

benchmark = Benchmark(db_connection=conn, number_of_runs=1000)
benchmark.set_sql("./test.sql")

for result in benchmark:
    # {'run': X, 'sent_at': <DATETIME WITH MS>, 'duration': '0.000064'}
    pass

""" View Summary """
print(benchmark.get_execution_results())
# {'runs': 1000, 'min_time': '0.00005', 'max_time': '0.000287', 'avg_time': '0.000072'}

You can also pass raw SQL as a String, instead of file

benchmark.set_sql("SELECT 1;")

It also supports SQLAlchemy connection engine

engine = create_engine("postgresql+psycopg2://.......")
conn = engine.connect()

# Set up benchmark class
benchmark = Benchmark(db_connection=conn, number_of_runs=5)

Example with Parallel or Threaded execution

⚠️ Please be careful. If you are running on Linux, pgbenchmark will load your cores on 100% !!!⚠️

from pgbenchmark import ParallelBenchmark  # <<-------- NEW IMPORT

conn_params = {
    "dbname": "postgres",
    "user": "postgres",
    "password": "",
    "host": "localhost",
    "port": "5432"
}

n_procs = 20  # Number of Processes (Cores basically)
n_runs_per_proc = 1_000

parallel_bench_pg = ParallelBenchmark(
    num_processes=n_procs,
    number_of_runs=n_runs_per_proc,
    db_connection_info=conn_params
)

parallel_bench_pg.set_sql("SELECT * from information_schema.tables;")  # Same as before

""" Unfortunately, as of now, you can't get execution results on the fly. """

parallel_bench_pg.run()  # RUN THE BENCHMARK 

results_pg = parallel_bench_pg.get_execution_results()
print(results_pg)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgbenchmark-0.0.9.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgbenchmark-0.0.9-py3-none-any.whl (45.2 kB view details)

Uploaded Python 3

File details

Details for the file pgbenchmark-0.0.9.tar.gz.

File metadata

  • Download URL: pgbenchmark-0.0.9.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pgbenchmark-0.0.9.tar.gz
Algorithm Hash digest
SHA256 1d06b2e1d01851c5a141d92ebdb1bc5f3ce2da707b5f54bec756f00559b49c44
MD5 c13efadb42703402079564d713fdfdaf
BLAKE2b-256 df1542a3f796953e01ce3f6887af0ef1e065eb32019cbdc3d6ad0b892b8617f6

See more details on using hashes here.

File details

Details for the file pgbenchmark-0.0.9-py3-none-any.whl.

File metadata

  • Download URL: pgbenchmark-0.0.9-py3-none-any.whl
  • Upload date:
  • Size: 45.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.12

File hashes

Hashes for pgbenchmark-0.0.9-py3-none-any.whl
Algorithm Hash digest
SHA256 8b0b4169b22e271b8d30928f1383b649f485c35f9d25f819607980b628556d25
MD5 863145f5d2cef4b35f20f50a5374cc2c
BLAKE2b-256 fd45193647a9e705cc64ee60a66bb8ab639ffc72371349fd8f1832a9cff8a331

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page