From c2b48a551f5a663c42614ee2a31ddf44eb75be0f Mon Sep 17 00:00:00 2001 From: Madhur Date: Sun, 17 Dec 2023 10:23:25 +0530 Subject: [PATCH] feat: change redis to redis-py-cluster --- Scripts/.venv | 1 + Scripts/redis.json | 5 +++++ Scripts/redis_get_set.py | 13 +++++++------ Scripts/redis_read.py | 15 ++++++++------- Scripts/redis_set.py | 11 +++++------ Scripts/requirements.txt | 29 +++++++++++++++++++++++++++++ Scripts/requirments.txt | 2 +- 7 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 Scripts/.venv create mode 100644 Scripts/redis.json create mode 100644 Scripts/requirements.txt diff --git a/Scripts/.venv b/Scripts/.venv new file mode 100644 index 0000000..8b3b34b --- /dev/null +++ b/Scripts/.venv @@ -0,0 +1 @@ +Scripts-clyv diff --git a/Scripts/redis.json b/Scripts/redis.json new file mode 100644 index 0000000..ef89f6a --- /dev/null +++ b/Scripts/redis.json @@ -0,0 +1,5 @@ +{ + "redis_host": "127.0.0.1", + "redis_port": "6379", + "redis_password": "" +} diff --git a/Scripts/redis_get_set.py b/Scripts/redis_get_set.py index 7f09ad6..7b0171d 100755 --- a/Scripts/redis_get_set.py +++ b/Scripts/redis_get_set.py @@ -11,7 +11,7 @@ import json import time from locust import User, events, TaskSet, task, constant -import redis +from rediscluster import RedisCluster import gevent.monkey gevent.monkey.patch_all() @@ -30,7 +30,8 @@ def load_config(filepath): class RedisClient(object): def __init__(self, host=configs["redis_host"], port=configs["redis_port"], password=configs["redis_password"]): - self.rc = redis.StrictRedis(host=host, port=port, password=password) + startup_nodes = [{"host": configs["redis_host"], "port": configs["redis_port"]}] + self.rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) def query(self, key, command='GET'): """Function to Test GET operation on Redis""" @@ -42,12 +43,12 @@ def query(self, key, command='GET'): result = '' except Exception as e: total_time = int((time.time() - start_time) * 1000) - events.request_failure.fire( + events.request.fire( request_type=command, name=key, response_time=total_time, exception=e) else: total_time = int((time.time() - start_time) * 1000) length = len(result) - events.request_success.fire( + events.request.fire( request_type=command, name=key, response_time=total_time, response_length=length) return result @@ -61,12 +62,12 @@ def write(self, key, value, command='SET'): result = '' except Exception as e: total_time = int((time.time() - start_time) * 1000) - events.request_failure.fire( + events.request.fire( request_type=command, name=key, response_time=total_time, exception=e) else: total_time = int((time.time() - start_time) * 1000) length = 1 - events.request_success.fire( + events.request.fire( request_type=command, name=key, response_time=total_time, response_length=length) return result diff --git a/Scripts/redis_read.py b/Scripts/redis_read.py index 723ba18..9ac407b 100755 --- a/Scripts/redis_read.py +++ b/Scripts/redis_read.py @@ -9,9 +9,9 @@ import json import time -from locust import Locust, events -from locust.core import TaskSet, task -import redis +from locust import User, events +from locust import TaskSet, task +from rediscluster import RedisCluster import gevent.monkey gevent.monkey.patch_all() @@ -27,7 +27,8 @@ def load_config(filepath): class RedisClient(object): def __init__(self, host=configs["redis_host"], port=configs["redis_port"]): - self.rc = redis.StrictRedis(host=host, port=port) + startup_nodes = [{"host": configs["redis_host"], "port": configs["redis_port"]}] + self.rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) print(host, port) def query(self, key, command='GET'): @@ -38,13 +39,13 @@ def query(self, key, command='GET'): total_time = int((time.time() - start_time) *1000000) if not result: result = '' - events.request_failure.fire(request_type=command, name=key, response_time=total_time, exception="Error") + events.request.fire(request_type=command, name=key, response_time=total_time, exception="Error") else: length = len(result) - events.request_success.fire(request_type=command, name=key, response_time=total_time, response_length=length) + events.request.fire(request_type=command, name=key, response_time=total_time, response_length=length) return result -class RedisLocust(Locust): +class RedisLocust(User): def __init__(self, *args, **kwargs): super(RedisLocust, self).__init__(*args, **kwargs) self.client = RedisClient() diff --git a/Scripts/redis_set.py b/Scripts/redis_set.py index 9898615..a93ecf9 100755 --- a/Scripts/redis_set.py +++ b/Scripts/redis_set.py @@ -8,7 +8,7 @@ import argparse import json -import redis +from rediscluster import RedisCluster def load_config(filepath): """For loading the connection details of Redis""" @@ -19,16 +19,15 @@ def load_config(filepath): def redis_populate(filepath): """Function to populate keys in Redis Server""" configs = load_config(filepath) - client = redis.StrictRedis(host=configs["redis_host"], port=configs["redis_port"]) + startup_nodes = [{"host": configs["redis_host"], "port": configs["redis_port"]}] + rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True) for i in range(100000): key='key'+str(i) value='value'+str(i) - client.set(key,value) + rc.set(key,value) print(key,value) if __name__ == "__main__": parser = argparse.ArgumentParser("Redis Performance Testing") parser.add_argument("--filepath", help="Path of the Json File(Default is redis.json)") - args = parser.parse_args() - if args.filepath is not None: - redis_populate(args.filepath) + redis_populate("redis.json") diff --git a/Scripts/requirements.txt b/Scripts/requirements.txt new file mode 100644 index 0000000..2562132 --- /dev/null +++ b/Scripts/requirements.txt @@ -0,0 +1,29 @@ +blinker==1.7.0 +Brotli==1.1.0 +certifi==2023.11.17 +charset-normalizer==3.3.2 +click==8.1.7 +ConfigArgParse==1.7 +Flask==3.0.0 +Flask-BasicAuth==0.2.0 +Flask-Cors==4.0.0 +gevent==23.9.1 +geventhttpclient==2.0.11 +greenlet==3.0.2 +idna==3.6 +itsdangerous==2.1.2 +Jinja2==3.1.2 +locust==2.19.1 +MarkupSafe==2.1.3 +msgpack==1.0.7 +psutil==5.9.6 +pyzmq==25.1.2 +redis==3.5.3 +redis-py-cluster==2.1.3 +requests==2.31.0 +roundrobin==0.0.4 +six==1.16.0 +urllib3==2.1.0 +Werkzeug==3.0.1 +zope.event==5.0 +zope.interface==6.1 diff --git a/Scripts/requirments.txt b/Scripts/requirments.txt index 5405703..546a613 100644 --- a/Scripts/requirments.txt +++ b/Scripts/requirments.txt @@ -1,3 +1,3 @@ locust argparse -redis +redis-py-cluster