|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | + * |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Threading; |
| 19 | +using BenchmarkDotNet.Attributes; |
| 20 | +using MongoDB.Bson; |
| 21 | +using MongoDB.Bson.TestHelpers; |
| 22 | +using MongoDB.Driver; |
| 23 | +using MongoDB.Driver.Core.Clusters; |
| 24 | +using MongoDB.Driver.Encryption; |
| 25 | +using MongoDB.Driver.TestHelpers; |
| 26 | +using MongoDB.Libmongocrypt; |
| 27 | + |
| 28 | +namespace MongoDB.Benchmarks |
| 29 | +{ |
| 30 | + public class LibmongocryptBindingBenchmark |
| 31 | + { |
| 32 | + private const int RepeatCount = 10; |
| 33 | + private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk"; |
| 34 | + |
| 35 | + private byte[] _encryptedValuesDocumentBytes; |
| 36 | + private DisposableMongoClient _disposableKeyVaultClient; |
| 37 | + private AutoEncryptionLibMongoCryptController _libMongoCryptController; |
| 38 | + private CryptClient _cryptClient; |
| 39 | + |
| 40 | + [Params(1, 2, 8, 64)] |
| 41 | + public int ThreadsCount { get; set; } |
| 42 | + |
| 43 | + [GlobalSetup] |
| 44 | + public void Setup() |
| 45 | + { |
| 46 | + var localMasterKey = Convert.FromBase64String(LocalMasterKey); |
| 47 | + |
| 48 | + var kmsProviders = new Dictionary<string, IReadOnlyDictionary<string, object>>(); |
| 49 | + var localKey = new Dictionary<string, object> { { "key", localMasterKey } }; |
| 50 | + kmsProviders.Add("local", localKey); |
| 51 | + |
| 52 | + var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault"); |
| 53 | + var autoEncryptionOptions = new AutoEncryptionOptions( |
| 54 | + keyVaultNamespace: keyVaultNamespace, |
| 55 | + kmsProviders: kmsProviders, |
| 56 | + bypassAutoEncryption: true); |
| 57 | + |
| 58 | + var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost"); |
| 59 | + clientSettings.AutoEncryptionOptions = autoEncryptionOptions; |
| 60 | + |
| 61 | + _disposableKeyVaultClient = new DisposableMongoClient(new MongoClient(clientSettings), null); |
| 62 | + |
| 63 | + var keyVaultDatabase = _disposableKeyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName); |
| 64 | + keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName); |
| 65 | + _disposableKeyVaultClient.DropDatabase("crypt-test"); |
| 66 | + |
| 67 | + var clientEncryptionSettings = new ClientEncryptionOptions( |
| 68 | + _disposableKeyVaultClient, |
| 69 | + keyVaultNamespace, |
| 70 | + kmsProviders); |
| 71 | + |
| 72 | + var encryptedValuesDocument = new BsonDocument(); |
| 73 | + using (var clientEncryption = new ClientEncryption(clientEncryptionSettings)) |
| 74 | + { |
| 75 | + var dataKeyId = clientEncryption.CreateDataKey( |
| 76 | + "local", |
| 77 | + new DataKeyOptions(), |
| 78 | + CancellationToken.None); |
| 79 | + |
| 80 | + var encryptOptions = new EncryptOptions( |
| 81 | + EncryptionAlgorithm.AEAD_AES_256_CBC_HMAC_SHA_512_Deterministic.ToString(), |
| 82 | + keyId: dataKeyId); |
| 83 | + |
| 84 | + for (int i = 0; i < 1500; i++) |
| 85 | + { |
| 86 | + var toEncryptString = $"value {(i + 1):D4}"; |
| 87 | + var encryptedString = |
| 88 | + clientEncryption.Encrypt(toEncryptString, encryptOptions, CancellationToken.None); |
| 89 | + encryptedValuesDocument.Add(new BsonElement($"key{(i + 1):D4}", encryptedString)); |
| 90 | + } |
| 91 | + } |
| 92 | + _encryptedValuesDocumentBytes = encryptedValuesDocument.ToBson(); |
| 93 | + |
| 94 | + // Create libmongocrypt binding that will be used for decryption |
| 95 | + _cryptClient = CryptClientCreator.CreateCryptClient(autoEncryptionOptions.ToCryptClientSettings()); |
| 96 | + _libMongoCryptController = AutoEncryptionLibMongoCryptController.Create(_disposableKeyVaultClient, _cryptClient, autoEncryptionOptions); |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark] |
| 100 | + public void BulkDecryptionUsingBinding() |
| 101 | + { |
| 102 | + ThreadingUtilities.ExecuteOnNewThreads(ThreadsCount, _ => |
| 103 | + { |
| 104 | + for (int i = 0; i < RepeatCount; i++) |
| 105 | + { |
| 106 | + _libMongoCryptController.DecryptFields(_encryptedValuesDocumentBytes, CancellationToken.None); |
| 107 | + } |
| 108 | + }, 20000); |
| 109 | + } |
| 110 | + |
| 111 | + [GlobalCleanup] |
| 112 | + public void Cleanup() |
| 113 | + { |
| 114 | + _cryptClient.Dispose(); |
| 115 | + _disposableKeyVaultClient.Dispose(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments