Commit e9e92cdd authored by Pascal Engélibert's avatar Pascal Engélibert 🚴
Browse files

Graph subcommand

parent 98999818
This diff is collapsed.
......@@ -11,6 +11,7 @@ license = "AGPL3"
[dependencies]
bincode = "1.2.1"
indicatif = "0.14.0"
plotters = "0.2.12"
rand = "0.7.3"
rayon = "1.3.0"
serde = { version = "1.0.106", features = ["derive"] }
......
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, convert::From, io, num::ParseFloatError, str::FromStr};
use std::{
cmp::Ordering, collections::HashMap, convert::From, io, num::ParseFloatError, str::FromStr,
};
use structopt::StructOpt;
pub const VERSION: u32 = 0;
#[derive(PartialEq, PartialOrd)]
pub struct NonNan<T>(pub T);
impl<T> Eq for NonNan<T> where T: PartialEq {}
impl<T> Ord for NonNan<T>
where
T: PartialOrd,
{
fn cmp(&self, other: &NonNan<T>) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Pubkey(pub u32);
......
use crate::common::*;
use crate::utils::cli;
use bincode;
use plotters::prelude::*;
use serde_cbor;
use serde_json;
use std::io::Read;
pub fn run_graph(opt: cli::GraphSubcommand) {
let stdin = std::io::stdin();
let mut handle = stdin.lock();
let mut raw = Vec::new();
handle.read_to_end(&mut raw).unwrap();
let export_results: ExportResults = match opt.input_format {
ExportFormat::Bincode => bincode::deserialize(&raw).unwrap(),
ExportFormat::Cbor => serde_cbor::from_slice(&raw).unwrap(),
ExportFormat::Json => serde_json::from_str(&std::str::from_utf8(&raw).unwrap()).unwrap(),
};
let root = BitMapBackend::new("security.png", (1280, 720)).into_drawing_area();
root.fill(&WHITE).unwrap();
let mean_max: f32 = export_results
.results
.iter()
.map(|(_, tx_result)| NonNan(tx_result.mean.0 as f32))
.max()
.unwrap()
.0;
let mean_min: f32 = export_results
.results
.iter()
.map(|(_, tx_result)| NonNan(tx_result.mean.0 as f32))
.min()
.unwrap()
.0;
let std_dev_max: f32 = export_results
.results
.iter()
.map(|(_, tx_result)| NonNan(tx_result.std_dev.0 as f32))
.max()
.unwrap()
.0;
let mut chart = ChartBuilder::on(&root)
.margin(4)
.caption("ĞMixer security", ("sans-serif", 30.0).into_font())
.x_label_area_size(30)
.y_label_area_size(40)
.right_y_label_area_size(40)
.build_ranged(0u32..export_results.settings.nb_blocks, mean_min..mean_max)
.unwrap()
.set_secondary_coord(0u32..export_results.settings.nb_blocks, 0f32..std_dev_max);
chart
.configure_mesh()
.x_desc("Blocks")
.y_desc("Probability")
.draw()
.unwrap();
chart
.configure_secondary_axes()
.y_desc("Probability standard deviation")
.draw()
.unwrap();
chart
.draw_series(PointSeries::of_element(
export_results
.results
.iter()
.map(|(block_id, tx_result)| (block_id.0, tx_result.mean.0 as f32))
.collect::<Vec<(u32, f32)>>(),
1,
&RED,
&|c, s, st| {
return EmptyElement::at(c) + Circle::new((0, 0), s, st.filled());
},
))
.unwrap();
chart
.draw_secondary_series(PointSeries::of_element(
export_results
.results
.iter()
.map(|(block_id, tx_result)| (block_id.0, tx_result.std_dev.0 as f32))
.collect::<Vec<(u32, f32)>>(),
1,
&BLUE,
&|c, s, st| {
return EmptyElement::at(c) + Circle::new((0, 0), s, st.filled());
},
))
.unwrap();
let root = BitMapBackend::new("effectiveness.png", (1280, 720)).into_drawing_area();
root.fill(&WHITE).unwrap();
let duration_max: u32 = export_results
.results
.iter()
.map(|(_, tx_result)| tx_result.duration)
.max()
.unwrap();
let duration_min: u32 = export_results
.results
.iter()
.map(|(_, tx_result)| tx_result.duration)
.min()
.unwrap();
let expired_max: u32 = *export_results.expired.iter().max().unwrap() as u32;
let mut chart = ChartBuilder::on(&root)
.margin(4)
.caption("ĞMixer effectiveness", ("sans-serif", 30.0).into_font())
.x_label_area_size(30)
.y_label_area_size(40)
.right_y_label_area_size(40)
.build_ranged(
0u32..export_results.settings.nb_blocks,
duration_min..duration_max,
)
.unwrap()
.set_secondary_coord(0u32..export_results.settings.nb_blocks, 0u32..expired_max);
chart
.configure_mesh()
.x_desc("Blocks")
.y_desc("Mix duration (blocks)")
.draw()
.unwrap();
chart
.configure_secondary_axes()
.y_desc("Expired mixes")
.draw()
.unwrap();
chart
.draw_series(PointSeries::of_element(
export_results
.results
.iter()
.map(|(block_id, tx_result)| (block_id.0, tx_result.duration))
.collect::<Vec<(u32, u32)>>(),
1,
&RED,
&|c, s, st| {
return EmptyElement::at(c) + Circle::new((0, 0), s, st.filled());
},
))
.unwrap();
chart
.draw_secondary_series(
Histogram::vertical(&chart)
.style(BLUE.mix(0.25).filled())
.data((0u32..).zip(export_results.expired.iter().map(|&x| x as u32))),
)
.unwrap();
}
mod client;
mod common;
mod convert;
mod graph;
mod local;
mod server;
mod utils;
......@@ -22,6 +23,7 @@ fn main() {
match opt.cmd {
cli::MainSubcommand::Client(cmd) => client::run_client(cmd),
cli::MainSubcommand::Convert(cmd) => convert::run_convert(cmd),
cli::MainSubcommand::Graph(cmd) => graph::run_graph(cmd),
cli::MainSubcommand::Local(cmd) => local::run_local(cmd),
cli::MainSubcommand::Server(cmd) => server::run_server(cmd),
}
......
......@@ -32,6 +32,12 @@ pub struct LocalSubcommand {
pub settings: Settings,
}
#[derive(Debug, StructOpt)]
pub struct GraphSubcommand {
#[structopt(short = "i")]
pub input_format: ExportFormat,
}
#[derive(Debug, StructOpt)]
pub struct ServerSubcommand {}
......@@ -39,6 +45,7 @@ pub struct ServerSubcommand {}
pub enum MainSubcommand {
Client(ClientSubcommand),
Convert(ConvertSubcommand),
Graph(GraphSubcommand),
Local(LocalSubcommand),
Server(ServerSubcommand),
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment