|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Runtime; |
| 6 | +using System.Text.Json; |
| 7 | +using System.Security.Cryptography; |
| 8 | +using Microsoft.Extensions.Logging; |
| 9 | +using AWS.Cryptography.EncryptionSDK; |
| 10 | +using AWS.Cryptography.MaterialProviders; |
| 11 | +using YamlDotNet.Serialization; |
| 12 | +using YamlDotNet.Serialization.NamingConventions; |
| 13 | +using ShellProgressBar; |
| 14 | +using Newtonsoft.Json; |
| 15 | + |
| 16 | +namespace EsdkBenchmark; |
| 17 | + |
| 18 | +public partial class ESDKBenchmark |
| 19 | +{ |
| 20 | + private readonly ILogger _logger; |
| 21 | + private readonly TestConfig _config; |
| 22 | + private readonly int _cpuCount; |
| 23 | + private readonly double _totalMemoryGb; |
| 24 | + private static readonly Random _random = new(); |
| 25 | + private readonly List<BenchmarkResult> _results = new(); |
| 26 | + |
| 27 | + // Public properties to match Go structure |
| 28 | + public TestConfig Config => _config; |
| 29 | + public List<BenchmarkResult> Results => _results; |
| 30 | + |
| 31 | + // ESDK components |
| 32 | + private MaterialProviders _materialProviders = null!; |
| 33 | + private ESDK _encryptionSdk = null!; |
| 34 | + private IKeyring _keyring = null!; |
| 35 | + |
| 36 | + // Constants for memory testing |
| 37 | + private const int MemoryTestIterations = 5; |
| 38 | + private const int SamplingIntervalMs = 1; |
| 39 | + private const int GcSettleTimeMs = 5; |
| 40 | + private const int FinalSampleWaitMs = 2; |
| 41 | + |
| 42 | + public ESDKBenchmark(string configPath, ILogger logger) |
| 43 | + { |
| 44 | + _logger = logger; |
| 45 | + _config = ConfigLoader.LoadConfig(configPath); |
| 46 | + |
| 47 | + // Get system information |
| 48 | + _cpuCount = Environment.ProcessorCount; |
| 49 | + _totalMemoryGb = GetTotalMemoryGb(); |
| 50 | + |
| 51 | + // Configure GC for performance |
| 52 | + GCSettings.LatencyMode = GCLatencyMode.SustainedLowLatency; |
| 53 | + |
| 54 | + // Setup ESDK |
| 55 | + var setupError = SetupEsdk(); |
| 56 | + if (setupError != null) |
| 57 | + { |
| 58 | + throw new InvalidOperationException($"Failed to setup ESDK: {setupError}"); |
| 59 | + } |
| 60 | + |
| 61 | + _logger.LogInformation("Initialized ESDK Benchmark - CPU cores: {CpuCount}, Memory: {Memory:F1}GB", |
| 62 | + _cpuCount, _totalMemoryGb); |
| 63 | + } |
| 64 | + |
| 65 | + private string? SetupEsdk() |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + _materialProviders = new MaterialProviders(new MaterialProvidersConfig()); |
| 70 | + |
| 71 | + // Create 256-bit AES key using .NET crypto |
| 72 | + var key = new byte[32]; |
| 73 | + using (var rng = RandomNumberGenerator.Create()) |
| 74 | + { |
| 75 | + rng.GetBytes(key); |
| 76 | + } |
| 77 | + |
| 78 | + // Create raw AES keyring |
| 79 | + var createKeyringInput = new CreateRawAesKeyringInput |
| 80 | + { |
| 81 | + KeyNamespace = "esdk-performance-test", |
| 82 | + KeyName = "test-aes-256-key", |
| 83 | + WrappingKey = new MemoryStream(key), |
| 84 | + WrappingAlg = AesWrappingAlg.ALG_AES256_GCM_IV12_TAG16 |
| 85 | + }; |
| 86 | + _keyring = _materialProviders.CreateRawAesKeyring(createKeyringInput); |
| 87 | + |
| 88 | + // Create ESDK client with commitment policy |
| 89 | + var esdkConfig = new AwsEncryptionSdkConfig |
| 90 | + { |
| 91 | + CommitmentPolicy = ESDKCommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT |
| 92 | + }; |
| 93 | + _encryptionSdk = new ESDK(esdkConfig); |
| 94 | + |
| 95 | + return null; |
| 96 | + } |
| 97 | + catch (Exception ex) |
| 98 | + { |
| 99 | + return ex.Message; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static double GetTotalMemoryGb() |
| 104 | + { |
| 105 | + try |
| 106 | + { |
| 107 | + var gcMemoryInfo = GC.GetGCMemoryInfo(); |
| 108 | + // Convert from bytes to GB |
| 109 | + return gcMemoryInfo.TotalAvailableMemoryBytes / (1024.0 * 1024.0 * 1024.0); |
| 110 | + } |
| 111 | + catch |
| 112 | + { |
| 113 | + // Fallback - estimate based on process memory |
| 114 | + return Environment.WorkingSet / (1024.0 * 1024.0 * 1024.0) * 4; // Rough estimate |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + private byte[] GenerateTestData(int size) |
| 119 | + { |
| 120 | + var data = new byte[size]; |
| 121 | + _random.NextBytes(data); |
| 122 | + return data; |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | + public void RunAllBenchmarks() |
| 128 | + { |
| 129 | + _results.Clear(); |
| 130 | + Console.WriteLine("Starting comprehensive ESDK benchmark suite"); |
| 131 | + |
| 132 | + // Combine all data sizes |
| 133 | + var dataSizes = _config.DataSizes.Small |
| 134 | + .Concat(_config.DataSizes.Medium) |
| 135 | + .Concat(_config.DataSizes.Large); |
| 136 | + |
| 137 | + // Run test suites |
| 138 | + if (ConfigLoader.ShouldRunTestType(_config, "throughput")) |
| 139 | + { |
| 140 | + RunThroughputTests(dataSizes, _config.Iterations.Measurement); |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + Console.WriteLine("Skipping throughput tests (not in test_types)"); |
| 145 | + } |
| 146 | + |
| 147 | + if (ConfigLoader.ShouldRunTestType(_config, "memory")) |
| 148 | + { |
| 149 | + RunMemoryTests(dataSizes); |
| 150 | + } |
| 151 | + else |
| 152 | + { |
| 153 | + Console.WriteLine("Skipping memory tests (not in test_types)"); |
| 154 | + } |
| 155 | + |
| 156 | + if (ConfigLoader.ShouldRunTestType(_config, "concurrency")) |
| 157 | + { |
| 158 | + RunConcurrencyTests(dataSizes, _config.ConcurrencyLevels); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + Console.WriteLine("Skipping concurrency tests (not in test_types)"); |
| 163 | + } |
| 164 | + |
| 165 | + Console.WriteLine($"Benchmark suite completed. Total results: {_results.Count}"); |
| 166 | + } |
| 167 | +} |
0 commit comments