Skip to content

Add pytest-benchmark and port the benchmarks from GraphQL.js #55

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 8 commits into from
Sep 10, 2019
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
87 changes: 22 additions & 65 deletions 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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ python = "^3.6"
[tool.poetry.dev-dependencies]
pytest = "^5"
pytest-asyncio = ">=0.10"
pytest-benchmark = "^3.2"
pytest-cov = "^2.7"
pytest-describe = ">=0.12"
pyyaml = "^5.1"
Expand Down
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ python-tag = py3

[aliases]
test = pytest

[tool:pytest]
# Only run benchmarks as tests.
# To actually run the benchmarks, use --benchmark-enable on the command line.
addopts = --benchmark-disable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also use benchmark-skip to completely skip them, but I agree this is better. They are probably a bit redundant, but not too slow if they run only as test.

Empty file added tests/benchmarks/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions tests/benchmarks/test_build_ast_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from graphql import parse, build_ast_schema, GraphQLSchema

# noinspection PyUnresolvedReferences
from ..fixtures import big_schema_sdl # noqa: F401


def test_build_schema_from_ast(benchmark, big_schema_sdl): # noqa: F811
schema_ast = parse(big_schema_sdl)
schema: GraphQLSchema = benchmark(
lambda: build_ast_schema(schema_ast, assume_valid=True)
)
assert schema.query_type is not None
15 changes: 15 additions & 0 deletions tests/benchmarks/test_build_client_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from graphql import build_client_schema, GraphQLSchema

# noinspection PyUnresolvedReferences
from ..fixtures import big_schema_introspection_result # noqa: F401


def test_build_schema_from_introspection(
benchmark, big_schema_introspection_result # noqa: F811
):
schema: GraphQLSchema = benchmark(
lambda: build_client_schema(
big_schema_introspection_result["data"], assume_valid=True
)
)
assert schema.query_type is not None
12 changes: 12 additions & 0 deletions tests/benchmarks/test_introspection_from_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from graphql import build_schema, parse, execute
from graphql.utilities import introspection_query

# noinspection PyUnresolvedReferences
from ..fixtures import big_schema_sdl # noqa: F401


def test_execute_introspection_query(benchmark, big_schema_sdl): # noqa: F811
schema = build_schema(big_schema_sdl, assume_valid=True)
query = parse(introspection_query.get_introspection_query())
result = benchmark(lambda: execute(schema, query))
assert result.errors is None
9 changes: 9 additions & 0 deletions tests/benchmarks/test_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from graphql import parse, DocumentNode

# noinspection PyUnresolvedReferences
from ..fixtures import kitchen_sink_query # noqa: F401


def test_parse_kitchen_sink(benchmark, kitchen_sink_query): # noqa: F811
query = benchmark(lambda: parse(kitchen_sink_query))
assert isinstance(query, DocumentNode)
12 changes: 12 additions & 0 deletions tests/benchmarks/test_validate_gql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from graphql import build_schema, parse, validate
from graphql.utilities import introspection_query

# noinspection PyUnresolvedReferences
from ..fixtures import big_schema_sdl # noqa: F401


def test_validate_introspection_query(benchmark, big_schema_sdl): # noqa: F811
schema = build_schema(big_schema_sdl, assume_valid=True)
query = parse(introspection_query.get_introspection_query())
result = benchmark(lambda: validate(schema, query))
assert result == []
11 changes: 11 additions & 0 deletions tests/benchmarks/test_validate_sdl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from graphql import parse
from graphql.validation.validate import validate_sdl

# noinspection PyUnresolvedReferences
from ..fixtures import big_schema_sdl # noqa: F401


def test_validate_sdl_document(benchmark, big_schema_sdl): # noqa: F811
sdl_ast = parse(big_schema_sdl)
result = benchmark(lambda: validate_sdl(sdl_ast))
assert result == []
24 changes: 22 additions & 2 deletions tests/fixtures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
"""Fixtures for graphql tests"""

import json
from os.path import dirname, join

from pytest import fixture # type: ignore

__all__ = ["kitchen_sink_query", "kitchen_sink_sdl"]
__all__ = [
"kitchen_sink_query",
"kitchen_sink_sdl",
"big_schema_sdl",
"big_schema_introspection_result",
]


def read_graphql(name):
path = join(dirname(__file__), name + ".graphql")
return open(path, encoding="utf-8").read()


def read_json(name):
path = join(dirname(__file__), name + ".json")
return json.load(open(path, encoding="utf-8"))


@fixture(scope="module")
def kitchen_sink_query():
return read_graphql("kitchen_sink")
Expand All @@ -20,3 +30,13 @@ def kitchen_sink_query():
@fixture(scope="module")
def kitchen_sink_sdl():
return read_graphql("schema_kitchen_sink")


@fixture(scope="module")
def big_schema_sdl():
return read_graphql("github_schema")


@fixture(scope="module")
def big_schema_introspection_result():
return read_json("github_schema")
Loading