In the previous post, I have explored the new gRPC service template from Visual Studio.
However, I haven’t done any check of the performance of the gRPC, which I think is an interesting thing to look at. The performance benefit is a “selling point” of gRPC, so it will be good to verify how big the gain is.
I’ll start with the results as they are probably the most interesting thing.
For a quick overview, see the charts below.
For details, click here to view raw data
|API Type||Message||Time [ms]|
As you see I’ve decided to compare 4 variants:
- gRPC - official suite of gRPC libraries that are associated with new Visual Studio template
- gRPC-CodeFirst - protobuf-net.Grpc library, which works also for .NET Framework
- System.Text.Json - the new JSON serialization API from .NET Core 3
- Newtonsoft.Json - probably doesn’t need introduction :)
Why these 4?
Firstly, I wanted to see how gRPC performs against API based on JSON. RPC over HTTP with JSON serialization is the most prevalent thing these days.
Secondly, I also wanted to look at different implementations of both gRPC & JSON serialization, mostly out of curiosity.
Test messages differ by size and data types used within them:
- small ints - a class with a few int properties
- small-mixed - a class with a few int/string properties and a nested class
- big-list - a big list of items (1000 elements, 190kB JSON file)
In the test, I’ve done the following:
- run the API methods sequentially 1000 times on localhost, on my machine
- all APIs were using HTTPS
- all APIs were running on ASP.NET Core 3
- all gRPC calls were unary
That’s not rigid science, but gives something roughly comparable.
The charts report total runtime of all calls.
You can review benchmark code on my GitHub repo
gRPC beats JSON by quite a big margin - from 30% to 50%.
2 different gRPC flavours perform pretty close to each other. Official gRPC libraries seems to be faster.
Newtonsoft.Json seems to be having problems with the serialization of big messages, which are not present in any other tested API. Or perhaps it’s System.Text.Json that’s optimised close to the metal with all new memory-efficient framework constructs.
So, if you send large JSONs, then migrate to System.Text.Json to get something like a 6x speedup of serialization.
If you fight for milliseconds in your APIs, then switch from JSON to gRPC is worth consideration.