Skip to content

Commit

Permalink
perf: Add more benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <dmitry@dygalo.dev>
  • Loading branch information
Stranger6667 committed May 12, 2024
1 parent 1161847 commit 11633f1
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 35 deletions.
1 change: 1 addition & 0 deletions benches/data/bbci.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions benches/data/stripe.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions benches/data/vmware.json

Large diffs are not rendered by default.

74 changes: 40 additions & 34 deletions benches/references.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
import pytest

import schemathesis
from schemathesis.specs.openapi.references import resolve_pointer


@pytest.mark.benchmark
def test_schema_resolution():
raw_schema = {
"openapi": "3.0.0",
"info": {"title": "Example API", "description": "An API to test Schemathesis", "version": "1.0.0"},
"components": {
"schemas": {
"Node": {
"type": "object",
"required": ["child"],
"properties": {"child": {"$ref": "#/components/schemas/Node"}},
}
RECURSIVE_SCHEMA = {
"openapi": "3.0.0",
"info": {"title": "Example API", "description": "An API to test Schemathesis", "version": "1.0.0"},
"components": {
"schemas": {
"Node": {
"type": "object",
"required": ["child"],
"properties": {"child": {"$ref": "#/components/schemas/Node"}},
}
},
"paths": {
"/foo": {
"post": {
"summary": "Test",
"description": "",
"requestBody": {
"required": True,
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Node"}}},
},
"responses": {
"200": {
"description": "OK",
"content": {},
}
},
}
}
},
"paths": {
"/foo": {
"post": {
"summary": "Test",
"description": "",
"requestBody": {
"required": True,
"content": {"application/json": {"schema": {"$ref": "#/components/schemas/Node"}}},
},
"responses": {
"200": {
"description": "OK",
"content": {},
}
},
}
},
}
}
},
}

schema = schemathesis.from_dict(raw_schema)

@pytest.mark.benchmark
def test_inlining_during_resolution():
schema = schemathesis.from_dict(RECURSIVE_SCHEMA)
schema.resolver.resolve_all(
raw_schema["paths"]["/foo"]["post"]["requestBody"]["content"]["application/json"]["schema"]
RECURSIVE_SCHEMA["paths"]["/foo"]["post"]["requestBody"]["content"]["application/json"]["schema"]
)


@pytest.mark.benchmark
def test_resolve_pointer():
resolve_pointer(RECURSIVE_SCHEMA, "/paths/~1foo/post/requestBody/content/application~1json/schema")
84 changes: 84 additions & 0 deletions benches/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import json
import pathlib

import pytest

import schemathesis
import hypothesis
from hypothesis import HealthCheck, Phase, Verbosity
from schemathesis.runner import from_schema

CURRENT_DIR = pathlib.Path(__file__).parent.absolute()
CATALOG_DIR = CURRENT_DIR / "data"


def read_from_catalog(path: str):
with (CATALOG_DIR / path).open() as fd:
return json.load(fd)


# Small size (~2k lines in YAML)
BBCI = read_from_catalog("bbci.json")
BBCI_SCHEMA = schemathesis.from_dict(BBCI)
BBCI_OPERATIONS = list(BBCI_SCHEMA.get_all_operations())
# Medium size (~8k lines in YAML)
VMWARE = read_from_catalog("vmware.json")
VMWARE_SCHEMA = schemathesis.from_dict(VMWARE)
VMWARE_OPERATIONS = list(VMWARE_SCHEMA.get_all_operations())
# Large size (~92k lines in YAML)
STRIPE = read_from_catalog("stripe.json")
STRIPE_SCHEMA = schemathesis.from_dict(STRIPE)


@pytest.mark.benchmark
@pytest.mark.parametrize("raw_schema", [BBCI, VMWARE], ids=("bbci", "vmware"))
def test_get_all_operations(raw_schema):
schema = schemathesis.from_dict(raw_schema)

for _ in schema.get_all_operations():
pass


@pytest.mark.benchmark
@pytest.mark.parametrize("operations", [BBCI_OPERATIONS, VMWARE_OPERATIONS], ids=("bbci", "vmware"))
def test_as_json_schema(operations):
for operation in operations:
for parameter in operation.ok().iter_parameters():
_ = parameter.as_json_schema(operation)


@pytest.mark.benchmark
def test_events():
runner = from_schema(
BBCI_SCHEMA,
checks=(),
count_operations=False,
count_links=False,
hypothesis_settings=hypothesis.settings(
deadline=None,
database=None,
max_examples=1,
derandomize=True,
suppress_health_check=list(HealthCheck),
phases=[Phase.explicit, Phase.generate],
verbosity=Verbosity.quiet,
),
)
for _ in runner.execute():
pass


@pytest.mark.benchmark
@pytest.mark.parametrize("raw_schema", [BBCI, VMWARE, STRIPE], ids=("bbci", "vmware", "stripe"))
def test_rewritten_components(raw_schema):
schema = schemathesis.from_dict(raw_schema)

_ = schema.rewritten_components


@pytest.mark.benchmark
@pytest.mark.parametrize("raw_schema", [BBCI, VMWARE, STRIPE], ids=("bbci", "vmware", "stripe"))
def test_links_count(raw_schema):
schema = schemathesis.from_dict(raw_schema)

_ = schema.links_count
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ cov = [
"coverage[toml]>=5.3",
]
bench = [
"pytest-codspeed==2.2.0",
"pytest-codspeed==2.2.1",
]
docs = [
"sphinx",
Expand Down

0 comments on commit 11633f1

Please sign in to comment.