Skip to content
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

Add pyserde to benchmark #361

Merged
merged 1 commit into from
Feb 22, 2022
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
65 changes: 65 additions & 0 deletions benchmark/benchmarks/serde.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from dataclasses import dataclass
from datetime import datetime
from typing import Optional

import serde
from common import Benchmark, Methods, Payment
from serde.core import SETTINGS

SETTINGS["debug"] = True


@serde.serde
@dataclass(frozen=True)
class Message:
title: str
body: str
addresses: Optional[list[str]] = None
persistence: Optional[int] = None


@serde.serde
@dataclass(frozen=True)
class Client:
id: int
firstName: str
lastName: str

def __post_init__(self): # The only way I've found to add constraints
if self.id < 0:
raise ValueError


@serde.serde
@dataclass(frozen=True)
class Item:
name: str
price: float
quantity: int = 1

def __post_init__(self):
if self.price < 0 or self.quantity < 1:
raise ValueError


@serde.serde
@dataclass(frozen=True)
class Receipt:
store: str
address: str
date: datetime
items: list[Item]
payment: Payment
client: Optional[Client] = None
specialOffers: Optional[float] = None

def __post_init__(self):
if self.specialOffers is not None and self.specialOffers < 0:
raise ValueError


def methods(cls: type) -> Methods:
return Methods(lambda data: serde.from_dict(cls, data), serde.to_dict)


benchmarks = Benchmark(methods(Message), methods(Receipt), "pyserde")
2 changes: 1 addition & 1 deletion benchmark/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


def time_it(func: Callable, arg: Any) -> float:
timer = timeit.Timer("func(arg)", globals=locals())
timer = timeit.Timer(lambda: func(arg))
number, _ = timer.autorange()
return min(timer.repeat(number=number)) / number

Expand Down
1 change: 1 addition & 0 deletions benchmark/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pandas==1.4.0
pydantic==1.9.0
typedload==2.15
typical==2.8.0
pyserde==0.7.0