Skip to content

Commit

Permalink
Fix for latest locust (opstree#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Leny96 <[email protected]>
  • Loading branch information
Leny1996 authored Dec 14, 2020
1 parent e5932d8 commit 1d238c7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
71 changes: 38 additions & 33 deletions Scripts/redis_get_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,31 @@
Author:- OpsTree Solutions
"""

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


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


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)

def query(self, key, command='GET'):
"""Function to Test GET operation on Redis"""
result = None
Expand All @@ -40,57 +42,60 @@ def query(self, key, command='GET'):
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)
events.request_failure.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(request_type=command, name=key, response_time=total_time, response_length=length)
events.request_success.fire(
request_type=command, name=key, 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)
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)
events.request_failure.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(request_type=command, name=key, response_time=total_time, response_length=length)
events.request_success.fire(
request_type=command, name=key, response_time=total_time, response_length=length)
return result

class RedisLocust(Locust):

class RedisLocust(User):
wait_time = constant(0.1)
key_range = 500

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

class RedisLua(RedisLocust):
min_wait = 100
max_wait = 100

class task_set(TaskSet):
@task(2)
def get_time(self):
for i in range(100):
self.key='key'+str(i)
self.client.query(self.key)

@task(1)
def write(self):
for i in range(100):
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,99))
self.key='key'+var
self.value='value'+var
@task(2)
def get_time(self):
for i in range(self.key_range):
self.key = 'key'+str(i)
self.client.query(self.key)

@task(1)
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
2 changes: 1 addition & 1 deletion Scripts/requirments.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
locustio
locust
argparse
redis

0 comments on commit 1d238c7

Please sign in to comment.