Skip to content

Commit

Permalink
feat: change redis to redis-py-cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
madhurgames committed Dec 17, 2023
1 parent 1d238c7 commit c2b48a5
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
1 change: 1 addition & 0 deletions Scripts/.venv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Scripts-clyv
5 changes: 5 additions & 0 deletions Scripts/redis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"redis_host": "127.0.0.1",
"redis_port": "6379",
"redis_password": ""
}
13 changes: 7 additions & 6 deletions Scripts/redis_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand 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"""
Expand All @@ -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

Expand All @@ -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

Expand Down
15 changes: 8 additions & 7 deletions Scripts/redis_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand 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'):
Expand All @@ -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()
Expand Down
11 changes: 5 additions & 6 deletions Scripts/redis_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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")
29 changes: 29 additions & 0 deletions Scripts/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion Scripts/requirments.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locust
argparse
redis
redis-py-cluster

0 comments on commit c2b48a5

Please sign in to comment.