Skip to content

Commit

Permalink
Add json to comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
torkleyy committed Oct 16, 2021
1 parent a91667c commit 8eeaa03
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*.md saneWs
Cargo.lock text

LICENSE saneWs

.gitattributes saneWs
.gitignore saneWs

Expand Down
12 changes: 0 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,6 @@ edition = "2018"

default-run = "ron-bench"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "ron-bench"
path = "src/main.rs"

[[bin]]
name = "convert-json"
path = "src/bin/convert-json.rs"
test = false
bench = false

[dependencies]
criterion = { version = "*", features = ["html_reports"] }
ron = { git = "https://github.com/ron-rs/ron" }
Expand Down
5 changes: 3 additions & 2 deletions src/bin/convert-json.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::fs::File;
use ron::ser::to_writer;
use serde_json::from_reader as json_from_reader;
use std::fs::File;

// Convert .json to .ron
fn main() {
let value: serde_json::Value = json_from_reader(File::open("../../data/canada.json").unwrap()).unwrap();
let value: serde_json::Value =
json_from_reader(File::open("../../data/canada.json").unwrap()).unwrap();
to_writer(File::create("../../data/canada.ron").unwrap(), &value).unwrap();
}
39 changes: 31 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
#![allow(dead_code)]

use criterion::{criterion_group, BenchmarkId, Criterion, PlottingBackend};
use std::fs::read_to_string;
use criterion::{criterion_group, Criterion, BenchmarkId, PlottingBackend};
use std::path::{Path, PathBuf};

fn json_de(content: &str) -> impl Sized {
serde_json::from_str::<ron::Value>(content)
}

fn ron_de_legacy(content: &str) -> impl Sized {
ron::from_str::<ron::Value>(content)
Expand All @@ -9,14 +16,27 @@ fn ron_de_reboot(content: &str) -> impl Sized {
ron_reboot::from_str::<ron::Value>(content)
}

fn with_extension(path: impl AsRef<Path>, ext: &str) -> PathBuf {
path.as_ref().with_extension(ext)
}

fn bench_serde_de(c: &mut Criterion) {
let mut group = c.benchmark_group("Serde Deserialization");
for i in ["data/canada.ron"].iter() {
let content = read_to_string(i).unwrap();
group.bench_with_input(BenchmarkId::new("ron", i), &content,
|b, i| b.iter(|| ron_de_legacy(i)));
group.bench_with_input(BenchmarkId::new("ron-reboot", i), &content,
|b, i| b.iter(|| ron_de_reboot(i)));
for file in ["data/canada"].iter() {
let content_json = read_to_string(&with_extension(file, "json")).unwrap();
let content = read_to_string(&with_extension(file, "json")).unwrap();
group.bench_with_input(BenchmarkId::new("json", file), &content_json, |b, i| {
b.iter(|| json_de(i))
});
group.bench_with_input(BenchmarkId::new("ron", file), &content, |b, i| {
b.iter(|| ron_de_legacy(i))
});
/*
TODO: infinite loop bug...
group.bench_with_input(BenchmarkId::new("ron-reboot", i), &content, |b, i| {
b.iter(|| ron_de_reboot(i))
});
*/
}
group.finish();
}
Expand All @@ -27,5 +47,8 @@ criterion_group!(benches, bench_serde_de);
fn main() {
benches();

Criterion::default().configure_from_args().plotting_backend(PlottingBackend::Plotters).final_summary();
Criterion::default()
.configure_from_args()
.plotting_backend(PlottingBackend::Plotters)
.final_summary();
}

0 comments on commit 8eeaa03

Please sign in to comment.