Skip to content

Commit b1eb07d

Browse files
Updating Caching Performance
1 parent 48366f9 commit b1eb07d

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

modules/writing_observer/writing_observer/awe_nlp.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import awe_components.components.viewpointFeatures
2323
import awe_components.components.lexicalClusters
2424
import awe_components.components.contentSegmentation
25+
from writing_observer.caching_performance import CachePerformance
2526
import json
2627
import warnings
2728

@@ -30,6 +31,7 @@
3031
import learning_observer.util
3132

3233
RUN_MODES = enum.Enum('RUN_MODES', 'MULTIPROCESSING SERIAL')
34+
cache_stats = CachePerformance()
3335

3436

3537
def init_nlp():
@@ -316,6 +318,15 @@ async def process_and_cache_missing_features(unfound_features, found_features, r
316318
await cache.set(text_hash, text_cache_data)
317319
return writing
318320

321+
async def update_cache_stats(hits=0, misses=0):
322+
"""
323+
Updates cache statistics like Hits and Misses.
324+
"""
325+
if hits!= 0:
326+
await cache_stats.increment_hit_count(hits)
327+
if misses!=0:
328+
await cache_stats.increment_miss_count(misses)
329+
319330

320331
async def process_writings_with_caching(writing_data, options=None, mode=RUN_MODES.MULTIPROCESSING, sleep_interval=1, wait_time_for_running_features=60):
321332
'''
@@ -360,18 +371,23 @@ async def process_writings_with_caching(writing_data, options=None, mode=RUN_MOD
360371
# If all options were found
361372
if found_features == requested_features:
362373
results.append(writing)
374+
await update_cache_stats(hits=len(found_features))
363375
continue
364376

365377
# Check if some options are a subset of running_features: features that are needed but are already running
366378
unfound_features, found_features, writing = await check_and_wait_for_running_features(writing, requested_features, found_features, cache, sleep_interval, wait_time_for_running_features, text_hash)
367379
# If all options are found
368380
if found_features == requested_features:
369381
results.append(writing)
382+
await update_cache_stats(hits=len(found_features))
370383
continue
371384

372385
# Add not found options to running_features and update cache
373386
results.append(await process_and_cache_missing_features(unfound_features, found_features, requested_features, cache, text_hash, writing))
374387

388+
# Update Cache Performance
389+
await update_cache_stats(hits=len(found_features), misses=len(unfound_features))
390+
375391
return results
376392

377393

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
11
import asyncio
22

3-
# In-Memory Counters
4-
HIT_COUNT = 0
5-
MISS_COUNT = 0
6-
73
# Asyncio Locks for the in-memory counters. https://docs.python.org/3/library/asyncio-sync.html
84
hit_lock = asyncio.Lock()
95
miss_lock = asyncio.Lock()
106

11-
async def increment_hit_count():
12-
"""
13-
Increments the hit count by 1
14-
"""
15-
global HIT_COUNT
16-
async with hit_lock: #Acquires and releases the locks after finished.
17-
HIT_COUNT += 1
18-
print(HIT_COUNT)
197

20-
async def increment_miss_count():
8+
class CachePerformance():
219
"""
22-
Increments the miss count by 1
10+
A class to track caching performance through cache hits and misses.
2311
"""
24-
global MISS_COUNT
25-
async with miss_lock: #Acquires and releases the locks after finished.
26-
MISS_COUNT += 1
12+
def __init__(self):
13+
# In-Memory Counters
14+
self.HIT_COUNT = 0
15+
self.MISS_COUNT = 0
16+
17+
async def increment_hit_count(self, hits=1):
18+
"""
19+
Increments the hit count by number of hits
20+
"""
21+
async with hit_lock: #Acquires and releases the locks after finished.
22+
self.HIT_COUNT += hits
23+
print(f"Hit Count: {self.HIT_COUNT}")
24+
25+
async def increment_miss_count(self, misses=1):
26+
"""
27+
Increments the miss count by number of misses.
28+
"""
29+
async with miss_lock: #Acquires and releases the locks after finished.
30+
self.MISS_COUNT += misses
31+
print(f"Miss Count: {self.MISS_COUNT}")

testcode/TestCachingPerformance.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import asyncio
2-
from writing_observer.caching_performance import increment_hit_count, increment_miss_count, HIT_COUNT, MISS_COUNT
2+
from writing_observer.caching_performance import CachePerformance
33

44
# entry point
55
async def main():
6-
print(f"Initial Hit Count: {HIT_COUNT}")
6+
cache_performance = CachePerformance()
77
number_of_coroutines = 10
88
# create many concurrent coroutines
9-
coros = [increment_hit_count() for i in range(number_of_coroutines)]
9+
coros = [cache_performance.increment_hit_count(2) for i in range(number_of_coroutines)]
1010
# execute and wait for tasks to complete
1111
await asyncio.gather(*coros)
12-
print(HIT_COUNT)
1312

1413
asyncio.run(main())

0 commit comments

Comments
 (0)