Skip to content

Commit c3fa793

Browse files
authored
perf: perfomance comparison with cbor and bson (#8)
* wip: perfom comparison with cbor and bson * lint
1 parent bdbc703 commit c3fa793

File tree

9 files changed

+882
-73
lines changed

9 files changed

+882
-73
lines changed

src/rust/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ serde-wasm-bindgen = "0.6.5"
4444
console_error_panic_hook = "0.1.7"
4545
console_log = "0.2"
4646

47+
#---Performance evaluation---
48+
bson = "2.13.0"
49+
serde_cbor = "0.10"
50+
4751
[dependencies]

src/rust/cli/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ clap = { workspace = true }
1010
anyhow = { workspace = true }
1111
serde = { workspace = true }
1212
serde_json = { workspace = true }
13+
bson = { workspace = true }
14+
serde_cbor = { workspace = true }
1315
thiserror = { workspace = true }

src/rust/cli/src/main.rs

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cjseq::{CityJSONFeature, Transform as CjTransform};
1+
use cjseq::{CityJSON, CityJSONFeature, Transform as CjTransform};
22
use clap::{Parser, Subcommand};
33
use fcb_core::error::Error;
44
use fcb_core::{
@@ -12,7 +12,6 @@ use std::{
1212
io::{self, BufReader, BufWriter, Read, Write},
1313
path::PathBuf,
1414
};
15-
1615
#[derive(Parser)]
1716
#[command(author, version, about = "CLI tool for CityJSON <-> FCB conversion")]
1817
struct Cli {
@@ -56,6 +55,26 @@ enum Commands {
5655
output: String,
5756
},
5857

58+
/// Convert CityJSON to CBOR
59+
Cbor {
60+
/// Input file (use '-' for stdin)
61+
#[arg(short, long)]
62+
input: String,
63+
/// Output file (use '-' for stdout)
64+
#[arg(short, long)]
65+
output: String,
66+
},
67+
68+
/// Convert CityJSON to BSON
69+
Bson {
70+
/// Input file (use '-' for stdin)
71+
#[arg(short, long)]
72+
input: String,
73+
/// Output file (use '-' for stdout)
74+
#[arg(short, long)]
75+
output: String,
76+
},
77+
5978
/// Show info about FCB file
6079
Info {
6180
/// Input FCB file
@@ -323,6 +342,55 @@ fn deserialize(input: &str, output: &str) -> Result<(), Error> {
323342
Ok(())
324343
}
325344

345+
fn encode_cbor(input: &str, output: &str) -> Result<(), Error> {
346+
let reader = BufReader::new(get_reader(input)?);
347+
let writer = BufWriter::new(get_writer(output)?);
348+
349+
let value: serde_json::Value = serde_json::from_reader(reader)?;
350+
serde_cbor::to_writer(writer, &value).map_err(|e| {
351+
Error::IoError(std::io::Error::new(
352+
std::io::ErrorKind::Other,
353+
format!("failed to encode to cbor: {}", e),
354+
))
355+
})?;
356+
357+
if output != "-" {
358+
eprintln!("successfully encoded to cbor");
359+
}
360+
Ok(())
361+
}
362+
363+
fn encode_bson(input: &str, output: &str) -> Result<(), Error> {
364+
let mut reader = BufReader::new(get_reader(input)?);
365+
let json_str = {
366+
let mut s = String::new();
367+
reader.read_to_string(&mut s)?;
368+
s
369+
};
370+
371+
let cityjson: CityJSON = serde_json::from_str(&json_str)?;
372+
let bson = bson::to_bson(&cityjson).map_err(|e| {
373+
Error::IoError(std::io::Error::new(
374+
std::io::ErrorKind::Other,
375+
format!("failed to encode to bson: {}", e),
376+
))
377+
})?;
378+
let doc = bson.as_document().unwrap();
379+
380+
let mut writer = get_writer(output)?;
381+
doc.to_writer(&mut writer).map_err(|e| {
382+
Error::IoError(std::io::Error::new(
383+
std::io::ErrorKind::Other,
384+
format!("failed to encode to bson: {}", e),
385+
))
386+
})?;
387+
388+
if output != "-" {
389+
eprintln!("successfully encoded to bson");
390+
}
391+
Ok(())
392+
}
393+
326394
fn show_info(input: PathBuf) -> Result<(), Error> {
327395
let reader = BufReader::new(File::open(input)?);
328396
let metadata = reader.get_ref().metadata()?.len() / 1024 / 1024; // show in megabytes
@@ -386,6 +454,8 @@ fn main() -> Result<(), Error> {
386454
ge,
387455
} => serialize(&input, &output, attr_index, bbox, ge),
388456
Commands::Deser { input, output } => deserialize(&input, &output),
457+
Commands::Cbor { input, output } => encode_cbor(&input, &output),
458+
Commands::Bson { input, output } => encode_bson(&input, &output),
389459
Commands::Info { input } => show_info(input),
390460
}
391461
}

src/rust/fcb_core/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ path = "src/lib.rs"
6161
name = "read"
6262
path = "src/bin/read.rs"
6363

64+
[[bin]]
65+
name = "read_profile"
66+
path = "benches/read_profile.rs"
67+
68+
[[bin]]
69+
name = "read_benches"
70+
path = "benches/read_profile.rs"
71+
6472

6573
[[bin]]
6674
name = "read_cj"
@@ -80,3 +88,5 @@ memory-stats = { workspace = true }
8088
pretty_assertions = { workspace = true }
8189
criterion = { workspace = true, features = ["async_tokio", "html_reports"] }
8290
tokio = { workspace = true, features = ["macros", "rt-multi-thread"] }
91+
bson = { workspace = true }
92+
serde_cbor = { workspace = true }

0 commit comments

Comments
 (0)