diff --git a/.travis.yml b/.travis.yml index 4aa9b7a1..989680ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,7 +21,7 @@ before_install: source "$HOME/virtualenvs/pypy-$PYPY_VERSION/bin/activate" fi install: -- pip install pytest-cov pytest-mock coveralls flake8 isort==3.9.6 gevent==1.1b5 six>=1.10.0 promise>=0.4.2 pytest-benchmark +- pip install pytest-cov pytest-mock coveralls flake8 gevent==1.1b5 six>=1.10.0 promise>=0.4.2 pytest-benchmark - pip install pytest==2.9.2 - pip install -e . script: @@ -36,5 +36,4 @@ matrix: - pip install pytest-asyncio script: - flake8 - - isort --check-only graphql/ -rc - py.test --cov=graphql graphql tests tests_py35 diff --git a/graphql/execution/tests/test_benchmark.py b/graphql/execution/tests/test_benchmark.py index 5eedb5c5..7e11d89f 100644 --- a/graphql/execution/tests/test_benchmark.py +++ b/graphql/execution/tests/test_benchmark.py @@ -5,12 +5,48 @@ GraphQLSchema, Source, execute, parse) -def test_big_list_of_ints(benchmark): - big_int_list = [x for x in range(5000)] +# set global fixtures +Container = namedtuple('Container', 'x y z o') +big_int_list = [x for x in range(5000)] +big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)] + +ContainerType = GraphQLObjectType('Container', + fields={ + 'x': GraphQLField(GraphQLInt), + 'y': GraphQLField(GraphQLInt), + 'z': GraphQLField(GraphQLInt), + 'o': GraphQLField(GraphQLInt), + }) + + +def resolve_all_containers(root, args, context, info): + return big_container_list - def resolve_all_ints(root, args, context, info): - return big_int_list +def resolve_all_ints(root, args, context, info): + return big_int_list + + +def test_big_list_of_ints_to_graphql_schema(benchmark): + @benchmark + def schema(): + Query = GraphQLObjectType('Query', fields={ + 'allInts': GraphQLField( + GraphQLList(GraphQLInt), + resolver=resolve_all_ints + ) + }) + return GraphQLSchema(Query) + + +def test_big_list_of_ints_to_graphql_ast(benchmark): + @benchmark + def ast(): + source = Source('{ allInts }') + return parse(source) + + +def test_big_list_of_ints_to_graphql_partial(benchmark): Query = GraphQLObjectType('Query', fields={ 'allInts': GraphQLField( GraphQLList(GraphQLInt), @@ -20,43 +56,55 @@ def resolve_all_ints(root, args, context, info): hello_schema = GraphQLSchema(Query) source = Source('{ allInts }') ast = parse(source) - big_list_query = partial(execute, hello_schema, ast) - result = benchmark(big_list_query) - # result = big_list_query() - assert not result.errors - assert result.data == {'allInts': big_int_list} + @benchmark + def b(): + return partial(execute, hello_schema, ast) + +def test_big_list_of_ints_to_graphql_total(benchmark): + @benchmark + def total(): + Query = GraphQLObjectType('Query', fields={ + 'allInts': GraphQLField( + GraphQLList(GraphQLInt), + resolver=resolve_all_ints + ) + }) + hello_schema = GraphQLSchema(Query) + source = Source('{ allInts }') + ast = parse(source) + return partial(execute, hello_schema, ast) -def test_big_list_of_ints_base(benchmark): - big_int_list = [x for x in range(5000)] +def test_big_list_of_ints_base_serialize(benchmark): from ..executor import complete_leaf_value - # def convert_item(i): - # return i - def convert_list(): - r = [] + @benchmark + def serialize(): for i in big_int_list: - r.append(GraphQLInt.serialize(i)) - return r - benchmark(convert_list) + GraphQLInt.serialize(i) -def test_big_list_of_containers_with_one_field(benchmark): - Container = namedtuple('Container', 'x y z o') +def test_total_big_list_of_containers_with_one_field_schema(benchmark): + @benchmark + def schema(): + Query = GraphQLObjectType('Query', fields={ + 'allContainers': GraphQLField( + GraphQLList(ContainerType), + resolver=resolve_all_containers + ) + }) + return GraphQLSchema(Query) - ContainerType = GraphQLObjectType('Container', fields={ - 'x': GraphQLField(GraphQLInt), - 'y': GraphQLField(GraphQLInt), - 'z': GraphQLField(GraphQLInt), - 'o': GraphQLField(GraphQLInt), - }) - big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)] +def test_total_big_list_of_containers_with_one_field_parse(benchmark): + @benchmark + def ast(): + source = Source('{ allContainers { x } }') + ast = parse(source) - def resolve_all_containers(root, args, context, info): - return big_container_list +def test_total_big_list_of_containers_with_one_field_partial(benchmark): Query = GraphQLObjectType('Query', fields={ 'allContainers': GraphQLField( GraphQLList(ContainerType), @@ -66,39 +114,55 @@ def resolve_all_containers(root, args, context, info): hello_schema = GraphQLSchema(Query) source = Source('{ allContainers { x } }') ast = parse(source) - big_list_query = partial(execute, hello_schema, ast) - result = benchmark(big_list_query) - # result = big_list_query() - assert not result.errors - assert result.data == {'allContainers': [{'x': c.x} for c in big_container_list]} + @benchmark + def b(): + return partial(execute, hello_schema, ast) -def test_big_list_of_containers_with_multiple_fields(benchmark): - Container = namedtuple('Container', 'x y z o') - ContainerType = GraphQLObjectType('Container', fields={ - 'x': GraphQLField(GraphQLInt), - 'y': GraphQLField(GraphQLInt), - 'z': GraphQLField(GraphQLInt), - 'o': GraphQLField(GraphQLInt), - }) +def test_total_big_list_of_containers_with_one_field_total(benchmark): + @benchmark + def total(): + Query = GraphQLObjectType('Query', fields={ + 'allContainers': GraphQLField( + GraphQLList(ContainerType), + resolver=resolve_all_containers + ) + }) + hello_schema = GraphQLSchema(Query) + source = Source('{ allContainers { x } }') + ast = parse(source) + result = partial(execute, hello_schema, ast) - big_container_list = [Container(x=x, y=x, z=x, o=x) for x in range(5000)] - - def resolve_all_containers(root, args, context, info): - return big_container_list +def test_total_big_list_of_containers_with_multiple_fields_partial(benchmark): Query = GraphQLObjectType('Query', fields={ 'allContainers': GraphQLField( GraphQLList(ContainerType), resolver=resolve_all_containers ) }) + hello_schema = GraphQLSchema(Query) source = Source('{ allContainers { x, y, z } }') ast = parse(source) - big_list_query = partial(execute, hello_schema, ast) - result = benchmark(big_list_query) - # result = big_list_query() - assert not result.errors - assert result.data == {'allContainers': [{'x': c.x, 'y': c.y, 'z': c.z} for c in big_container_list]} + + @benchmark + def b(): + return partial(execute, hello_schema, ast) + + +def test_total_big_list_of_containers_with_multiple_fields(benchmark): + @benchmark + def total(): + Query = GraphQLObjectType('Query', fields={ + 'allContainers': GraphQLField( + GraphQLList(ContainerType), + resolver=resolve_all_containers + ) + }) + + hello_schema = GraphQLSchema(Query) + source = Source('{ allContainers { x, y, z } }') + ast = parse(source) + result = partial(execute, hello_schema, ast) diff --git a/setup.py b/setup.py index bc0cd357..2e4d3383 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ from setuptools import setup, find_packages +from setuptools.command.test import test as TestCommand import sys if sys.version_info[0] < 3: @@ -12,10 +13,39 @@ # the numpy distutils extensions that are used by scikit-learn to recursively # build the compiled extensions in sub-packages is based on the Python import # machinery. -builtins.__GRAPHQL_SETUP__ = True +if 'test' not in sys.argv: + builtins.__GRAPHQL_SETUP__ = True version = __import__('graphql').get_version() +install_requires = [ + 'six>=1.10.0', + 'promise>=0.4.2' +] + +tests_requires = [ + 'pytest==3.0.2', + 'pytest-django==2.9.1', + 'pytest-cov==2.3.1', + 'gevent==1.1rc1', + 'six>=1.10.0', + 'pytest-benchmark==3.0.0', + 'pytest-mock==1.2', +] + +class PyTest(TestCommand): + def finalize_options(self): + TestCommand.finalize_options(self) + self.test_args = ['graphql', '-vrsx'] + self.test_suite = True + + def run_tests(self): + #import here, cause outside the eggs aren't loaded + import pytest + errno = pytest.main(self.test_args) + sys.exit(errno) + + setup( name='graphql-core', version=version, @@ -43,18 +73,9 @@ keywords='api graphql protocol rest', packages=find_packages(exclude=['tests', 'tests_py35']), - install_requires=[ - 'six>=1.10.0', - 'promise>=0.4.2' - ], - tests_require=[ - 'pytest==2.9.2', - 'pytest-benchmark', - 'pytest-cov', - 'gevent==1.1rc1', - 'six>=1.10.0', - 'pytest-mock' - ], + install_requires=install_requires, + tests_require=tests_requires, + cmdclass = {'test': PyTest}, extras_require={ 'gevent': [ 'gevent==1.1rc1'