Skip to content

Commit

Permalink
Get DA costs from predefined data (#2157)
Browse files Browse the repository at this point in the history
## Linked Issues/PRs
FuelLabs/fuel-core#2138

## Description
This PR allows the analysis CLI tool to read data from a .csv file. 

Along with that work, I did significant refactoring and. cleanup to help
other engineers use the tool.

There are still some bugs that need to be sorted out as follow-up to
this issue:
FuelLabs/fuel-core#2167
FuelLabs/fuel-core#2164

### Before requesting review
- [x] I have reviewed the code myself
- [x] I have created follow-up issues caused by this PR and linked them
here

---------

Co-authored-by: green <xgreenx9999@gmail.com>
  • Loading branch information
2 people authored and rymnc committed Sep 6, 2024
1 parent 55fc69a commit 416f9db
Show file tree
Hide file tree
Showing 13 changed files with 632 additions and 354 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
Cargo.lock
gas-price.png
gas-prices.png
predefined_data/
charts/
3 changes: 3 additions & 0 deletions crates/fuel-gas-price-algorithm/gas-price-analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ publish = false
[workspace]

[dependencies]
anyhow = "1.0.86"
clap = { version = "4.5.16", features = ["derive"] }
csv = "1.3.0"
fuel-gas-price-algorithm = { path = ".." }
plotters = "0.3.5"
rand = "0.8.5"
rand_distr = "0.4.3"
serde = { version = "1.0.209", features = ["derive"] }
150 changes: 113 additions & 37 deletions crates/fuel-gas-price-algorithm/gas-price-analysis/src/charts.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,81 @@
use super::*;
use std::{
fs,
path::PathBuf,
};

pub fn draw_chart(
results: SimulationResults,
p_comp: i64,
d_comp: i64,
file_path: &str,
) -> anyhow::Result<()> {
let SimulationResults {
gas_prices,
exec_gas_prices,
da_gas_prices,
fullness,
bytes_and_costs,
actual_profit,
projected_profit,
pessimistic_costs,
} = results;

let plot_width = 640 * 2 * 2;
let plot_height = 480 * 3;

let path: PathBuf = file_path.into();

if path.is_dir() {
println!("Creating chart at: {}", file_path);
fs::create_dir_all(file_path)?;
} else {
let new_path = path.parent().ok_or(anyhow::anyhow!("Path has no parent"))?;
println!("Creating chart at: {}", new_path.display());
fs::create_dir_all(new_path)?;
}
let root =
BitMapBackend::new(file_path, (plot_width, plot_height)).into_drawing_area();
root.fill(&WHITE).unwrap();
let (window_one, lower) = root.split_vertically(plot_height / 4);
let (window_two, new_lower) = lower.split_vertically(plot_height / 4);
let (window_three, window_four) = new_lower.split_vertically(plot_height / 4);

draw_fullness(&window_one, &fullness, "Fullness")?;

draw_bytes_and_cost_per_block(&window_two, &bytes_and_costs, "Bytes Per Block")?;

draw_profit(
&window_three,
&actual_profit,
&projected_profit,
&pessimistic_costs,
&format!(
"Profit p_comp: {}, d_comp: {}",
prettify_number(p_comp),
prettify_number(d_comp)
),
)?;
draw_gas_prices(
&window_four,
&gas_prices,
&exec_gas_prices,
&da_gas_prices,
"Gas Prices",
)?;

root.present()?;

Ok(())
}

pub fn draw_gas_prices(
drawing_area: &DrawingArea<BitMapBackend, Shift>,
gas_prices: &[u64],
_exec_gas_prices: &[u64],
da_gas_prices: &[u64],
title: &str,
) {
) -> anyhow::Result<()> {
// const GAS_PRICE_COLOR: RGBColor = BLACK;
// const EXEC_GAS_PRICE_COLOR: RGBColor = RED;
const DA_GAS_PRICE_COLOR: RGBColor = BLUE;
Expand All @@ -19,15 +88,13 @@ pub fn draw_gas_prices(
.x_label_area_size(40)
.y_label_area_size(100)
.right_y_label_area_size(100)
.build_cartesian_2d(0..gas_prices.len(), min..max)
.unwrap();
.build_cartesian_2d(0..gas_prices.len(), min..max)?;

chart
.configure_mesh()
.y_desc("DA Gas Price")
.x_desc("Block")
.draw()
.unwrap();
.draw()?;

// chart
// .draw_series(LineSeries::new(
Expand Down Expand Up @@ -55,24 +122,24 @@ pub fn draw_gas_prices(
.draw_series(LineSeries::new(
da_gas_prices.iter().enumerate().map(|(x, y)| (x, *y)),
DA_GAS_PRICE_COLOR,
))
.unwrap()
))?
.label("DA Gas Price")
.legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], DA_GAS_PRICE_COLOR));

chart
.configure_series_labels()
.background_style(WHITE.mix(0.8))
.border_style(BLACK)
.draw()
.unwrap();
.draw()?;

Ok(())
}

pub fn draw_fullness(
drawing_area: &DrawingArea<BitMapBackend, Shift>,
fullness: &Vec<(u64, u64)>,
title: &str,
) {
) -> anyhow::Result<()> {
const FULLNESS_COLOR: RGBColor = BLACK;

let min = 0;
Expand All @@ -84,15 +151,13 @@ pub fn draw_fullness(
.x_label_area_size(40)
.y_label_area_size(100)
.right_y_label_area_size(100)
.build_cartesian_2d(0..fullness.len(), min..max)
.unwrap();
.build_cartesian_2d(0..fullness.len(), min..max)?;

chart
.configure_mesh()
.y_desc("Fullness Percentage")
.x_desc("Block")
.draw()
.unwrap();
.draw()?;

chart
.draw_series(LineSeries::new(
Expand All @@ -111,15 +176,16 @@ pub fn draw_fullness(
.configure_series_labels()
.background_style(WHITE.mix(0.8))
.border_style(BLACK)
.draw()
.unwrap();
.draw()?;

Ok(())
}

pub fn draw_bytes_and_cost_per_block(
drawing_area: &DrawingArea<BitMapBackend, Shift>,
bytes_and_costs_per_block: &[(u64, u64)],
title: &str,
) {
) -> anyhow::Result<()> {
const BYTES_PER_BLOCK_COLOR: RGBColor = BLACK;
let (bytes, costs): (Vec<u64>, Vec<u64>) =
bytes_and_costs_per_block.iter().cloned().unzip();
Expand Down Expand Up @@ -177,25 +243,39 @@ pub fn draw_bytes_and_cost_per_block(
.border_style(BLACK)
.draw()
.unwrap();

Ok(())
}

pub fn draw_profit(
drawing_area: &DrawingArea<BitMapBackend, Shift>,
actual_profit: &[i64],
projected_profit: &[i64],
pessimistic_block_costs: &[u64],
actual_profit: &[i128],
projected_profit: &[i128],
pessimistic_block_costs: &[u128],
title: &str,
) {
) -> anyhow::Result<()> {
const ACTUAL_PROFIT_COLOR: RGBColor = BLACK;
const PROJECTED_PROFIT_COLOR: RGBColor = RED;
const PESSIMISTIC_BLOCK_COST_COLOR: RGBColor = BLUE;
let min = *std::cmp::min(
actual_profit.iter().min().unwrap(),
projected_profit.iter().min().unwrap_or(&0),
actual_profit
.iter()
.min()
.ok_or(anyhow::anyhow!("Path has no parent"))?,
projected_profit
.iter()
.min()
.ok_or(anyhow::anyhow!("Path has no parent"))?,
);
let max = *std::cmp::max(
actual_profit.iter().max().unwrap(),
projected_profit.iter().max().unwrap_or(&0),
actual_profit
.iter()
.max()
.ok_or(anyhow::anyhow!("Path has no parent"))?,
projected_profit
.iter()
.max()
.ok_or(anyhow::anyhow!("Path has no parent"))?,
);

let mut chart = ChartBuilder::on(drawing_area)
Expand All @@ -215,21 +295,18 @@ pub fn draw_profit(
.configure_mesh()
.y_desc("Profit")
.x_desc("Block")
.draw()
.unwrap();
.draw()?;

chart
.configure_secondary_axes()
.y_desc("Pessimistic cost")
.draw()
.unwrap();
.draw()?;

chart
.draw_series(LineSeries::new(
actual_profit.iter().enumerate().map(|(x, y)| (x, *y)),
ACTUAL_PROFIT_COLOR,
))
.unwrap()
))?
.label("Actual Profit")
.legend(|(x, y)| {
PathElement::new(vec![(x, y), (x + 20, y)], ACTUAL_PROFIT_COLOR)
Expand All @@ -239,8 +316,7 @@ pub fn draw_profit(
.draw_series(LineSeries::new(
projected_profit.iter().enumerate().map(|(x, y)| (x, *y)),
PROJECTED_PROFIT_COLOR,
))
.unwrap()
))?
.label("Projected Profit")
.legend(|(x, y)| {
PathElement::new(vec![(x, y), (x + 20, y)], PROJECTED_PROFIT_COLOR)
Expand All @@ -254,8 +330,7 @@ pub fn draw_profit(
.enumerate()
.map(|(x, y)| (x, *y)),
PESSIMISTIC_BLOCK_COST_COLOR,
))
.unwrap()
))?
.label("Pessimistic Block Costs")
.legend(|(x, y)| {
PathElement::new(vec![(x, y), (x + 20, y)], PESSIMISTIC_BLOCK_COST_COLOR)
Expand All @@ -265,6 +340,7 @@ pub fn draw_profit(
.configure_series_labels()
.background_style(WHITE.mix(0.8))
.border_style(BLACK)
.draw()
.unwrap();
.draw()?;

Ok(())
}
Loading

0 comments on commit 416f9db

Please sign in to comment.