Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions jans-cedarling/cedarling/benches/authz_authorize_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,31 @@ use tokio::runtime::Runtime;

const POLICY_STORE: &str = include_str!("../../test_files/policy-store_ok.yaml");

// Validates that the cedarling instance actually works before benchmarking.
async fn validate_cedarling_works(cedarling: &Cedarling, request: &Request) {
let result = cedarling
.authorize(request.clone())
.await
.expect("authorization call failed");

let is_allowed = match result.cedar_decision() {
cedar_policy::Decision::Allow => true,
cedar_policy::Decision::Deny => false,
};

assert!(is_allowed, "got invalid authorization result");
}

fn without_jwt_validation_benchmark(c: &mut Criterion) {
let runtime = Runtime::new().expect("init tokio runtime");

let cedarling = runtime
.block_on(prepare_cedarling_without_jwt_validation())
.expect("should initialize Cedarling");

let request =
prepare_cedarling_request_for_without_jwt_validation().expect("should prepare r:equest");
let request = prepare_cedarling_request_for_without_jwt_validation();

runtime.block_on(validate_cedarling_works(&cedarling, &request));

c.bench_with_input(
BenchmarkId::new("authz_authorize_without_jwt_validation", "tokio runtime"),
Expand All @@ -53,8 +69,10 @@ fn with_jwt_validation_hs256_benchmark(c: &mut Criterion) {
))
.expect("should initialize Cedarling");

let request = prepare_cedarling_request_for_with_jwt_validation(mock1.keys)
.expect("should prepare request");
let request =
prepare_cedarling_request_for_with_jwt_validation(&mock1.keys, &mock1.base_idp_url);

runtime.block_on(validate_cedarling_works(&cedarling, &request));

c.bench_with_input(
BenchmarkId::new("authz_authorize_with_jwt_validation_hs256", "tokio runtime"),
Expand Down Expand Up @@ -163,15 +181,14 @@ async fn prepare_cedarling_with_jwt_validation(
Cedarling::new(&bootstrap_config).await
}

pub fn prepare_cedarling_request_for_without_jwt_validation() -> Result<Request, serde_json::Error>
{
pub fn prepare_cedarling_request_for_without_jwt_validation() -> Request {
Request::deserialize(serde_json::json!(
{
"tokens": {
"access_token": generate_token_using_claims(json!({
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"code": "bf1934f6-3905-420a-8299-6b2e3ffddd6e",
"iss": "https://admin-ui-test.gluu.org",
"iss": "https://test.jans.org",
"token_type": "Bearer",
"client_id": "5b4487c4-8db1-409d-a653-f907b8094039",
"aud": "5b4487c4-8db1-409d-a653-f907b8094039",
Expand Down Expand Up @@ -201,7 +218,7 @@ pub fn prepare_cedarling_request_for_without_jwt_validation() -> Result<Request,
"exp": 1724835859,
"iat": 1724832259,
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"iss": "https://admin-ui-test.gluu.org",
"iss": "https://test.jans.org",
"jti": "sk3T40NYSYuk5saHZNpkZw",
"nonce": "c3872af9-a0f5-4c3f-a1af-f9d0e8846e81",
"sid": "6a7fe50a-d810-454d-be5d-549d29595a09",
Expand All @@ -222,7 +239,7 @@ pub fn prepare_cedarling_request_for_without_jwt_validation() -> Result<Request,
"email": "user@example.com",
"username": "UserNameExample",
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"iss": "https://admin-ui-test.gluu.org",
"iss": "https://test.jans.org",
"given_name": "Admin",
"middle_name": "Admin",
"inum": "8d1cde6a-1447-4766-b3c8-16663e13b458",
Expand Down Expand Up @@ -251,18 +268,20 @@ pub fn prepare_cedarling_request_for_without_jwt_validation() -> Result<Request,
"context": {},
}
))
.expect("should build request")
}

pub fn prepare_cedarling_request_for_with_jwt_validation(
keys1: KeyPair,
) -> Result<Request, serde_json::Error> {
keys1: &KeyPair,
issuer_url: &str,
) -> Request {
Request::deserialize(serde_json::json!(
{
"tokens": {
"access_token": generate_token_using_claims_and_keypair(&json!({
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"code": "bf1934f6-3905-420a-8299-6b2e3ffddd6e",
"iss": "https://admin-ui-test.gluu.org",
"iss": issuer_url,
"token_type": "Bearer",
"client_id": "5b4487c4-8db1-409d-a653-f907b8094039",
"aud": "5b4487c4-8db1-409d-a653-f907b8094039",
Expand All @@ -284,15 +303,15 @@ pub fn prepare_cedarling_request_for_with_jwt_validation(
"uri": "https://admin-ui-test.gluu.org/jans-auth/restv1/status_list"
}
}
}), &keys1),
}), keys1),
"id_token": generate_token_using_claims_and_keypair(&json!({
"acr": "basic",
"amr": "10",
"aud": "5b4487c4-8db1-409d-a653-f907b8094039",
"exp": 1724835859,
"iat": 1724832259,
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"iss": "https://admin-ui-test.gluu.org",
"iss": issuer_url,
"jti": "sk3T40NYSYuk5saHZNpkZw",
"nonce": "c3872af9-a0f5-4c3f-a1af-f9d0e8846e81",
"sid": "6a7fe50a-d810-454d-be5d-549d29595a09",
Expand All @@ -307,13 +326,13 @@ pub fn prepare_cedarling_request_for_with_jwt_validation(
}
},
"role":"Admin"
}),&keys1),
}), keys1),
"userinfo_token": generate_token_using_claims_and_keypair(&json!({
"country": "US",
"email": "user@example.com",
"username": "UserNameExample",
"sub": "boG8dfc5MKTn37o7gsdCeyqL8LpWQtgoO41m1KZwdq0",
"iss": "https://admin-ui-test.gluu.org",
"iss": issuer_url,
"given_name": "Admin",
"middle_name": "Admin",
"inum": "8d1cde6a-1447-4766-b3c8-16663e13b458",
Expand All @@ -328,7 +347,7 @@ pub fn prepare_cedarling_request_for_with_jwt_validation(
"api-admin"
],
"exp": 1724945978
}), &keys1),
}), keys1),
},
"action": "Jans::Action::\"Update\"",
"resource": {
Expand All @@ -342,4 +361,5 @@ pub fn prepare_cedarling_request_for_with_jwt_validation(
"context": {},
}
))
.expect("should build request")
}
19 changes: 16 additions & 3 deletions jans-cedarling/scripts/check_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

EXCLUDE_BENCHMARKS = {"cedarling_startup"}

# The following benchmarks have a high threshold just to make sure that the benchmark tests are not failing in CI.
# This should be removed once issue https://github.com/JanssenProject/jans/issues/12947 is fixed.
EXCLUSION_THRESHOLD = 1_500_000 # 1.5 milliseconds in nanoseconds
PROBLEMATIC_BENCHMARKS = {
"authz_authorize_without_jwt_validation",
"authz_authorize_with_jwt_validation_hs256",
}


def check_benchmarks():
failed = False
Expand All @@ -27,14 +35,19 @@ def check_benchmarks():
if estimate is None:
continue

if estimate > THRESHOLD_NS:
if benchmark_name in PROBLEMATIC_BENCHMARKS:
threshold = EXCLUSION_THRESHOLD
else:
threshold = THRESHOLD_NS

if estimate > threshold:
print(
f"❌ {benchmark_full_name}: {estimate:.0f} ns > {THRESHOLD_NS} ns"
f"❌ {benchmark_full_name}: {estimate:.0f} ns > {threshold} ns"
)
failed = True
else:
print(
f"✅ {benchmark_full_name}: {estimate:.0f} ns <= {THRESHOLD_NS} ns"
f"✅ {benchmark_full_name}: {estimate:.0f} ns <= {threshold} ns"
)

if failed:
Expand Down
Loading