Added simple benchmarks

This commit is contained in:
Syrus Akbary 2016-08-17 21:26:05 -07:00
parent bf5e0e2881
commit 0f76e8f817
4 changed files with 126 additions and 2 deletions

View File

@ -23,7 +23,7 @@ before_install:
install:
- |
if [ "$TEST_TYPE" = build ]; then
pip install pytest pytest-cov coveralls six
pip install pytest pytest-cov pytest-benchmark coveralls six
pip install -e .
python setup.py develop
elif [ "$TEST_TYPE" = lint ]; then

View File

@ -153,3 +153,9 @@ def test_objecttype_as_container_invalid_kwargs():
Container(unexisting_field="3")
assert "'unexisting_field' is an invalid keyword argument for Container" == str(excinfo.value)
def test_objecttype_container_benchmark(benchmark):
@benchmark
def create_objecttype():
Container(field1='field1', field2='field2')

View File

@ -1,8 +1,11 @@
from functools import partial
from graphql import execute, Source, parse
from ..objecttype import ObjectType
from ..scalars import String
from ..scalars import String, Int
from ..schema import Schema
from ..structures import List
def test_query():
@ -50,3 +53,117 @@ def test_query_middlewares():
executed = hello_schema.execute('{ hello, other }')
assert not executed.errors
assert executed.data == {'hello': 'dlroW', 'other': 'rehto'}
def test_big_list_query_benchmark(benchmark):
big_list = range(10000)
class Query(ObjectType):
all_ints = List(Int)
def resolve_all_ints(self, args, context, info):
return big_list
hello_schema = Schema(Query)
big_list_query = partial(hello_schema.execute, '{ allInts }')
result = benchmark(big_list_query)
assert not result.errors
assert result.data == {'allInts': big_list}
def test_big_list_query_compiled_query_benchmark(benchmark):
big_list = range(10000)
class Query(ObjectType):
all_ints = List(Int)
def resolve_all_ints(self, args, context, info):
return big_list
hello_schema = Schema(Query)
source = Source('{ allInts }')
query_ast = parse(source)
big_list_query = partial(execute, hello_schema, query_ast)
result = benchmark(big_list_query)
assert not result.errors
assert result.data == {'allInts': big_list}
def test_big_list_of_containers_query_benchmark(benchmark):
big_list = range(10000)
class Container(ObjectType):
x = Int()
class Query(ObjectType):
all_containers = List(Container)
def resolve_all_containers(self, args, context, info):
return (Container(x=x) for x in big_list)
hello_schema = Schema(Query)
big_list_query = partial(hello_schema.execute, '{ allContainers { x } }')
result = benchmark(big_list_query)
assert not result.errors
assert result.data == {'allContainers': [{'x': x} for x in big_list]}
def test_big_list_of_containers_multiple_fields_query_benchmark(benchmark):
big_list = range(10000)
class Container(ObjectType):
x = Int()
y = Int()
z = Int()
o = Int()
class Query(ObjectType):
all_containers = List(Container)
def resolve_all_containers(self, args, context, info):
return (Container(x=x, y=x, z=x, o=x) for x in big_list)
hello_schema = Schema(Query)
big_list_query = partial(hello_schema.execute, '{ allContainers { x, y, z, o } }')
result = benchmark(big_list_query)
assert not result.errors
assert result.data == {'allContainers': [{'x': x, 'y':x, 'z':x, 'o': x} for x in big_list]}
def test_big_list_of_containers_multiple_fields_custom_resolvers_query_benchmark(benchmark):
big_list = range(10000)
class Container(ObjectType):
x = Int()
y = Int()
z = Int()
o = Int()
def resolve_x(self, args, context, info):
return self.x
def resolve_y(self, args, context, info):
return self.y
def resolve_z(self, args, context, info):
return self.z
def resolve_o(self, args, context, info):
return self.o
class Query(ObjectType):
all_containers = List(Container)
def resolve_all_containers(self, args, context, info):
return (Container(x=x, y=x, z=x, o=x) for x in big_list)
hello_schema = Schema(Query)
big_list_query = partial(hello_schema.execute, '{ allContainers { x, y, z, o } }')
result = benchmark(big_list_query)
assert not result.errors
assert result.data == {'allContainers': [{'x': x, 'y':x, 'z':x, 'o': x} for x in big_list]}

View File

@ -76,6 +76,7 @@ setup(
],
tests_require=[
'pytest>=2.7.2',
'pytest-benchmark',
'mock',
],
extras_require={