How to Load Test gRPC Applications With Locust.io in Python

Manoj Singh
The Startup
Published in
5 min readAug 29, 2020

--

This article gives a brief introduction to how you can load test gRPC based applications written in any programming languages like Java or Python using an open-source tool called Locust.io. This article preassumes that you have a good understanding of gRPC service and python as a programming language. Locust doesn’t support gRPC officially but this blog will help you in implementing it when you need to load test your applications based on gRPC.

To learn about gRPC, you can browse through the official documentation. To understand the capability of Locust as a tool for load test you can read it’s documents and implementations.

Steps to Load Test

  1. The first requirement is we need to have a server that consumes gRPC services, please note that the server can be written in any programming language and it does not impact our load test model. For simplicity, I have taken a dummy `Hello world` server in python from the gRPC official code repository.
  2. Clone the code from the language of your choice, gRPC by default gives dummy server in their different examples as listed in the python repository.

We will take a basic example of HelloWorld and implement its load test in the further part of the blog. If in case you choose a server in different languages then what needs to be done here is start your server and compile your proto file in your load test repository and use the locust code as shared in the blogs.

3. To simplify even more I have prepared a code repository with all default servers in python provided by gRPC and implemented locust on HelloWorld example. Here is a link to clone the repository or download it as zip.

Clone: https://github.com/ManojSingh0302/automation.gitDowloand: https://github.com/ManojSingh0302/automation

4. Now as you have codes, we need to install few dependencies before we start execution. Open the repository in the editor of your choice and simply install it by running the below commands on the terminal.

Note: It is advised to create a virtual environment in python 3.7 and follow the below steps.

----- For creating Virtual environment -----
$ python -m virtualenv venv
$ source venv/bin/activate
----- For Installing dependencies -----$ pip install -r requirement.txt
$ pip install -e .

5. To start the server run the following command
$ python server/python/helloworld/greeter_server.py

6. To manually test the server you can run the default client by the following commands
$ python server/python/helloworld/greeter_client.py
Yeah! we got the response from the server and so the server is running successfully.

7. Now in a given scenario to test gRPC, you will be given the .proto file, from this file we need to generate the python code which we will be using in our test scripts for making a call to the server. Follow the below steps to generate compile files in python from .proto file. For understanding the repository contains a separate folder as proto which will have a proto file and compile files.

fig: Proto file

Run the following command from the project directory (grpc-load-test-with-locust).

command syntax:$ grpc_tools.protoc -I=$SRC_DIR - python_out=$DST_DIR - grpc_python_out=$SRC_DIR proto/<app-name>.protoActual command:
$ python3 -m grpc_tools.protoc -I=server/proto --python_out=server/proto --grpc_python_out=server/proto server/proto/helloworld.proto

After the successful execution of the command, the python compile files will be generated as shown below.

Fig: Python Compile files

8. For our load test, we already have compiled files in the official repository of gRPC to run from our code I have just updated the imports w.r.t our framework. Now we need to load test this server and for the job, we have the locust code inlocust/load_test_grpc.py

9. Now to Run the locust in web mode run the following command
$ locust -f locust/load_test_grpc.py

10. Now in the logs, we can observe that the Locust load test sever started on default port[8089] and we can access by URL http://localhost:8089/ in any browser.

11. Now to start the test select Number of total users to simulate along with the Hatch rate (users spawned/second) of your choice as shown below.

Note: initially select smaller number so that your system responde to the load.

12. Now Start Swarming and observe the statistics and charts, you can stop the locust at any given point.

Charts

Fig: Charts

Statistics

Fig: Statistics

It also provides you the feature to download the statistics from the load test.

Fig: Download link

Ways to run locust

To run locust from command line, without web-ui, run following command
locust -f locust/load_test_grpc.py --headless -u <no-of-users> -r <hatch/second> -t <stop-time>

e.g. locust -f locust/load_test_grpc.py --headless -u 10 -r 5 -t 3s

WHERE
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users. Only used together
with --headless
-r HATCH_RATE, --hatch-rate HATCH_RATE
The rate per second in which users are spawned. Only
used together with --headless
-t RUN_TIME, --run-time RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with
--headless

Console Output

Fig: Console output

Hurray!! We have done it. :-)

I hope this blog was useful to you. Please leave comments or send me an email if you think I missed any important details or if you have any other questions or feedback about this topic.

References:

--

--

Manoj Singh
The Startup

Computer Science Engineer working as software development engineer in test with a passion for Machine Learning, AI & Data Visualisations.