Skip to content

Commit

Permalink
Add test data and benchmarking script
Browse files Browse the repository at this point in the history
  • Loading branch information
danhje committed Mar 30, 2024
1 parent 0f916fc commit cf65901
Show file tree
Hide file tree
Showing 11 changed files with 1,111,847 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.2
rev: v0.3.4
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
- repo: https://github.com/doublify/pre-commit-rust
rev: master
- repo: https://github.com/FeryET/pre-commit-rust
rev: v1.1.0
hooks:
- id: fmt
- id: cargo-check
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ poetry run pytest
```

Be sure to run `poetry run maturin develop` after making changes to the Rust code.
Add the `-r` flag for a release build (for example if you want to run benchmarks).

It's recommended to install the pre-commit hooks:
```bash
Expand Down
33 changes: 33 additions & 0 deletions benchmarking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Simple benchmarking script.
Important! Remember to build the Rust extension with the -r flag to enable release mode.
Without this, the Rust extension will be compiled in debug mode, which is significantly slower.
poetry run maturin develop -r
"""

import timeit
from pathlib import Path

from quick_xmltodict import parse as quickparse # noqa: F401
from xmltodict import parse as pyparse # noqa: F401

DATA_DIR = Path(__file__).parent / "tests/data"

DATA = {
"simple": (DATA_DIR / "simple.xml").read_text(),
"time_series": (DATA_DIR / "time-series.xml").read_text(),
"forecast": (DATA_DIR / "forecast.xml").read_text(),
"eic_codes": (DATA_DIR / "eic-codes.xml").read_text(),
}

results = {}
for name, xml in DATA.items(): # noqa: B007, PERF102
print(f"Running benchmarks for {name}...")
quick_time = timeit.timeit("quickparse(xml)", globals=globals(), number=3)
py_time = timeit.timeit("pyparse(xml)", globals=globals(), number=3)
results[name] = {"quick_xmltodict": quick_time, "xmltodict": py_time, "ratio": py_time / quick_time}

print("Relative performance:")
for name, result in results.items():
print(f"{name}: {result['ratio']:.2f}")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ convention = "google"

[tool.ruff.lint.per-file-ignores]
"tests/test*.py" = ["ANN001", "ANN201"]
"benchmarking.py" = ["T201"]

[tool.ruff.lint.flake8-import-conventions]
[tool.ruff.lint.flake8-import-conventions.aliases]
Expand Down
1 change: 1 addition & 0 deletions tests/data/eic-codes.json

Large diffs are not rendered by default.

Loading

0 comments on commit cf65901

Please sign in to comment.