Skip to content

Commit 1b1627e

Browse files
committed
Added cpu_load metric to db_replicator
1 parent dc0a24d commit 1b1627e

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

mysql_ch_replicator/db_replicator.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class Statistics:
8383
erase_events_count: int = 0
8484
erase_records_count: int = 0
8585
no_events_count: int = 0
86+
cpu_load: float = 0.0
8687

8788

8889
class DbReplicator:
@@ -119,6 +120,7 @@ def __init__(self, config: Settings, database: str, target_database: str = None,
119120
self.last_save_state_time = 0
120121
self.stats = Statistics()
121122
self.last_dump_stats_time = 0
123+
self.last_dump_stats_process_time = 0
122124
self.records_to_insert = defaultdict(dict) # table_name => {record_id=>record, ...}
123125
self.records_to_delete = defaultdict(set) # table_name => {record_id, ...}
124126
self.last_records_upload_time = 0
@@ -420,7 +422,17 @@ def log_stats_if_required(self):
420422
curr_time = time.time()
421423
if curr_time - self.last_dump_stats_time < DbReplicator.STATS_DUMP_INTERVAL:
422424
return
425+
426+
curr_process_time = time.process_time()
427+
428+
time_spent = curr_time - self.last_dump_stats_time
429+
process_time_spent = curr_process_time - self.last_dump_stats_process_time
430+
431+
if time_spent > 0.0:
432+
self.stats.cpu_load = process_time_spent / time_spent
433+
423434
self.last_dump_stats_time = curr_time
435+
self.last_dump_stats_process_time = curr_process_time
424436
logger.info(f'stats: {json.dumps(self.stats.__dict__)}')
425437
self.stats = Statistics()
426438

0 commit comments

Comments
 (0)