Skip to content

Commit

Permalink
Add marshmallow for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
dkraczkowski committed Sep 1, 2023
1 parent 71848cb commit d9d1d23
Show file tree
Hide file tree
Showing 9 changed files with 213 additions and 13 deletions.
2 changes: 2 additions & 0 deletions benchmarks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ benchmark:
'poetry run python benchmarks/chili_decode.py'\
'poetry run python benchmarks/pydantic_decode.py'\
'poetry run python benchmarks/attrs_decode.py'\
'poetry run python benchmarks/marshmallow_decode.py'\
'poetry run python benchmarks/chili_encode.py'\
'poetry run python benchmarks/pydantic_encode.py'\
'poetry run python benchmarks/attrs_encode.py'\
'poetry run python benchmarks/marshmallow_encode.py'\

version:
poetry version
14 changes: 8 additions & 6 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `poetry run python benchmarks/chili_decode.py` | 249.4 ± 4.1 | 245.5 | 258.8 | 1.01 ± 0.02 |
| `poetry run python benchmarks/pydantic_decode.py` | 295.5 ± 12.5 | 287.6 | 327.1 | 1.19 ± 0.05 |
| `poetry run python benchmarks/attrs_decode.py` | 260.9 ± 8.6 | 253.2 | 283.5 | 1.05 ± 0.04 |
| `poetry run python benchmarks/chili_encode.py` | 247.8 ± 2.3 | 245.4 | 253.0 | 1.00 |
| `poetry run python benchmarks/pydantic_encode.py` | 292.4 ± 4.7 | 287.1 | 302.5 | 1.18 ± 0.02 |
| `poetry run python benchmarks/attrs_encode.py` | 258.2 ± 2.1 | 254.4 | 261.4 | 1.04 ± 0.01 |
| `poetry run python benchmarks/chili_decode.py` | 246.4 ± 1.6 | 244.8 | 249.6 | 1.00 |
| `poetry run python benchmarks/pydantic_decode.py` | 288.2 ± 1.8 | 284.5 | 291.0 | 1.17 ± 0.01 |
| `poetry run python benchmarks/attrs_decode.py` | 256.6 ± 1.8 | 253.3 | 259.5 | 1.04 ± 0.01 |
| `poetry run python benchmarks/marshmallow_decode.py` | 256.8 ± 5.4 | 252.2 | 271.0 | 1.04 ± 0.02 |
| `poetry run python benchmarks/chili_encode.py` | 248.8 ± 4.2 | 243.6 | 256.5 | 1.01 ± 0.02 |
| `poetry run python benchmarks/pydantic_encode.py` | 298.0 ± 14.4 | 288.3 | 325.7 | 1.21 ± 0.06 |
| `poetry run python benchmarks/attrs_encode.py` | 257.9 ± 3.6 | 252.6 | 263.4 | 1.05 ± 0.02 |
| `poetry run python benchmarks/marshmallow_encode.py` | 254.0 ± 2.7 | 250.9 | 260.6 | 1.03 ± 0.01 |
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/attrs_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ class Book:
),
]

encoded = unstructure(books)
print(encoded)
raw_data = unstructure(books)
print(raw_data)
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/chili_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ class Book:
),
]

encoded = encode(books, List[Book])
print(encoded)
raw_data = encode(books, List[Book])
print(raw_data)
82 changes: 82 additions & 0 deletions benchmarks/benchmarks/marshmallow_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from marshmallow import Schema, fields, post_load


class AuthorSchema(Schema):
first_name = fields.Str()
last_name = fields.Str()

@post_load
def make_author(self, data, **kwargs):
return Author(**data)


class Author:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name


class TagSchema(Schema):
name = fields.Str()

@post_load
def make_tag(self, data, **kwargs):
return Tag(**data)


class Tag:
def __init__(self, name):
self.name = name


class Book:
def __init__(self, name, author, tags, isbn=None):
self.name = name
self.author = author
self.tags = tags
self.isbn = isbn


class BookSchema(Schema):
name = fields.Str()
author = fields.Nested(AuthorSchema)
tags = fields.Nested(TagSchema, many=True)
isbn = fields.Str()

@post_load
def make_book(self, data, **kwargs):
return Book(**data)


raw_data = [
{
"name": "The Hobbit",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
},
{
"name": "The Lord of the Rings",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
},
{
"name": "The Silmarillion",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
},
{
"name": "The Children of Húrin",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
},
{
"name": "The Fall of Gondolin",
"author": {"first_name": "J.R.R.", "last_name": "Tolkien"},
"tags": [{"name": "Fantasy"}, {"name": "Adventure"}],
},
]

book_schema = BookSchema(many=True)

books = book_schema.load(raw_data)
print(books)
82 changes: 82 additions & 0 deletions benchmarks/benchmarks/marshmallow_encode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from marshmallow import Schema, fields, post_load


class AuthorSchema(Schema):
first_name = fields.Str()
last_name = fields.Str()

@post_load
def make_author(self, data, **kwargs):
return Author(**data)


class Author:
def __init__(self, first_name, last_name):
self.first_name = first_name
self.last_name = last_name


class TagSchema(Schema):
name = fields.Str()

@post_load
def make_tag(self, data, **kwargs):
return Tag(**data)


class Tag:
def __init__(self, name):
self.name = name


class Book:
def __init__(self, name, author, tags, isbn=None):
self.name = name
self.author = author
self.tags = tags
self.isbn = isbn


class BookSchema(Schema):
name = fields.Str()
author = fields.Nested(AuthorSchema)
tags = fields.Nested(TagSchema, many=True)
isbn = fields.Str()

@post_load
def make_book(self, data, **kwargs):
return Book(**data)

books = [
Book(
name="The Hobbit",
author=Author(first_name="J.R.R.", last_name="Tolkien"),
tags=[Tag(name="Fantasy"), Tag(name="Adventure")],
),
Book(
name="The Lord of the Rings",
author=Author(first_name="J.R.R.", last_name="Tolkien"),
tags=[Tag(name="Fantasy"), Tag(name="Adventure")],
),
Book(
name="The Silmarillion",
author=Author(first_name="J.R.R.", last_name="Tolkien"),
tags=[Tag(name="Fantasy"), Tag(name="Adventure")],
isbn="978-0261102736",
),
Book(
name="The Children of Húrin",
author=Author(first_name="J.R.R.", last_name="Tolkien"),
tags=[Tag(name="Fantasy"), Tag(name="Adventure")],
),
Book(
name="The Fall of Gondolin",
author=Author(first_name="J.R.R.", last_name="Tolkien"),
tags=[Tag(name="Fantasy"), Tag(name="Adventure")],
),
]

book_schema = BookSchema(many=True)

raw_data = book_schema.dump(books)
print(raw_data)
4 changes: 2 additions & 2 deletions benchmarks/benchmarks/pydantic_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ class Book(BaseModel):
),
]

encoded = [book.model_dump() for book in books]
print(encoded)
raw_data = [book.model_dump() for book in books]
print(raw_data)
33 changes: 32 additions & 1 deletion benchmarks/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benchmarks/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pydantic = "^2.3.0"
msgspec = "^0.18.2"
attrs = "^23.1.0"
cattrs = "^23.1.2"
marshmallow = "^3.20.1"


[build-system]
Expand Down

0 comments on commit d9d1d23

Please sign in to comment.