forked from dotnetcore/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixEnumerationBenchmark.cs
More file actions
69 lines (55 loc) · 2.02 KB
/
Copy pathMatrixEnumerationBenchmark.cs
File metadata and controls
69 lines (55 loc) · 2.02 KB
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
// Licensed to the .NET Core Community under one or more agreements.
// The .NET Core Community licenses this file to you under the MIT license.
using BenchmarkDotNet.Attributes;
using Mocha.Core.Storage.Prometheus.Metrics;
using Mocha.Query.Prometheus.PromQL.Engine;
using Mocha.Query.Prometheus.PromQL.Values;
[MemoryDiagnoser]
public class MatrixEnumerationBenchmark
{
[Params(1_000, 10_000)] public int SampleCount { get; set; }
[Params(15)] public int SampleIntervalSeconds { get; set; }
[Params(30, 60)] public int SelectorRangeSeconds { get; set; }
private List<TimeSeriesSample> _samples = null!;
private long _startTs;
private long _endTs;
[GlobalSetup]
public void Setup()
{
_samples = new List<TimeSeriesSample>(SampleCount);
long ts = 0;
for (int i = 0; i < SampleCount; i++)
{
_samples.Add(new TimeSeriesSample { TimestampUnixSec = ts, Value = i });
ts += SampleIntervalSeconds;
}
_startTs = _samples[0].TimestampUnixSec;
_endTs = _samples[^1].TimestampUnixSec;
}
[Benchmark(Baseline = true)]
public void LinqEveryStep()
{
for (var ts = _startTs; ts <= _endTs; ts += SelectorRangeSeconds)
{
var maxTs = ts;
var minTs = maxTs - SelectorRangeSeconds;
var points = _samples
.Where(s => s.TimestampUnixSec >= minTs &&
s.TimestampUnixSec <= maxTs)
.Select(s => new DoublePoint { TimestampUnixSec = s.TimestampUnixSec, Value = s.Value })
.ToList();
}
}
[Benchmark]
public void EnumeratorSlidingWindow()
{
var reusePoints = new List<DoublePoint>();
using var enumerator = new MatrixEnumerator(_samples);
for (var ts = _startTs; ts <= _endTs; ts += SelectorRangeSeconds)
{
var maxTs = ts;
var minTs = maxTs - SelectorRangeSeconds;
enumerator.Enumerate(minTs, maxTs, reusePoints);
}
}
}