Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change graph to use VecDeque instead of Vec #564

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,11 @@ autoclockspeed-0.1.11-x86_64.tar.gz
autoclockspeed-bin-0.1.11-1-x86_64.pkg.tar.zst
scripts/output.log
# ----------------#


# Auto-generated by autoignore:
# --------------- #
benches/bench-after-queue-9-24-2024.ignore
scripts/plot_temp.py
scripts/venv/
# ----------------#
9 changes: 5 additions & 4 deletions benches/graph_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use autoclockspeed::graph::{Graph, Grapher};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::collections::VecDeque;

fn update_all_benchmark(c: &mut Criterion) {
let mut graph = Graph::new();
let vec = vec![0.0; 100];
let vec: VecDeque<f64> = vec![0.0; 100].into();
graph.vals = vec;
c.bench_function("update_all", |b| b.iter(|| graph.update_all()));
}

fn update_one_benchmark(c: &mut Criterion) {
let graph = Graph::new();
let mut vec = vec![0.0; 100];
let mut vec: VecDeque<f64> = vec![0.0; 100].into();
c.bench_function("update_one", |b| {
b.iter(|| black_box(graph.update_one(&mut vec)))
});
}

fn clear_before_benchmark(c: &mut Criterion) {
let graph = Graph::new();
let mut vec = vec![0.0; 100];
let mut vec: VecDeque<f64> = vec![0.0; 100].into();
c.bench_function("clear_before", |b| {
b.iter(|| {
graph.clear_before(&mut vec);
Expand All @@ -29,7 +30,7 @@ fn clear_before_benchmark(c: &mut Criterion) {

fn plot_benchmark(c: &mut Criterion) {
let graph = Graph::new();
let nums = vec![0.0; 100];
let nums: VecDeque<f64> = vec![0.0; 100].into();
c.bench_function("plot", |b| b.iter(|| black_box(graph.plot(nums.clone()))));
}

Expand Down
10 changes: 7 additions & 3 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,15 +360,19 @@ impl Checker for Daemon {

// Update the data in the graph and render it
if self.settings.graph == GraphType::Usage {
self.grapher.vals.push(check_cpu_usage(&self.cpus) as f64);
self.grapher
.vals
.push_back(check_cpu_usage(&self.cpus) as f64);
}
if self.settings.graph == GraphType::Frequency {
self.grapher.vals.push(check_cpu_freq(&self.cpus) as f64);
self.grapher
.vals
.push_back(check_cpu_freq(&self.cpus) as f64);
}
if self.settings.graph == GraphType::Temperature {
self.grapher
.vals
.push((check_cpu_temperature(&self.cpus) / 1000.0) as f64);
.push_back((check_cpu_temperature(&self.cpus) / 1000.0) as f64);
}

Ok(())
Expand Down
24 changes: 14 additions & 10 deletions src/graph.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#![forbid(unsafe_code)]
use rasciigraph::{plot, Config};
use std::collections::VecDeque;
use std::default::Default;
use std::fmt;

pub trait Grapher {
fn update_all(&mut self);
fn update_one(&self, vec: &mut Vec<f64>) -> String;
fn clear_before(&self, vec: &mut Vec<f64>);
fn plot(&self, nums: Vec<f64>) -> String;
fn update_one(&self, vec: &mut VecDeque<f64>) -> String;
fn clear_before(&self, vec: &mut VecDeque<f64>);
fn plot(&self, nums: VecDeque<f64>) -> String;
fn new() -> Self;
}

pub struct Graph {
/// The values that get graphed
pub vals: Vec<f64>,
pub vals: VecDeque<f64>,
max: usize,
}

Expand All @@ -22,27 +23,30 @@ impl Grapher for Graph {
self.update_one(&mut self.vals.clone());
}

fn update_one(&self, vec: &mut Vec<f64>) -> String {
fn update_one(&self, vec: &mut VecDeque<f64>) -> String {
self.clear_before(vec);
self.plot(vec.clone())
}

fn clear_before(&self, vec: &mut Vec<f64>) {
fn clear_before(&self, vec: &mut VecDeque<f64>) {
while vec.len() > self.max {
vec.remove(0);
vec.pop_front();
}
}

fn plot(&self, nums: Vec<f64>) -> String {
fn plot(&self, nums: VecDeque<f64>) -> String {
format!(
"\n{}",
plot(nums, Config::default().with_offset(10).with_height(10))
plot(
nums.into(),
Config::default().with_offset(10).with_height(10)
)
)
}

fn new() -> Self {
Graph {
vals: Vec::<f64>::new(),
vals: VecDeque::<f64>::new(),
max: 40,
}
}
Expand Down
Loading