|
| 1 | +using BenchmarkDotNet.Reports; |
| 2 | +using BenchmarkDotNet.Running; |
| 3 | + |
| 4 | +namespace TurboHttp.Benchmarks.Internal; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Extracts <see cref="BenchmarkResult"/> instances directly from a BenchmarkDotNet |
| 8 | +/// <see cref="Summary"/>, bypassing the CSV artifact round-trip. |
| 9 | +/// </summary> |
| 10 | +public static class SummaryExtractor |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Converts all completed reports in <paramref name="summary"/> to |
| 14 | + /// <see cref="BenchmarkResult"/> instances. Reports with missing statistics |
| 15 | + /// (cancelled or errored runs) are silently skipped. |
| 16 | + /// </summary> |
| 17 | + public static IReadOnlyList<BenchmarkResult> Extract(Summary summary) |
| 18 | + { |
| 19 | + var results = new List<BenchmarkResult>(summary.Reports.Length); |
| 20 | + |
| 21 | + foreach (var report in summary.Reports) |
| 22 | + { |
| 23 | + var statistics = report.ResultStatistics; |
| 24 | + if (statistics is null) |
| 25 | + { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + var name = BuildName(report.BenchmarkCase); |
| 30 | + var allocBytes = report.GcStats.GetBytesAllocatedPerOperation(report.BenchmarkCase) ?? 0L; |
| 31 | + |
| 32 | + results.Add(new BenchmarkResult( |
| 33 | + name, |
| 34 | + statistics.Mean, |
| 35 | + statistics.Percentiles.P50, |
| 36 | + statistics.Percentiles.P95, |
| 37 | + statistics.Percentiles.Percentile(99), |
| 38 | + allocBytes)); |
| 39 | + } |
| 40 | + |
| 41 | + return results; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Builds a scenario name matching the format used by the CSV-based pipeline: |
| 46 | + /// <c>{Method} / CL={ConcurrencyLevel} / {PayloadType} / HTTP {HttpVersion}</c>. |
| 47 | + /// </summary> |
| 48 | + private static string BuildName(BenchmarkCase benchmarkCase) |
| 49 | + { |
| 50 | + var method = benchmarkCase.Descriptor.WorkloadMethod.Name; |
| 51 | + var concurrency = GetParam(benchmarkCase, "ConcurrencyLevel") ?? "1"; |
| 52 | + var payload = GetParam(benchmarkCase, "PayloadType") ?? "light"; |
| 53 | + var version = GetParam(benchmarkCase, "HttpVersion") ?? "1.1"; |
| 54 | + |
| 55 | + return $"{method} / CL={concurrency} / {payload} / HTTP {version}"; |
| 56 | + } |
| 57 | + |
| 58 | + private static string? GetParam(BenchmarkCase benchmarkCase, string paramName) |
| 59 | + => benchmarkCase.Parameters.Items |
| 60 | + .FirstOrDefault(p => p.Name == paramName) |
| 61 | + ?.Value?.ToString(); |
| 62 | +} |
| 63 | + |
| 64 | +/// <summary> |
| 65 | +/// Extension helpers for <see cref="Summary"/>. |
| 66 | +/// </summary> |
| 67 | +public static class SummaryExtensions |
| 68 | +{ |
| 69 | + /// <summary> |
| 70 | + /// Returns <see langword="true"/> when <paramref name="summary"/> contains at least |
| 71 | + /// one benchmark case belonging to <typeparamref name="T"/>. |
| 72 | + /// </summary> |
| 73 | + public static bool HasBenchmarksOf<T>(this Summary summary) |
| 74 | + => summary.BenchmarksCases.Any(c => c.Descriptor.Type == typeof(T)); |
| 75 | +} |
0 commit comments