Skip to content

Commit 1a38979

Browse files
gearamaCopilot
andauthored
Key Vault Keys Perf tests (#2558)
* initial setup * first test compiling * first test compiling * Refactor benchmarks: rename create_key_benchmark to key_operations_benchmark and add get_key benchmark * Update package testing commands to include separate tests for lib, bins, examples, and tests * Refactor benchmarks and testing scripts: improve readability and add comment regarding excluding bench target from tests * Update sdk/keyvault/azure_security_keyvault_keys/benches/benchmarks.rs Co-authored-by: Copilot <[email protected]> * Refactor testing commands in Test-Packages.ps1 to consolidate cargo test invocations and improve efficiency; streamline key creation logic in benchmarks.rs for clarity and performance. * Refactor comments in benchmarks.rs for clarity: update key creation and retrieval descriptions * Refactor Test-Packages.ps1 to streamline test commands and improve efficiency; enhance benchmarks.rs to check environment variable before running benchmarks --------- Co-authored-by: Copilot <[email protected]>
1 parent 7ea8ce1 commit 1a38979

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/keyvault/azure_security_keyvault_keys/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ azure_core_test = { workspace = true, features = [
2727
] }
2828
azure_identity.workspace = true
2929
azure_security_keyvault_test = { path = "../azure_security_keyvault_test" }
30+
criterion.workspace = true
3031
rand.workspace = true
3132
sha2.workspace = true
3233
tokio.workspace = true
@@ -36,3 +37,7 @@ rustc_version.workspace = true
3637

3738
[lints]
3839
workspace = true
40+
41+
[[bench]]
42+
name = "benchmarks"
43+
harness = false
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
use azure_identity::DefaultAzureCredential;
2+
use azure_security_keyvault_keys::{
3+
models::{CreateKeyParameters, CurveName, Key, KeyType},
4+
KeyClient,
5+
};
6+
use criterion::{black_box, criterion_group, criterion_main, Criterion};
7+
8+
fn key_operations_benchmark(c: &mut Criterion) {
9+
const ENV_NAME: &str = "AZURE_KEYVAULT_URL";
10+
11+
// Check if the environment variable is set thus allowing the benchmarks to run
12+
if std::env::var(ENV_NAME).is_err() {
13+
println!("Skipping benchmarks. Set {} to run.", ENV_NAME);
14+
return;
15+
}
16+
17+
let rt = tokio::runtime::Runtime::new().unwrap();
18+
const KEY_NAME: &str = "test-key";
19+
20+
// Setup the KeyClient and the CreateKeyParameters
21+
async fn setup_key_client() -> (KeyClient, CreateKeyParameters) {
22+
let keyvault_url: String =
23+
std::env::var(ENV_NAME).unwrap_or_else(|e| panic!("{} not set: {}", ENV_NAME, e));
24+
let credential = DefaultAzureCredential::new().unwrap();
25+
let client: KeyClient = KeyClient::new(&keyvault_url, credential.clone(), None).unwrap();
26+
let body = CreateKeyParameters {
27+
kty: Some(KeyType::EC),
28+
curve: Some(CurveName::P256),
29+
..Default::default()
30+
};
31+
(client, body)
32+
}
33+
34+
let (client, body) = rt.block_on(async { setup_key_client().await });
35+
36+
// prep key in order to run the benchmark in a clean state
37+
let _ = rt.block_on(async { create_key(KEY_NAME, &client, body.clone()).await });
38+
39+
// Create a key with name
40+
async fn create_key(
41+
name: &str,
42+
client: &KeyClient,
43+
body: CreateKeyParameters,
44+
) -> Result<Key, azure_core::Error> {
45+
// Create a new key version
46+
client
47+
.create_key(name, body.try_into()?, None)
48+
.await?
49+
.into_body()
50+
.await
51+
}
52+
53+
// Get a key by name
54+
async fn get_key(key_name: &str, client: &KeyClient) -> Result<Key, azure_core::Error> {
55+
// Get the key
56+
client.get_key(key_name, "", None).await?.into_body().await
57+
}
58+
59+
// Benchmark create key
60+
c.bench_function("create_key", |b| {
61+
b.to_async(&rt).iter(|| async {
62+
create_key(KEY_NAME, &client, body.clone())
63+
.await
64+
.unwrap_or_else(|e| panic!("Failed to create key {}", e));
65+
black_box(());
66+
});
67+
});
68+
69+
// Benchmark create key
70+
c.bench_function("get_key", |b| {
71+
b.to_async(&rt).iter(|| async {
72+
get_key(KEY_NAME, &client)
73+
.await
74+
.unwrap_or_else(|e| panic!("Failed to get key {}", e));
75+
black_box(());
76+
});
77+
});
78+
}
79+
80+
// Main benchmark configuration
81+
criterion_group! {
82+
name = benchmarks;
83+
config = Criterion::default().sample_size(10).warm_up_time(std::time::Duration::new(1, 0));
84+
targets = key_operations_benchmark
85+
}
86+
87+
criterion_main!(benchmarks);

0 commit comments

Comments
 (0)