GraphQL GET response time is slow when comparing t

2020-03-21 09:33发布

I wanted to test the response times of a GraphQL endpoint, and a RESTful endpoint as I haven't ever used GraphQL before, and I am about to use it in my next Laravel project.

So I am using Lighthouse PHP package to serve a GraphQL endpoint from my Laravel app, and also I have created a RESTful endpoint.

Both endpoints(GraphQL and RESTful) are intended to get all Users(250 users) from my local Database.

So based on the test what I have noticed here is that, when I tested this both endpoints on Postman, the RESTful endpoint response is faster than GraphQL endpoint.

Can I know why GraphQL endpoint's response takes more time than RESTful while both endpoints are getting same data?

GraphQL endpoint result for GET request (response time: 88ms) enter image description here

GraphQL endpoint result for POST request (response time: 88ms) enter image description here

RESTful endpoint result (response time: 44ms) enter image description here

3条回答
甜甜的少女心
2楼-- · 2020-03-21 10:14

TLDR: Your REST example is simple and less complicated

In Lighthouse it is creating a AST for parsing the graphql request and your schema. It then afterwards passes all the directives and so on to figure out what you are trying to do. It also has to validate your query, to see if you can actually run it on the schema.

Depending on how you defined it in your application, there is a lot of steps it is passing through. However this can be reduced by multiple different ways, the parsing of your graphql schema can be cached, you could cache the result, use deferred fields (prob. wont speed up this example). You can read more about this in the performance section of the docs.

You are not specifying how your REST is setup, if you are using some kind of REST standard where it has to parse the data also. If you add more features, there is more code to run through, hence higher load speed.

查看更多
戒情不戒烟
3楼-- · 2020-03-21 10:16

As of Lighthouse v4, we have made significant performance increases by lazy-loading the minimally required fields and types from the schema. That turns out to bring about a 3x to 10x performance increase, depending on the size of your schema.

You probably still won't beat a single REST endpoint for such a simple query. Lighthouse will begin to shine on more heavily nested queries that join across multiple relationships.

查看更多
甜甜的少女心
4楼-- · 2020-03-21 10:30

There's no such thing as a free lunch.

GraphQL offers a lot of useful features, but those same features invariably incur some overhead. While a REST endpoint can effectively pull data from some source and regurgitate it back to the client, even for a relatively small dataset, GraphQL will have to do some additional processing to resolve and validate each individual field in the response. Not to mention the processing required to parse and validate the request itself. And this overhead only gets bigger with the size of the data returned.

If you were to introduce additional features to your REST endpoint (request and response validation, support for partial responses, ability to alias individual response fields, etc.) that mirrored GraphQL, you would see the performance gap between the two shrink. Even then, though, it's still somewhat of an apples and oranges comparison, since a GraphQL service will go through certain motions simply because that's what the spec says to do.

查看更多
登录 后发表回答