Performance Testing

How to perform Load Test on REDIS with Python3

Manoj Singh

--

This article gives a brief introduction to how you can load test REDIS in Python using an open-source tool called Locust.io. This article preassumes that you have a basic understanding of REDIS and python as a programming language.

To learn about REDIS, you can browse through some online tutorials/documentation. To understand the capability of Locust as a tool for load tests you can read its documents and implementations.

To load the test Redis server, first, we need to set it up locally and then we will move on to python code. Commands used here are with respect to macOS this may change depending on the OS of your system.

Set up Redis Server

This guide shows you how to install Redis on macOS using Homebrew. Homebrew is the easiest way to install Redis on macOS.

Prerequisites

First, make sure you have Homebrew installed. From the terminal, run:
$ brew --version

If this command fails, you’ll need to follow the Homebrew installation instructions.

Installation

From the terminal, run:
$ brew install redis
This will install Redis on your system.

Starting and stopping Redis in the foreground

To test your Redis installation, you can run the redis-server executable from the command line:
$ redis-server
If successful, you’ll see the startup logs for Redis, and Redis will be running in the foreground.
To stop Redis, enter Ctrl-C.

Starting and stopping Redis in the background

  1. Using launchd
    As an alternative to running Redis in the foreground, you can also use launchd it to start the process in the background:
    $ brew services start redis
    This launches Redis and restarts it at login. You can check the status of a launchd managed Redis by running the following:
    $ brew services info redis
    To stop the service, run:
    $ brew services stop redis
  2. Using Configuration File
    Redis is highly configurable. While it runs fine out of the box, let’s take a minute to set some bare-bones configuration options that relate to database persistence and basic security.
    $ sudo su root
    $ mkdir -p /etc/redis/
    $ touch /etc/redis/6379.conf
    Now, write the following to /etc/redis/6379.conf. We’ll cover what most of these mean gradually throughout the tutorial:
# /etc/redis/6379.conf
port 6379
daemonize yes
save 60 1
bind 127.0.0.1
tcp-keepalive 300
dbfilename dump.rdb
dir ./
rdbcompression yes

We set the daemonize configuration option to yes, so the server runs in the background. (Otherwise, use --daemonize yes it as an option to redis-server.) Now to start the server in the background run the following command.

$ redis-server /etc/redis/6379.conf

Now you’re ready to launch the Redis REPL. Enter redis-cli on your command line. You’ll see the server’s host: port pair followed by a > prompt:

Preparing Load Test Client

I have already published the client code in the git repository.
You can either download the file redis_locust_load_test_clients.py or clone the repository using the below URLs under the repository
performance-test/redis_load_test.

Dowload url: https://github.com/ManojSingh0302/performance-testClone url: https://github.com/ManojSingh0302/performance-test.git

Source Code

Following is the source code of the same.

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.8 and follow the below steps.

----- For creating Virtual environment -----
$ python -m virtualenv venv
$ source venv/bin/activate
----- For Installing dependencies -----
Note: Please use requirements.txt inside redis_load_test directory
$ pip install -r requirements.txt

Once all the dependencies are installed, we now need to load test the Redis server and to do this, we have the locust code in redis_load_test/redis_locust_load_test_clients.py

Running the Load

Locust in Headless Mode

To run locust from the command line, without web-UI, run the following command.

locust -f redis_load_test/redis_locust_load_test_clients.py — headless -u <no-of-users> -r <hatch/second> -t <stop-time>

e.g. locust -f redis_load_test/redis_locust_load_test_clients.py --headless -u 1 -r 1 -t 10s

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
--tag,
provide the tag name of task you want to run and if not provided then it will run for all task irrespective of the tags.
e.g. locust -f redis_load_test/redis_locust_load_test_clients.py --headless -u 1 -r 1 -t 10s --tags hash

Test Results

When you run the scripts with the above command, you will get test results similar to the below screen.

Execution Result

Locust can be run in different modes for their load execution, below we have two other modes you can check out.

Running locust in Master-Slave Mode

Please refer to the link https://manojsingh0302.medium.com/how-to-load-test-rest-apis-with-locust-io-in-python-59f5fb430d73

Locust in Web mode

Please refer to the link https://manojsingh0302.medium.com/how-to-load-test-rest-apis-with-locust-io-in-python-59f5fb430d73

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

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