|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +#[cfg_attr(target_os = "macos", allow(unused_imports))] |
| 4 | +use azure_core::{ |
| 5 | + credentials::TokenCredential, |
| 6 | + fmt::SafeDebug, |
| 7 | + http::{ |
| 8 | + ClientMethodOptions, ClientOptions, HttpClient, Method, Pipeline, RawResponse, Request, |
| 9 | + TransportOptions, Url, |
| 10 | + }, |
| 11 | + Result, |
| 12 | +}; |
| 13 | +#[cfg_attr(target_os = "macos", allow(unused_imports))] |
| 14 | +use azure_identity::DeveloperToolsCredential; |
| 15 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 16 | +use std::sync::Arc; |
| 17 | + |
| 18 | +#[derive(Clone, SafeDebug)] |
| 19 | +pub struct TestServiceClientOptions { |
| 20 | + pub client_options: ClientOptions, |
| 21 | + pub api_version: Option<String>, |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for TestServiceClientOptions { |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + client_options: ClientOptions::default(), |
| 28 | + api_version: Some("2023-10-01".to_string()), |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub struct TestServiceClient { |
| 34 | + endpoint: Url, |
| 35 | + api_version: String, |
| 36 | + pipeline: Pipeline, |
| 37 | +} |
| 38 | + |
| 39 | +#[derive(Default, SafeDebug)] |
| 40 | +pub struct TestServiceClientGetMethodOptions<'a> { |
| 41 | + pub method_options: ClientMethodOptions<'a>, |
| 42 | +} |
| 43 | + |
| 44 | +impl TestServiceClient { |
| 45 | + pub fn new( |
| 46 | + endpoint: &str, |
| 47 | + _credential: Arc<dyn TokenCredential>, |
| 48 | + options: Option<TestServiceClientOptions>, |
| 49 | + ) -> Result<Self> { |
| 50 | + let options = options.unwrap_or_default(); |
| 51 | + let mut endpoint = Url::parse(endpoint)?; |
| 52 | + if !endpoint.scheme().starts_with("http") { |
| 53 | + return Err(azure_core::Error::message( |
| 54 | + azure_core::error::ErrorKind::Other, |
| 55 | + format!("{endpoint} must use http(s)"), |
| 56 | + )); |
| 57 | + } |
| 58 | + endpoint.set_query(None); |
| 59 | + |
| 60 | + Ok(Self { |
| 61 | + endpoint, |
| 62 | + api_version: options.api_version.unwrap_or_default(), |
| 63 | + pipeline: Pipeline::new( |
| 64 | + option_env!("CARGO_PKG_NAME"), |
| 65 | + option_env!("CARGO_PKG_VERSION"), |
| 66 | + options.client_options, |
| 67 | + Vec::default(), |
| 68 | + Vec::default(), |
| 69 | + ), |
| 70 | + }) |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns the Url associated with this client. |
| 74 | + pub fn endpoint(&self) -> &Url { |
| 75 | + &self.endpoint |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the result of a Get verb against the configured endpoint with the specified path. |
| 79 | + /// |
| 80 | + /// This method demonstrates a service client which does not have per-method spans but which will create |
| 81 | + /// HTTP client spans if the `InstrumentationOptions` are configured in the client options. |
| 82 | + /// |
| 83 | + pub async fn get( |
| 84 | + &self, |
| 85 | + path: &str, |
| 86 | + options: Option<TestServiceClientGetMethodOptions<'_>>, |
| 87 | + ) -> Result<RawResponse> { |
| 88 | + let options = options.unwrap_or_default(); |
| 89 | + let mut url = self.endpoint.clone(); |
| 90 | + url.set_path(path); |
| 91 | + url.query_pairs_mut() |
| 92 | + .append_pair("api-version", &self.api_version); |
| 93 | + |
| 94 | + let mut request = Request::new(url, azure_core::http::Method::Get); |
| 95 | + |
| 96 | + let response = self |
| 97 | + .pipeline |
| 98 | + .send(&options.method_options.context, &mut request) |
| 99 | + .await?; |
| 100 | + if !response.status().is_success() { |
| 101 | + return Err(azure_core::Error::message( |
| 102 | + azure_core::error::ErrorKind::HttpResponse { |
| 103 | + status: response.status(), |
| 104 | + error_code: None, |
| 105 | + }, |
| 106 | + format!("Failed to GET {}: {}", request.url(), response.status()), |
| 107 | + )); |
| 108 | + } |
| 109 | + Ok(response) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +pub fn new_reqwest_client_disable_connection_pool() -> Arc<dyn HttpClient> { |
| 114 | + let client = ::reqwest::ClientBuilder::new() |
| 115 | + .pool_max_idle_per_host(0) |
| 116 | + .build() |
| 117 | + .expect("failed to build `reqwest` client"); |
| 118 | + |
| 119 | + Arc::new(client) |
| 120 | +} |
| 121 | + |
| 122 | +pub fn new_default_reqwest_client() -> Arc<dyn HttpClient> { |
| 123 | + let client = ::reqwest::Client::new(); |
| 124 | + |
| 125 | + Arc::new(client) |
| 126 | +} |
| 127 | + |
| 128 | +#[cfg_attr(target_os = "macos", allow(unused_variables))] |
| 129 | +pub fn simple_http_transport_test(c: &mut Criterion) { |
| 130 | + #[cfg(target_os = "macos")] |
| 131 | + return; |
| 132 | + |
| 133 | + #[cfg(not(target_os = "macos"))] |
| 134 | + { |
| 135 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 136 | + |
| 137 | + let endpoint = "https://azuresdkforcpp.azurewebsites.net"; |
| 138 | + let credential = DeveloperToolsCredential::new(None).unwrap(); |
| 139 | + let options = TestServiceClientOptions::default(); |
| 140 | + |
| 141 | + let client = TestServiceClient::new(endpoint, credential, Some(options)).unwrap(); |
| 142 | + |
| 143 | + // Benchmark GET and POST requests |
| 144 | + c.bench_function("default_http_pipeline_test", |b| { |
| 145 | + b.to_async(&rt).iter(|| async { |
| 146 | + let response = client.get("get", None).await; |
| 147 | + assert!(response.is_ok()); |
| 148 | + let response = response.unwrap(); |
| 149 | + assert_eq!(response.status(), azure_core::http::StatusCode::Ok); |
| 150 | + }); |
| 151 | + }); |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#[cfg_attr(target_os = "macos", allow(unused_variables))] |
| 156 | +pub fn disable_pooling_http_transport_test(c: &mut Criterion) { |
| 157 | + #[cfg(target_os = "macos")] |
| 158 | + return; |
| 159 | + |
| 160 | + #[cfg(not(target_os = "macos"))] |
| 161 | + { |
| 162 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 163 | + |
| 164 | + let endpoint = "https://azuresdkforcpp.azurewebsites.net"; |
| 165 | + let credential = DeveloperToolsCredential::new(None).unwrap(); |
| 166 | + let transport = new_reqwest_client_disable_connection_pool(); |
| 167 | + let options = TestServiceClientOptions { |
| 168 | + client_options: ClientOptions { |
| 169 | + transport: Some(TransportOptions::new(transport)), |
| 170 | + ..Default::default() |
| 171 | + }, |
| 172 | + ..Default::default() |
| 173 | + }; |
| 174 | + |
| 175 | + let client = TestServiceClient::new(endpoint, credential, Some(options)).unwrap(); |
| 176 | + |
| 177 | + // Benchmark GET and POST requests |
| 178 | + c.bench_function("disable_pooling_http_pipeline_test", |b| { |
| 179 | + b.to_async(&rt).iter(|| async { |
| 180 | + let response = client.get("get", None).await; |
| 181 | + assert!(response.is_ok()); |
| 182 | + let response = response.unwrap(); |
| 183 | + assert_eq!(response.status(), azure_core::http::StatusCode::Ok); |
| 184 | + }); |
| 185 | + }); |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +#[cfg_attr(target_os = "macos", allow(unused_variables))] |
| 190 | +pub fn baseline_http_transport_test(c: &mut Criterion) { |
| 191 | + #[cfg(target_os = "macos")] |
| 192 | + return; |
| 193 | + |
| 194 | + #[cfg(not(target_os = "macos"))] |
| 195 | + { |
| 196 | + let rt = tokio::runtime::Runtime::new().unwrap(); |
| 197 | + let endpoint = "https://azuresdkforcpp.azurewebsites.net"; |
| 198 | + |
| 199 | + let http_client = new_default_reqwest_client(); |
| 200 | + |
| 201 | + // Benchmark GET and POST requests |
| 202 | + c.bench_function("baseline_http_pipeline_test", |b| { |
| 203 | + b.to_async(&rt).iter(|| async { |
| 204 | + let request = Request::new( |
| 205 | + Url::parse(&format!("{}/get", endpoint)).unwrap(), |
| 206 | + Method::Get, |
| 207 | + ); |
| 208 | + let response = http_client.execute_request(&request).await; |
| 209 | + assert!(response.is_ok()); |
| 210 | + let response = response.unwrap(); |
| 211 | + assert_eq!(response.status(), azure_core::http::StatusCode::Ok); |
| 212 | + }); |
| 213 | + }); |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// Main benchmark configuration |
| 218 | +criterion_group!(name=http_transport_benchmarks; |
| 219 | + config=Criterion::default() |
| 220 | + .sample_size(100) |
| 221 | + .warm_up_time(std::time::Duration::new(10, 0)) |
| 222 | + .measurement_time(std::time::Duration::new(50, 0)); |
| 223 | + targets=simple_http_transport_test, disable_pooling_http_transport_test, baseline_http_transport_test |
| 224 | +); |
| 225 | + |
| 226 | +criterion_main!(http_transport_benchmarks); |
0 commit comments