frame_benchmarking_cli/storage/
template.rs1use sc_cli::Result;
19use sc_service::Configuration;
20
21use log::info;
22use serde::Serialize;
23use std::{env, fs, path::PathBuf};
24
25use super::cmd::StorageParams;
26use crate::shared::{Stats, UnderscoreHelper};
27
28static VERSION: &str = env!("CARGO_PKG_VERSION");
29static TEMPLATE: &str = include_str!("./weights.hbs");
30
31#[derive(Serialize, Default, Debug, Clone)]
33pub(crate) struct TemplateData {
34 db_name: String,
36 block_number: String,
38 runtime_name: String,
40 version: String,
42 date: String,
44 hostname: String,
46 cpuname: String,
48 header: String,
50 args: Vec<String>,
52 params: StorageParams,
54 read_weight: u64,
56 write_weight: u64,
58 read: Option<(Stats, Stats)>,
61 write: Option<(Stats, Stats)>,
64}
65
66impl TemplateData {
67 pub fn new(cfg: &Configuration, params: &StorageParams) -> Result<Self> {
69 let header = params
70 .header
71 .as_ref()
72 .map(|p| std::fs::read_to_string(p))
73 .transpose()?
74 .unwrap_or_default();
75
76 Ok(TemplateData {
77 db_name: if params.is_validate_block_mode() {
78 String::from("InMemoryDb")
79 } else {
80 format!("{}", cfg.database)
81 },
82 runtime_name: cfg.chain_spec.name().into(),
83 version: VERSION.into(),
84 date: chrono::Utc::now().format("%Y-%m-%d (Y/M/D)").to_string(),
85 hostname: params.hostinfo.hostname(),
86 cpuname: params.hostinfo.cpuname(),
87 header,
88 args: env::args().collect::<Vec<String>>(),
89 params: params.clone(),
90 ..Default::default()
91 })
92 }
93
94 pub fn set_stats(
96 &mut self,
97 read: Option<(Stats, Stats)>,
98 write: Option<(Stats, Stats)>,
99 ) -> Result<()> {
100 if let Some(read) = read {
101 self.read_weight = self.params.weight_params.calc_weight(&read.0)?;
102 self.read = Some(read);
103 }
104 if let Some(write) = write {
105 self.write_weight = self.params.weight_params.calc_weight(&write.0)?;
106 self.write = Some(write);
107 }
108 Ok(())
109 }
110
111 pub fn set_block_number(&mut self, block_number: String) {
113 self.block_number = block_number
114 }
115
116 pub fn write(&self, path: &Option<PathBuf>, hbs_template: &Option<PathBuf>) -> Result<()> {
119 let mut handlebars = handlebars::Handlebars::new();
120 handlebars.register_helper("underscore", Box::new(UnderscoreHelper));
122 handlebars.register_escape_fn(|s| -> String { s.to_string() });
124 let template = match hbs_template {
126 Some(template) if template.is_file() => fs::read_to_string(template)?,
127 Some(_) => return Err("Handlebars template is not a valid file!".into()),
128 None => TEMPLATE.to_string(),
129 };
130
131 let out_path = self.build_path(path);
132 let mut fd = fs::File::create(&out_path)?;
133 info!("Writing weights to {:?}", fs::canonicalize(&out_path)?);
134
135 handlebars
136 .render_template_to_write(&template, &self, &mut fd)
137 .map_err(|e| format!("HBS template write: {:?}", e).into())
138 }
139
140 fn build_path(&self, weight_out: &Option<PathBuf>) -> PathBuf {
142 let mut path = match weight_out {
143 Some(p) => PathBuf::from(p),
144 None => PathBuf::new(),
145 };
146
147 if path.is_dir() || path.as_os_str().is_empty() {
148 path.push(format!("{}_weights", self.db_name.to_lowercase()));
149 path.set_extension("rs");
150 }
151 path
152 }
153}