-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
117 lines (93 loc) · 3.52 KB
/
basic.py
File metadata and controls
117 lines (93 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
A Very basic demonstration of how to use cachify
Without any arguments, cachify will use the default settings and initialize
a default session.
Run this example:
# cwd: examples/caching
$ python basic.py
"""
import time
import asyncio
from lazyops.utils.times import Timer
from kvdb.io import cachify
from kvdb.utils.logs import logger
DEBUG_ENABLED = False
@cachify.register(ttl = 10, verbosity = 2 if DEBUG_ENABLED else None, cache_max_size = 15)
async def async_fibonacci(number: int):
if number == 0: return 0
elif number == 1: return 1
return await async_fibonacci(number - 1) + await async_fibonacci(number - 2)
@cachify.register(ttl = 10, verbosity = 2 if DEBUG_ENABLED else None)
def fibonacci(number: int):
if number == 0: return 0
elif number == 1: return 1
return fibonacci(number - 1) + fibonacci(number - 2)
# No Cache Versions
async def async_fibonacci_nc(number: int):
if number == 0: return 0
elif number == 1: return 1
return await async_fibonacci_nc(number - 1) + await async_fibonacci_nc(number - 2)
def fibonacci_nc(number: int):
if number == 0: return 0
elif number == 1: return 1
return fibonacci_nc(number - 1) + fibonacci_nc(number - 2)
async def run_tests(
start_n: int = 1,
runs: int = 10,
print_every: int = 5,
):
"""
Test that both results are the same.
"""
t = Timer(format_ms=True)
# Test Sync
st = Timer(format_ms=True)
for i in range(runs):
r = fibonacci(start_n+i)
d = st.duration_s
if i % print_every == 0:
logger.info(f'[Sync - {i}/{runs}] Result: {r} | Time: {d}')
logger.info(f'[Sync] Cache Average Time: {st.total_average_s(runs)} | Total Time: {st.total_s}')
logger.info(fibonacci.cache_info(), prefix = '[Sync] Cache Info')
# Test Async
at = Timer(format_ms=True)
for i in range(runs):
r = await async_fibonacci(start_n+i)
d = at.duration_s
if i % print_every == 0:
logger.info(f'[Async - {i}/{runs}] Result: {r} | Time: {d}')
logger.info(f'[Async] Cache Average Time: {at.total_average_s(runs)} | Total Time: {at.total_s}')
logger.info(await async_fibonacci.cache_info(), prefix = '[Async] Cache Info')
logger.info(t.total_s, prefix = 'Total Time')
# Clear the Cache
# You can explictly call the function's internally wrapped
# methods to clear the cache.
fibonacci.clear()
logger.info(fibonacci.cache_info(), prefix = '[Sync] Cache Info')
await async_fibonacci.clear()
logger.info(await async_fibonacci.cache_info(), prefix = '[Async] Cache Info')
logger.info('Testing Non-Cached Functions')
t = Timer(format_ms=True)
# Test Sync
st = Timer(format_ms=True)
for i in range(runs):
r = fibonacci_nc(start_n+i)
d = st.duration_s
if i % print_every == 0:
logger.info(f'[Sync - {i}/{runs}] Result: {r} | Time: {d}')
logger.info(f'[Sync] Cache Average Time: {st.total_average_s(runs)} | Total Time: {st.total_s}')
# Test Async
at = Timer(format_ms=True)
for i in range(runs):
r = await async_fibonacci_nc(start_n+i)
d = at.duration_s
if i % print_every == 0:
logger.info(f'[Async - {i}/{runs}] Result: {r} | Time: {d}')
logger.info(f'[Async] Cache Average Time: {at.total_average_s(runs)} | Total Time: {at.total_s}')
logger.info(t.total_s, prefix = 'Total Time')
if __name__ == '__main__':
asyncio.run(run_tests(
start_n = 5,
runs = 10,
print_every = 5,
))