forked from prometheus-net/prometheus-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSdkComparisonBenchmarks.cs
234 lines (191 loc) · 8.47 KB
/
SdkComparisonBenchmarks.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System.Diagnostics;
using System.Diagnostics.Metrics;
using BenchmarkDotNet.Attributes;
using OpenTelemetry.Metrics;
using Prometheus;
namespace Benchmark.NetCore;
/// <summary>
/// We compare pure measurement (not serializing the data) with prometheus-net SDK and OpenTelemetry .NET SDK.
/// </summary>
/// <remarks>
/// Design logic:
/// * Metrics are initialized once on application startup.
/// * Metrics typically measure "sessions" - there are sets of metrics that are related through shared identifiers and a shared lifetime (e.g. HTTP request),
/// with all the identifiers for the metrics created when the sesison is initialized (e.g. when the HTTP connection is established).
///
/// Excluded from measurement:
/// * Meter setup (because meters are created once on application setup and not impactful later).
/// * Test data generation (session numbers and identifier strings) as it is SDK-neutral.
///
/// We have a separate benchmark to compare the setup stage (just re-runs the setup logic in measured phase).
///
/// We also do not benchmark "observable" metrics that are only polled at time of collection.
/// Both SDKs support it as an optimization (though OpenTelemetry forces it for counters) but let's try keep the logic here simple and exclude it for now.
/// </remarks>
[MemoryDiagnoser]
public class SdkComparisonBenchmarks
{
private const int CounterCount = 100;
private const int HistogramCount = 100;
// Unique sets of label/tag values per metric. You can think of each one as a "session" we are reporting data for.
private const int TimeseriesPerMetric = 100;
private static readonly string[] LabelNames = new[] { "environment", "server", "session_id" };
private const string Label1Value = "production";
private const string Label2Value = "hkhk298599-qps010-n200";
// How many observations we take during a single benchmark invocation (for each timeseries).
private const int ObservationCount = 1000;
private static readonly string[] SessionIds = new string[TimeseriesPerMetric];
static SdkComparisonBenchmarks()
{
for (var i = 0; i < SessionIds.Length; i++)
SessionIds[i] = Guid.NewGuid().ToString();
}
[Params(MetricsSdk.PrometheusNet, MetricsSdk.OpenTelemetry)]
public MetricsSdk Sdk { get; set; }
public enum MetricsSdk
{
PrometheusNet,
OpenTelemetry
}
/// <summary>
/// Contains all the context that gets initialized at iteration setup time.
///
/// This data set is:
/// 1) Not included in the performance measurements.
/// 2) Reused for each invocation that is part of the same iteration.
/// </summary>
private abstract class MetricsContext : IDisposable
{
/// <summary>
/// Records an observation with all the counter-type metrics for each session.
/// </summary>
public abstract void ObserveCounter(double value);
/// <summary>
/// Records an observation with all the histogram-type metrics for each session.
/// </summary>
public abstract void ObserveHistogram(double value);
public virtual void Dispose() { }
}
private sealed class PrometheusNetMetricsContext : MetricsContext
{
private readonly List<Prometheus.Counter.Child> _counterInstances = new(CounterCount * TimeseriesPerMetric);
private readonly List<Histogram.Child> _histogramInstances = new(HistogramCount * TimeseriesPerMetric);
public PrometheusNetMetricsContext()
{
var registry = Metrics.NewCustomRegistry();
var factory = Metrics.WithCustomRegistry(registry);
// Do not emit any exemplars in this benchmark, as they are not yet equally supported by the SDKs.
factory.ExemplarBehavior = ExemplarBehavior.NoExemplars();
for (var counterIndex = 0; counterIndex < CounterCount; counterIndex++)
{
var counter = factory.CreateCounter("counter_" + counterIndex, "", LabelNames);
for (var i = 0; i < TimeseriesPerMetric; i++)
_counterInstances.Add(counter.WithLabels(Label1Value, Label2Value, SessionIds[i]));
}
for (var histogramIndex = 0; histogramIndex < HistogramCount; histogramIndex++)
{
var histogram = factory.CreateHistogram("histogram_" + histogramIndex, "", LabelNames);
for (var i = 0; i < TimeseriesPerMetric; i++)
_histogramInstances.Add(histogram.WithLabels(Label1Value, Label2Value, SessionIds[i]));
}
}
public override void ObserveCounter(double value)
{
foreach (var counter in _counterInstances)
counter.Inc(value);
}
public override void ObserveHistogram(double value)
{
foreach (var histogram in _histogramInstances)
histogram.Observe(value);
}
}
private sealed class OpenTelemetryMetricsContext : MetricsContext
{
private const string MeterBaseName = "benchmark";
private readonly Meter _meter;
private readonly MeterProvider _provider;
private readonly List<Counter<double>> _counters = new(CounterCount);
private readonly List<Histogram<double>> _histograms = new(HistogramCount);
private readonly List<TagList> _sessions = new(TimeseriesPerMetric);
public OpenTelemetryMetricsContext()
{
// We use a randomized name every time because otherwise there appears to be some "shared state" between benchmark invocations,
// at least for the "setup" benchmark which keeps getting slower every time we call it with the same metric name.
_meter = new Meter(MeterBaseName + Guid.NewGuid());
_provider = OpenTelemetry.Sdk.CreateMeterProviderBuilder()
.AddPrometheusExporter()
.AddMeter(_meter.Name)
.Build();
for (var i = 0; i < CounterCount; i++)
_counters.Add(_meter.CreateCounter<double>("counter_" + i));
for (var i = 0; i < HistogramCount; i++)
_histograms.Add(_meter.CreateHistogram<double>("histogram_" + i));
for (var i = 0; i < TimeseriesPerMetric; i++)
{
var tag1 = new KeyValuePair<string, object>(LabelNames[0], Label1Value);
var tag2 = new KeyValuePair<string, object>(LabelNames[1], Label2Value);
var tag3 = new KeyValuePair<string, object>(LabelNames[2], SessionIds[i]);
var tagList = new TagList(new[] { tag1, tag2, tag3 });
_sessions.Add(tagList);
}
}
public override void ObserveCounter(double value)
{
foreach (var session in _sessions)
{
foreach (var counter in _counters)
counter.Add(value, session);
}
}
public override void ObserveHistogram(double value)
{
foreach (var session in _sessions)
{
foreach (var histogram in _histograms)
histogram.Record(value, session);
}
}
public override void Dispose()
{
base.Dispose();
_provider.Dispose();
}
}
private MetricsContext _context;
[IterationSetup]
public void Setup()
{
_context = Sdk switch
{
MetricsSdk.PrometheusNet => new PrometheusNetMetricsContext(),
MetricsSdk.OpenTelemetry => new OpenTelemetryMetricsContext(),
_ => throw new NotImplementedException(),
};
}
[Benchmark]
public void CounterMeasurements()
{
for (var observation = 0; observation < ObservationCount; observation++)
_context.ObserveCounter(observation);
}
[Benchmark]
public void HistogramMeasurements()
{
for (var observation = 0; observation < ObservationCount; observation++)
_context.ObserveHistogram(observation);
}
[IterationCleanup]
public void Cleanup()
{
_context.Dispose();
}
[Benchmark]
public void SetupBenchmark()
{
// Here we just do the setup again, but this time as part of the measured data set, to compare the setup cost between SDKs.
// We need to dispose of the automatically created context, in case there are any SDK-level singleton resources (which we do not want to accidentally reuse).
_context.Dispose();
Setup();
}
}