47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use serde_json::{Value, value::RawValue};
|
|
use std::hint::black_box;
|
|
use std::time::Instant;
|
|
|
|
#[derive(Deserialize)]
|
|
struct Input {
|
|
source: Box<RawValue>,
|
|
update: bool,
|
|
ticks: String,
|
|
}
|
|
|
|
#[derive(Serialize)]
|
|
struct Sample {
|
|
elapsed_ns: u128,
|
|
checksum: usize,
|
|
output: String,
|
|
}
|
|
|
|
fn transform(source: &str, update: bool) -> String {
|
|
let mut value: Value = serde_json::from_str(source).expect("valid JSON input");
|
|
let object = value.as_object_mut().expect("object input");
|
|
if update {
|
|
object.insert("active".into(), Value::Bool(true));
|
|
serde_json::to_string(&value).expect("serializable object")
|
|
} else {
|
|
serde_json::to_string(object.get("data").expect("data field")).expect("serializable value")
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
let argument = std::env::args().nth(1).expect("one JSON argument");
|
|
let input: Input = serde_json::from_str(&argument).expect("valid input fields");
|
|
let output = transform(input.source.get(), input.update);
|
|
let mut checksum = 0;
|
|
let started = Instant::now();
|
|
for _ in input.ticks.bytes() {
|
|
checksum += transform(black_box(input.source.get()), input.update).len();
|
|
}
|
|
let sample = Sample {
|
|
elapsed_ns: started.elapsed().as_nanos(),
|
|
checksum,
|
|
output,
|
|
};
|
|
println!("{}", serde_json::to_string(&sample).unwrap());
|
|
}
|