Skip to content

Update benchmark to use PyMongo 4.11 and Python 3.13 #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,22 @@ Using bsonjs with pymongo to insert a RawBSONDocument.
Speed
=====

bsonjs is roughly 10-15x faster than PyMongo's json_util at decoding BSON to
bsonjs is roughly 3-4x faster than PyMongo's json_util at decoding BSON to
JSON and encoding JSON to BSON. See `benchmark.py`::

$ python benchmark.py
Timing: bsonjs.dumps(b)
10000 loops, best of 3: 0.110911846161
Timing: json_util.dumps(bson.BSON(b).decode())
10000 loops, best of 3: 1.46571397781
bsonjs is 13.22x faster than json_util
10000 loops, best of 3: 0.04682216700166464
Timing: json_util.dumps(bson.decode(b))
10000 loops, best of 3: 0.17319270805455744
bsonjs is 3.70x faster than json_util

Timing: bsonjs.loads(j)
10000 loops, best of 3: 0.0628039836884
Timing: bson.BSON().encode(json_util.loads(j))
10000 loops, best of 3: 0.683200120926
bsonjs is 11.72x faster than json_util
10000 loops, best of 3: 0.053156834095716476
Timing: bson.encode(json_util.loads(j))
10000 loops, best of 3: 0.15982166700996459
bsonjs is 3.01x faster than json_util


Limitations
===========
Expand Down
6 changes: 3 additions & 3 deletions benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ def main(iterations):
" 'date': datetime.datetime(2009, 12, 9, 15),\n"
" 'regex': bson.Regex('.*', 'i'),\n"
"}\n"
"b = bson.BSON.encode(doc)\n"
"b = bson.encode(doc)\n"
"j = bsonjs.dumps(b)\n")

# dumps
compare("bsonjs.dumps(b)",
"json_util.dumps(bson.BSON(b).decode())",
"json_util.dumps(bson.decode(b))",
iterations,
setup)
# loads
compare("bsonjs.loads(j)",
"bson.BSON().encode(json_util.loads(j))",
"bson.encode(json_util.loads(j))",
iterations,
setup)

Expand Down
2 changes: 1 addition & 1 deletion build-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ for PYBIN in /opt/python/*/bin; do
fi
"${PYBIN}/pip" install python-bsonjs --no-index -f dist
# The tests require PyMongo.
"${PYBIN}/pip" install 'pymongo>=3.4'
"${PYBIN}/pip" install 'pymongo>=4'
for TEST_FILE in "${BSONJS_SOURCE_DIRECTORY}"/test/test_*.py; do
"${PYBIN}/python" "$TEST_FILE" -v
done
Expand Down
8 changes: 4 additions & 4 deletions test/test_bsonjs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@

def to_object(bson_bytes):
"""Return deserialized object from BSON bytes"""
return bson.BSON(bson_bytes).decode(CodecOptions(document_class=SON,
tz_aware=True,
uuid_representation=UuidRepresentation.PYTHON_LEGACY))
return bson.decode(bson_bytes, CodecOptions(document_class=SON,
tz_aware=True,
uuid_representation=UuidRepresentation.PYTHON_LEGACY))


def to_bson(obj):
"""Return serialized BSON string from object"""
return bson.BSON.encode(obj, codec_options=CodecOptions(
return bson.encode(obj, codec_options=CodecOptions(
uuid_representation=UuidRepresentation.PYTHON_LEGACY))


Expand Down