Skip to content

Middleware overhead #423

@rapthead

Description

@rapthead

Empty middleware adds 5 times overhead.

import timeit
import graphene

class Track(graphene.ObjectType):
    track_id = graphene.ID()
    title = graphene.String()

class Album(graphene.ObjectType):
    album_id = graphene.ID()
    title = graphene.String()

    tracks = graphene.List(Track)
    @staticmethod
    def resolve_tracks(self, args, context, info):
        return [
            Track(track_id=i, title=i) for i in range(10)
        ]

class Query(graphene.ObjectType):
    albums = graphene.List(Album)

    @staticmethod
    def resolve_albums(self, args, context, info):
        return [
            Album(album_id=i, title=i) for i in range(300)
        ]

schema = graphene.Schema(query=Query)

class TestMiddleware(object):
    def resolve(self, next, root, args, context, info):
        return next(root, args, context, info)

def execute1():
    return schema.execute('''
            query {
                albums {
                    tracks {
                        title
                    }
                }
            }
        ''',
        middleware=[TestMiddleware()]
    )

def execute2():
    return schema.execute('''
            query {
                albums {
                    tracks {
                        title
                    }
                }
            }
        ''',
    )

if __name__ == '__main__':
    print('with empty middleware: ' + str(timeit.timeit(execute1, number=1)))
    print('without middleware:    ' + str(timeit.timeit(execute2, number=1)))

Outputs:

with empty middleware: 0.8273355230048764
without middleware:    0.17398804999538697

I think this is not acceptable. I found that problem at wrapping every resolve function in Promise on creating middlewares chain.
https://github.com/graphql-python/graphql-core/blob/master/graphql/execution/middleware.py#L49

At my use cases removing promise wrap from chain not breaks functionality. Even resolvers retuns Promise. Do we realy need this code?

I can make pull request If my conclusions is correct.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions