Skip to content

Instantly share code, notes, and snippets.

@skyrocknroll
Created April 3, 2017 14:01
Show Gist options
  • Save skyrocknroll/a27328705ab39f6b84ad9cd37bf2ca64 to your computer and use it in GitHub Desktop.
Save skyrocknroll/a27328705ab39f6b84ad9cd37bf2ca64 to your computer and use it in GitHub Desktop.
locust redis load testing
import time
import redis
from locust import Locust, events
from locust.core import TaskSet, task
class RedisClient(object):
def __init__(self, host="localhost", port=6379):
self.rc = redis.StrictRedis(host=host, port=port)
def query(self, key, command='GET'):
result = None
start_time = time.time()
try:
result = self.rc.get(key)
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)
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)
return result
class RedisLocust(Locust):
def __init__(self, *args, **kwargs):
super(RedisLocust, self).__init__(*args, **kwargs)
self.client = RedisClient()
class RedisLua(RedisLocust):
# host = "http://127.0.0.1:8877/"
min_wait = 0
max_wait = 0
class task_set(TaskSet):
@task(10)
def get_time(self):
self.client.query("Boo")
# @task(5)
# def get_random_number(self):
# self.client.get_random_number(0, 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment