Skip to content

Commit

Permalink
Save results in crusader-results by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Aug 9, 2024
1 parent b081eb5 commit 5b607c4
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions android/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ fn android_main(app: AndroidApp) {
tester.file_loader = Some(Box::new(|_| load_file().unwrap()));
tester.plot_saver = Some(Box::new(move |result| {
let path = temp_plot.as_deref().unwrap();
crusader_lib::plot::save_graph_to_path(path, &PlotConfig::default(), result);
crusader_lib::plot::save_graph_to_path(path, &PlotConfig::default(), result).unwrap();
let data = fs::read(path).unwrap();
fs::remove_file(path).unwrap();
let name = format!("{}.png", crusader_lib::test::timed("plot"));
save_file(true, name, data).unwrap();
}));
tester.raw_saver = Some(Box::new(|result| {
let mut writer = Cursor::new(Vec::new());
result.save_to_writer(&mut writer);
result.save_to_writer(&mut writer).unwrap();
let data = writer.into_inner();
let name = format!("{}.crr", crusader_lib::test::timed("data"));
save_file(false, name, data).unwrap();
Expand Down
14 changes: 10 additions & 4 deletions src/crusader-gui-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -843,11 +843,13 @@ impl Tester {
saver(&self.result.as_ref().unwrap().result);
}
None => {
self.result_saved = Some(plot::save_graph(
self.result_saved = plot::save_graph(
&PlotConfig::default(),
&self.result.as_ref().unwrap().result,
"plot",
));
Path::new("crusader-results"),
)
.ok();
}
}
}
Expand All @@ -863,8 +865,12 @@ impl Tester {
saver(self.raw_result.as_ref().unwrap());
}
None => {
self.raw_result_saved =
Some(test::save_raw(self.raw_result.as_ref().unwrap(), "data"));
self.raw_result_saved = test::save_raw(
self.raw_result.as_ref().unwrap(),
"data",
Path::new("crusader-results"),
)
.ok();
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/crusader-lib/src/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,20 @@ impl RawResult {
Self::load_from_reader(File::open(path).ok()?)
}

pub fn save_to_writer(&self, writer: impl Write) {
pub fn save_to_writer(&self, writer: impl Write) -> Result<(), anyhow::Error> {
let mut file = BufWriter::new(writer);

bincode::serialize_into(&mut file, &RawHeader::default()).unwrap();
bincode::serialize_into(&mut file, &RawHeader::default())?;

let mut compressor = snap::write::FrameEncoder::new(file);

self.serialize(&mut rmp_serde::Serializer::new(&mut compressor).with_struct_map())
.unwrap();
self.serialize(&mut rmp_serde::Serializer::new(&mut compressor).with_struct_map())?;

compressor.flush().unwrap();
compressor.flush()?;
Ok(())
}

pub fn save(&self, name: &Path) {
self.save_to_writer(File::create(name).unwrap())
pub fn save(&self, name: &Path) -> Result<(), anyhow::Error> {
self.save_to_writer(File::create(name)?)
}
}
24 changes: 17 additions & 7 deletions src/crusader-lib/src/plot.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use image::{ImageBuffer, ImageFormat, Rgb};
use plotters::coord::types::RangedCoordf64;
use plotters::coord::Shift;
Expand Down Expand Up @@ -153,16 +153,26 @@ pub struct TestResult {
pub stream_groups: Vec<TestStreamGroup>,
}

pub fn save_graph(config: &PlotConfig, result: &TestResult, name: &str) -> String {
pub fn save_graph(
config: &PlotConfig,
result: &TestResult,
name: &str,
root_path: &Path,
) -> Result<String, anyhow::Error> {
std::fs::create_dir_all(root_path)?;
let file = unique(name, "png");
save_graph_to_path(file.as_ref(), config, result);
file
save_graph_to_path(&root_path.join(&file), config, result)?;
Ok(file)
}

pub fn save_graph_to_path(path: &Path, config: &PlotConfig, result: &TestResult) {
let img = save_graph_to_mem(config, result).expect("Unable to write plot to file");
pub fn save_graph_to_path(
path: &Path,
config: &PlotConfig,
result: &TestResult,
) -> Result<(), anyhow::Error> {
let img = save_graph_to_mem(config, result).context("Unable to plot")?;
img.save_with_format(&path, ImageFormat::Png)
.expect("Unable to write plot to file");
.context("Unable to write plot to file")
}

pub(crate) fn save_graph_to_mem(
Expand Down
6 changes: 3 additions & 3 deletions src/crusader-lib/src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ async fn handle_client(
let data = task::spawn_blocking(move || {
let mut data = Vec::new();

result.save_to_writer(&mut data);
data
result.save_to_writer(&mut data)?;
Ok::<_, anyhow::Error>(data)
})
.await?;
.await??;
socket.send(Message::Binary(data)).await?;

(state.msg)(&format!("Remote client running from {}", who.ip()));
Expand Down
22 changes: 15 additions & 7 deletions src/crusader-lib/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,10 +598,11 @@ pub(crate) async fn test_async(
Ok(raw_result)
}

pub fn save_raw(result: &RawResult, name: &str) -> String {
pub fn save_raw(result: &RawResult, name: &str, root_path: &Path) -> Result<String, anyhow::Error> {
std::fs::create_dir_all(root_path)?;
let name = unique(name, "crr");
result.save(Path::new(&name));
name
result.save(&root_path.join(&name))?;
Ok(name)
}

fn setup_loaders(
Expand Down Expand Up @@ -894,10 +895,17 @@ pub fn test(
}
};
println!("{}", with_time("Writing data..."));
let raw = save_raw(&result, "data");
println!("{}", with_time(&format!("Saved raw data as {}", raw)));
let file = save_graph(&plot, &result.to_test_result(), "plot");
println!("{}", with_time(&format!("Saved plot as {}", file)));
let path = Path::new("crusader-results");
let raw = save_raw(&result, "data", path)?;
println!(
"{}",
with_time(&format!("Saved raw data as {}", path.join(raw).display()))
);
let plot = save_graph(&plot, &result.to_test_result(), "plot", path)?;
println!(
"{}",
with_time(&format!("Saved plot as {}", path.join(plot).display()))
);
Ok(())
}

Expand Down
9 changes: 7 additions & 2 deletions src/crusader/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use crusader_lib::{protocol, LIB_VERSION};
use crusader_lib::{with_time, Config};
#[cfg(feature = "client")]
use std::path::PathBuf;
use std::process;
#[cfg(feature = "client")]
use std::time::Duration;
use std::{path::Path, process};

#[derive(Parser)]
#[command(version = LIB_VERSION)]
Expand Down Expand Up @@ -198,14 +198,19 @@ fn run() -> Result<(), anyhow::Error> {
#[cfg(feature = "client")]
Commands::Plot { data, plot } => {
let result = RawResult::load(data).expect("Unable to load data");
let root = data.parent().unwrap_or(Path::new(""));
let file = crusader_lib::plot::save_graph(
&plot.config(),
&result.to_test_result(),
data.file_stem()
.and_then(|name| name.to_str())
.unwrap_or("plot"),
data.parent().unwrap_or(Path::new("")),
)?;
println!(
"{}",
with_time(&format!("Saved plot as {}", root.join(file).display()))
);
println!("{}", with_time(&format!("Saved plot as {}", file)));
Ok(())
}
}
Expand Down

0 comments on commit 5b607c4

Please sign in to comment.