Skip to content

Commit 82a5e64

Browse files
authored
Merge pull request #6 from linksplatform/add_rust_benchmark
Add Rust benchmark
2 parents 66f1a65 + 577c2a4 commit 82a5e64

File tree

16 files changed

+2138
-0
lines changed

16 files changed

+2138
-0
lines changed

.github/workflows/rust.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Benchmark Rust version
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 --manifest-path rust/Cargo.toml
45+
- name: Run benchmark
46+
working-directory: rust
47+
run: cargo bench --bench bench -- --output-format bencher | tee out.txt
48+
- name: Prepare benchmark results
49+
run: |
50+
cd rust
51+
pip install numpy matplotlib
52+
python3 out.py
53+
- name: Save benchmark results
54+
uses: actions/upload-artifact@v3
55+
with:
56+
name: Benchmark results
57+
path: |
58+
rust/Bench1.png
59+
rust/Bench2.png

rust/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "linkspsql"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
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"
12+
13+
[[bench]]
14+
name = "bench"
15+
harness = false

rust/benches/bench.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(allocator_api)]
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,
11+
};
12+
mod create;
13+
mod delete;
14+
mod each;
15+
mod update;
16+
17+
criterion_group!(
18+
benches,
19+
create_links,
20+
delete_links,
21+
each_identity,
22+
each_concrete,
23+
each_outgoing,
24+
each_incoming,
25+
each_all,
26+
update_links
27+
);
28+
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)