Skip to content

Commit 9e603ac

Browse files
committed
Cleaned up test creation logic
1 parent b168643 commit 9e603ac

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

sdk/core/azure_core/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
### Breaking Changes
1818

19-
2019
- Changed `ClientOptions::retry` from `Option<RetryOptions>` to `RetryOptions`.
2120
- Changed `DeserializeWith::deserialize_with()` to be sync.
2221
- Changed `Pipeline::send()` to return a `Result<RawResponse>`.

sdk/core/azure_core_test/src/perf/framework_tests.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,25 @@ fn create_fibonacci1_test(runner: &PerfRunner) -> CreatePerfTestReturn {
5151
Ok(())
5252
}
5353
}
54-
// Manually handle the Result instead of using ? because this function does not return a Result.
55-
let count: Option<&String> = match runner.try_get_test_arg("count") {
56-
Ok(v) => v,
57-
Err(e) => {
58-
// Return a future that immediately yields the error.
59-
return Box::pin(async move { Err(e) });
60-
}
61-
};
62-
println!("Fibonacci1Test with count: {:?}", count);
63-
let count = count.expect("count argument is mandatory");
64-
let count = match count.parse::<u32>() {
65-
Ok(v) => v,
66-
Err(e) => {
67-
let err = azure_core::Error::with_message(
54+
55+
// Helper function to handle the async creation of the test.
56+
async fn create_test(runner: PerfRunner) -> Result<Box<dyn PerfTest>> {
57+
let count: Option<&String> = runner.try_get_test_arg("count")?;
58+
59+
println!("Fibonacci1Test with count: {:?}", count);
60+
let count = count.expect("count argument is mandatory");
61+
let count = count.parse::<u32>().map_err(|e| {
62+
azure_core::Error::with_error(
6863
azure_core::error::ErrorKind::Other,
69-
format!("invalid count argument: {}", e),
70-
);
71-
return Box::pin(async move { Err(err) });
72-
}
73-
};
74-
Box::pin(async move { Ok(Box::new(Fibonacci1Test { count }) as Box<dyn PerfTest>) })
64+
e,
65+
"Invalid count argument",
66+
)
67+
})?;
68+
Ok(Box::new(Fibonacci1Test { count }) as Box<dyn PerfTest>)
69+
}
70+
71+
// Return a pinned future that creates the test.
72+
Box::pin(create_test(runner.clone()))
7573
}
7674

7775
#[tokio::test]

sdk/core/azure_core_test/src/perf/mod.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
#![doc = include_str!("README.md")]
5+
#![cfg(not(target_arch = "wasm32"))]
56

67
use crate::TestContext;
78
use azure_core::{time::Duration, Error, Result};
@@ -17,8 +18,7 @@ use std::{
1718
};
1819
use tokio::{select, task::JoinSet};
1920

20-
#[cfg_attr(target_arch = "wasm32", async_trait::async_trait(?Send))]
21-
#[cfg_attr(not(target_arch = "wasm32"), async_trait::async_trait)]
21+
#[async_trait::async_trait]
2222
pub trait PerfTest: Send + Sync {
2323
async fn setup(&self, context: &TestContext) -> azure_core::Result<()>;
2424
async fn run(&self /*, context: &TestContext*/) -> azure_core::Result<()>;
@@ -67,15 +67,15 @@ pub struct TestOption {
6767
pub sensitive: bool,
6868
}
6969

70-
#[derive(Debug)]
71-
#[allow(dead_code)]
70+
#[derive(Debug, Clone)]
7271
struct PerfRunnerOptions {
7372
no_cleanup: bool,
7473
iterations: u32,
7574
parallel: usize,
7675
duration: Duration,
7776
warmup: Duration,
7877
disable_progress: bool,
78+
#[allow(dead_code)]
7979
test_results_filename: String,
8080
}
8181

@@ -109,10 +109,9 @@ impl From<&ArgMatches> for PerfRunnerOptions {
109109
}
110110

111111
/// Context information required by performance tests.
112-
#[derive(Debug)]
112+
#[derive(Debug, Clone)]
113113
pub struct PerfRunner {
114114
options: PerfRunnerOptions,
115-
#[allow(dead_code)]
116115
tests: Vec<TestMetadata>,
117116
arguments: ArgMatches,
118117
package_dir: &'static str,
@@ -264,7 +263,7 @@ impl PerfRunner {
264263
let operations_per_second =
265264
self.options.duration.as_seconds_f64() / iteration_count as f64;
266265
let duration_per_operation = Duration::seconds_f64(operations_per_second);
267-
println!("{} seconds/operation", duration_per_operation);
266+
println!("{:4} seconds/operation", duration_per_operation);
268267
}
269268
Ok(())
270269
}
@@ -298,7 +297,7 @@ impl PerfRunner {
298297
_ = async {
299298
loop {
300299
tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
301-
println!("{:?} elapsed: {} op/sec, {} sec/ operation.",
300+
println!("{:<10?} elapsed: {:.5} op/sec, {:4} sec/operation.",
302301
start.elapsed(),
303302
self.progress.load(Ordering::SeqCst) as f64 / start.elapsed().as_secs_f64(),
304303
Duration::seconds_f64( start.elapsed().as_secs_f64() / self.progress.load(Ordering::SeqCst) as f64 ));

sdk/keyvault/azure_security_keyvault_secrets/perf/get_secret.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,19 @@ impl GetSecrets {
3535
}
3636

3737
fn create_new_test(runner: &PerfRunner) -> CreatePerfTestReturn {
38-
let vault_url_ref: Option<&String> = match runner.try_get_test_arg("vault_url") {
39-
Ok(v) => v,
40-
Err(e) => {
41-
// Return a future that immediately yields the error.
42-
return Box::pin(async move { Err(e) });
43-
}
44-
};
45-
// Own the String so the future can be 'static.
46-
let vault_url = vault_url_ref
47-
.expect("vault_url argument is mandatory")
48-
.clone();
49-
Box::pin(async move {
38+
async fn create_secret_client(runner: PerfRunner) -> Result<Box<dyn PerfTest>> {
39+
let vault_url_ref: Option<&String> = runner.try_get_test_arg("vault_url")?;
40+
let vault_url = vault_url_ref
41+
.expect("vault_url argument is mandatory")
42+
.clone();
5043
Ok(Box::new(GetSecrets {
5144
vault_url,
5245
random_key_name: OnceLock::new(),
5346
client: OnceLock::new(),
5447
}) as Box<dyn PerfTest>)
55-
})
48+
}
49+
50+
Box::pin(create_secret_client(runner.clone()))
5651
}
5752

5853
fn create_random_key_name() -> String {
@@ -103,8 +98,7 @@ impl PerfTest for GetSecrets {
10398
.unwrap()
10499
.get_secret(self.get_random_key_name(), None)
105100
.await?
106-
.into_body()
107-
.await?;
101+
.into_body()?;
108102
Ok(())
109103
}
110104
}

0 commit comments

Comments
 (0)