Skip to content

Commit 32a2e6b

Browse files
ktosiekCito
authored andcommitted
Add pytest-benchmark and port the benchmarks from GraphQL.js (#55)
1 parent 4a6b7f1 commit 32a2e6b

14 files changed

+70116
-67
lines changed

poetry.lock

+22-65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ python = "^3.6"
3333
[tool.poetry.dev-dependencies]
3434
pytest = "^5"
3535
pytest-asyncio = ">=0.10"
36+
pytest-benchmark = "^3.2"
3637
pytest-cov = "^2.7"
3738
pytest-describe = ">=0.12"
3839
pyyaml = "^5.1"

setup.cfg

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ python-tag = py3
33

44
[aliases]
55
test = pytest
6+
7+
[tool:pytest]
8+
# Only run benchmarks as tests.
9+
# To actually run the benchmarks, use --benchmark-enable on the command line.
10+
addopts = --benchmark-disable

tests/benchmarks/__init__.py

Whitespace-only changes.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from graphql import parse, build_ast_schema, GraphQLSchema
2+
3+
# noinspection PyUnresolvedReferences
4+
from ..fixtures import big_schema_sdl # noqa: F401
5+
6+
7+
def test_build_schema_from_ast(benchmark, big_schema_sdl): # noqa: F811
8+
schema_ast = parse(big_schema_sdl)
9+
schema: GraphQLSchema = benchmark(
10+
lambda: build_ast_schema(schema_ast, assume_valid=True)
11+
)
12+
assert schema.query_type is not None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from graphql import build_client_schema, GraphQLSchema
2+
3+
# noinspection PyUnresolvedReferences
4+
from ..fixtures import big_schema_introspection_result # noqa: F401
5+
6+
7+
def test_build_schema_from_introspection(
8+
benchmark, big_schema_introspection_result # noqa: F811
9+
):
10+
schema: GraphQLSchema = benchmark(
11+
lambda: build_client_schema(
12+
big_schema_introspection_result["data"], assume_valid=True
13+
)
14+
)
15+
assert schema.query_type is not None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from graphql import build_schema, parse, execute
2+
from graphql.utilities import introspection_query
3+
4+
# noinspection PyUnresolvedReferences
5+
from ..fixtures import big_schema_sdl # noqa: F401
6+
7+
8+
def test_execute_introspection_query(benchmark, big_schema_sdl): # noqa: F811
9+
schema = build_schema(big_schema_sdl, assume_valid=True)
10+
query = parse(introspection_query.get_introspection_query())
11+
result = benchmark(lambda: execute(schema, query))
12+
assert result.errors is None

tests/benchmarks/test_parser.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from graphql import parse, DocumentNode
2+
3+
# noinspection PyUnresolvedReferences
4+
from ..fixtures import kitchen_sink_query # noqa: F401
5+
6+
7+
def test_parse_kitchen_sink(benchmark, kitchen_sink_query): # noqa: F811
8+
query = benchmark(lambda: parse(kitchen_sink_query))
9+
assert isinstance(query, DocumentNode)

tests/benchmarks/test_validate_gql.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from graphql import build_schema, parse, validate
2+
from graphql.utilities import introspection_query
3+
4+
# noinspection PyUnresolvedReferences
5+
from ..fixtures import big_schema_sdl # noqa: F401
6+
7+
8+
def test_validate_introspection_query(benchmark, big_schema_sdl): # noqa: F811
9+
schema = build_schema(big_schema_sdl, assume_valid=True)
10+
query = parse(introspection_query.get_introspection_query())
11+
result = benchmark(lambda: validate(schema, query))
12+
assert result == []

tests/benchmarks/test_validate_sdl.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from graphql import parse
2+
from graphql.validation.validate import validate_sdl
3+
4+
# noinspection PyUnresolvedReferences
5+
from ..fixtures import big_schema_sdl # noqa: F401
6+
7+
8+
def test_validate_sdl_document(benchmark, big_schema_sdl): # noqa: F811
9+
sdl_ast = parse(big_schema_sdl)
10+
result = benchmark(lambda: validate_sdl(sdl_ast))
11+
assert result == []

tests/fixtures/__init__.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
"""Fixtures for graphql tests"""
2-
2+
import json
33
from os.path import dirname, join
44

55
from pytest import fixture # type: ignore
66

7-
__all__ = ["kitchen_sink_query", "kitchen_sink_sdl"]
7+
__all__ = [
8+
"kitchen_sink_query",
9+
"kitchen_sink_sdl",
10+
"big_schema_sdl",
11+
"big_schema_introspection_result",
12+
]
813

914

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

1419

20+
def read_json(name):
21+
path = join(dirname(__file__), name + ".json")
22+
return json.load(open(path, encoding="utf-8"))
23+
24+
1525
@fixture(scope="module")
1626
def kitchen_sink_query():
1727
return read_graphql("kitchen_sink")
@@ -20,3 +30,13 @@ def kitchen_sink_query():
2030
@fixture(scope="module")
2131
def kitchen_sink_sdl():
2232
return read_graphql("schema_kitchen_sink")
33+
34+
35+
@fixture(scope="module")
36+
def big_schema_sdl():
37+
return read_graphql("github_schema")
38+
39+
40+
@fixture(scope="module")
41+
def big_schema_introspection_result():
42+
return read_json("github_schema")

0 commit comments

Comments
 (0)