mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Plugin: Add benchmark for different encoding protocol (#6384)
* add MessagePack as a plugin protocol * tmp merge from remote * add benchmark * use less benchmark group, and add README for analysing benchmark result * update README * update README * rewrite * remove comment * rename * fmt Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
@ -18,3 +18,10 @@ byte-order = "0.3.0"
|
||||
rmp = "0.8.11"
|
||||
rmp-serde = "1.1.0"
|
||||
rmpv = "1.0.0"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
name = "encoder_benchmark"
|
||||
harness = false
|
||||
|
@ -16,3 +16,8 @@ The steps are as follows:
|
||||
7. Modify the serialize/deserialize functions. Check the following PRs for details:
|
||||
* https://github.com/nushell/nushell/pull/4980
|
||||
* https://github.com/nushell/nushell/pull/4920
|
||||
|
||||
## Benchmark
|
||||
Here is a simple benchmark for different protocol for encoding/decoding nushell table, with different rows and columns. You can simply run `cargo bench` to run benchmark.
|
||||
|
||||
The relative html report is in `target/criterion/report/index.html`.
|
||||
|
76
crates/nu-plugin/benches/encoder_benchmark.rs
Normal file
76
crates/nu-plugin/benches/encoder_benchmark.rs
Normal file
@ -0,0 +1,76 @@
|
||||
use criterion::{criterion_group, criterion_main, Criterion};
|
||||
use nu_plugin::{EncodingType, PluginResponse};
|
||||
use nu_protocol::{Span, Value};
|
||||
|
||||
// generate a new table data with `row_cnt` rows, `col_cnt` columns.
|
||||
fn new_test_data(row_cnt: usize, col_cnt: usize) -> Value {
|
||||
let columns: Vec<String> = (0..col_cnt).map(|x| format!("col_{x}")).collect();
|
||||
let vals: Vec<Value> = (0..col_cnt as i64).map(|i| Value::test_int(i)).collect();
|
||||
|
||||
Value::List {
|
||||
vals: (0..row_cnt)
|
||||
.map(|_| Value::test_record(columns.clone(), vals.clone()))
|
||||
.collect(),
|
||||
span: Span::test_data(),
|
||||
}
|
||||
}
|
||||
|
||||
fn bench_encoding(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Encoding");
|
||||
let test_cnt_pairs = [
|
||||
(100, 5),
|
||||
(100, 10),
|
||||
(100, 15),
|
||||
(1000, 5),
|
||||
(1000, 10),
|
||||
(1000, 15),
|
||||
(10000, 5),
|
||||
(10000, 10),
|
||||
(10000, 15),
|
||||
];
|
||||
for (row_cnt, col_cnt) in test_cnt_pairs.into_iter() {
|
||||
for fmt in ["capnp", "json", "msgpack"] {
|
||||
group.bench_function(&format!("{fmt} encode {row_cnt} * {col_cnt}"), |b| {
|
||||
let mut res = vec![];
|
||||
let test_data = PluginResponse::Value(Box::new(new_test_data(row_cnt, col_cnt)));
|
||||
let encoder = EncodingType::try_from_bytes(fmt.as_bytes()).unwrap();
|
||||
b.iter(|| encoder.encode_response(&test_data, &mut res))
|
||||
});
|
||||
}
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
fn bench_decoding(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("Decoding");
|
||||
let test_cnt_pairs = [
|
||||
(100, 5),
|
||||
(100, 10),
|
||||
(100, 15),
|
||||
(1000, 5),
|
||||
(1000, 10),
|
||||
(1000, 15),
|
||||
(10000, 5),
|
||||
(10000, 10),
|
||||
(10000, 15),
|
||||
];
|
||||
for (row_cnt, col_cnt) in test_cnt_pairs.into_iter() {
|
||||
for fmt in ["capnp", "json", "msgpack"] {
|
||||
group.bench_function(&format!("{fmt} decode for {row_cnt} * {col_cnt}"), |b| {
|
||||
let mut res = vec![];
|
||||
let test_data = PluginResponse::Value(Box::new(new_test_data(row_cnt, col_cnt)));
|
||||
let encoder = EncodingType::try_from_bytes(fmt.as_bytes()).unwrap();
|
||||
encoder.encode_response(&test_data, &mut res).unwrap();
|
||||
let mut binary_data = std::io::Cursor::new(res);
|
||||
b.iter(|| {
|
||||
binary_data.set_position(0);
|
||||
encoder.decode_response(&mut binary_data)
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, bench_encoding, bench_decoding);
|
||||
criterion_main!(benches);
|
@ -6,7 +6,7 @@ mod serializers;
|
||||
mod plugin_capnp;
|
||||
|
||||
pub use plugin::{get_signature, serve_plugin, Plugin, PluginDeclaration};
|
||||
pub use protocol::{EvaluatedCall, LabeledError, PluginData};
|
||||
pub use protocol::{EvaluatedCall, LabeledError, PluginData, PluginResponse};
|
||||
pub use serializers::{
|
||||
capnp::CapnpSerializer, json::JsonSerializer, msgpack::MsgPackSerializer, EncodingType,
|
||||
};
|
||||
|
Reference in New Issue
Block a user