-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows inserts into separate tables to be executed concurrently across multiple threads
- Loading branch information
Showing
10 changed files
with
263 additions
and
104 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,103 @@ | ||
use std::env::current_dir; | ||
use std::{fs, process, thread}; | ||
use tempfile::NamedTempFile; | ||
|
||
use rand::rngs::StdRng; | ||
use rand::{Rng, SeedableRng}; | ||
use redb::{Database, ReadableTable, TableDefinition}; | ||
use std::time::Instant; | ||
|
||
const ELEMENTS: usize = 1_000_000; | ||
const RNG_SEED: u64 = 3; | ||
|
||
const TABLE1: TableDefinition<u128, u128> = TableDefinition::new("x"); | ||
const TABLE2: TableDefinition<u128, u128> = TableDefinition::new("y"); | ||
|
||
// TODO: multi-threaded inserts are slower. Probably due to lock contention checking dirty pages | ||
fn main() { | ||
let mut rng = StdRng::seed_from_u64(RNG_SEED); | ||
let mut values = vec![]; | ||
for _ in 0..ELEMENTS { | ||
values.push(rng.gen()); | ||
} | ||
|
||
let tmpdir = current_dir().unwrap().join(".benchmark"); | ||
fs::create_dir(&tmpdir).unwrap(); | ||
|
||
let tmpdir2 = tmpdir.clone(); | ||
ctrlc::set_handler(move || { | ||
fs::remove_dir_all(&tmpdir2).unwrap(); | ||
process::exit(1); | ||
}) | ||
.unwrap(); | ||
|
||
{ | ||
let tmpfile: NamedTempFile = NamedTempFile::new_in(current_dir().unwrap()).unwrap(); | ||
let db = unsafe { Database::builder().create_mmapped(tmpfile.path()).unwrap() }; | ||
|
||
let start = Instant::now(); | ||
let write_txn = db.begin_write().unwrap(); | ||
{ | ||
let mut table1 = write_txn.open_table(TABLE1).unwrap(); | ||
let mut table2 = write_txn.open_table(TABLE2).unwrap(); | ||
|
||
for value in values.iter() { | ||
table1.insert(value, value).unwrap(); | ||
table2.insert(value, value).unwrap(); | ||
} | ||
} | ||
write_txn.commit().unwrap(); | ||
let end = Instant::now(); | ||
let duration = end - start; | ||
println!( | ||
"single threaded load: {} pairs in {}ms", | ||
2 * ELEMENTS, | ||
duration.as_millis() | ||
); | ||
let read_txn = db.begin_read().unwrap(); | ||
let table = read_txn.open_table(TABLE1).unwrap(); | ||
assert_eq!(table.len().unwrap(), ELEMENTS); | ||
let table = read_txn.open_table(TABLE2).unwrap(); | ||
assert_eq!(table.len().unwrap(), ELEMENTS); | ||
} | ||
|
||
{ | ||
let tmpfile: NamedTempFile = NamedTempFile::new_in(current_dir().unwrap()).unwrap(); | ||
let db = unsafe { Database::builder().create_mmapped(tmpfile.path()).unwrap() }; | ||
|
||
let start = Instant::now(); | ||
let write_txn = db.begin_write().unwrap(); | ||
{ | ||
let mut table1 = write_txn.open_table(TABLE1).unwrap(); | ||
let mut table2 = write_txn.open_table(TABLE2).unwrap(); | ||
|
||
thread::scope(|s| { | ||
s.spawn(|| { | ||
for value in values.iter() { | ||
table1.insert(value, value).unwrap(); | ||
} | ||
}); | ||
s.spawn(|| { | ||
for value in values.iter() { | ||
table2.insert(value, value).unwrap(); | ||
} | ||
}); | ||
}); | ||
} | ||
write_txn.commit().unwrap(); | ||
let end = Instant::now(); | ||
let duration = end - start; | ||
println!( | ||
"2 threaded load: {} pairs in {}ms", | ||
2 * ELEMENTS, | ||
duration.as_millis() | ||
); | ||
let read_txn = db.begin_read().unwrap(); | ||
let table = read_txn.open_table(TABLE1).unwrap(); | ||
assert_eq!(table.len().unwrap(), ELEMENTS); | ||
let table = read_txn.open_table(TABLE2).unwrap(); | ||
assert_eq!(table.len().unwrap(), ELEMENTS); | ||
} | ||
|
||
fs::remove_dir_all(&tmpdir).unwrap(); | ||
} |
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
Oops, something went wrong.