Skip to content

Commit

Permalink
save request results into sqlite database
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonsimpson21 authored and hatoo committed Aug 3, 2024
1 parent de0e252 commit 39a8b10
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pin-project-lite = "0.2.14"
http-body-util = "0.1.2"
hyper-util = { version = "0.1.6", features = ["tokio"] }
tokio-vsock = { version = "0.5.0", optional = true }
rusqlite = "0.32.1"

[target.'cfg(unix)'.dependencies]
rlimit = "0.10.1"
Expand Down
65 changes: 65 additions & 0 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use rusqlite::Connection;

use crate::client::{ClientError, RequestResult};

fn create_db(conn: &Connection) -> Result<usize, rusqlite::Error> {
conn.execute(
"CREATE TABLE loadtest (
url TEXT NOT NULL,
duration REAL,
status INTEGER,
len_bytes INTEGER
)",
(),
)
}

pub fn store(
db_url: &str,
req_url: String,
request_records: &[RequestResult],
) -> Result<usize, rusqlite::Error> {
let conn = Connection::open(db_url)?;
_ = create_db(&conn);

let request_url = req_url
.replace("https", "")
.replace("http", "")
.replace("://", "");

let affected_rows =
request_records
.into_iter()
.map(|req| {
conn.execute(
"INSERT INTO loadtest (url, duration, status, len_bytes) VALUES (?1, ?2, ?3, ?4)",
(&request_url, req.duration().as_secs_f32(), req.status.as_u16() as u32, req.len_bytes),
).unwrap_or(0)
})
.sum();

Ok(affected_rows)
}

#[cfg(test)]
mod test_db {
use super::*;

#[test]
fn test_store() {
let conn = Connection::open_in_memory().unwrap();
let _ = create_db(&conn);
let test_val = RequestResult {
status: hyper::StatusCode::OK,
len_bytes: 100,
start_latency_correction: None,
start: std::time::Instant::now(),
connection_time: None,
end: std::time::Instant::now(),
};
let test_vec = vec![test_val.clone(), test_val.clone()];
let result = store("test.db", "test.com".to_owned(), &test_vec);
assert_eq!(result.unwrap(), 2);
std::fs::remove_file("test.db").unwrap();
}
}
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use url::Url;
use url_generator::UrlGenerator;

mod client;
mod db;
mod histogram;
mod monitor;
mod printer;
Expand Down Expand Up @@ -188,6 +189,8 @@ Note: If qps is specified, burst will be ignored",
long = "stats-success-breakdown"
)]
stats_success_breakdown: bool,
#[clap(help = "sqlite datebase url E.G test.db", long = "db-url")]
db_url: Option<String>,
}

/// An entry specified by `connect-to` to override DNS resolution and default
Expand Down Expand Up @@ -654,6 +657,10 @@ async fn main() -> anyhow::Result<()> {
opts.stats_success_breakdown,
)?;

if let Some(db_url) = opts.db_url {
let _ = db::store(&db_url, opts.url, res.success());
}

Ok(())
}

Expand Down

0 comments on commit 39a8b10

Please sign in to comment.