Home Technology Benchmarking gRPC powered micro-services

Benchmarking gRPC powered micro-services

by rakesh kumar

At BigBasket, we are in the process of breaking the monolith application into a number of Microservices. One of the most challenging parts of this process is to efficiently connect the polyglot services. We are using gRPC for high-performance communication between the services. This works beautifully but the big challenge is in benchmarking the performance of the microservices over gRPC. The objective of this post is to benchmark the communication between gRPC client & server using Apache JMeter.

About gRPC:

gRPC is a high-performance cross-platform RPC framework which helps us efficiently connect different services. Although JSON over REST is the most preferred combination today, gRPC gives a lot more efficiency when compared to textual JSON mixed with REST. The reason for this is because gRPC works on HTTP/2.0 protocol compared to the old and less efficient HTTP/1.1 protocol. gRPC allows a client application to directly call methods on a remote server application which may be running in a completely different environment and platform.

About protocol-buffers:

Protocol-buffers or simply called as protobuf is google’s platform-neutral, open source mechanism for serializing structured data for use in communications protocols. The basic step involved in working with gRPC is to define the rules of communication between client and server inside a proto file. Proto file is a ordinary text file with a .proto extension. A basic proto file looks something like this:

There are three important parts in a typical proto file: Syntax, service and message. Syntax defines the protocol buffer version to be used by both client and server.

‘syntax=proto2’ is the default protocol buffer version but it is recommended to use proto3 with gRPC because of its simplified syntax and better language support. More details about protobuf can be found at https://developers.google.com/protocol-buffers/docs/overview. We are going to use above mentioned protobuf data as a reference in our example.

Benchmarking gRPC communication using JMeter:

JMeter is a pure Java open source software, designed to load test functional behaviour and measuring the performance of a variety of services. Although JMeter comes pre-installed with cool plugins and samplers which are helpful for testing all kind of web-services, no direct plugins for gRPC applications. But the great thing about JMeter is that it allows users to use custom samplers with JMeter to perform load testing.

Below steps are required to load test a gRPC application:

  • Writing a gRPC client in Java
  • Writing a JMeter custom sampler in Java
  • Exporting project jar to JMeter external library folder
  • Using the custom Java Request sampler inside JMeter

PS: Server-side implementation of rpc is out of scope for this post.

Step 1/4: Writing gRPC client in Java

The first step of benchmarking gRPC request is to create a maven project and place the proto file in any source package. Other build tools like gradle can also be used for this. Next step is to include a protobuf compiler plugin in the project which compiles the proto file and generates data access classes in the target folder. Include following protobuf compiler plugin in pom.xml.

Once the above plugin is included in the project, build your project to generate data access classes. In the target folder, six Java classes will be generated for the given proto file. These are:

  • TestingBlockingStub.java
  • TestingGrpc.java
  • TestingRequest.java
  • TestingRequestOrBuilder.java
  • TestingResponse.java
  • TestingResponseOrBuilder.java
  • TestingServiceProto.java

Once these data access classes are generated, we can start writing our Java client inside a new class “TestingServiceClient.java”.

The first step is to create a channel. We are using “NettyChannelBuilder” for creating a ManagedChannel.
Next step is to create a blocking stub, which waits for the server to respond, and will either return a response or raise an exception.
Third step is to create custom request invoking remote methods and sending it to the server via ‘stub’ created in previous step.

The client can communicate with the server now. A public method needs to be exposed, which can be used by our JMeter sampler to send final request.

To inject desired metadata headers into request we create a custom client interceptor “HeaderClientInterceptor.java”

End result should be:

Step 2/4: Writing a Jmeter custom sampler in Java

In any source package, create a JMeter sampler “TestingServiceSampler.java” which extends apache JMeter library “AbstractJavaSamplerClient”.

Our Sampler should look like this:

Step 3/4: Exporting project jar to JMeter external library folder.

Next step is to create a runnable jar for this project and export it to external libraries of JMeter. In eclipse, right click inside project explorer, then

  1. Click “Export..”
  2. Choose “Java -> Runable Jar File”
  3. Select launch configuration and export destination(JMeter/lib/ext)
  4. Finish.

A runnable jar “filename.jar” is exported to JMeter external library folder .

Step 4/4: Using the custom Java Request sampler inside JMeter

We have now successfully created a gRPC request sampler and included it as a part of JMeter. Next step is to start JMeter application inside JMeter/bin/jmeter. Once the JMeter window opens, we can start creating test plan.

Add “User defined variable” from the list of config elements and add following key/value pairs to the User defined variables:

  • threads : ${__P(threads, 100)}
  • rampPeriod : ${__P(rampPeriod, 1)}
  • loopCount : ${__P(loopCount, 5)}
  • host : ${__P(host, abc.xyz.com)}
  • port : ${__P(port, 5556)}

Next, add a loop controller after adding a thread group with thread properties as “$threads” and “$rampPeriod”. Use user variable $loopCount inside loop controller to control number of loops.
Inside loop controller, add a new Sampler of type “Java Request”. Inside Java Request classname, choose our custom java sampler which we exported as a jar. Provide the following properties which were required by our sampler as a parameter:

  • host : ${host}
  • port : ${port}
  • varOne : “one”
  • varTwo : “two”
  • varThree : “three”

Save the test plan in any local directory and close the JMeter window.

Run the following command to start load testing for the given host.

JMeter will start sending requests to the gRPC host according to the run parameters and generate a results.jtl file for results.

Source code:
The source code of the example demo is available here:
https://github.com/rakesh822/GRPC-Benchmark.git

Summary:
The objective of this post is to give insight into how JMeter can be used to perform load testing on gRPC powered micro-services. With the server-side implementation already running on a host, this post can help people build a test plan in JMeter to perform load testing on gRPC applications.

You may also like

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: