Skip to content

Commit bdaa097

Browse files
committed
Add first version of rust benchmark
1 parent eae1173 commit bdaa097

File tree

18 files changed

+2090
-348
lines changed

18 files changed

+2090
-348
lines changed

.github/workflows/rust.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Benchmark
2+
3+
on:
4+
push:
5+
branches:
6+
- add_rust_benchmark
7+
8+
env:
9+
toolchain: nightly-2022-08-22
10+
11+
jobs:
12+
benchmark:
13+
runs-on: ubuntu-latest
14+
15+
services:
16+
postgres:
17+
image: postgres:latest
18+
env:
19+
POSTGRES_USER: postgres
20+
POSTGRES_PASSWORD: postgres
21+
POSTGRES_DB: postgres
22+
options: >-
23+
--health-cmd pg_isready
24+
--health-interval 10s
25+
--health-timeout 5s
26+
--health-retries 5
27+
ports:
28+
- 5432:5432
29+
30+
steps:
31+
- name: Checkout repository
32+
uses: actions/checkout@v2
33+
- name: Setup Rust
34+
uses: actions-rs/toolchain@v1
35+
with:
36+
profile: minimal
37+
toolchain: ${{env.toolchain}}
38+
components: rustfmt, clippy
39+
default: true
40+
- name: Build benchmark
41+
uses: actions-rs/cargo@v1
42+
with:
43+
command: build
44+
args: --release --all-features
45+
- name: Run benchmark
46+
uses: actions-rs/cargo@v1
47+
with:
48+
command: bench
49+
args: --bench bench -- --output-format bencher | tee out.txt
50+
- name: Prepare benchmark results
51+
run: |
52+
cd rust
53+
pip install numpy matplotlib
54+
python3 out.py
55+
- name: Save benchmark results
56+
uses: actions/upload-artifact@v3
57+
with:
58+
name: Benchmark results
59+
path: |
60+
rust/Bench1.png
61+
rust/Bench2.png

rust/Cargo.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
tokio = { version = "1.19", features = ["full"] }
8-
tokio-postgres = "0.7.6"
9-
criterion = "0.3.6"
10-
platform-data = "0.1.0-alpha.1"
11-
doublets = "0.1.0-pre+beta.15"
12-
async-trait = "0.1.57"
7+
tokio = { version = "1.29.1", features = ["full"] }
8+
tokio-postgres = "0.7.8"
9+
criterion = "0.5.1"
10+
doublets = { git = "https://github.com/linksplatform/doublets-rs.git" }
11+
async-trait = "0.1.72"
1312

1413
[[bench]]
1514
name = "bench"

rust/benches/bench.rs

Lines changed: 21 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,28 @@
11
#![feature(allocator_api)]
2-
3-
use criterion::{criterion_group, criterion_main, Criterion};
4-
use doublets::{
5-
mem::{AllocMem, FileMappedMem},
6-
unit, Doublets,
2+
#![feature(box_syntax)]
3+
4+
use criterion::criterion_group;
5+
use {
6+
create::create_links,
7+
criterion::criterion_main,
8+
delete::delete_links,
9+
each::{each_all, each_concrete, each_identity, each_incoming, each_outgoing},
10+
update::update_links,
711
};
8-
use tokio_postgres::{Error, NoTls};
9-
10-
use linkspsql::{make_bench, Client, Cruds};
11-
12-
async fn connect() -> Result<Client, Error> {
13-
let (client, connection) = tokio_postgres::connect("", NoTls).await.unwrap();
14-
tokio::spawn(async move {
15-
if let Err(err) = connection.await {
16-
return Err(err);
17-
}
18-
Ok(())
19-
});
20-
Client::new(client).await
21-
}
22-
23-
fn create_thousand_links_without_transaction(c: &mut Criterion) {
24-
let runtime = tokio::runtime::Runtime::new().unwrap();
25-
let mut client = runtime.block_on(connect()).unwrap();
26-
make_bench!(
27-
c,
28-
"create_thousand_links_without_transaction",
29-
client,
30-
runtime
31-
);
32-
}
33-
34-
fn create_thousand_links_with_transaction(c: &mut Criterion) {
35-
let runtime = tokio::runtime::Runtime::new().unwrap();
36-
let mut client = runtime.block_on(connect()).unwrap();
37-
make_bench!(
38-
c,
39-
"create_thousand_links_with_transaction",
40-
client,
41-
runtime,
42-
transaction
43-
);
44-
}
45-
46-
fn doublets_benchmark_ram(c: &mut Criterion) {
47-
let allocator = std::alloc::Global;
48-
let storage = AllocMem::new(allocator);
49-
let mut links = unit::Store::<usize, _>::new(storage).unwrap();
50-
c.bench_function("doublets_benchmark_ram", |b| {
51-
b.iter(|| {
52-
for _ in 1..=1_000_000 {
53-
links.create_point().unwrap();
54-
}
55-
});
56-
links.delete_all().unwrap();
57-
});
58-
}
59-
60-
fn doublets_benchmark_file(c: &mut Criterion) {
61-
let file = std::fs::File::options()
62-
.create(true)
63-
.write(true)
64-
.read(true)
65-
.open("db.links")
66-
.unwrap();
67-
let storage = FileMappedMem::new(file).unwrap();
68-
let mut links = unit::Store::<usize, _>::new(storage).unwrap();
69-
c.bench_function("doublets_benchmark_file", |b| {
70-
b.iter(|| {
71-
for _ in 1..=1_000_000 {
72-
links.create_point().unwrap();
73-
}
74-
});
75-
links.delete_all().unwrap();
76-
});
77-
}
12+
mod create;
13+
mod delete;
14+
mod each;
15+
mod update;
7816

7917
criterion_group!(
8018
benches,
81-
create_thousand_links_without_transaction,
82-
create_thousand_links_with_transaction,
83-
doublets_benchmark_ram,
84-
doublets_benchmark_file,
19+
create_links,
20+
delete_links,
21+
each_identity,
22+
each_concrete,
23+
each_outgoing,
24+
each_incoming,
25+
each_all,
26+
update_links
8527
);
8628
criterion_main!(benches);

rust/benches/create.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#![feature(allocator_api)]
2+
3+
use {
4+
criterion::Criterion,
5+
doublets::{
6+
mem::{Alloc, FileMapped},
7+
split, unit, Doublets,
8+
},
9+
linkspsql::{benchmark, connect, elapsed, prepare_file, SetupTeardown, BACKGROUND_LINKS},
10+
std::{
11+
error::Error,
12+
time::{Duration, Instant},
13+
},
14+
tokio::runtime::Runtime,
15+
};
16+
17+
pub fn create_links(c: &mut Criterion) -> Result<(), Box<dyn Error>> {
18+
let runtime = Runtime::new()?;
19+
let mut group = c.benchmark_group("Create");
20+
let allocator = std::alloc::Global;
21+
let united = prepare_file("united.links")?;
22+
let split_data = prepare_file("split_data.links")?;
23+
let split_index = prepare_file("split_index.links")?;
24+
{
25+
let mut client = runtime.block_on(connect())?;
26+
let mut background = BACKGROUND_LINKS;
27+
let _ = client.setup();
28+
benchmark! {
29+
group,
30+
"PSQL_NonTransaction",
31+
{
32+
for _ in 0..1_000 {
33+
let _ = client.create_point();
34+
}
35+
},
36+
{
37+
for index in (background + 1..=background + 1_000).rev() {
38+
let _ = client.delete(index);
39+
}
40+
background += 1_000;
41+
}
42+
}
43+
let _ = client.teardown();
44+
}
45+
{
46+
let mut client = runtime.block_on(connect())?;
47+
let mut background = BACKGROUND_LINKS;
48+
let mut transaction = client.transaction()?;
49+
let _ = transaction.setup();
50+
let _ = transaction.commit();
51+
benchmark! {
52+
group,
53+
"PSQL_Transaction",
54+
let mut transaction = client.transaction().unwrap(),
55+
{
56+
for _ in 0..1_000 {
57+
let _ = transaction.create_point();
58+
}
59+
let _ = transaction.commit();
60+
},
61+
{
62+
let mut transaction = client.transaction().unwrap();
63+
for index in (background + 1..=background + 1_000).rev() {
64+
let _ = transaction.delete(index);
65+
}
66+
let _ = transaction.commit();
67+
background += 1_000;
68+
}
69+
}
70+
let _ = client.transaction()?.teardown();
71+
}
72+
{
73+
let storage = Alloc::new(allocator);
74+
let mut links = unit::Store::<usize, _>::new(storage)?;
75+
let _ = links.setup();
76+
benchmark! {
77+
group,
78+
"Doublets_United_Volatile",
79+
{
80+
for _ in 0..1_000 {
81+
let _ = links.create_point();
82+
}
83+
},
84+
{
85+
for index in (BACKGROUND_LINKS + 1..=BACKGROUND_LINKS + 1_000).rev() {
86+
let _ = links.delete(index);
87+
}
88+
}
89+
}
90+
let _ = links.teardown();
91+
}
92+
{
93+
let storage = FileMapped::new(united)?;
94+
let mut links = unit::Store::<usize, _>::new(storage)?;
95+
let _ = links.setup();
96+
benchmark! {
97+
group,
98+
"Doublets_United_NonVolatile",
99+
{
100+
for _ in 0..1_000 {
101+
let _ = links.create_point();
102+
}
103+
},
104+
{
105+
for index in (BACKGROUND_LINKS + 1..=BACKGROUND_LINKS + 1_000).rev() {
106+
let _ = links.delete(index);
107+
}
108+
}
109+
}
110+
let _ = links.teardown();
111+
}
112+
{
113+
let data = Alloc::new(allocator);
114+
let index = Alloc::new(allocator);
115+
let mut links = split::Store::<usize, _, _>::new(data, index)?;
116+
let _ = links.setup();
117+
benchmark! {
118+
group,
119+
"Doublets_Split_Volatile",
120+
{
121+
for _ in 0..1_000 {
122+
let _ = links.create_point();
123+
}
124+
},
125+
{
126+
for index in (BACKGROUND_LINKS + 1..=BACKGROUND_LINKS + 1_000).rev() {
127+
let _ = links.delete(index);
128+
}
129+
}
130+
}
131+
let _ = links.teardown();
132+
}
133+
{
134+
let split_data = FileMapped::new(split_data)?;
135+
let split_index = FileMapped::new(split_index)?;
136+
let mut links = split::Store::<usize, _, _>::new(split_data, split_index)?;
137+
let _ = links.setup();
138+
benchmark! {
139+
group,
140+
"Doublets_Split_NonVolatile",
141+
{
142+
for _ in 0..1_000 {
143+
let _ = links.create_point();
144+
}
145+
},
146+
{
147+
for index in (BACKGROUND_LINKS + 1..=BACKGROUND_LINKS + 1_000).rev() {
148+
let _ = links.delete(index);
149+
}
150+
}
151+
}
152+
let _ = links.teardown();
153+
}
154+
group.finish();
155+
Ok(())
156+
}

0 commit comments

Comments
 (0)