Created
January 19, 2021 19:28
-
-
Save syphar/949cdb2ee297ca5695bad179cfd99a6e to your computer and use it in GitHub Desktop.
simple test for random crate search
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
use async_std::prelude::*; | |
use sqlx::postgres::PgPoolOptions; | |
#[async_std::main] | |
pub async fn main() -> Result<(), sqlx::Error> { | |
let pool = PgPoolOptions::new() | |
.connect(&std::env::var("DATABASE_URL").unwrap()) | |
.await?; | |
let mut count_with_result = 0; | |
let mut count_without_result = 0; | |
let records = 10000; | |
for _ in 0..records { | |
if let Some(_) = sqlx::query!( | |
"WITH params AS ( | |
-- get maximum possible id-value in crates-table | |
SELECT last_value AS max_id FROM crates_id_seq | |
) | |
SELECT | |
crates.name, | |
releases.version, | |
releases.target_name | |
FROM ( | |
-- generate 500 random numbers in the ID-range. | |
-- this might have to be increased when we have to repeat | |
-- this query too often. | |
-- it depends on the percentage of crates with > 100 stars | |
SELECT DISTINCT 1 + trunc(random() * params.max_id)::INTEGER AS id | |
FROM params, generate_series(1, 100) | |
) AS r | |
INNER JOIN crates ON r.id = crates.id | |
INNER JOIN releases ON crates.latest_version_id = releases.id | |
INNER JOIN github_repos ON releases.github_repo = github_repos.id | |
WHERE | |
releases.rustdoc_status = TRUE AND | |
github_repos.stars >= 100 | |
LIMIT 1" | |
) | |
.fetch_optional(&pool) | |
.await? | |
{ | |
count_with_result += 1; | |
} else { | |
count_without_result += 1; | |
} | |
} | |
println!("records: {}", records); | |
println!("with result: {}", count_with_result); | |
println!("without result: {}", count_without_result); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for this, seeing this I think I should have added it myself :D