Skip to content

Commit

Permalink
Fix python benchmarks (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored May 22, 2024
1 parent be454d6 commit f71024f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ jobs:
env:
RUST_BACKTRACE: 1

- run: python crates/jiter-python/bench.py
env:
FAST: 1

- run: coverage-prepare lcov $(python -c 'import jiter.jiter;print(jiter.jiter.__file__)')

- uses: codecov/codecov-action@v4
Expand Down
23 changes: 15 additions & 8 deletions crates/jiter-python/bench.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import argparse
import os
import timeit
from pathlib import Path

import json

FAST = bool(os.getenv('FAST'))
THIS_DIR = Path(__file__).parent

cases = [
("medium_response", Path("../jiter/benches/medium_response.json").read_bytes()),
("medium_response", (THIS_DIR / "../jiter/benches/medium_response.json").read_bytes()),
(
"massive_ints_array",
Path("../jiter/benches/massive_ints_array.json").read_bytes(),
(THIS_DIR / "../jiter/benches/massive_ints_array.json").read_bytes(),
),
("array_short_strings", "[{}]".format(", ".join('"123"' for _ in range(100_000)))),
(
Expand All @@ -31,10 +35,13 @@ def run_bench(func, d):
timer = timeit.Timer(
"func(json_data)", setup="", globals={"func": func, "json_data": d}
)
n, t = timer.autorange()
iter_time = t / n
# print(f'{func.__module__}.{func.__name__}', iter_time)
return iter_time
if FAST:
return timer.timeit(1)
else:
n, t = timer.autorange()
iter_time = t / n
# print(f'{func.__module__}.{func.__name__}', iter_time)
return iter_time


def setup_orjson():
Expand All @@ -46,13 +53,13 @@ def setup_orjson():
def setup_jiter_cache():
import jiter

return lambda data: jiter.from_json(data, cache_strings=True)
return lambda data: jiter.from_json(data, cache_mode=True)


def setup_jiter():
import jiter

return lambda data: jiter.from_json(data, cache_strings=False)
return lambda data: jiter.from_json(data, cache_mode=False)


def setup_ujson():
Expand Down
2 changes: 2 additions & 0 deletions crates/jiter-python/tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pytest
pytest-pretty
dirty_equals
orjson
ujson

0 comments on commit f71024f

Please sign in to comment.