-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MrCroxx <[email protected]>
- Loading branch information
Showing
65 changed files
with
567 additions
and
62 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
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
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
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
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
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,111 @@ | ||
// Copyright 2024 MrCroxx | ||
// | ||
// Licensed 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 std::{ | ||
sync::Arc, | ||
time::{Duration, Instant, SystemTime, UNIX_EPOCH}, | ||
}; | ||
|
||
use clap::Parser; | ||
use foyer_common::runtime::BackgroundShutdownRuntime; | ||
use foyer_experimental::tombstone::{Tombstone, TombstoneLog, TombstoneLogConfig}; | ||
use itertools::Itertools; | ||
use rand::{rngs::StdRng, Rng, SeedableRng}; | ||
|
||
#[derive(Parser, Debug, Clone)] | ||
#[command(author, version, about)] | ||
pub struct Args { | ||
/// dir for cache data | ||
#[arg(short, long)] | ||
dir: String, | ||
|
||
/// writer concurrency | ||
#[arg(short, long, default_value_t = 1024)] | ||
concurrency: usize, | ||
|
||
/// time (s) | ||
#[arg(short, long, default_value_t = 10)] | ||
time: usize, | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let args = Args::parse(); | ||
|
||
let rt = tokio::runtime::Builder::new_multi_thread() | ||
.enable_all() | ||
.build() | ||
.unwrap(); | ||
let rt = BackgroundShutdownRuntime::from(rt); | ||
let rt = Arc::new(rt); | ||
|
||
let config = TombstoneLogConfig { | ||
id: 0, | ||
dir: args.dir.clone().into(), | ||
}; | ||
let log = TombstoneLog::open(config).await.unwrap(); | ||
|
||
let handles = (0..args.concurrency) | ||
.map(|_| { | ||
let log = log.clone(); | ||
let args = args.clone(); | ||
let rt = rt.clone(); | ||
tokio::spawn(async move { | ||
write(log.clone(), args, rt).await; | ||
}) | ||
}) | ||
.collect_vec(); | ||
|
||
for handle in handles { | ||
handle.await.unwrap(); | ||
} | ||
|
||
println!("Bench finishes."); | ||
|
||
log.close().await.unwrap(); | ||
} | ||
|
||
async fn write(log: TombstoneLog<u64>, args: Args, rt: Arc<BackgroundShutdownRuntime>) { | ||
let start = Instant::now(); | ||
let mut log = log; | ||
|
||
let mut rng = StdRng::seed_from_u64( | ||
SystemTime::now() | ||
.duration_since(UNIX_EPOCH) | ||
.unwrap() | ||
.as_nanos() as _, | ||
); | ||
loop { | ||
if start.elapsed() >= Duration::from_secs(args.time as _) { | ||
return; | ||
} | ||
|
||
// let now = Instant::now(); | ||
|
||
let tombstone = Tombstone::new(rng.gen(), rng.gen()); | ||
log = rt | ||
.spawn(async move { | ||
log.append(tombstone).await.unwrap(); | ||
log | ||
}) | ||
.await | ||
.unwrap(); | ||
|
||
// let duration = now.elapsed(); | ||
|
||
// if duration.as_micros() >= 1000 { | ||
// println!("slow: {:?}", duration); | ||
// } | ||
} | ||
} |
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,65 @@ | ||
// Copyright 2024 MrCroxx | ||
// | ||
// Licensed 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 std::backtrace::Backtrace; | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
#[error("{0}")] | ||
pub struct Error(Box<ErrorInner>); | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
#[error("{source:?}")] | ||
struct ErrorInner { | ||
#[from] | ||
source: ErrorKind, | ||
backtrace: Backtrace, | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum ErrorKind { | ||
#[error("io error: {0}")] | ||
Io(#[from] std::io::Error), | ||
#[error("other error: {0}")] | ||
Other(#[from] anyhow::Error), | ||
} | ||
|
||
impl From<ErrorKind> for Error { | ||
fn from(value: ErrorKind) -> Self { | ||
value.into() | ||
} | ||
} | ||
|
||
impl From<std::io::Error> for Error { | ||
fn from(value: std::io::Error) -> Self { | ||
value.into() | ||
} | ||
} | ||
|
||
impl From<anyhow::Error> for Error { | ||
fn from(value: anyhow::Error) -> Self { | ||
value.into() | ||
} | ||
} | ||
|
||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_error_size() { | ||
assert_eq!(std::mem::size_of::<Error>(), std::mem::size_of::<usize>()); | ||
} | ||
} |
Oops, something went wrong.