MCPcopy Create free account
hub / github.com/requests-cache/requests-cache / CacheRPSProgress

Class CacheRPSProgress

examples/rps_graph.py:45–99  ·  view source on GitHub ↗

Track requests per second plus cache size in a single live view

Source from the content-addressed store, hash-verified

43
44
45class CacheRPSProgress:
46 """Track requests per second plus cache size in a single live view"""
47
48 def __init__(self, n_unique_requests: int = 100):
49 self.rps_progress = RPSProgress()
50 self.cache_progress = Progress(
51 BarColumn(complete_style='blue'),
52 '[cyan]Requests cached:',
53 MofNCompleteColumn(),
54 )
55 header = Progress(BarColumn(), '[cyan]Requests per second')
56 header.add_task('')
57 self.cache_task = self.cache_progress.add_task('', total=n_unique_requests)
58 self.n_unique_requests = n_unique_requests
59 self.start_time = time()
60
61 self.table = Table.grid()
62 self.table.add_row(header)
63 self.table.add_row(self.rps_progress)
64 self.table.add_row(self.cache_progress)
65 self.live = Live(self.table, refresh_per_second=10)
66
67 def __enter__(self):
68 """Start live view on ctx enter"""
69 self.live.__enter__()
70 self.log(
71 '[cyan]Measuring request rate with '
72 f'[white]{self.n_unique_requests}[cyan] total unique requests'
73 )
74 self.log('[cyan]Press [white]Ctrl+C[cyan] to exit')
75 return self
76
77 def __exit__(self, *args):
78 """Show stats on ctx exit"""
79 self.live.__exit__(*args)
80 elapsed = time() - self.start_time
81 self.log(
82 f'[cyan]Sent a total of [white]{self.total_requests}[cyan] '
83 f'requests in [white]{elapsed:.2f}[cyan] seconds '
84 )
85
86 self.log(f'[cyan]Average: [white]{int(self.total_requests/elapsed)}[cyan] requests/second')
87
88 @property
89 def total_requests(self):
90 return self.rps_progress.total_requests
91
92 def count_request(self):
93 self.rps_progress.count_request()
94
95 def update_cache_size(self, size: int):
96 self.cache_progress.update(self.cache_task, completed=size)
97
98 def log(self, msg: str):
99 self.cache_progress.log(msg)
100
101
102def test_rps(session):

Callers 1

test_rpsFunction · 0.85

Calls

no outgoing calls

Tested by 1

test_rpsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…