|
| 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