Skip to content

Commit

Permalink
Adds extra env var for memory prof
Browse files Browse the repository at this point in the history
* That extra env var is for a specific case with graphql and no auth
* Adds remaining tests
* Adds step by step doc
  • Loading branch information
helio-frota committed Oct 21, 2024
1 parent 8033d27 commit 2d0ebb0
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 70 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ log = "0.4"
openid = "0.14"
reqwest = "0.12"
tokio = { version = "1.38.0", features = ["sync"] }
urlencoding = "2"

[patch.crates-io]
#goose = { path = "../../goose" }
Expand Down
57 changes: 57 additions & 0 deletions graphql-mem-prof.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# graphql memory profiling with heaptrack and disabled auth

* Install [heaptrack](https://github.com/KDE/heaptrack)

```shell
sudo dnf install heaptrack
```

* Clone trustify and generate a database dump

```shell
cargo run --bin xtask generate-dump
```

* Use the generated dump
* Change `trustify/etc/deploy/compose/compose.yaml`
* Add the following:

```yaml
volumes:
- /dump_path_here/dump.sql:/docker-entrypoint-initdb.d/dump.sql:Z
```
* Open a terminal and start postgres
```shell
podman-compose -f etc/deploy/compose/compose.yaml up
```

* Change `trustify/Cargo.toml`
* Add the following:

```yaml
[profile.release]
debug = true
```

* Clean and build with `--release`

```shell
cargo clean ; cargo build --release
```

* Open a terminal and start trustify with heaptrack and graphql feature

```shell
cd target/release/
TRUSTD_WITH_GRAPHQL=true heaptrack ./trustd api --db-password eggs --devmode --auth-disabled
```

* Run loadtest

```shell
MEM_PROF=true cargo run --release --bin loadtest -- --host http://localhost:8080 -u 256
```

* Stop loadtest and trustify, and heaptrack will show the results
51 changes: 45 additions & 6 deletions src/graphql.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,49 @@
use goose::goose::{GooseUser, TransactionResult};
use urlencoding::encode;

pub async fn graphql_query_advisory(user: &mut GooseUser) -> TransactionResult {
let query = r#"{"query": "{getAdvisories { id sha256 published organization { id website } vulnerabilities { id title }}}" }"#;
let encoded_query = encode(query);
let url = format!("/graphql?query={}", encoded_query);
let _response = user.get(&url).await?;
pub async fn g_get_advisories(user: &mut GooseUser) -> TransactionResult {
let body = r#"{"query": "{getAdvisories { id sha256 published organization { id website } vulnerabilities { id title }}}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_advisory_by_id(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ getAdvisoryById(id: \"37292820-00ee-4299-8097-a11f8348bdf8\") { id title }}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_organization_by_name(user: &mut GooseUser) -> TransactionResult {
let body = r#"{"query": "{ getOrganizationByName(name: \"Red Hat\") { id name }}"}"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_sbom_by_id(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ getSbomById(id: \"01926efa-bbb2-7222-9f4d-692bd5e40a46\") {sbomId, authors}}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_sbom_by_labels(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ getSbomsByLabels(labels: \"type:spdx\") { sbomId sourceDocumentId authors labels }}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_cves_by_sbom(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ cvesBySbom(id: \"01926efa-bbb2-7222-9f4d-692bd5e40a46\") { vulnerabilityId status packages { id name version } }}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_vulnerability_by_id(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ getVulnerabilityById(identifier: \"CVE-2024-28111\") { id published }}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}

pub async fn g_get_vulnerabilities(user: &mut GooseUser) -> TransactionResult {
let body = r#"{ "query": "{ getVulnerabilities { id published withdrawn }}" }"#;
let _response = user.post("/graphql", body).await?;
Ok(())
}
151 changes: 88 additions & 63 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod restapi;
mod website;

use crate::{
graphql::graphql_query_advisory,
graphql::*,
oidc::{OpenIdTokenProvider, OpenIdTokenProviderConfigArguments},
restapi::*,
website::*,
Expand All @@ -25,75 +25,100 @@ macro_rules! tx {

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let mem_prof: bool = std::env::var("MEM_PROF")
.map(|s| s.parse().unwrap_or(false))
.unwrap_or(false);

let wait_time_from: u64 = std::env::var("WAIT_TIME_FROM")
.map(|s| s.parse().unwrap_or(5))
.unwrap_or(5);
let wait_time_to: u64 = std::env::var("WAIT_TIME_TO")
.map(|s| s.parse().unwrap_or(15))
.unwrap_or(15);

let provider = create_oidc_provider().await?;
let custom_client = Transaction::new(Arc::new(move |user| {
let provider = provider.clone();
Box::pin(async move { setup_custom_client(&provider, user).await })
}));

GooseAttack::initialize()?
.register_scenario(
scenario!("WebsiteUser")
// .set_weight(1)?
.register_transaction(custom_client.clone().set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(tx!(website_index))
.register_transaction(tx!(website_openapi))
.register_transaction(tx!(website_sboms))
.register_transaction(tx!(website_packages))
.register_transaction(tx!(website_advisories))
.register_transaction(tx!(website_importers)),
)
.register_scenario(
scenario!("RestAPIUser")
// .set_weight(1)?
.register_transaction(custom_client.clone().set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(tx!(list_organizations))
.register_transaction(tx!(list_advisory))
.register_transaction(tx!(list_advisory_paginated))
.register_transaction(tx!(get_advisory_by_doc_id))
.register_transaction(tx!(list_vulnerabilities))
.register_transaction(tx!(list_vulnerabilities_paginated))
.register_transaction(tx!(list_importer))
.register_transaction(tx!(list_packages))
.register_transaction(tx!(list_packages_paginated))
.register_transaction(tx!(search_packages))
.register_transaction(tx!(list_products))
.register_transaction(tx!(list_sboms))
.register_transaction(tx!(list_sboms_paginated)),
)
.register_scenario(
scenario!("GraphQLUser")
// .set_weight(1)?
.register_transaction(custom_client.set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(
tx!(graphql_query_advisory).set_name("query advisory with /graphql"),
),
)
.execute()
.await?;

if !mem_prof {
let provider = create_oidc_provider().await?;
let custom_client = Transaction::new(Arc::new(move |user| {
let provider = provider.clone();
Box::pin(async move { setup_custom_client(&provider, user).await })
}));

GooseAttack::initialize()?
.register_scenario(
scenario!("WebsiteUser")
// .set_weight(1)?
.register_transaction(custom_client.clone().set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(tx!(website_index))
.register_transaction(tx!(website_openapi))
.register_transaction(tx!(website_sboms))
.register_transaction(tx!(website_packages))
.register_transaction(tx!(website_advisories))
.register_transaction(tx!(website_importers)),
)
.register_scenario(
scenario!("RestAPIUser")
// .set_weight(1)?
.register_transaction(custom_client.clone().set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(tx!(list_organizations))
.register_transaction(tx!(list_advisory))
.register_transaction(tx!(list_advisory_paginated))
.register_transaction(tx!(get_advisory_by_doc_id))
.register_transaction(tx!(list_vulnerabilities))
.register_transaction(tx!(list_vulnerabilities_paginated))
.register_transaction(tx!(list_importer))
.register_transaction(tx!(list_packages))
.register_transaction(tx!(list_packages_paginated))
.register_transaction(tx!(search_packages))
.register_transaction(tx!(list_products))
.register_transaction(tx!(list_sboms))
.register_transaction(tx!(list_sboms_paginated)),
)
.register_scenario(
scenario!("GraphQLUser")
// .set_weight(1)?
.register_transaction(custom_client.set_name("logon"))
// After each transactions runs, sleep randomly from 5 to 15 seconds.
.set_wait_time(
Duration::from_secs(wait_time_from),
Duration::from_secs(wait_time_to),
)?
.register_transaction(tx!(g_get_advisories))
.register_transaction(tx!(g_get_advisory_by_id))
.register_transaction(tx!(g_get_organization_by_name))
.register_transaction(tx!(g_get_sbom_by_id))
.register_transaction(tx!(g_get_sbom_by_labels))
.register_transaction(tx!(g_cves_by_sbom))
.register_transaction(tx!(g_get_vulnerability_by_id))
.register_transaction(tx!(g_get_vulnerabilities)),
)
.execute()
.await?;
} else {
GooseAttack::initialize()?
.register_scenario(
scenario!("GraphQLUser")
.register_transaction(tx!(g_get_advisories))
.register_transaction(tx!(g_get_advisory_by_id))
.register_transaction(tx!(g_get_organization_by_name))
.register_transaction(tx!(g_get_sbom_by_id))
.register_transaction(tx!(g_get_sbom_by_labels))
.register_transaction(tx!(g_cves_by_sbom))
.register_transaction(tx!(g_get_vulnerability_by_id))
.register_transaction(tx!(g_get_vulnerabilities)),
)
.execute()
.await?;
}
Ok(())
}

Expand Down

0 comments on commit 2d0ebb0

Please sign in to comment.