Skip to content

Commit 1d238c7

Browse files
authored
Fix for latest locust (#6)
Signed-off-by: Leny96 <8152038+Leny1996@users.noreply.github.com>
1 parent e5932d8 commit 1d238c7

File tree

2 files changed

+39
-34
lines changed

2 files changed

+39
-34
lines changed

Scripts/redis_get_set.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,31 @@
77
Author:- OpsTree Solutions
88
"""
99

10+
from random import randint
1011
import json
1112
import time
12-
from locust import Locust, events
13-
from locust.core import TaskSet, task
13+
from locust import User, events, TaskSet, task, constant
1414
import redis
1515
import gevent.monkey
1616
gevent.monkey.patch_all()
17-
from random import randint
17+
1818

1919
def load_config(filepath):
2020
"""For loading the connection details of Redis"""
2121
with open(filepath) as property_file:
2222
configs = json.load(property_file)
2323
return configs
2424

25+
2526
filename = "redis.json"
2627

2728
configs = load_config(filename)
2829

30+
2931
class RedisClient(object):
3032
def __init__(self, host=configs["redis_host"], port=configs["redis_port"], password=configs["redis_password"]):
3133
self.rc = redis.StrictRedis(host=host, port=port, password=password)
32-
34+
3335
def query(self, key, command='GET'):
3436
"""Function to Test GET operation on Redis"""
3537
result = None
@@ -40,57 +42,60 @@ def query(self, key, command='GET'):
4042
result = ''
4143
except Exception as e:
4244
total_time = int((time.time() - start_time) * 1000)
43-
events.request_failure.fire(request_type=command, name=key, response_time=total_time, exception=e)
45+
events.request_failure.fire(
46+
request_type=command, name=key, response_time=total_time, exception=e)
4447
else:
4548
total_time = int((time.time() - start_time) * 1000)
4649
length = len(result)
47-
events.request_success.fire(request_type=command, name=key, response_time=total_time, response_length=length)
50+
events.request_success.fire(
51+
request_type=command, name=key, response_time=total_time, response_length=length)
4852
return result
4953

5054
def write(self, key, value, command='SET'):
5155
"""Function to Test SET operation on Redis"""
5256
result = None
5357
start_time = time.time()
5458
try:
55-
result=self.rc.set(key,value)
59+
result = self.rc.set(key, value)
5660
if not result:
5761
result = ''
5862
except Exception as e:
5963
total_time = int((time.time() - start_time) * 1000)
60-
events.request_failure.fire(request_type=command, name=key, response_time=total_time, exception=e)
64+
events.request_failure.fire(
65+
request_type=command, name=key, response_time=total_time, exception=e)
6166
else:
6267
total_time = int((time.time() - start_time) * 1000)
6368
length = 1
64-
events.request_success.fire(request_type=command, name=key, response_time=total_time, response_length=length)
69+
events.request_success.fire(
70+
request_type=command, name=key, response_time=total_time, response_length=length)
6571
return result
6672

67-
class RedisLocust(Locust):
73+
74+
class RedisLocust(User):
75+
wait_time = constant(0.1)
76+
key_range = 500
77+
6878
def __init__(self, *args, **kwargs):
6979
super(RedisLocust, self).__init__(*args, **kwargs)
7080
self.client = RedisClient()
7181
self.key = 'key1'
7282
self.value = 'value1'
7383

74-
class RedisLua(RedisLocust):
75-
min_wait = 100
76-
max_wait = 100
77-
78-
class task_set(TaskSet):
79-
@task(2)
80-
def get_time(self):
81-
for i in range(100):
82-
self.key='key'+str(i)
83-
self.client.query(self.key)
84-
85-
@task(1)
86-
def write(self):
87-
for i in range(100):
88-
self.key='key'+str(i)
89-
self.value='value'+str(i)
90-
self.client.write(self.key,self.value)
91-
92-
@task(1)
93-
def get_key(self):
94-
var=str(randint(1,99))
95-
self.key='key'+var
96-
self.value='value'+var
84+
@task(2)
85+
def get_time(self):
86+
for i in range(self.key_range):
87+
self.key = 'key'+str(i)
88+
self.client.query(self.key)
89+
90+
@task(1)
91+
def write(self):
92+
for i in range(self.key_range):
93+
self.key = 'key'+str(i)
94+
self.value = 'value'+str(i)
95+
self.client.write(self.key, self.value)
96+
97+
@task(1)
98+
def get_key(self):
99+
var = str(randint(1, self.key_range-1))
100+
self.key = 'key'+var
101+
self.value = 'value'+var

Scripts/requirments.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
locustio
1+
locust
22
argparse
33
redis

0 commit comments

Comments
 (0)