diff --git a/test/Benchmarks/Metrics/ExponentialHistogramBenchmarks.cs b/test/Benchmarks/Metrics/ExponentialHistogramBenchmarks.cs new file mode 100644 index 000000000..074766568 --- /dev/null +++ b/test/Benchmarks/Metrics/ExponentialHistogramBenchmarks.cs @@ -0,0 +1,90 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System.Diagnostics.Metrics; +using BenchmarkDotNet.Attributes; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Tests; + +/* +BenchmarkDotNet=v0.13.5, OS=macOS Ventura 13.4 (22F66) [Darwin 22.5.0] +Apple M1 Max, 1 CPU, 10 logical and 10 physical cores +.NET SDK=7.0.101 + [Host] : .NET 7.0.1 (7.0.122.56804), Arm64 RyuJIT AdvSIMD + DefaultJob : .NET 7.0.1 (7.0.122.56804), Arm64 RyuJIT AdvSIMD + + +| Method | Scale | Mean | Error | StdDev | Allocated | +|----------------- |------ |---------:|---------:|---------:|----------:| +| HistogramHotPath | -11 | 29.79 ns | 0.054 ns | 0.042 ns | - | +| HistogramHotPath | 3 | 32.10 ns | 0.086 ns | 0.080 ns | - | +| HistogramHotPath | 20 | 32.08 ns | 0.076 ns | 0.063 ns | - | +*/ + +namespace Benchmarks.Metrics; + +public class ExponentialHistogramBenchmarks +{ + private const int MaxValue = 10000; + private readonly Random random = new(); + private Histogram histogram; + private MeterProvider provider; + private Meter meter; + + // This is a simple benchmark that records values in the range [0, 10000]. + // The reason the following scales are benchmarked are as follows: + // + // -11: Non-positive scales should perform better than positive scales. + // The algorithm to map values to buckets for non-positive scales is more efficient. + // 3: The benchmark records values in the range [0, 10000] and uses the default max number of buckets (160). + // Scale 3 is the maximum scale that will fit this range of values given the number of buckets. + // That is, no scale down will occur. + // 20: Scale 20 should perform the same as scale 3. During warmup the histogram should scale down to 3. + [Params(-11, 3, 20)] + public int Scale { get; set; } + + [GlobalSetup] + public void Setup() + { + this.meter = new Meter(Utils.GetCurrentMethodName()); + this.histogram = this.meter.CreateHistogram("histogram"); + + var exportedItems = new List(); + + this.provider = Sdk.CreateMeterProviderBuilder() + .AddMeter(this.meter.Name) + .AddInMemoryExporter(exportedItems, metricReaderOptions => + { + metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = 1000; + }) + .AddView("histogram", new Base2ExponentialBucketHistogramConfiguration() { MaxScale = this.Scale, MaxSize = Metric.DefaultExponentialHistogramMaxBuckets }) + .Build(); + } + + [GlobalCleanup] + public void Cleanup() + { + this.meter?.Dispose(); + this.provider?.Dispose(); + } + + [Benchmark] + public void HistogramHotPath() + { + this.histogram.Record(this.random.Next(MaxValue)); + } +} diff --git a/test/Benchmarks/Metrics/ExponentialHistogramMapToIndexBenchmarks.cs b/test/Benchmarks/Metrics/ExponentialHistogramMapToIndexBenchmarks.cs new file mode 100644 index 000000000..a2fff3d2d --- /dev/null +++ b/test/Benchmarks/Metrics/ExponentialHistogramMapToIndexBenchmarks.cs @@ -0,0 +1,57 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using BenchmarkDotNet.Attributes; +using OpenTelemetry.Metrics; + +/* +BenchmarkDotNet=v0.13.5, OS=macOS Ventura 13.4 (22F66) [Darwin 22.5.0] +Apple M1 Max, 1 CPU, 10 logical and 10 physical cores +.NET SDK=7.0.101 + [Host] : .NET 7.0.1 (7.0.122.56804), Arm64 RyuJIT AdvSIMD + DefaultJob : .NET 7.0.1 (7.0.122.56804), Arm64 RyuJIT AdvSIMD + + +| Method | Scale | Mean | Error | StdDev | Allocated | +|----------- |------ |---------:|---------:|---------:|----------:| +| MapToIndex | -11 | 11.60 ns | 0.057 ns | 0.053 ns | - | +| MapToIndex | 3 | 14.63 ns | 0.135 ns | 0.126 ns | - | +| MapToIndex | 20 | 14.40 ns | 0.026 ns | 0.024 ns | - | +*/ + +namespace Benchmarks.Metrics; + +public class ExponentialHistogramMapToIndexBenchmarks +{ + private const int MaxValue = 10000; + private readonly Random random = new(); + private Base2ExponentialBucketHistogram exponentialHistogram; + + [Params(-11, 3, 20)] + public int Scale { get; set; } + + [GlobalSetup] + public void Setup() + { + this.exponentialHistogram = new Base2ExponentialBucketHistogram(scale: this.Scale); + } + + [Benchmark] + public void MapToIndex() + { + this.exponentialHistogram.MapToIndex(this.random.Next(MaxValue)); + } +}