Skip to content

Instantly share code, notes, and snippets.

@syphar
Created January 19, 2021 19:28
Show Gist options
  • Save syphar/949cdb2ee297ca5695bad179cfd99a6e to your computer and use it in GitHub Desktop.
Save syphar/949cdb2ee297ca5695bad179cfd99a6e to your computer and use it in GitHub Desktop.
simple test for random crate search
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(())
}
@jyn514
Copy link

jyn514 commented Jan 19, 2021

Here is a Cargo.toml that makes this run:

[package]
name = "test-query"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-std = { version = "*", features = ["attributes"] }
sqlx = { version = "*", features = ["runtime-async-std-rustls", "postgres"] }

@syphar
Copy link
Author

syphar commented Jan 20, 2021

thanks for this, seeing this I think I should have added it myself :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment