|
| 1 | +using System.Buffers.Binary; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using System.Security.Cryptography; |
| 4 | +using System.Text.Json; |
| 5 | + |
| 6 | +namespace KeyLoad.BenchmarkScenarios.Features.BenchmarkComparisons; |
| 7 | + |
| 8 | +internal static class SampleChunkBenchmarkManifest |
| 9 | +{ |
| 10 | + internal const string DirectoryEnvironment = "KEYLOAD_CHUNK_BENCHMARK_MANIFEST_DIRECTORY"; |
| 11 | + internal const string SourceHeadEnvironment = "KEYLOAD_CHUNK_SOURCE_HEAD"; |
| 12 | + internal const string SourceInventoryEnvironment = "KEYLOAD_CHUNK_SOURCE_INVENTORY_SHA256"; |
| 13 | + private const string InvalidSourceMessage = "The sample chunk measurement source identity is missing or malformed."; |
| 14 | + |
| 15 | + internal static void Write(int records, string corpus, SampleChunkBenchmarkState state) |
| 16 | + { |
| 17 | + var directory = Environment.GetEnvironmentVariable(DirectoryEnvironment); |
| 18 | + if (string.IsNullOrWhiteSpace(directory)) |
| 19 | + { |
| 20 | + return; |
| 21 | + } |
| 22 | + var head = RequireHex(SourceHeadEnvironment, 40); |
| 23 | + var sourceInventory = RequireHex(SourceInventoryEnvironment, 64); |
| 24 | + var output = Describe(records, corpus, state, head, sourceInventory); |
| 25 | + var bytes = JsonSerializer.SerializeToUtf8Bytes(output, JsonDefaults.Options); |
| 26 | + Directory.CreateDirectory(directory); |
| 27 | + var path = Path.Combine(directory, records.ToString(System.Globalization.CultureInfo.InvariantCulture) + "-" + corpus + ".json"); |
| 28 | + if (File.Exists(path)) |
| 29 | + { |
| 30 | + if (!File.ReadAllBytes(path).AsSpan().SequenceEqual(bytes)) |
| 31 | + { |
| 32 | + throw new InvalidOperationException("The sample chunk corpus manifest already belongs to a different source or corpus."); |
| 33 | + } |
| 34 | + return; |
| 35 | + } |
| 36 | + using var stream = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None); |
| 37 | + stream.Write(bytes); |
| 38 | + } |
| 39 | + |
| 40 | + private static object Describe(int records, string corpus, SampleChunkBenchmarkState state, |
| 41 | + string head, string sourceInventory) |
| 42 | + => new |
| 43 | + { |
| 44 | + schemaVersion = 1, |
| 45 | + scope = "development_codec_microbenchmark_control_only", |
| 46 | + fixture = nameof(SampleChunkSerializationBenchmarks), |
| 47 | + sourceHead = head, |
| 48 | + sourceInventorySha256 = sourceInventory, |
| 49 | + corpus, |
| 50 | + seed = SampleChunkBenchmarkCorpus.Seed, |
| 51 | + corpusGenerator = "SHA256_seed_sample_lane_Int32LE", |
| 52 | + actualRecordCount = records, |
| 53 | + nativeValueBytes = state.NativeBytes, |
| 54 | + chunkValueBytes = state.ChunkBytes, |
| 55 | + nativeValueBytesPerSample = (double)state.NativeBytes / state.RecordCount, |
| 56 | + chunkValueBytesPerSample = (double)state.ChunkBytes / state.RecordCount, |
| 57 | + orderedNativeValuesSha256 = HashNative(state.NativeValues), |
| 58 | + chunkValueSha256 = Convert.ToHexStringLower(SHA256.HashData(state.ChunkValue)), |
| 59 | + correctness = "every_field_original_offset_and_IEEE_bits_verified_before_timing", |
| 60 | + accounting = "complete_native_value_envelopes_excluding_keys_WAL_replication_indexes_and_storage_overhead", |
| 61 | + machineName = Environment.MachineName, |
| 62 | + processorCount = Environment.ProcessorCount, |
| 63 | + architecture = RuntimeInformation.ProcessArchitecture.ToString(), |
| 64 | + operatingSystem = RuntimeInformation.OSDescription, |
| 65 | + runtime = RuntimeInformation.FrameworkDescription, |
| 66 | + databaseScaleEvidence = false, |
| 67 | + githubQualified = false, |
| 68 | + canonicalChunkStorage = false, |
| 69 | + rewriteCostQualified = false, |
| 70 | + correctionRecoveryQualified = false |
| 71 | + }; |
| 72 | + |
| 73 | + private static string HashNative(IReadOnlyList<byte[]> values) |
| 74 | + { |
| 75 | + using var hash = IncrementalHash.CreateHash(HashAlgorithmName.SHA256); |
| 76 | + Span<byte> length = stackalloc byte[sizeof(int)]; |
| 77 | + foreach (var value in values) |
| 78 | + { |
| 79 | + BinaryPrimitives.WriteInt32LittleEndian(length, value.Length); |
| 80 | + hash.AppendData(length); |
| 81 | + hash.AppendData(value); |
| 82 | + } |
| 83 | + return Convert.ToHexStringLower(hash.GetHashAndReset()); |
| 84 | + } |
| 85 | + |
| 86 | + private static string RequireHex(string environment, int length) |
| 87 | + { |
| 88 | + var value = Environment.GetEnvironmentVariable(environment); |
| 89 | + if (value is null || value.Length != length || value.Any(character => !char.IsAsciiHexDigit(character))) |
| 90 | + { |
| 91 | + throw new InvalidOperationException(InvalidSourceMessage); |
| 92 | + } |
| 93 | + return value; |
| 94 | + } |
| 95 | +} |
0 commit comments