|
| 1 | +using System.Text; |
| 2 | + |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | + |
| 5 | +using ColumnizerLib; |
| 6 | + |
| 7 | +using CsvColumnizer; |
| 8 | + |
| 9 | +using Moq; |
| 10 | + |
| 11 | +namespace LogExpert.Benchmarks; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Benchmarks for CsvColumnizer covering PreProcessLine, Selected, and SplitLine operations |
| 15 | +/// across varying line counts and column widths. |
| 16 | +/// </summary> |
| 17 | +[MemoryDiagnoser] |
| 18 | +[RankColumn] |
| 19 | +public class CsvColumnizerBenchmarks |
| 20 | +{ |
| 21 | + private ILogLineMemory[] _dataLines = null!; |
| 22 | + private CsvColumnizer.CsvColumnizer _columnizer = null!; |
| 23 | + |
| 24 | + [Params(100, 1_000, 10_000)] |
| 25 | + public int LineCount { get; set; } |
| 26 | + |
| 27 | + [Params(5, 15)] |
| 28 | + public int ColumnCount { get; set; } |
| 29 | + |
| 30 | + [GlobalSetup] |
| 31 | + public void Setup () |
| 32 | + { |
| 33 | + // Build header and data lines |
| 34 | + var headerParts = new string[ColumnCount]; |
| 35 | + for (var i = 0; i < ColumnCount; i++) |
| 36 | + { |
| 37 | + headerParts[i] = $"Column{i}"; |
| 38 | + } |
| 39 | + |
| 40 | + var header = string.Join(";", headerParts); |
| 41 | + |
| 42 | + // Initialize columnizer with header |
| 43 | + _columnizer = new CsvColumnizer.CsvColumnizer(); |
| 44 | + _columnizer.PreProcessLine(header.AsMemory(), 0, 0); |
| 45 | + |
| 46 | + var mockCallback = new Mock<ILogLineMemoryColumnizerCallback>(); |
| 47 | + _columnizer.Selected(mockCallback.Object); |
| 48 | + |
| 49 | + // Generate data lines |
| 50 | + _dataLines = new ILogLineMemory[LineCount]; |
| 51 | + var random = new Random(42); |
| 52 | + |
| 53 | + for (var i = 0; i < LineCount; i++) |
| 54 | + { |
| 55 | + var parts = new string[ColumnCount]; |
| 56 | + for (var j = 0; j < ColumnCount; j++) |
| 57 | + { |
| 58 | + parts[j] = GenerateFieldValue(random, j); |
| 59 | + } |
| 60 | + |
| 61 | + _dataLines[i] = new CsvLogLine(string.Join(";", parts), i + 1); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + [Benchmark(Description = "SplitLine: parse all lines")] |
| 66 | + public int SplitAllLines () |
| 67 | + { |
| 68 | + var totalColumns = 0; |
| 69 | + for (var i = 0; i < _dataLines.Length; i++) |
| 70 | + { |
| 71 | + var result = _columnizer.SplitLine(null, _dataLines[i]); |
| 72 | + totalColumns += result.ColumnValues.Length; |
| 73 | + } |
| 74 | + |
| 75 | + return totalColumns; |
| 76 | + } |
| 77 | + |
| 78 | + [Benchmark(Description = "PreProcessLine: preprocess all lines")] |
| 79 | + public int PreProcessAllLines () |
| 80 | + { |
| 81 | + var processed = 0; |
| 82 | + for (var i = 0; i < _dataLines.Length; i++) |
| 83 | + { |
| 84 | + var result = _columnizer.PreProcessLine(_dataLines[i].FullLine, i + 1, i + 1); |
| 85 | + if (!result.IsEmpty) |
| 86 | + { |
| 87 | + processed++; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return processed; |
| 92 | + } |
| 93 | + |
| 94 | + [Benchmark(Description = "Selected: re-detect columns from header")] |
| 95 | + public int RedetectColumns () |
| 96 | + { |
| 97 | + var mockCallback = new Mock<ILogLineMemoryColumnizerCallback>(); |
| 98 | + _columnizer.Selected(mockCallback.Object); |
| 99 | + return _columnizer.GetColumnCount(); |
| 100 | + } |
| 101 | + |
| 102 | + private static string GenerateFieldValue (Random random, int columnIndex) |
| 103 | + { |
| 104 | + // Mix of value types: numbers, short text, quoted text with commas |
| 105 | + return (columnIndex % 4) switch |
| 106 | + { |
| 107 | + 0 => random.Next(1, 100000).ToString(), |
| 108 | + 1 => $"text_{random.Next(1, 9999)}", |
| 109 | + 2 => $"\"Value, with quotes {random.Next(1, 999)}\"", |
| 110 | + _ => new string((char)('A' + random.Next(0, 26)), random.Next(5, 20)), |
| 111 | + }; |
| 112 | + } |
| 113 | +} |
0 commit comments