Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sdk-metrics] Improve the perf of histogram snapshots #5366

Merged

Conversation

CodeBlanch
Copy link
Member

Changes

  • For histograms with buckets switch from two arrays (one for running values and one for snapshot values) to a single array containing a structure with both values. This speeds up the snapshot logic because we can now elide bounds checking. That is important because when it comes to histograms the snapshot blocks the recording of values. Making it faster reduces contention which increases throughput when a collection occurs.

Benchmarks

Method OutputDelta Mean Error StdDev
UpdateOld False 0.2130 ns 0.0222 ns 0.0208 ns
UpdateNew False 0.1810 ns 0.0264 ns 0.0247 ns
SnapshotOld False 9.8634 ns 0.1283 ns 0.1200 ns
SnapshotNew False 4.0386 ns 0.0894 ns 0.0836 ns
UpdateOld True 0.1948 ns 0.0247 ns 0.0231 ns
UpdateNew True 0.1815 ns 0.0120 ns 0.0107 ns
SnapshotOld True 10.7866 ns 0.2127 ns 0.1885 ns
SnapshotNew True 7.3044 ns 0.1561 ns 0.1603 ns
Code
public class HistogramSnapshotBenchmarks
{
    private readonly HistogramBucketValues[] bucketCounts = new HistogramBucketValues[16];
    private readonly long[] runningBucketCounts = new long[16];
    private readonly long[] snapshotBucketCounts = new long[16];

    [Params(true, false)]
    public bool OutputDelta { get; set; }

    [Benchmark]
    public long UpdateOld()
    {
        return this.runningBucketCounts[0]++;
    }

    [Benchmark]
    public long UpdateNew()
    {
        return this.bucketCounts[0].RunningValue++;
    }

    [Benchmark]
    public void SnapshotOld()
    {
        for (int i = 0; i < this.runningBucketCounts.Length; i++)
        {
            this.snapshotBucketCounts[i] = this.runningBucketCounts[i];
            if (this.OutputDelta)
            {
                this.runningBucketCounts[i] = 0;
            }
        }
    }

    [Benchmark]
    public void SnapshotNew()
    {
        var bucketCounts = this.bucketCounts;

        if (this.OutputDelta)
        {
            for (int i = 0; i < bucketCounts.Length; i++)
            {
                ref var values = ref bucketCounts[i];
                ref var running = ref values.RunningValue;
                values.SnapshotValue = running;
                running = 0L;
            }
        }
        else
        {
            for (int i = 0; i < bucketCounts.Length; i++)
            {
                ref var values = ref bucketCounts[i];
                values.SnapshotValue = values.RunningValue;
            }
        }
    }

    internal struct HistogramBucketValues
    {
        public long RunningValue;
        public long SnapshotValue;
    }
}
## Merge requirement checklist
  • CONTRIBUTING guidelines followed (license requirements, nullable enabled, static analysis, etc.)

@CodeBlanch CodeBlanch added the metrics Metrics signal related label Feb 16, 2024
@CodeBlanch CodeBlanch requested a review from a team February 16, 2024 19:17
Copy link

codecov bot commented Feb 16, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (6250307) 83.38% compared to head (805ccb5) 83.13%.
Report is 85 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #5366      +/-   ##
==========================================
- Coverage   83.38%   83.13%   -0.26%     
==========================================
  Files         297      276      -21     
  Lines       12531    12001     -530     
==========================================
- Hits        10449     9977     -472     
+ Misses       2082     2024      -58     
Flag Coverage Δ
unittests ?
unittests-Solution-Experimental 82.88% <95.23%> (?)
unittests-Solution-Stable 83.13% <95.23%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
src/OpenTelemetry/Metrics/HistogramBuckets.cs 100.00% <100.00%> (ø)
src/OpenTelemetry/Metrics/MetricPoint.cs 68.45% <83.33%> (-0.02%) ⬇️

... and 45 files with indirect coverage changes

@CodeBlanch CodeBlanch merged commit dd877fe into open-telemetry:main Feb 16, 2024
37 checks passed
@CodeBlanch CodeBlanch deleted the sdk-histogram-perf-improvements branch February 16, 2024 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics Metrics signal related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants