|
| 1 | +// Copyright (c) 2019-2026 ReactiveUI and Contributors. All rights reserved. |
| 2 | +// ReactiveUI and Contributors licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for full license information. |
| 4 | + |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using BenchmarkDotNet.Attributes; |
| 7 | +using BenchmarkDotNet.Configs; |
| 8 | +using BenchmarkDotNet.Diagnosers; |
| 9 | + |
| 10 | +namespace Refit.Benchmarks; |
| 11 | + |
| 12 | +/// <summary>Compares the chained concatenation and interpolated-string forms emitted for nested query-object keys.</summary> |
| 13 | +/// <remarks>The inputs mirror generated code: the parent key is known only at runtime, while the delimiter, property |
| 14 | +/// prefix, alias, and CLR property name are compile-time literals.</remarks> |
| 15 | +[MemoryDiagnoser] |
| 16 | +[EventPipeProfiler(EventPipeProfile.GcVerbose)] |
| 17 | +[ShortRunJob] |
| 18 | +[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)] |
| 19 | +[CategoriesColumn] |
| 20 | +public class NestedQueryKeyCompositionBenchmarks |
| 21 | +{ |
| 22 | + /// <summary>The compile-time delimiter and property prefix emitted before a nested property name.</summary> |
| 23 | + private const string NestedPrefix = ".billing-"; |
| 24 | + |
| 25 | + /// <summary>The compile-time CLR property name passed to the key formatter.</summary> |
| 26 | + private const string PropertyName = "PostalCode"; |
| 27 | + |
| 28 | + /// <summary>The runtime key of the enclosing query object.</summary> |
| 29 | + private readonly string _parentKey = "customer"; |
| 30 | + |
| 31 | + /// <summary>Settings used by the key-formatter path.</summary> |
| 32 | + private readonly RefitSettings _settings = new(); |
| 33 | + |
| 34 | + /// <summary>Builds an aliased nested key using the current generated concatenation.</summary> |
| 35 | + /// <returns>The composed query key.</returns> |
| 36 | + [Benchmark(Baseline = true)] |
| 37 | + [BenchmarkCategory("Alias")] |
| 38 | + [SuppressMessage("Roslynator", "RCS1190", Justification = "The benchmark intentionally measures the generated concatenation shape.")] |
| 39 | + [SuppressMessage("Style", "SST2249", Justification = "The benchmark intentionally measures the generated concatenation shape.")] |
| 40 | + public string ConcatenationAlias() => _parentKey + NestedPrefix + "postal_code"; |
| 41 | + |
| 42 | + /// <summary>Builds an aliased nested key using the proposed generated interpolation.</summary> |
| 43 | + /// <returns>The composed query key.</returns> |
| 44 | + [Benchmark] |
| 45 | + [BenchmarkCategory("Alias")] |
| 46 | + public string InterpolationAlias() => $"{_parentKey}.billing-postal_code"; |
| 47 | + |
| 48 | + /// <summary>Builds a pre-escaped nested CLR-name key using the current generated concatenation.</summary> |
| 49 | + /// <returns>The composed query key.</returns> |
| 50 | + [Benchmark(Baseline = true)] |
| 51 | + [BenchmarkCategory("PreEscaped")] |
| 52 | + [SuppressMessage("Roslynator", "RCS1190", Justification = "The benchmark intentionally measures the generated concatenation shape.")] |
| 53 | + [SuppressMessage("Style", "SST2249", Justification = "The benchmark intentionally measures the generated concatenation shape.")] |
| 54 | + public string ConcatenationPreEscaped() => _parentKey + NestedPrefix + PropertyName; |
| 55 | + |
| 56 | + /// <summary>Builds a pre-escaped nested CLR-name key using the proposed generated interpolation.</summary> |
| 57 | + /// <returns>The composed query key.</returns> |
| 58 | + [Benchmark] |
| 59 | + [BenchmarkCategory("PreEscaped")] |
| 60 | + public string InterpolationPreEscaped() => $"{_parentKey}.billing-PostalCode"; |
| 61 | + |
| 62 | + /// <summary>Builds the nested prefix passed to the key formatter using the current generated concatenation.</summary> |
| 63 | + /// <returns>The formatted query key.</returns> |
| 64 | + [Benchmark(Baseline = true)] |
| 65 | + [BenchmarkCategory("Formatted")] |
| 66 | + [SuppressMessage("Style", "SST2249", Justification = "The benchmark intentionally measures the generated concatenation shape.")] |
| 67 | + public string ConcatenationFormatted() => |
| 68 | + GeneratedRequestRunner.BuildQueryKey(_settings, PropertyName, null, _parentKey + NestedPrefix); |
| 69 | + |
| 70 | + /// <summary>Builds the nested prefix passed to the key formatter using the proposed generated interpolation.</summary> |
| 71 | + /// <returns>The formatted query key.</returns> |
| 72 | + [Benchmark] |
| 73 | + [BenchmarkCategory("Formatted")] |
| 74 | + public string InterpolationFormatted() => |
| 75 | + GeneratedRequestRunner.BuildQueryKey(_settings, PropertyName, null, $"{_parentKey}.billing-"); |
| 76 | +} |
0 commit comments