Skip to content

Commit

Permalink
chore: 🤖 setup cpu&memory usage tracking in demo-player
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf committed Jan 31, 2024
1 parent 5bf0bc1 commit 84ea226
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions demo-player/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
[dependencies]
minifb = "0.25"
dotlottie_player = { path = "../dotlottie-rs" }
sysinfo = "0.30"
35 changes: 32 additions & 3 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use dotlottie_player_core::{Config, DotLottiePlayer, Mode, Observer};
use minifb::{Key, Window, WindowOptions};
use std::fs::{self, File};
use std::io::Read;
use std::sync::Arc;
use std::thread;
use std::{env, path, time::Instant};

use dotlottie_player_core::{Config, DotLottiePlayer, Mode, Observer};
use minifb::{Key, Window, WindowOptions};
use sysinfo::System;

pub const WIDTH: usize = 500;
pub const HEIGHT: usize = 500;
Expand Down Expand Up @@ -142,6 +143,28 @@ fn main() {

let mut timer = Timer::new();

let sys = System::new_all();

let cpu_memory_monitor_thread = thread::spawn(move || {
loop {
sys.refresh_all();

for (pid, process) in sys.processes() {
if pid.as_u32() == std::process::id() {
println!(
"CPU: {} % | Memory: {} MB",
process.cpu_usage(),
process.memory() / 1024 / 1024,
);
}
}

thread::sleep(std::time::Duration::from_secs(1)); // Adjust sleep duration as needed
}
});

let mut cpu_memory_monitor_timer = Instant::now();

while window.is_open() && !window.is_key_down(Key::Escape) {
timer.tick(&mut lottie_player);

Expand Down Expand Up @@ -174,11 +197,17 @@ fn main() {
lottie_player.unsubscribe(&observer2);
}

if cpu_memory_monitor_timer.elapsed().as_secs() >= 1 {
cpu_memory_monitor_timer = Instant::now();
}

let (buffer_ptr, buffer_len) = (lottie_player.buffer_ptr(), lottie_player.buffer_len());

let buffer =
unsafe { std::slice::from_raw_parts(buffer_ptr as *const u32, buffer_len as usize) };

window.update_with_buffer(buffer, WIDTH, HEIGHT).unwrap();
}

cpu_memory_monitor_thread.join().unwrap();
}

0 comments on commit 84ea226

Please sign in to comment.