|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | + |
| 7 | +namespace LightningDB.Benchmarks; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Focused benchmarks for integer comparers testing their optimized 4-byte and 8-byte paths. |
| 11 | +/// SignedIntegerComparer and UnsignedIntegerComparer have optimized fast paths for |
| 12 | +/// int/uint (4 bytes) and long/ulong (8 bytes) that use direct memory reads. |
| 13 | +/// </summary> |
| 14 | +[MemoryDiagnoser] |
| 15 | +public class IntegerComparerBenchmarks |
| 16 | +{ |
| 17 | + private string _path; |
| 18 | + private LightningEnvironment _env; |
| 19 | + private LightningDatabase _db; |
| 20 | + private byte[][] _keys; |
| 21 | + private byte[] _valueBuffer; |
| 22 | + |
| 23 | + [ParamsSource(nameof(IntegerComparers))] |
| 24 | + public ComparerDescriptor Comparer { get; set; } |
| 25 | + |
| 26 | + public static IEnumerable<ComparerDescriptor> IntegerComparers |
| 27 | + => ComparerDescriptor.IntegerComparers; |
| 28 | + |
| 29 | + [Params(4, 8)] |
| 30 | + public int KeySize { get; set; } |
| 31 | + |
| 32 | + [Params(1000, 10000)] |
| 33 | + public int OpsPerTransaction { get; set; } |
| 34 | + |
| 35 | + [GlobalSetup] |
| 36 | + public void GlobalSetup() |
| 37 | + { |
| 38 | + Console.WriteLine($"Global Setup Begin - Comparer: {Comparer.Name}, KeySize: {KeySize}"); |
| 39 | + |
| 40 | + _path = $"IntBenchDir_{Guid.NewGuid():N}"; |
| 41 | + if (Directory.Exists(_path)) |
| 42 | + Directory.Delete(_path, true); |
| 43 | + |
| 44 | + _env = new LightningEnvironment(_path) { MaxDatabases = 1 }; |
| 45 | + _env.Open(); |
| 46 | + |
| 47 | + var config = new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }; |
| 48 | + if (Comparer.Comparer != null) |
| 49 | + config.CompareWith(Comparer.Comparer); |
| 50 | + |
| 51 | + using (var tx = _env.BeginTransaction()) { |
| 52 | + _db = tx.OpenDatabase(configuration: config); |
| 53 | + tx.Commit(); |
| 54 | + } |
| 55 | + |
| 56 | + _valueBuffer = new byte[64]; |
| 57 | + _keys = GenerateIntegerKeys(OpsPerTransaction, KeySize); |
| 58 | + |
| 59 | + Console.WriteLine("Global Setup End"); |
| 60 | + } |
| 61 | + |
| 62 | + private static byte[][] GenerateIntegerKeys(int count, int keySize) |
| 63 | + { |
| 64 | + var keys = new byte[count][]; |
| 65 | + |
| 66 | + for (var i = 0; i < count; i++) { |
| 67 | + keys[i] = new byte[keySize]; |
| 68 | + if (keySize == 4) |
| 69 | + MemoryMarshal.Write(keys[i], in i); |
| 70 | + else // keySize == 8 |
| 71 | + MemoryMarshal.Write(keys[i], (long)i); |
| 72 | + } |
| 73 | + |
| 74 | + return keys; |
| 75 | + } |
| 76 | + |
| 77 | + [Benchmark] |
| 78 | + public void WriteIntegers() |
| 79 | + { |
| 80 | + using var tx = _env.BeginTransaction(); |
| 81 | + |
| 82 | + for (var i = 0; i < OpsPerTransaction; i++) { |
| 83 | + tx.Put(_db, _keys[i], _valueBuffer); |
| 84 | + } |
| 85 | + |
| 86 | + tx.Commit(); |
| 87 | + } |
| 88 | + |
| 89 | + [GlobalCleanup] |
| 90 | + public void GlobalCleanup() |
| 91 | + { |
| 92 | + Console.WriteLine("Global Cleanup Begin"); |
| 93 | + |
| 94 | + try { |
| 95 | + _db?.Dispose(); |
| 96 | + _env?.Dispose(); |
| 97 | + |
| 98 | + if (Directory.Exists(_path)) |
| 99 | + Directory.Delete(_path, true); |
| 100 | + } |
| 101 | + catch (Exception ex) { |
| 102 | + Console.WriteLine(ex.ToString()); |
| 103 | + } |
| 104 | + |
| 105 | + Console.WriteLine("Global Cleanup End"); |
| 106 | + } |
| 107 | +} |
0 commit comments