Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,28 @@ pip3 install -r requirments.txt

## Usage

The use of this utility is not a fancy thing, just need to update the **[redis.json](./Scripts/redis.json)** with your redis connection details. Content of file should be like this:-
The use of this utility is not a fancy thing, just need to update the **[redis.json](./Scripts/redis.json)** with your redis connection details.
Values key_length, key_range, error_timeout, get_int, set_int, expire_seconds - is used only for GET and SET simultaneously operation in Redis.
key_length - how long is key - set as random string length n
key_range - how many keys is stored in REDIS
expire_seconds - how long key is stored in REDIS in seconds, if set to zero, then stored without time limit
error_timeout - if response is above this limit then event is registred as error, if set to zero then error_timout is ignored
get_int - set from 1 to 10 - how many get tasks is fired per user - to get proportion with set operations
set_int - set from 1 to 10 - how many set tasks is fired per user - to get proportion with get operations

Content of file should be like this:-

```json
{
"redis_host": "18.215.118.208",
"redis_port": "6379",
"redis_password": ""
"redis_password": "",
"key_length": 1000,
"key_range": 1000,
"expire_seconds": 1200,
"error_timeout": 60000,
"get_int": 1,
"set_int": 1
}
```

Expand Down
65 changes: 40 additions & 25 deletions Scripts/redis_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
This script will use locust as framework.

Author:- OpsTree Solutions
Edited by: zviedris
"""

from random import randint
import json
import time
from locust import User, events, TaskSet, task, constant
import redis
import string
import random
import gevent.monkey
gevent.monkey.patch_all()

Expand All @@ -22,15 +25,18 @@ def load_config(filepath):
configs = json.load(property_file)
return configs

def randStr(chars = string.ascii_uppercase + string.digits, N=10):
return ''.join(random.choice(chars) for _ in range(N))

filename = "redis.json"

configs = load_config(filename)


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)
self.errorTime = configs["error_timeout"]
self.expirationTime = configs["expire_seconds"]

def query(self, key, command='GET'):
"""Function to Test GET operation on Redis"""
Expand All @@ -43,59 +49,68 @@ def query(self, key, command='GET'):
except Exception as e:
total_time = int((time.time() - start_time) * 1000)
events.request_failure.fire(
request_type=command, name=key, response_time=total_time, exception=e)
request_type=command, name="get", response_time=total_time, response_length=1, exception=e)
else:
total_time = int((time.time() - start_time) * 1000)
length = len(result)
events.request_success.fire(
request_type=command, name=key, response_time=total_time, response_length=length)
if self.errorTime > 0 and total_time > self.errorTime:
events.request_failure.fire(
request_type=command, name="get", response_time=total_time, response_length=length, exception="timeout exception")
else:
events.request_success.fire(
request_type=command, name="get", response_time=total_time, response_length=length)
return result

def write(self, key, value, command='SET'):
"""Function to Test SET operation on Redis"""
result = None
start_time = time.time()
try:
result = self.rc.set(key, value)
if self.expirationTime > 0:
result = self.rc.set(key, value, self.expirationTime)
else:
result = self.rc.set(key, value)
if not result:
result = ''
except Exception as e:
total_time = int((time.time() - start_time) * 1000)
events.request_failure.fire(
request_type=command, name=key, response_time=total_time, exception=e)
request_type=command, name="set", response_time=total_time, response_length=1, exception=e)
else:
total_time = int((time.time() - start_time) * 1000)
length = 1
events.request_success.fire(
request_type=command, name=key, response_time=total_time, response_length=length)
length = len(value)
#if time is greater than normal timeout - it is registred as timeout
if self.errorTime > 0 and total_time > self.errorTime:
events.request_failure.fire(
request_type=command, name="set", response_time=total_time, response_length=length, exception="timeout exception")
else:
events.request_success.fire(
request_type=command, name="set", response_time=total_time, response_length=length)
return result


class RedisLocust(User):
wait_time = constant(0.1)
key_range = 500
key_range = configs["key_range"]
key_length = configs["key_length"]

def __init__(self, *args, **kwargs):
super(RedisLocust, self).__init__(*args, **kwargs)
self.client = RedisClient()
self.key = 'key1'
self.value = 'value1'

@task(2)
#get task - get one random key
@task(configs["get_int"])
def get_time(self):
for i in range(self.key_range):
self.key = 'key'+str(i)
self.client.query(self.key)
i = randint(1, self.key_range-1)
self.key = 'key'+str(i)
self.client.query(self.key)

@task(1)
#set task - set one random key
@task(configs["set_int"])
def write(self):
for i in range(self.key_range):
self.key = 'key'+str(i)
self.value = 'value'+str(i)
self.client.write(self.key, self.value)

@task(1)
def get_key(self):
var = str(randint(1, self.key_range-1))
self.key = 'key'+var
self.value = 'value'+var
i = randint(1, self.key_range-1)
self.key = 'key'+str(i)
self.value = randStr(N=self.key_length)
self.client.write(self.key, self.value)
8 changes: 7 additions & 1 deletion Scripts/redis_orig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"redis_host": "$REDIS_HOST",
"redis_port": "$REDIS_PORT",
"redis_password": "$REDIS_PW"
"redis_password": "$REDIS_PW",
"key_length": 5000,
"key_range": 1000,
"expire_seconds": 900,
"error_timeout": 60000,
"get_int": 1,
"set_int": 1
}