|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.Json; |
| 3 | +using BenchmarkDotNet.Attributes; |
| 4 | +using BenchmarkDotNet.Running; |
| 5 | +using Devlooped; |
| 6 | + |
| 7 | +if (Debugger.IsAttached) |
| 8 | +{ |
| 9 | + await RunBenchmarksUnderDebuggerAsync(); |
| 10 | + return; |
| 11 | +} |
| 12 | + |
| 13 | +BenchmarkRunner.Run<WhatsAppMessageBenchmarks>(); |
| 14 | + |
| 15 | +static async Task RunBenchmarksUnderDebuggerAsync() |
| 16 | +{ |
| 17 | + var benchmarks = new WhatsAppMessageBenchmarks(); |
| 18 | + |
| 19 | + benchmarks.Setup(); |
| 20 | + |
| 21 | + Console.WriteLine($"{nameof(WhatsAppMessageBenchmarks.DevloopedJQ)}: {await benchmarks.DevloopedJQ()}"); |
| 22 | + Console.WriteLine($"{nameof(WhatsAppMessageBenchmarks.JQSharpWithoutExpressionCaching)}: {benchmarks.JQSharpWithoutExpressionCaching()}"); |
| 23 | + Console.WriteLine($"{nameof(WhatsAppMessageBenchmarks.JQSharpWithExpressionCaching)}: {benchmarks.JQSharpWithExpressionCaching()}"); |
| 24 | +} |
| 25 | + |
| 26 | +[MemoryDiagnoser] |
| 27 | +public class WhatsAppMessageBenchmarks |
| 28 | +{ |
| 29 | + byte[] messageJsonUtf8 = []; |
| 30 | + string messageJson = string.Empty; |
| 31 | + string query = string.Empty; |
| 32 | + JqExpression cachedExpression = default!; |
| 33 | + int expectedResultsPerMessage; |
| 34 | + |
| 35 | + [GlobalSetup] |
| 36 | + public void Setup() |
| 37 | + { |
| 38 | + var basePath = AppContext.BaseDirectory; |
| 39 | + messageJsonUtf8 = File.ReadAllBytes(Path.Combine(basePath, "WhatsApp", "Text.json")); |
| 40 | + messageJson = File.ReadAllText(Path.Combine(basePath, "WhatsApp", "Text.json")); |
| 41 | + query = File.ReadAllText(Path.Combine(basePath, "WhatsApp", "Message.jq")); |
| 42 | + cachedExpression = Jq.Parse(query); |
| 43 | + |
| 44 | + using (var document = JsonDocument.Parse(messageJsonUtf8)) |
| 45 | + expectedResultsPerMessage = ConsumeResults(cachedExpression.Evaluate(document.RootElement)); |
| 46 | + |
| 47 | + if (expectedResultsPerMessage <= 0) |
| 48 | + throw new InvalidOperationException("The benchmark query did not produce any results for the sample WhatsApp payload."); |
| 49 | + |
| 50 | + var wrapperSanity = JQ.ExecuteAsync(new JqParams(query) |
| 51 | + { |
| 52 | + Json = messageJson, |
| 53 | + RawOutput = false, |
| 54 | + CompactOutput = true, |
| 55 | + MonochromeOutput = true, |
| 56 | + }).GetAwaiter().GetResult(); |
| 57 | + |
| 58 | + if (wrapperSanity.ExitCode != 0) |
| 59 | + throw new InvalidOperationException($"Devlooped.JQ failed during setup: {wrapperSanity.StandardError}"); |
| 60 | + |
| 61 | + var wrapperResults = CountLines(wrapperSanity.StandardOutput); |
| 62 | + if (wrapperResults != expectedResultsPerMessage) |
| 63 | + throw new InvalidOperationException($"Expected {expectedResultsPerMessage} wrapper results but got {wrapperResults}."); |
| 64 | + } |
| 65 | + |
| 66 | + [Benchmark(Baseline = true)] |
| 67 | + public async Task<int> DevloopedJQ() |
| 68 | + { |
| 69 | + var result = await JQ.ExecuteAsync(new JqParams(query) |
| 70 | + { |
| 71 | + Json = messageJson, |
| 72 | + RawOutput = false, |
| 73 | + CompactOutput = true, |
| 74 | + MonochromeOutput = true, |
| 75 | + }); |
| 76 | + |
| 77 | + if (result.ExitCode != 0) |
| 78 | + throw new InvalidOperationException(result.StandardError); |
| 79 | + |
| 80 | + var outputCount = CountLines(result.StandardOutput); |
| 81 | + if (outputCount != expectedResultsPerMessage) |
| 82 | + throw new InvalidOperationException($"Expected {expectedResultsPerMessage} jq.exe results but got {outputCount}."); |
| 83 | + |
| 84 | + return outputCount; |
| 85 | + } |
| 86 | + |
| 87 | + [Benchmark] |
| 88 | + public int JQSharpWithoutExpressionCaching() |
| 89 | + { |
| 90 | + using var document = JsonDocument.Parse(messageJsonUtf8); |
| 91 | + var totalResults = ConsumeResults(Jq.Evaluate(query, document.RootElement)); |
| 92 | + |
| 93 | + if (totalResults != expectedResultsPerMessage) |
| 94 | + throw new InvalidOperationException($"Expected {expectedResultsPerMessage} uncached jqsharp results but got {totalResults}."); |
| 95 | + |
| 96 | + return totalResults; |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark] |
| 100 | + public int JQSharpWithExpressionCaching() |
| 101 | + { |
| 102 | + using var document = JsonDocument.Parse(messageJsonUtf8); |
| 103 | + var totalResults = ConsumeResults(cachedExpression.Evaluate(document.RootElement)); |
| 104 | + |
| 105 | + if (totalResults != expectedResultsPerMessage) |
| 106 | + throw new InvalidOperationException($"Expected {expectedResultsPerMessage} cached jqsharp results but got {totalResults}."); |
| 107 | + |
| 108 | + return totalResults; |
| 109 | + } |
| 110 | + |
| 111 | + static int ConsumeResults(IEnumerable<JsonElement> results) |
| 112 | + { |
| 113 | + var count = 0; |
| 114 | + |
| 115 | + foreach (var result in results) |
| 116 | + { |
| 117 | + _ = result.ValueKind; |
| 118 | + count++; |
| 119 | + } |
| 120 | + |
| 121 | + return count; |
| 122 | + } |
| 123 | + |
| 124 | + static int CountLines(string output) |
| 125 | + { |
| 126 | + if (string.IsNullOrWhiteSpace(output)) |
| 127 | + return 0; |
| 128 | + |
| 129 | + var count = 1; |
| 130 | + |
| 131 | + foreach (var ch in output) |
| 132 | + { |
| 133 | + if (ch == '\n') |
| 134 | + count++; |
| 135 | + } |
| 136 | + |
| 137 | + return count; |
| 138 | + } |
| 139 | +} |
0 commit comments