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.

Results

I’ll start with the results as they are probably the most interesting thing.

For a quick overview, see the charts below.

Performance results for small message with integers

Performance results for small message with mixed data types

Performance results for big message

For details, click here to view raw data

API Type Message Time [ms]
gRPC small-ints 2505
gRPC-CodeFirst small-ints 3080
System.Text.Json small-ints 3589
Newtonsoft.Json small-ints 3488
gRPC small-mixed 1982
gRPC-CodeFirst small-mixed 2770
System.Text.Json small-mixed 3825
Newtonsoft.Json small-mixed 3625
gRPC big-list 2813
gRPC-CodeFirst big-list 4678
System.Text.Json big-list 5338
Newtonsoft.Json big-list 38329

API types

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

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)

Test procedure

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

Conclusions

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.


Paweł Kmieć

Software jack of all trades, tech enthusiast, working ever since with .NET web stack and related stuff