Image for post
Image for post

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

Manoj Singh
Aug 29, 2020 · 5 min read

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

Image for post
Image for post

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.

Image for post
Image for post
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.

Image for post
Image for post
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.

Image for post
Image for post

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

Image for post
Image for post
Fig: Charts

Statistics

Image for post
Image for post
Fig: Statistics

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

Image for post
Image for post
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

Image for post
Image for post
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:

The Startup

Get smarter at building your thing. Join The Startup’s +776K followers.

Manoj Singh

Written by

Computer Science Engineer with a passion for Machine Learning, AI & Data Visualization. Transforming world with data.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +776K followers.

Manoj Singh

Written by

Computer Science Engineer with a passion for Machine Learning, AI & Data Visualization. Transforming world with data.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +776K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store