-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add benchmark for buf write (#2922)
* Add benchmark Signed-off-by: Xuanwo <[email protected]> * Improve Signed-off-by: Xuanwo <[email protected]> * Fix build Signed-off-by: Xuanwo <[email protected]> --------- Signed-off-by: Xuanwo <[email protected]>
- Loading branch information
Showing
6 changed files
with
199 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
mod utils; | ||
mod write; | ||
|
||
use criterion::criterion_group; | ||
use criterion::criterion_main; | ||
|
||
criterion_group!( | ||
benches, | ||
write::bench_at_least_buf_write, | ||
write::bench_exact_buf_write, | ||
); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use async_trait::async_trait; | ||
use bytes::Bytes; | ||
use opendal::raw::oio; | ||
use opendal::raw::oio::Streamer; | ||
use rand::prelude::ThreadRng; | ||
use rand::RngCore; | ||
|
||
/// BlackHoleWriter will discard all data written to it so we can measure the buffer's cost. | ||
pub struct BlackHoleWriter; | ||
|
||
#[async_trait] | ||
impl oio::Write for BlackHoleWriter { | ||
async fn write(&mut self, _: Bytes) -> opendal::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn sink(&mut self, _: u64, _: Streamer) -> opendal::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn abort(&mut self) -> opendal::Result<()> { | ||
Ok(()) | ||
} | ||
|
||
async fn close(&mut self) -> opendal::Result<()> { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn gen_bytes(rng: &mut ThreadRng, size: usize) -> Bytes { | ||
let mut content = vec![0; size]; | ||
rng.fill_bytes(&mut content); | ||
|
||
content.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use criterion::Criterion; | ||
use once_cell::sync::Lazy; | ||
use opendal::raw::oio::{AtLeastBufWriter, ExactBufWriter, Write}; | ||
use rand::thread_rng; | ||
use size::Size; | ||
|
||
use super::utils::*; | ||
|
||
pub static TOKIO: Lazy<tokio::runtime::Runtime> = | ||
Lazy::new(|| tokio::runtime::Runtime::new().expect("build tokio runtime")); | ||
|
||
pub fn bench_at_least_buf_write(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("at_least_buf_write"); | ||
|
||
let mut rng = thread_rng(); | ||
|
||
for size in [ | ||
Size::from_kibibytes(4), | ||
Size::from_kibibytes(256), | ||
Size::from_mebibytes(4), | ||
Size::from_mebibytes(16), | ||
] { | ||
let content = gen_bytes(&mut rng, size.bytes() as usize); | ||
|
||
group.throughput(criterion::Throughput::Bytes(size.bytes() as u64)); | ||
group.bench_with_input(size.to_string(), &content, |b, content| { | ||
b.to_async(&*TOKIO).iter(|| async { | ||
let mut w = AtLeastBufWriter::new(BlackHoleWriter, 256 * 1024); | ||
w.write(content.clone()).await.unwrap(); | ||
w.close().await.unwrap(); | ||
}) | ||
}); | ||
} | ||
|
||
group.finish() | ||
} | ||
|
||
pub fn bench_exact_buf_write(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("exact_buf_write"); | ||
|
||
let mut rng = thread_rng(); | ||
|
||
for size in [ | ||
Size::from_kibibytes(4), | ||
Size::from_kibibytes(256), | ||
Size::from_mebibytes(4), | ||
Size::from_mebibytes(16), | ||
] { | ||
let content = gen_bytes(&mut rng, size.bytes() as usize); | ||
|
||
group.throughput(criterion::Throughput::Bytes(size.bytes() as u64)); | ||
group.bench_with_input(size.to_string(), &content, |b, content| { | ||
b.to_async(&*TOKIO).iter(|| async { | ||
let mut w = ExactBufWriter::new(BlackHoleWriter, 256 * 1024); | ||
w.write(content.clone()).await.unwrap(); | ||
w.close().await.unwrap(); | ||
}) | ||
}); | ||
} | ||
|
||
group.finish() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters