Skip to content

Commit

Permalink
win opti + bench for win
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes committed Dec 4, 2023
1 parent 2e36997 commit 9d551b6
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .config/settings.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
unit = "Celsius"
update_delay = 2500
current_config = "lin.toml"
update_delay = 1500
current_config = "win.toml"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [x] impl windows code
- [x] impl linux code
- [x] test on real hardware
- [ ] Windows optimization
- [x] Windows optimization
- [ ] End to End testing
- [ ] package multiple format with [cargo-bundle](https://github.com/burtonageo/cargo-bundle)
- [ ] Msi
Expand Down
2 changes: 1 addition & 1 deletion data/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Default for Settings {
}

fn default_update_delay() -> u64 {
2500
1500
}

impl Settings {
Expand Down
8 changes: 8 additions & 0 deletions data/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ impl Update {
root_nodes: &RootNodes,
bridge: &mut HardwareBridgeT,
) {
if let Err(e) = bridge.update() {
error!("{:?}", e);
return;
}
if self.config_changed {
self.set_invalid_controls_to_auto(nodes, root_nodes, bridge);
}
Expand All @@ -59,6 +63,10 @@ impl Update {
}

pub fn all(&mut self, nodes: &mut Nodes, root_nodes: &RootNodes, bridge: &mut HardwareBridgeT) {
if let Err(e) = bridge.update() {
error!("{:?}", e);
return;
}
if self.config_changed {
self.set_invalid_controls_to_auto(nodes, root_nodes, bridge);
}
Expand Down
6 changes: 5 additions & 1 deletion hardware/LibreHardwareMonitorWrapper/HardwareManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ public HardwareManager()
_hardwareList = _hardwareResearcher.GetHardwareList();
}

public int GetValue(int index)
public void Update()
{
_hardwareResearcher.Update();
}

public int GetValue(int index)
{
var hardware = _hardwareList[index];
return hardware.Type switch
{
Expand Down
6 changes: 5 additions & 1 deletion hardware/LibreHardwareMonitorWrapper/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ public enum Command
SetAuto = 1,
SetValue = 2,
GetValue = 3,
Shutdown = 4
Shutdown = 4,
Update = 5
}

public class Server
Expand Down Expand Up @@ -75,6 +76,9 @@ public void WaitForCommand(HardwareManager hardwareManager)
break;
case Command.Shutdown:
return;
case Command.Update:
hardwareManager.Update();
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
7 changes: 7 additions & 0 deletions hardware/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ pub trait HardwareBridge {
fn get_value(&mut self, internal_index: &usize) -> Result<Value, HardwareError>;
fn set_value(&mut self, internal_index: &usize, value: Value) -> Result<(), HardwareError>;
fn set_mode(&mut self, internal_index: &usize, value: Value) -> Result<(), HardwareError>;

// use on Windows, because we update all sensors in one function, so
// we don't want to update at each call, instead, we call this function
// one time in each update iteration
fn update(&mut self) -> Result<(), HardwareError> {
Ok(())
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
85 changes: 58 additions & 27 deletions hardware/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ impl HardwareBridge for WindowsBridge {
// todo: take a result
Ok(())
}

fn update(&mut self) -> Result<(), HardwareError> {
let command: Packet = Command::Update.into();
self.stream.write_all(&command).unwrap();
Ok(())
}
}

fn try_connect() -> TcpStream {
Expand Down Expand Up @@ -178,6 +184,7 @@ enum Command {
SetValue = 2,
GetValue = 3,
Shutdown = 4,
Update = 5,
}

impl From<Command> for [u8; 4] {
Expand Down Expand Up @@ -267,49 +274,73 @@ fn is_little_endian() -> bool {
#[cfg(test)]
mod test {
use super::WindowsBridge;
use crate::{HardwareBridge, HardwareBridgeT};
use std::time::Instant;
use crate::{Hardware, HardwareBridge, HardwareBridgeT};
use std::{
thread::sleep,
time::{Duration, Instant},
};

#[test]
fn test_time() {
let f = || {
let (hardware, mut bridge) = WindowsBridge::generate_hardware();
let now = Instant::now();
let (hardware, mut bridge) = WindowsBridge::generate_hardware();
println!("generation took {} millis", now.elapsed().as_millis());

for _ in 0..5 {
bench(
|| {
update(&mut bridge, &hardware);
"all sensors".to_string()
},
"update",
);
sleep(Duration::from_millis(500))
}
}

for h in &hardware.controls {
get_value(&mut bridge, &h.internal_index, &h.name);
}
for h in &hardware.temps {
get_value(&mut bridge, &h.internal_index, &h.name);
}
for h in &hardware.fans {
get_value(&mut bridge, &h.internal_index, &h.name);
}
};
bench(f, "all");
fn update(bridge: &mut HardwareBridgeT, hardware: &Hardware) {
println!();

bench(
|| {
bridge.update().unwrap();
"lhm".to_string()
},
"update",
);

for h in &hardware.controls {
get_value(bridge, &h.internal_index, &h.name);
}
for h in &hardware.temps {
get_value(bridge, &h.internal_index, &h.name);
}
for h in &hardware.fans {
get_value(bridge, &h.internal_index, &h.name);
}
}

fn get_value(bridge: &mut HardwareBridgeT, index: &usize, name: &str) {
bench(
|| {
match bridge.get_value(index) {
Ok(value) => {
println!("value of control {} = {}", name, value);
}
Err(e) => {
println!("error of control {} = {:?}", name, e);
}
};
|| match bridge.get_value(index) {
Ok(value) => {
format!("{} = {}", name, value)
}
Err(e) => {
format!("error for {}: {:?}", name, e)
}
},
"get_value",
);
}

fn bench(f: impl FnOnce(), info: &str) {
fn bench(f: impl FnOnce() -> String, info: &str) {
let now = Instant::now();
f();
let output = f();
println!(
"fun: {}, time taken: {} millis",
"{}: {} in {} millis",
info,
output,
now.elapsed().as_millis()
);
}
Expand Down

0 comments on commit 9d551b6

Please sign in to comment.