Skip to content

Commit

Permalink
Add some performance profiling for redis
Browse files Browse the repository at this point in the history
  • Loading branch information
MelissaAutumn committed Sep 10, 2024
1 parent 9e4e57e commit ee6fdfa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 13 additions & 0 deletions backend/src/appointment/controller/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@

import json
import logging
import time
import zoneinfo
import os

import caldav.lib.error
import requests
import sentry_sdk
from redis import Redis, RedisCluster
from caldav import DAVClient
from fastapi import BackgroundTasks
Expand Down Expand Up @@ -56,10 +58,15 @@ def get_cached_events(self, key_scope):

key_scope = self.obscure_key(key_scope)

timer_boot = time.perf_counter_ns()

encrypted_events = self.redis_instance.get(f'{REDIS_REMOTE_EVENTS_KEY}:{self.get_key_body()}:{key_scope}')
if encrypted_events is None:
sentry_sdk.set_measurement('redis_get_miss_time', time.perf_counter_ns() - timer_boot, 'nanosecond')
return None

sentry_sdk.set_measurement('redis_get_hit_time', time.perf_counter_ns() - timer_boot, 'nanosecond')

return [schemas.Event.model_load_redis(blob) for blob in json.loads(encrypted_events)]

def put_cached_events(self, key_scope, events: list[schemas.Event], expiry=os.getenv('REDIS_EVENT_EXPIRE_SECONDS')):
Expand All @@ -68,11 +75,13 @@ def put_cached_events(self, key_scope, events: list[schemas.Event], expiry=os.ge
return False

key_scope = self.obscure_key(key_scope)
timer_boot = time.perf_counter_ns()

encrypted_events = json.dumps([event.model_dump_redis() for event in events])
self.redis_instance.set(
f'{REDIS_REMOTE_EVENTS_KEY}:{self.get_key_body()}:{key_scope}', value=encrypted_events, ex=expiry
)
sentry_sdk.set_measurement('redis_put_time', time.perf_counter_ns() - timer_boot, 'nanosecond')

return True

Expand All @@ -82,6 +91,8 @@ def bust_cached_events(self, all_calendars=False):
if self.redis_instance is None:
return False

timer_boot = time.perf_counter_ns()

# Scan returns a tuple like: (Cursor start, [...keys found])
ret = self.redis_instance.scan(
0, f'{REDIS_REMOTE_EVENTS_KEY}:{self.get_key_body(only_subscriber=all_calendars)}:*'
Expand All @@ -93,6 +104,8 @@ def bust_cached_events(self, all_calendars=False):
# Expand the list in position 1, which is a list of keys found from the scan
self.redis_instance.delete(*ret[1])

sentry_sdk.set_measurement('redis_bust_time', time.perf_counter_ns() - timer_boot, 'nanosecond')

return True


Expand Down
15 changes: 13 additions & 2 deletions backend/src/appointment/dependencies/database.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
import time

import sentry_sdk.metrics
from redis import Redis, RedisCluster
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
Expand Down Expand Up @@ -41,20 +43,29 @@ def get_redis() -> Redis | RedisCluster | None:
password = os.getenv('REDIS_PASSWORD')
ssl = True if os.getenv('REDIS_USE_SSL') and (os.getenv('REDIS_USE_SSL').lower() == 'true' or os.getenv('REDIS_USE_SSL').lower() == '1') else False

timer_boot = time.perf_counter_ns()

if os.getenv('REDIS_USE_CLUSTER'):
return RedisCluster(
cluster = RedisCluster(
host=host,
port=port,
password=password,
ssl=ssl,
decode_responses=True,
)

return Redis(
sentry_sdk.set_measurement('redis_boot_time', time.perf_counter_ns() - timer_boot, 'nanosecond')

return cluster

redis = Redis(
host=host,
port=port,
db=db,
password=password,
ssl=ssl,
decode_responses=True,
)

sentry_sdk.set_measurement('redis_boot_time', time.perf_counter_ns() - timer_boot, 'nanosecond')
return redis

0 comments on commit ee6fdfa

Please sign in to comment.