Skip to content

Commit 79502f3

Browse files
committed
lol
1 parent d9aad66 commit 79502f3

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

rust/benches/benchmarks/create.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use {
2-
linkspsql::{connect, Sql},
32
criterion::{measurement::WallTime, BenchmarkGroup, Criterion},
43
doublets::{data::LinkType, mem::Alloc, split, unit, Doublets},
54
linkspsql::{
65
benchmark, elapsed, prepare_file, Benched, Client, Fork, Result, Transaction,
76
BACKGROUND_LINKS,
87
},
8+
linkspsql::{connect, Sql},
99
std::{
1010
alloc::Global,
1111
time::{Duration, Instant},
@@ -14,7 +14,7 @@ use {
1414
};
1515

1616
fn bench<S: Benched + Doublets<T>, T: LinkType>(
17-
builder: S::Builder<'_>,
17+
builder: S::Builder,
1818
id: &str,
1919
group: &mut BenchmarkGroup<WallTime>,
2020
) -> Result<()> {
@@ -38,8 +38,8 @@ pub fn create_links(c: &mut Criterion) -> Result<()> {
3838
//let split_index = prepare_file("split_index.links")?;
3939
//bench::<Client<usize>, _>(&runtime, "PSQL_NonTransaction", &mut group).unwrap();
4040
{
41-
let runtime = Runtime::new().unwrap();
42-
let mut client = runtime.block_on(connect()).unwrap();
41+
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
42+
let mut client = connect(runtime).unwrap();
4343
bench::<Transaction<'_, usize>, _>(&mut client, "PSQL_Transaction", &mut group).unwrap();
4444
}
4545

rust/src/benched.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ use {
1212
};
1313

1414
pub trait Benched: Sized {
15-
type Builder<'a>;
15+
type Builder;
1616

17-
fn setup(builder: Self::Builder<'_>) -> Result<Self>;
17+
fn setup(builder: Self::Builder) -> Result<Self>;
1818

1919
fn drop_storage(&mut self) -> Result<()>;
2020

@@ -24,9 +24,9 @@ pub trait Benched: Sized {
2424
}
2525

2626
impl<T: LinkType> Benched for unit::Store<T, FileMapped<LinkPart<T>>> {
27-
type Builder<'a> = FileMapped<LinkPart<T>>;
27+
type Builder = FileMapped<LinkPart<T>>;
2828

29-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
29+
fn setup(builder: Self::Builder) -> Result<Self> {
3030
let mut links = Self::new(builder)?;
3131
for _ in 0..BACKGROUND_LINKS {
3232
links.create_point()?;
@@ -41,10 +41,10 @@ impl<T: LinkType> Benched for unit::Store<T, FileMapped<LinkPart<T>>> {
4141
}
4242

4343
impl<T: LinkType> Benched for unit::Store<T, Alloc<LinkPart<T>, Global>> {
44-
type Builder<'a> = Global;
44+
type Builder = ();
4545

46-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
47-
let storage = Alloc::new(builder);
46+
fn setup(builder: Self::Builder) -> Result<Self> {
47+
let storage = Alloc::new(Global);
4848
let mut links = Self::new(storage)?;
4949
for _ in 0..BACKGROUND_LINKS {
5050
links.create_point()?;
@@ -59,9 +59,9 @@ impl<T: LinkType> Benched for unit::Store<T, Alloc<LinkPart<T>, Global>> {
5959
}
6060

6161
impl<T: LinkType> Benched for split::Store<T, FileMapped<DataPart<T>>, FileMapped<IndexPart<T>>> {
62-
type Builder<'a> = (FileMapped<DataPart<T>>, FileMapped<IndexPart<T>>);
62+
type Builder = (FileMapped<DataPart<T>>, FileMapped<IndexPart<T>>);
6363

64-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
64+
fn setup(builder: Self::Builder) -> Result<Self> {
6565
let (data, index) = builder;
6666
let mut links = Self::new(data, index)?;
6767
for _ in 0..BACKGROUND_LINKS {
@@ -79,10 +79,10 @@ impl<T: LinkType> Benched for split::Store<T, FileMapped<DataPart<T>>, FileMappe
7979
impl<T: LinkType> Benched
8080
for split::Store<T, Alloc<DataPart<T>, Global>, Alloc<IndexPart<T>, Global>>
8181
{
82-
type Builder<'a> = Global;
82+
type Builder = ();
8383

84-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
85-
let (data, index) = (Alloc::new(builder), Alloc::new(builder));
84+
fn setup(builder: Self::Builder) -> Result<Self> {
85+
let (data, index) = (Alloc::new(Global), Alloc::new(Global));
8686
let mut links = Self::new(data, index)?;
8787
for _ in 0..BACKGROUND_LINKS {
8888
links.create_point()?;
@@ -97,10 +97,10 @@ impl<T: LinkType> Benched
9797
}
9898

9999
impl<T: LinkType> Benched for Client<T> {
100-
type Builder<'a> = &'a Runtime;
100+
type Builder = Runtime;
101101

102-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
103-
let mut client = builder.block_on(connect())?;
102+
fn setup(builder: Self::Builder) -> Result<Self> {
103+
let mut client = connect(builder)?;
104104
for _ in 0..BACKGROUND_LINKS {
105105
client.create_point()?;
106106
}
@@ -118,9 +118,9 @@ impl<T: LinkType> Benched for Client<T> {
118118
}
119119

120120
impl<'a, T: LinkType> Benched for Transaction<'a, T> {
121-
type Builder<'b> = &'a mut Client<T>;
121+
type Builder = &'a mut Client<T>;
122122

123-
fn setup(builder: Self::Builder<'_>) -> Result<Self> {
123+
fn setup(builder: Self::Builder) -> Result<Self> {
124124
let mut transaction = builder.transaction().unwrap();
125125
transaction.create_table().unwrap();
126126
for _ in 0..BACKGROUND_LINKS {

rust/src/client.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ pub struct Client<T: LinkType> {
1616
}
1717

1818
impl<T: LinkType> Client<T> {
19-
pub async fn new(client: postgres::Client) -> crate::Result<Self> {
20-
client
19+
pub fn new(client: postgres::Client, runtime: Runtime) -> crate::Result<Self> {
20+
runtime.block_on(async {
21+
client
2122
.query(
2223
"CREATE TABLE IF NOT EXISTS Links (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, from_id bigint, to_id bigint);",
2324
&[],
@@ -35,10 +36,12 @@ impl<T: LinkType> Client<T> {
3536
&[],
3637
)
3738
.await?;
39+
Ok::<(), postgres::Error>(())
40+
})?;
3841
Ok(Self {
3942
client,
4043
constants: LinksConstants::<T>::new(),
41-
runtime: Runtime::new()?,
44+
runtime,
4245
})
4346
}
4447

@@ -250,13 +253,13 @@ impl<T: LinkType> Links<T> for Client<T> {
250253
old_links[0].get::<_, i64>(1).try_into().unwrap(),
251254
old_links[0].get::<_, i64>(2).try_into().unwrap(),
252255
);
253-
let _ = self
256+
self
254257
.client
255258
.query(
256259
"UPDATE Links SET from_id = $1, to_id = $2 WHERE id = $3;",
257260
&[&source.as_i64(), &target.as_i64(), &id.as_i64()],
258261
)
259-
.await;
262+
.await.unwrap();
260263
Ok(handler(
261264
Link::new(id, old_source, old_target),
262265
Link::new(id, source, target),

rust/src/lib.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ macro_rules! benchmark {
3131
pub use {benched::Benched, client::Client, fork::Fork, transaction::Transaction};
3232
use {
3333
doublets::{data::LinkType, mem::FileMapped},
34-
std::{fs::File, io, error::Error},
34+
std::{error::Error, fs::File, io},
35+
tokio::runtime::Runtime,
3536
tokio_postgres::NoTls,
3637
};
3738

@@ -43,18 +44,22 @@ mod transaction;
4344
pub type Result<T> = core::result::Result<T, Box<dyn Error>>;
4445

4546
pub const BACKGROUND_LINKS: usize = 3_000;
46-
const OPTIONS: &str = "user=postgres dbname=postgres password=postgres host=localhost port=5432";
47+
//const OPTIONS: &str = "user=postgres dbname=postgres password=postgres host=localhost port=5432";
48+
const OPTIONS: &str = "user=zaharyan dbname=zaharyan password=zaharyan host=localhost port=5432";
4749

48-
pub async fn connect<T: LinkType>() -> Result<Client<T>> {
49-
let (client, connection) = tokio_postgres::connect(OPTIONS, NoTls).await?;
50-
tokio::spawn(async move {
51-
if let Err(e) = connection.await {
52-
eprintln!("Connection error: {e}");
53-
return Err(e);
54-
}
55-
Ok(())
50+
pub fn connect<T: LinkType>(runtime: Runtime) -> Result<Client<T>> {
51+
let client = runtime.block_on(async {
52+
let (client, connection) = tokio_postgres::connect(OPTIONS, NoTls).await.unwrap();
53+
tokio::spawn(async move {
54+
if let Err(e) = connection.await {
55+
eprintln!("Connection error: {e}");
56+
return Err(e);
57+
}
58+
Ok(())
59+
});
60+
client
5661
});
57-
Client::new(client).await
62+
Client::new(client, runtime)
5863
}
5964

6065
pub fn prepare_file<T: Default>(filename: &str) -> io::Result<FileMapped<T>> {

0 commit comments

Comments
 (0)