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:
WindSoilder
2022-08-24 00:49:51 +08:00
committed by GitHub
parent 2a310ef187
commit a7295c8f1b
5 changed files with 213 additions and 2 deletions

View File

@ -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

View File

@ -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`.

View 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);

View File

@ -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,
};