Skip to content

Commit

Permalink
bench: simplify config passing
Browse files Browse the repository at this point in the history
  • Loading branch information
ohsayan committed Apr 26, 2024
1 parent d3faec9 commit 2c81da4
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 97 deletions.
57 changes: 20 additions & 37 deletions sky-bench/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
*/

use {
crate::error::{BenchError, BenchResult},
crate::{
error::{BenchError, BenchResult},
setup,
},
libsky::{env_vars, CliAction},
std::{collections::hash_map::HashMap, env},
};
Expand Down Expand Up @@ -63,36 +66,13 @@ pub enum BenchWorkload {

#[derive(Debug)]
pub struct BenchConfig {
pub host: String,
pub port: u16,
pub root_pass: String,
pub threads: usize,
pub key_size: usize,
pub query_count: usize,
pub workload: BenchType,
pub connections: usize,
}

impl BenchConfig {
pub fn new(
host: String,
port: u16,
root_pass: String,
threads: usize,
key_size: usize,
query_count: usize,
bench_type: BenchType,
connections: usize,
) -> Self {
pub fn new(bench_type: BenchType) -> Self {
Self {
host,
port,
root_pass,
threads,
key_size,
query_count,
workload: bench_type,
connections,
}
}
}
Expand All @@ -114,7 +94,7 @@ fn cdig(n: usize) -> usize {
}
}

pub fn parse() -> BenchResult<Task> {
pub fn parse_and_setup() -> BenchResult<Task> {
let mut args = match load_env()? {
TaskInner::HelpMsg(msg) => return Ok(Task::HelpMsg(msg)),
TaskInner::CheckConfig(args) => args,
Expand Down Expand Up @@ -152,7 +132,7 @@ pub fn parse() -> BenchResult<Task> {
}
};
// password
let passsword = match args.remove("--password") {
let password = match args.remove("--password") {
Some(p) => p,
None => {
// check env?
Expand Down Expand Up @@ -239,16 +219,19 @@ pub fn parse() -> BenchResult<Task> {
},
};
if args.is_empty() {
Ok(Task::BenchConfig(BenchConfig::new(
host,
port,
passsword,
thread_count,
key_size,
query_count,
workload,
connections,
)))
unsafe {
setup::configure(
"root".into(),
password,
host,
port,
thread_count,
connections,
key_size,
query_count,
)
}
Ok(Task::BenchConfig(BenchConfig::new(workload)))
} else {
Err(BenchError::ArgsErr(format!("unrecognized arguments")))
}
Expand Down
66 changes: 34 additions & 32 deletions sky-bench/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use skytable::response::Value;

use crate::{
args::{BenchEngine, BenchType, BenchWorkload},
workload,
setup, workload,
};

use {
Expand Down Expand Up @@ -118,15 +118,16 @@ impl rookie::ThreadedBombardTask for BombardTask {
*/

pub fn run(bench: BenchConfig) -> error::BenchResult<()> {
let config_instance = unsafe { setup::instance() };
let bench_config = BombardTask::new(Config::new(
&bench.host,
bench.port,
"root",
&bench.root_pass,
config_instance.host(),
config_instance.port(),
config_instance.username(),
config_instance.password(),
));
let mut main_thread_db;
let stats = match bench.workload {
BenchType::Workload(BenchWorkload::UniformV1) => return workload::run_bench(&bench),
BenchType::Workload(BenchWorkload::UniformV1) => return workload::run_bench(),
BenchType::Legacy(l) => {
warn!("using `--engine` is now deprecated. please consider switching to `--workload`");
info!("running preliminary checks and creating model `bench.bench` with definition: `{{un: binary, pw: uint8}}`");
Expand All @@ -136,8 +137,8 @@ pub fn run(bench: BenchConfig) -> error::BenchResult<()> {
"create model {BENCHMARK_SPACE_ID}.{BENCHMARK_MODEL_ID}(un: binary, pw: uint8)"
)))?;
match l {
BenchEngine::Rookie => bench_rookie(bench_config, bench),
BenchEngine::Fury => bench_fury(bench),
BenchEngine::Rookie => bench_rookie(bench_config),
BenchEngine::Fury => bench_fury(),
}
}
};
Expand Down Expand Up @@ -259,21 +260,22 @@ impl BenchItem {
}
}

fn prepare_bench_spec(bench: &BenchConfig) -> Vec<BenchItem> {
fn prepare_bench_spec() -> Vec<BenchItem> {
let config_instance = unsafe { setup::instance() };
vec![
BenchItem::new(
"INSERT",
BenchmarkTask::new(
bench.key_size,
config_instance.object_size(),
|me, current| query!("insert into bench(?, ?)", me.fmt_pk(current), 0u64),
|_, _, actual_resp| actual_resp == Response::Empty,
),
bench.query_count,
config_instance.object_count(),
),
BenchItem::new(
"SELECT",
BenchmarkTask::new(
bench.key_size,
config_instance.object_size(),
|me, current| query!("select * from bench where un = ?", me.fmt_pk(current)),
|me, current, resp| match resp {
Response::Row(r) => {
Expand All @@ -282,12 +284,12 @@ fn prepare_bench_spec(bench: &BenchConfig) -> Vec<BenchItem> {
_ => false,
},
),
bench.query_count,
config_instance.object_count(),
),
BenchItem::new(
"UPDATE",
BenchmarkTask::new(
bench.key_size,
config_instance.object_size(),
|me, current| {
query!(
"update bench set pw += ? where un = ?",
Expand All @@ -297,16 +299,16 @@ fn prepare_bench_spec(bench: &BenchConfig) -> Vec<BenchItem> {
},
|_, _, resp| resp == Response::Empty,
),
bench.query_count,
config_instance.object_count(),
),
BenchItem::new(
"DELETE",
BenchmarkTask::new(
bench.key_size,
config_instance.object_size(),
|me, current| query!("delete from bench where un = ?", me.fmt_pk(current)),
|_, _, resp| resp == Response::Empty,
),
bench.query_count,
config_instance.object_count(),
),
]
}
Expand All @@ -324,20 +326,19 @@ fn fmt_u64(n: u64) -> String {
result.chars().rev().collect()
}

fn bench_rookie(
task: BombardTask,
bench: BenchConfig,
) -> BenchResult<(u64, Vec<(&'static str, RuntimeStats)>)> {
fn bench_rookie(task: BombardTask) -> BenchResult<(u64, Vec<(&'static str, RuntimeStats)>)> {
// initialize pool
let config_instance = unsafe { setup::instance() };
info!(
"initializing connections. engine=rookie, threads={}, primary key size ={} bytes",
bench.threads, bench.key_size
config_instance.threads(),
config_instance.object_size()
);
let mut pool = rookie::BombardPool::new(bench.threads, task)?;
let mut pool = rookie::BombardPool::new(config_instance.threads(), task)?;
// prepare benches
let benches = prepare_bench_spec(&bench);
let benches = prepare_bench_spec();
// bench
let total_queries = bench.query_count as u64 * benches.len() as u64;
let total_queries = config_instance.object_count() as u64 * benches.len() as u64;
let mut results = vec![];
for task in benches {
let name = task.name;
Expand All @@ -348,26 +349,27 @@ fn bench_rookie(
Ok((total_queries, results))
}

fn bench_fury(bench: BenchConfig) -> BenchResult<(u64, Vec<(&'static str, RuntimeStats)>)> {
fn bench_fury() -> BenchResult<(u64, Vec<(&'static str, RuntimeStats)>)> {
let config_instance = unsafe { setup::instance() };
let rt = tokio::runtime::Builder::new_multi_thread()
.worker_threads(bench.threads)
.worker_threads(config_instance.threads())
.enable_all()
.build()
.unwrap();
rt.block_on(async move {
info!(
"initializing connections. engine=fury, threads={}, connections={}, primary key size ={} bytes",
bench.threads, bench.connections, bench.key_size
config_instance.threads(), config_instance.connections(), config_instance.object_size()
);
let mut pool = fury::Fury::new(
bench.connections,
Config::new(&bench.host, bench.port, "root", &bench.root_pass),
config_instance.connections(),
Config::new(config_instance.host(), config_instance.port(), config_instance.username(), config_instance.password()),
)
.await?;
// prepare benches
let benches = prepare_bench_spec(&bench);
let benches = prepare_bench_spec();
// bench
let total_queries = bench.query_count as u64 * benches.len() as u64;
let total_queries = config_instance.object_count() as u64 * benches.len() as u64;
let mut results = vec![];
for task in benches {
let name = task.name;
Expand Down
3 changes: 2 additions & 1 deletion sky-bench/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod args;
mod bench;
mod error;
mod legacy;
mod setup;
mod stats;
mod workload;

Expand All @@ -47,7 +48,7 @@ fn main() {
}

fn run() -> error::BenchResult<()> {
let task = args::parse()?;
let task = args::parse_and_setup()?;
match task {
args::Task::HelpMsg(msg) => println!("{msg}"),
args::Task::BenchConfig(bench) => bench::run(bench)?,
Expand Down
100 changes: 100 additions & 0 deletions sky-bench/src/setup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* Created on Wed Apr 24 2024
*
* This file is a part of Skytable
* Skytable (formerly known as TerrabaseDB or Skybase) is a free and open-source
* NoSQL database written by Sayan Nandan ("the Author") with the
* vision to provide flexibility in data modelling without compromising
* on performance, queryability or scalability.
*
* Copyright (c) 2024, Sayan Nandan <nandansayan@outlook.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/

use std::ptr::addr_of;

static mut SETUP: RunnerSetup = RunnerSetup {
username: String::new(),
password: String::new(),
host: String::new(),
port: 0,
threads: 0,
connections: 0,
object_size: 0,
object_count: 0,
};

pub struct RunnerSetup {
username: String,
password: String,
host: String,
port: u16,
threads: usize,
connections: usize,
object_size: usize,
object_count: usize,
}

impl RunnerSetup {
pub fn username(&self) -> &str {
&self.username
}
pub fn password(&self) -> &str {
&self.password
}
pub fn host(&self) -> &str {
&self.host
}
pub fn port(&self) -> u16 {
self.port
}
pub fn threads(&self) -> usize {
self.threads
}
pub fn connections(&self) -> usize {
self.connections
}
pub fn object_size(&self) -> usize {
self.object_size
}
pub fn object_count(&self) -> usize {
self.object_count
}
}

pub unsafe fn instance() -> &'static RunnerSetup {
&*addr_of!(SETUP)
}

pub unsafe fn configure(
username: String,
password: String,
host: String,
port: u16,
threads: usize,
connections: usize,
object_size: usize,
object_count: usize,
) {
SETUP.host = host;
SETUP.port = port;
SETUP.username = username;
SETUP.password = password;
SETUP.threads = threads;
SETUP.connections = connections;
SETUP.object_size = object_size;
SETUP.object_count = object_count;
}
Loading

0 comments on commit 2c81da4

Please sign in to comment.