serialization_census/
serialization_census.rs1use decl_lang::serialization_diagnostics::census;
7use decl_lang::session::{BindSource, EditKind, Mode, Op, Session};
8use serde_json::json;
9use std::io::Write;
10use std::path::Path;
11
12#[global_allocator]
13static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
14
15const USAGE: &str =
16 "usage: serialization-census MODEL INPUT_NAME INPUT_FILE ROOT OUTPUT_FILE [EDIT_PATH VALUE...]";
17
18fn main() {
19 std::thread::Builder::new()
20 .stack_size(1 << 30)
21 .spawn(run)
22 .unwrap()
23 .join()
24 .unwrap();
25}
26
27fn run() {
28 let args: Vec<String> = std::env::args().collect();
29 assert!(args.len() == 6 || args.len() >= 8, "{USAGE}");
30 let model = Path::new(&args[1]).canonicalize().unwrap();
31 let input = Path::new(&args[3]).canonicalize().unwrap();
32 let (input_name, root, output_file) = (&args[2], &args[4], Path::new(&args[5]));
33 assert!(!output_file.exists(), "refusing to overwrite the output");
34 let edit_path = args.get(6);
35 let mut steps: Vec<(&str, Option<&String>)> = vec![("initial", None), ("repeated", None)];
36 steps.extend(args.iter().skip(7).map(|value| ("update", Some(value))));
37
38 let mut session = Session::new(Some(model.to_str().unwrap()));
39 session
40 .apply(Op::Bind {
41 name: input_name.clone(),
42 src: BindSource::Inline {
43 text: std::fs::read_to_string(&input).unwrap(),
44 },
45 })
46 .unwrap();
47 let mut rows = Vec::with_capacity(steps.len());
48 for (index, (operation, value)) in steps.into_iter().enumerate() {
49 if let Some(value) = value {
50 session
51 .apply(Op::Edit {
52 kind: EditKind::Update,
53 path: edit_path.expect("an edit value without a path").clone(),
54 expr: Some(value.clone()),
55 })
56 .unwrap();
57 }
58 let run = session.run(Mode::Full);
59 let eng = run.eng.as_ref().expect("Session did not return an engine");
60 let value_root = eng.env.root(root).expect("the root was not evaluated");
61 let output_bytes = eng.serialize(&value_root, root, false).len();
63 let counted = census(&value_root, false);
64 rows.push(json!({
65 "operation": operation, "index": index, "value": value, "output_bytes": output_bytes,
66 "diagnostics": {
67 "load": run.load_diags.len(), "checks": run.checks.len(),
68 "session_checks": run.session_checks.len(), "evaluation": run.diags.len(),
69 },
70 "census": counted,
71 }));
72 }
73 let report = json!({
74 "schema": 1, "kind": "serialization-census", "timing_claim": false,
75 "allocator": "MiMalloc", "root": root, "input_name": input_name, "edit_path": edit_path,
76 "query_engine_environment": {
77 "DECL_QENGINE": std::env::var("DECL_QENGINE").ok(),
78 "DECL_QENGINE_STRICT": std::env::var("DECL_QENGINE_STRICT").ok(),
79 },
80 "rows": rows,
81 });
82 let mut file = std::fs::OpenOptions::new()
83 .write(true)
84 .create_new(true)
85 .open(output_file)
86 .unwrap();
87 file.write_all(&serde_json::to_vec_pretty(&report).unwrap())
88 .unwrap();
89 file.write_all(b"\n").unwrap();
90}