|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +pub mod json; |
| 5 | +#[cfg(feature = "xml")] |
| 6 | +pub mod xml; |
| 7 | + |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use azure_core::{ |
| 11 | + base64::{ |
| 12 | + self, |
| 13 | + option::{deserialize, serialize}, |
| 14 | + }, |
| 15 | + http::{headers::Headers, BufResponse, ClientOptions, Pipeline, StatusCode, Transport}, |
| 16 | + time::{self, OffsetDateTime}, |
| 17 | + Bytes, |
| 18 | +}; |
| 19 | +use azure_core_test::http::MockHttpClient; |
| 20 | +use futures::FutureExt as _; |
| 21 | +use serde::{Deserialize, Serialize}; |
| 22 | + |
| 23 | +const DEFAULT_COUNT: usize = 25; |
| 24 | + |
| 25 | +#[derive(Debug, Default, Deserialize, Serialize)] |
| 26 | +pub struct List { |
| 27 | + #[serde(default, rename = "name")] |
| 28 | + name: Option<String>, |
| 29 | + |
| 30 | + #[serde(default, rename = "container")] |
| 31 | + container: Option<ListItemsContainer>, |
| 32 | + |
| 33 | + #[serde(default, rename = "next")] |
| 34 | + next: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Default, Deserialize, Serialize)] |
| 38 | +pub struct ListItemsContainer { |
| 39 | + #[serde(default, rename = "items")] |
| 40 | + items: Option<Vec<ListItem>>, |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Default, Deserialize, Serialize)] |
| 44 | +pub struct ListItem { |
| 45 | + #[serde(default, rename = "name")] |
| 46 | + name: Option<String>, |
| 47 | + |
| 48 | + #[serde(default, rename = "properties")] |
| 49 | + properties: Option<ListItemProperties>, |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Debug, Default, Deserialize, Serialize)] |
| 53 | +pub struct ListItemProperties { |
| 54 | + #[serde(default, rename = "etag")] |
| 55 | + etag: Option<azure_core::http::Etag>, |
| 56 | + |
| 57 | + #[serde(default, rename = "creationTime", with = "time::rfc7231::option")] |
| 58 | + creation_time: Option<OffsetDateTime>, |
| 59 | + |
| 60 | + #[serde(default, rename = "lastModified", with = "time::rfc7231::option")] |
| 61 | + last_modified: Option<OffsetDateTime>, |
| 62 | + |
| 63 | + #[serde( |
| 64 | + default, |
| 65 | + rename = "contentMD5", |
| 66 | + serialize_with = "serialize", |
| 67 | + deserialize_with = "deserialize" |
| 68 | + )] |
| 69 | + content_md5: Option<Vec<u8>>, |
| 70 | +} |
| 71 | + |
| 72 | +fn create_pipeline<F>(count: usize, f: F) -> azure_core::Result<Pipeline> |
| 73 | +where |
| 74 | + F: Fn(&List) -> azure_core::Result<Bytes>, |
| 75 | +{ |
| 76 | + let mut list = List { |
| 77 | + name: Some("t0123456789abcdef".into()), |
| 78 | + ..Default::default() |
| 79 | + }; |
| 80 | + let mut items = Vec::with_capacity(count); |
| 81 | + let now = OffsetDateTime::now_utc(); |
| 82 | + for i in 0..count { |
| 83 | + let name = format!("testItem{i}"); |
| 84 | + let hash = base64::encode(&name).into_bytes(); |
| 85 | + items.push(ListItem { |
| 86 | + name: Some(name), |
| 87 | + properties: Some(ListItemProperties { |
| 88 | + etag: Some(i.to_string().into()), |
| 89 | + creation_time: Some(now), |
| 90 | + last_modified: Some(now), |
| 91 | + content_md5: Some(hash), |
| 92 | + }), |
| 93 | + }); |
| 94 | + } |
| 95 | + list.container = Some(ListItemsContainer { items: Some(items) }); |
| 96 | + |
| 97 | + let body = f(&list)?; |
| 98 | + println!("Serialized {count} items in {} bytes", body.len()); |
| 99 | + |
| 100 | + let client = Arc::new(MockHttpClient::new(move |_| { |
| 101 | + let body = body.clone(); |
| 102 | + async move { |
| 103 | + // Yield simulates an expected network call but kills performance by ~45%. |
| 104 | + tokio::task::yield_now().await; |
| 105 | + Ok(BufResponse::from_bytes( |
| 106 | + StatusCode::Ok, |
| 107 | + Headers::new(), |
| 108 | + body, |
| 109 | + )) |
| 110 | + } |
| 111 | + .boxed() |
| 112 | + })); |
| 113 | + let options = ClientOptions { |
| 114 | + transport: Some(Transport::new(client)), |
| 115 | + ..Default::default() |
| 116 | + }; |
| 117 | + let pipeline = Pipeline::new( |
| 118 | + Some("perf"), |
| 119 | + Some("0.1.0"), |
| 120 | + options, |
| 121 | + Vec::new(), |
| 122 | + Vec::new(), |
| 123 | + None, |
| 124 | + ); |
| 125 | + Ok(pipeline) |
| 126 | +} |
0 commit comments