Skip to content

Commit

Permalink
ci: add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
KKould committed Sep 9, 2024
1 parent 1c9d3fb commit c19e326
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: CI
on:
push:
pull_request:
workflow_dispatch:

env:
CARGO_TERM_COLOR: always
Expand Down Expand Up @@ -66,3 +67,45 @@ jobs:
with:
command: fmt
args: -- --check

benchmark:
name: Rust benchmark
runs-on: self-hosted
steps:
- uses: actions/checkout@v4
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true

# `cargo check` command here will use installed `nightly`
# as it is set as an "override" for current directory

- name: Run cargo bench
uses: actions-rs/cargo@v1
with:
command: bench
args: --all-features

- name: Save Benchmark Output
id: save_output
run: |
echo "::set-output name=read_result::$(cat read_benchmark.log | sed ':a;N;$!ba;s/\n/\\n/g')"
echo "::set-output name=write_result::$(cat write_benchmark.log | sed ':a;N;$!ba;s/\n/\\n/g')"
- name: Comment on PR using GitHub CLI
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr comment ${{ github.event.pull_request.number }} --body "
ReadBenchmark:
```shell
$(echo "${{ steps.save_output.outputs.read_result }}" | tr '\n' '\n')
```
WriteBenchmark:
```shell
$(echo "${{ steps.save_output.outputs.write_result }}" | tr '\n' '\n')
```"
32 changes: 18 additions & 14 deletions benches/read_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ mod common;
use std::{
collections::Bound,
env::current_dir,
path::Path,
path::{Path, PathBuf},
sync::Arc,
time::{Duration, Instant},
};

use futures_util::{future::join_all, StreamExt};
use tokio::io::AsyncWriteExt;
use tonbo::{executor::tokio::TokioExecutor, fs::FileProvider};

use crate::common::{
read_tbl, BenchDatabase, BenchReadTransaction, BenchReader, RedbBenchDatabase,
Expand Down Expand Up @@ -124,15 +126,19 @@ async fn benchmark<T: BenchDatabase + Send + Sync>(

#[tokio::main]
async fn main() {
let data_dir = current_dir().unwrap().join("benchmark_data");
let data_dir = PathBuf::from("/home/kkould/benchmark");

#[cfg(feature = "load_tbl")]
{
use crate::common::{BenchInserter, BenchWriteTransaction};

let tbl_path = current_dir().unwrap().join("./benchmark/customer.tbl");
let tbl_path = data_dir.join("customer.tbl");

async fn load<T: BenchDatabase>(tbl_path: impl AsRef<Path>, path: impl AsRef<Path>) {
if tbl_path.as_ref().exists() {
return;
}

println!("{}: start loading", T::db_type_name());
let database = T::build(path).await;

Expand All @@ -148,39 +154,37 @@ async fn main() {

load::<TonboBenchDataBase>(&tbl_path, data_dir.join("tonbo")).await;
load::<RocksdbBenchDatabase>(&tbl_path, data_dir.join("rocksdb")).await;
load::<RedbBenchDatabase>(&tbl_path, data_dir.join("redb")).await;
load::<SledBenchDatabase>(&tbl_path, data_dir.join("sled")).await;
}

let tonbo_latency_results = { benchmark::<TonboBenchDataBase>(data_dir.join("tonbo")).await };
let redb_latency_results = { benchmark::<RedbBenchDatabase>(data_dir.join("redb")).await };
let rocksdb_results = { benchmark::<RocksdbBenchDatabase>(data_dir.join("rocksdb")).await };
let sled_results = { benchmark::<SledBenchDatabase>(data_dir.join("sled")).await };

let mut rows: Vec<Vec<String>> = Vec::new();

for (benchmark, _duration) in &tonbo_latency_results {
rows.push(vec![benchmark.to_string()]);
}

for results in [
tonbo_latency_results,
redb_latency_results,
rocksdb_results,
sled_results,
] {
for results in [tonbo_latency_results, rocksdb_results] {
for (i, (_benchmark, duration)) in results.iter().enumerate() {
rows[i].push(format!("{}ms", duration.as_millis()));
}
}

let mut table = comfy_table::Table::new();
table.set_width(100);
table.set_header(["", "tonbo", "redb", "rocksdb", "sled"]);
table.set_header(["", "tonbo", "rocksdb"]);
for row in rows {
table.add_row(row);
}

println!();
println!("{table}");

let mut file = TokioExecutor::open("read_benchmark.log").await.unwrap();
for line in table.lines() {
file.write_all(line.as_bytes()).await.unwrap();
file.write_all(b"\n").await.unwrap();
}
file.flush().await.unwrap();
}
8 changes: 8 additions & 0 deletions benches/write_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::{
use common::*;
use futures_util::future::join_all;
use tempfile::TempDir;
use tokio::io::AsyncWriteExt;
use tonbo::{executor::tokio::TokioExecutor, fs::FileProvider};

const WRITE_TIMES: usize = 500_000;
const WRITE_BATCH_TIMES: usize = 5000;
Expand Down Expand Up @@ -224,4 +226,10 @@ async fn main() {

println!();
println!("{table}");

let mut file = TokioExecutor::open("write_benchmark.log").await.unwrap();
for line in table.lines() {
file.write_all(line.as_bytes()).await.unwrap();
file.write_all(b"\n").await.unwrap();
}
}

0 comments on commit c19e326

Please sign in to comment.