-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed issues with routes not using Executor struct. Worked on benchmark
- Loading branch information
1 parent
10084fe
commit ab91abf
Showing
3 changed files
with
88 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,89 @@ | ||
use reqwest::StatusCode; | ||
use tokio::time::{Duration, Instant}; | ||
|
||
use crate::routes; | ||
|
||
/** | ||
* TODO: | ||
- Run loop for x seconds. | ||
- Report back to main thread the result of test | ||
*/ | ||
|
||
pub fn run_benchmark(test: routes::CreateTest) { | ||
struct Result { | ||
pub connection_id: u64, | ||
pub second: u64, | ||
pub error_codes: Vec<StatusCode>, | ||
pub requests: u64, | ||
} | ||
|
||
pub fn run_benchmark(test: routes::CreateTest) -> Vec<Result> { | ||
let mut results: Vec<Result> = vec![]; | ||
|
||
for c in 0..test.connections { | ||
tokio::spawn(async move { | ||
match test.method.to_uppercase().as_str() { | ||
"GET" => { | ||
let resp = match reqwest::get(test.url).await { | ||
Ok(r) => {} | ||
Err(err) => {} | ||
}; | ||
} | ||
"POST" => { | ||
let client = reqwest::Client::new(); | ||
let req = client.post(test.url); | ||
let thread_test = test.clone(); | ||
|
||
tokio::task::spawn(async move { | ||
let mut total_result: Vec<Result> = vec![]; | ||
|
||
// We need to fill the vec | ||
for s in 0..test.seconds { | ||
total_result.push(Result { | ||
connection_id: c, | ||
second: s, | ||
error_codes: vec![], | ||
requests: 0, | ||
}) | ||
} | ||
|
||
let total_duration = Duration::new(test.seconds, 0); | ||
let start_time = Instant::now(); | ||
|
||
while start_time.elapsed() < total_duration { | ||
let second = start_time.elapsed().as_secs() as usize; | ||
|
||
if test.body.is_some() { | ||
req = req.body(test.body); // TODO: only append body if we get body. Also | ||
// set header depending on content_type | ||
let resp = match thread_test.method.to_uppercase().as_str() { | ||
"GET" => reqwest::get(thread_test.url.clone()).await, | ||
"POST" => { | ||
let client = reqwest::Client::new(); | ||
let mut req = client.post(thread_test.url.clone()); | ||
|
||
if let Some(b) = thread_test.body.clone() { | ||
req = req.body(b); | ||
} | ||
|
||
if let Some(c) = thread_test.content_type.clone() { | ||
req = req.header("Content-Type", c); | ||
} | ||
|
||
req.send().await | ||
} | ||
_ => { | ||
panic!("method {} not supported", thread_test.method) | ||
} | ||
}; | ||
|
||
if total_result.get(second).is_some() { | ||
total_result[second].requests += 1; | ||
|
||
match resp { | ||
Ok(res) => { | ||
if !res.status().is_success() { | ||
total_result[second].error_codes.push(res.status()); | ||
} | ||
} | ||
Err(err) => { | ||
if let Some(err_status) = err.status() { | ||
total_result[second].error_codes.push(err_status); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
for r in total_result { | ||
// results.push(r); | ||
} | ||
}); | ||
} | ||
|
||
results | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
use anyhow::Result; | ||
use sqlx::SqlitePool; | ||
use sqlx::{migrate, Pool, Sqlite}; | ||
|
||
pub async fn setup() -> Result<SqlitePool> { | ||
let pool = sqlx::SqlitePool::connect("sqlite::memory:").await?; | ||
pub async fn setup() -> Result<Pool<Sqlite>> { | ||
let pool = Pool::<Sqlite>::connect("sqlite::memory:").await?; | ||
|
||
sqlx::migrate!("./migrations").run(&pool).await?; | ||
migrate!("./migrations").run(&pool).await?; | ||
|
||
Ok(pool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters