|
| 1 | +// <copyright file="ModernizationBenchmark.cs" company="MPCoreDeveloper"> |
| 2 | +// Copyright (c) 2024-2025 MPCoreDeveloper and GitHub Copilot. All rights reserved. |
| 3 | +// Licensed under the MIT License. See LICENSE file in the project root for full license information. |
| 4 | +// </copyright> |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using BenchmarkDotNet.Order; |
| 7 | +using SharpCoreDB.DataStructures; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace SharpCoreDB.Benchmarks; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Benchmarks comparing OLD (Dictionary-based) vs NEW (Generic type-safe) implementations. |
| 14 | +/// Measures the impact of our C# 14 modernization with generics. |
| 15 | +/// </summary> |
| 16 | +[MemoryDiagnoser] |
| 17 | +[Orderer(SummaryOrderPolicy.FastestToSlowest)] |
| 18 | +[RankColumn] |
| 19 | +public class ModernizationBenchmark |
| 20 | +{ |
| 21 | + private const int RecordCount = 10_000; |
| 22 | + private GenericHashIndex<int>? _genericIndex; |
| 23 | + private Dictionary<int, List<long>>? _oldStyleIndex; |
| 24 | + private Random? _random; |
| 25 | + |
| 26 | + [GlobalSetup] |
| 27 | + public void Setup() |
| 28 | + { |
| 29 | + Console.WriteLine("=== C# 14 Modernization Benchmark ==="); |
| 30 | + Console.WriteLine($"Testing with {RecordCount:N0} records"); |
| 31 | + Console.WriteLine(); |
| 32 | + |
| 33 | + _random = new Random(42); |
| 34 | + |
| 35 | + // Setup NEW generic index |
| 36 | + _genericIndex = new GenericHashIndex<int>("test_column"); |
| 37 | + for (int i = 0; i < RecordCount; i++) |
| 38 | + { |
| 39 | + _genericIndex.Add(i, i * 100); |
| 40 | + } |
| 41 | + |
| 42 | + // Setup OLD dictionary-based index |
| 43 | + _oldStyleIndex = new Dictionary<int, List<long>>(RecordCount); |
| 44 | + for (int i = 0; i < RecordCount; i++) |
| 45 | + { |
| 46 | + if (!_oldStyleIndex.TryGetValue(i, out var positions)) |
| 47 | + { |
| 48 | + positions = new List<long>(); |
| 49 | + _oldStyleIndex[i] = positions; |
| 50 | + } |
| 51 | + positions.Add(i * 100); |
| 52 | + } |
| 53 | + |
| 54 | + Console.WriteLine("? Setup completed - Ready to benchmark!"); |
| 55 | + } |
| 56 | + |
| 57 | + #region Lookup Benchmarks |
| 58 | + |
| 59 | + [Benchmark(Baseline = true, Description = "OLD: Dictionary Lookup")] |
| 60 | + public long OldDictionaryLookup() |
| 61 | + { |
| 62 | + long sum = 0; |
| 63 | + for (int i = 0; i < 1000; i++) |
| 64 | + { |
| 65 | + var key = _random!.Next(RecordCount); |
| 66 | + if (_oldStyleIndex!.TryGetValue(key, out var positions)) |
| 67 | + { |
| 68 | + sum += positions[0]; |
| 69 | + } |
| 70 | + } |
| 71 | + return sum; |
| 72 | + } |
| 73 | + |
| 74 | + [Benchmark(Description = "NEW: Generic Type-Safe Lookup")] |
| 75 | + public long NewGenericLookup() |
| 76 | + { |
| 77 | + long sum = 0; |
| 78 | + for (int i = 0; i < 1000; i++) |
| 79 | + { |
| 80 | + var key = _random!.Next(RecordCount); |
| 81 | + var positions = _genericIndex!.Find(key); |
| 82 | + sum += positions.FirstOrDefault(); |
| 83 | + } |
| 84 | + return sum; |
| 85 | + } |
| 86 | + |
| 87 | + #endregion |
| 88 | + |
| 89 | + #region Insert Benchmarks |
| 90 | + |
| 91 | + [Benchmark(Description = "OLD: Dictionary Insert (1000 records)")] |
| 92 | + public void OldDictionaryInsert() |
| 93 | + { |
| 94 | + var dict = new Dictionary<int, List<long>>(); |
| 95 | + for (int i = 0; i < 1000; i++) |
| 96 | + { |
| 97 | + if (!dict.TryGetValue(i, out var positions)) |
| 98 | + { |
| 99 | + positions = new List<long>(); // ALLOCATES! |
| 100 | + dict[i] = positions; |
| 101 | + } |
| 102 | + positions.Add(i); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + [Benchmark(Description = "NEW: Generic Insert (1000 records)")] |
| 107 | + public void NewGenericInsert() |
| 108 | + { |
| 109 | + var index = new GenericHashIndex<int>("test"); |
| 110 | + for (int i = 0; i < 1000; i++) |
| 111 | + { |
| 112 | + index.Add(i, i); // Optimized with pre-sized Dictionary |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + #endregion |
| 117 | + |
| 118 | + #region Memory Efficiency |
| 119 | + |
| 120 | + [Benchmark(Description = "OLD: Memory Usage (10k records)")] |
| 121 | + public Dictionary<int, List<long>> OldMemoryUsage() |
| 122 | + { |
| 123 | + var dict = new Dictionary<int, List<long>>(); |
| 124 | + for (int i = 0; i < RecordCount; i++) |
| 125 | + { |
| 126 | + var list = new List<long> { i }; |
| 127 | + dict[i] = list; |
| 128 | + } |
| 129 | + return dict; |
| 130 | + } |
| 131 | + |
| 132 | + [Benchmark(Description = "NEW: Memory Usage (10k records)")] |
| 133 | + public GenericHashIndex<int> NewMemoryUsage() |
| 134 | + { |
| 135 | + var index = new GenericHashIndex<int>("test"); |
| 136 | + for (int i = 0; i < RecordCount; i++) |
| 137 | + { |
| 138 | + index.Add(i, i); |
| 139 | + } |
| 140 | + return index; |
| 141 | + } |
| 142 | + |
| 143 | + #endregion |
| 144 | + |
| 145 | + #region Statistics Overhead |
| 146 | + |
| 147 | + [Benchmark(Description = "NEW: Get Index Statistics")] |
| 148 | + public void GetIndexStatistics() |
| 149 | + { |
| 150 | + var stats = _genericIndex!.GetStatistics(); |
| 151 | + // Access properties to ensure not optimized away |
| 152 | + _ = stats.UniqueKeys; |
| 153 | + _ = stats.Selectivity; |
| 154 | + _ = stats.MemoryUsageBytes; |
| 155 | + } |
| 156 | + |
| 157 | + #endregion |
| 158 | + |
| 159 | + [GlobalCleanup] |
| 160 | + public void Cleanup() |
| 161 | + { |
| 162 | + Console.WriteLine(); |
| 163 | + Console.WriteLine("=== Benchmark Complete ==="); |
| 164 | + |
| 165 | + if (_genericIndex != null) |
| 166 | + { |
| 167 | + var stats = _genericIndex.GetStatistics(); |
| 168 | + Console.WriteLine($"Final Index Statistics:"); |
| 169 | + Console.WriteLine($" Unique Keys: {stats.UniqueKeys:N0}"); |
| 170 | + Console.WriteLine($" Total Entries: {stats.TotalEntries:N0}"); |
| 171 | + Console.WriteLine($" Memory Usage: {stats.MemoryUsageBytes / 1024.0:F2} KB"); |
| 172 | + Console.WriteLine($" Selectivity: {stats.Selectivity:F4}"); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments