|
| 1 | +use anyhow::Result; |
| 2 | +use mongodb::{ |
| 3 | + bson::{doc, Document}, |
| 4 | + options::{DeleteOneModel, InsertOneModel, ReplaceOneModel, WriteModel}, |
| 5 | + Client, |
| 6 | + Namespace, |
| 7 | +}; |
| 8 | +use once_cell::sync::Lazy; |
| 9 | + |
| 10 | +use super::{drop_database, Benchmark, COLL_NAME, DATABASE_NAME}; |
| 11 | + |
| 12 | +pub struct InsertBulkWriteBenchmark { |
| 13 | + client: Client, |
| 14 | + uri: String, |
| 15 | + write_models: Vec<WriteModel>, |
| 16 | +} |
| 17 | + |
| 18 | +pub struct Options { |
| 19 | + pub uri: String, |
| 20 | + pub doc: Document, |
| 21 | + pub num_models: usize, |
| 22 | +} |
| 23 | + |
| 24 | +#[async_trait::async_trait] |
| 25 | +impl Benchmark for InsertBulkWriteBenchmark { |
| 26 | + type Options = Options; |
| 27 | + type TaskState = Vec<WriteModel>; |
| 28 | + |
| 29 | + async fn setup(options: Self::Options) -> Result<Self> { |
| 30 | + let client = Client::with_uri_str(&options.uri).await?; |
| 31 | + drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?; |
| 32 | + |
| 33 | + let write_models = vec![ |
| 34 | + WriteModel::InsertOne( |
| 35 | + InsertOneModel::builder() |
| 36 | + .namespace(Namespace::new(DATABASE_NAME.as_str(), COLL_NAME.as_str())) |
| 37 | + .document(options.doc.clone()) |
| 38 | + .build() |
| 39 | + ); |
| 40 | + options.num_models |
| 41 | + ]; |
| 42 | + |
| 43 | + Ok(Self { |
| 44 | + client, |
| 45 | + uri: options.uri, |
| 46 | + write_models, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + async fn before_task(&self) -> Result<Self::TaskState> { |
| 51 | + self.client |
| 52 | + .database(&DATABASE_NAME) |
| 53 | + .collection::<Document>(&COLL_NAME) |
| 54 | + .drop() |
| 55 | + .await?; |
| 56 | + self.client |
| 57 | + .database(&DATABASE_NAME) |
| 58 | + .create_collection(COLL_NAME.as_str()) |
| 59 | + .await?; |
| 60 | + Ok(self.write_models.clone()) |
| 61 | + } |
| 62 | + |
| 63 | + async fn do_task(&self, write_models: Self::TaskState) -> Result<()> { |
| 64 | + self.client.bulk_write(write_models).await?; |
| 65 | + Ok(()) |
| 66 | + } |
| 67 | + |
| 68 | + async fn teardown(&self) -> Result<()> { |
| 69 | + drop_database(self.uri.as_str(), DATABASE_NAME.as_str()).await?; |
| 70 | + Ok(()) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +static COLLECTION_NAMES: Lazy<Vec<String>> = |
| 75 | + Lazy::new(|| (1..=10).map(|i| format!("corpus_{}", i)).collect()); |
| 76 | + |
| 77 | +pub struct MixedBulkWriteBenchmark { |
| 78 | + client: Client, |
| 79 | + uri: String, |
| 80 | + write_models: Vec<WriteModel>, |
| 81 | +} |
| 82 | + |
| 83 | +#[async_trait::async_trait] |
| 84 | +impl Benchmark for MixedBulkWriteBenchmark { |
| 85 | + type Options = Options; |
| 86 | + type TaskState = Vec<WriteModel>; |
| 87 | + |
| 88 | + async fn setup(options: Self::Options) -> Result<Self> { |
| 89 | + let client = Client::with_uri_str(&options.uri).await?; |
| 90 | + drop_database(options.uri.as_str(), DATABASE_NAME.as_str()).await?; |
| 91 | + |
| 92 | + let mut write_models = Vec::new(); |
| 93 | + for i in 0..options.num_models { |
| 94 | + let collection_name = COLLECTION_NAMES.get(i % 10).unwrap(); |
| 95 | + let namespace = Namespace::new(DATABASE_NAME.as_str(), collection_name); |
| 96 | + if i % 3 == 0 { |
| 97 | + write_models.push( |
| 98 | + InsertOneModel::builder() |
| 99 | + .namespace(namespace) |
| 100 | + .document(options.doc.clone()) |
| 101 | + .build() |
| 102 | + .into(), |
| 103 | + ); |
| 104 | + } else if i % 3 == 1 { |
| 105 | + write_models.push( |
| 106 | + ReplaceOneModel::builder() |
| 107 | + .namespace(namespace) |
| 108 | + .filter(doc! {}) |
| 109 | + .replacement(options.doc.clone()) |
| 110 | + .build() |
| 111 | + .into(), |
| 112 | + ); |
| 113 | + } else { |
| 114 | + write_models.push( |
| 115 | + DeleteOneModel::builder() |
| 116 | + .namespace(namespace) |
| 117 | + .filter(doc! {}) |
| 118 | + .build() |
| 119 | + .into(), |
| 120 | + ); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + Ok(Self { |
| 125 | + client, |
| 126 | + uri: options.uri, |
| 127 | + write_models, |
| 128 | + }) |
| 129 | + } |
| 130 | + |
| 131 | + async fn before_task(&self) -> Result<Self::TaskState> { |
| 132 | + let database = self.client.database(&DATABASE_NAME); |
| 133 | + database.drop().await?; |
| 134 | + for collection_name in COLLECTION_NAMES.iter() { |
| 135 | + database.create_collection(collection_name).await?; |
| 136 | + } |
| 137 | + Ok(self.write_models.clone()) |
| 138 | + } |
| 139 | + |
| 140 | + async fn do_task(&self, write_models: Self::TaskState) -> Result<()> { |
| 141 | + self.client.bulk_write(write_models).await?; |
| 142 | + Ok(()) |
| 143 | + } |
| 144 | + |
| 145 | + async fn teardown(&self) -> Result<()> { |
| 146 | + drop_database(self.uri.as_str(), DATABASE_NAME.as_str()).await?; |
| 147 | + Ok(()) |
| 148 | + } |
| 149 | +} |
0 commit comments