Skip to content

Commit f9e24b0

Browse files
committed
Improved readme, added store time, timeout
1 parent 73ab47e commit f9e24b0

File tree

3 files changed

+55
-28
lines changed

3 files changed

+55
-28
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,28 @@ pip3 install -r requirments.txt
3939

4040
## Usage
4141

42-
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:-
42+
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.
43+
Values key_length, key_range, error_timeout, get_int, set_int, expire_seconds - is used only for GET and SET simultaneously operation in Redis.
44+
key_length - how long is key - set as random string length n
45+
key_range - how many keys is stored in REDIS
46+
expire_seconds - how long key is stored in REDIS in seconds, if set to zero, then stored without time limit
47+
error_timeout - if response is above this limit then event is registred as error, if set to zero then error_timout is ignored
48+
get_int - set from 1 to 10 - how many get tasks is fired per user - to get proportion with set operations
49+
set_int - set from 1 to 10 - how many set tasks is fired per user - to get proportion with get operations
50+
51+
Content of file should be like this:-
4352

4453
```json
4554
{
4655
"redis_host": "18.215.118.208",
4756
"redis_port": "6379",
48-
"redis_password": ""
57+
"redis_password": "",
58+
"key_length": 1000,
59+
"key_range": 1000,
60+
"expire_seconds": 1200,
61+
"error_timeout": 60000,
62+
"get_int": 1,
63+
"set_int": 1
4964
}
5065
```
5166

Scripts/redis_get_set.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
This script will use locust as framework.
66
77
Author:- OpsTree Solutions
8+
Edited by: zviedris
89
"""
910

1011
from random import randint
@@ -28,13 +29,14 @@ def randStr(chars = string.ascii_uppercase + string.digits, N=10):
2829
return ''.join(random.choice(chars) for _ in range(N))
2930

3031
filename = "redis.json"
31-
3232
configs = load_config(filename)
3333

3434

3535
class RedisClient(object):
3636
def __init__(self, host=configs["redis_host"], port=configs["redis_port"], password=configs["redis_password"]):
3737
self.rc = redis.StrictRedis(host=host, port=port, password=password)
38+
self.errorTime = configs["error_timeout"]
39+
self.expirationTime = configs["expire_seconds"]
3840

3941
def query(self, key, command='GET'):
4042
"""Function to Test GET operation on Redis"""
@@ -47,31 +49,43 @@ def query(self, key, command='GET'):
4749
except Exception as e:
4850
total_time = int((time.time() - start_time) * 1000)
4951
events.request_failure.fire(
50-
request_type=command, name=key, response_time=total_time, exception=e)
52+
request_type=command, name="get", response_time=total_time, response_length=1, exception=e)
5153
else:
5254
total_time = int((time.time() - start_time) * 1000)
5355
length = len(result)
54-
events.request_success.fire(
55-
request_type=command, name=key, response_time=total_time, response_length=length)
56+
if self.errorTime > 0 and total_time > self.errorTime:
57+
events.request_failure.fire(
58+
request_type=command, name="get", response_time=total_time, response_length=length, exception="timeout exception")
59+
else:
60+
events.request_success.fire(
61+
request_type=command, name="get", response_time=total_time, response_length=length)
5662
return result
5763

5864
def write(self, key, value, command='SET'):
5965
"""Function to Test SET operation on Redis"""
6066
result = None
6167
start_time = time.time()
6268
try:
63-
result = self.rc.set(key, value)
69+
if self.expirationTime > 0:
70+
result = self.rc.set(key, value, self.expirationTime)
71+
else:
72+
result = self.rc.set(key, value)
6473
if not result:
6574
result = ''
6675
except Exception as e:
6776
total_time = int((time.time() - start_time) * 1000)
6877
events.request_failure.fire(
69-
request_type=command, name=key, response_time=total_time, exception=e)
78+
request_type=command, name="set", response_time=total_time, response_length=1, exception=e)
7079
else:
7180
total_time = int((time.time() - start_time) * 1000)
7281
length = len(value)
73-
events.request_success.fire(
74-
request_type=command, name=key, response_time=total_time, response_length=length)
82+
#if time is greater than normal timeout - it is registred as timeout
83+
if self.errorTime > 0 and total_time > self.errorTime:
84+
events.request_failure.fire(
85+
request_type=command, name="set", response_time=total_time, response_length=length, exception="timeout exception")
86+
else:
87+
events.request_success.fire(
88+
request_type=command, name="set", response_time=total_time, response_length=length)
7589
return result
7690

7791

@@ -86,23 +100,17 @@ def __init__(self, *args, **kwargs):
86100
self.key = 'key1'
87101
self.value = 'value1'
88102

89-
@task(2)
103+
#get task - get one random key
104+
@task(configs["get_int"])
90105
def get_time(self):
91-
#for i in range(self.key_range):
92-
i = randint(1, self.key_range-1)
93-
self.key = 'key'+str(i)
94-
self.client.query(self.key)
106+
i = randint(1, self.key_range-1)
107+
self.key = 'key'+str(i)
108+
self.client.query(self.key)
95109

96-
@task(1)
110+
#set task - set one random key
111+
@task(configs["set_int"])
97112
def write(self):
98-
#for i in range(self.key_range):
99-
i = randint(1, self.key_range-1)
100-
self.key = 'key'+str(i)
101-
self.value = randStr(N=self.key_length)
102-
self.client.write(self.key, self.value)
103-
104-
# @task(1)
105-
# def get_key(self):
106-
# var = str(randint(1, self.key_range-1))
107-
# self.key = 'key'+var
108-
# self.value = 'value'+var
113+
i = randint(1, self.key_range-1)
114+
self.key = 'key'+str(i)
115+
self.value = randStr(N=self.key_length)
116+
self.client.write(self.key, self.value)

Scripts/redis_orig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"redis_port": "$REDIS_PORT",
44
"redis_password": "$REDIS_PW",
55
"key_length": 5000,
6-
"key_range": 1000
6+
"key_range": 1000,
7+
"expire_seconds": 900,
8+
"error_timeout": 60000,
9+
"get_int": 1,
10+
"set_int": 1
711
}

0 commit comments

Comments
 (0)