Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private double MemoryPercentage(Func<ulong> getMemoryUsage)
{
if (now >= _refreshAfterMemory)
{
_memoryPercentage = Math.Min(Hundred, memoryUsage / _memoryLimit * Hundred); // Don't change calculation order, otherwise we loose some precision
_memoryPercentage = Math.Min(Hundred, memoryUsage / _memoryLimit);
_refreshAfterMemory = now.Add(_memoryRefreshInterval);
}

Expand Down Expand Up @@ -229,7 +229,7 @@ private double CpuPercentage()
var timeTickDelta = (now.Ticks - _oldCpuTimeTicks) * _cpuLimit;
if (usageTickDelta > 0 && timeTickDelta > 0)
{
_cpuPercentage = Math.Min(Hundred, usageTickDelta / timeTickDelta * Hundred); // Don't change calculation order, otherwise precision is lost.
_cpuPercentage = Math.Min(Hundred, usageTickDelta / timeTickDelta);

Log.CpuContainerUsageData(
_logger, basicAccountingInfo.TotalKernelTime, basicAccountingInfo.TotalUserTime, _oldCpuUsageTicks, timeTickDelta, _cpuLimit, _cpuPercentage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private double MemoryPercentage()
{
if (now >= _refreshAfterMemory)
{
_memoryPercentage = Math.Min(Hundred, currentMemoryUsage / _totalMemory * Hundred); // Don't change calculation order, otherwise we loose some precision
_memoryPercentage = Math.Min(Hundred, currentMemoryUsage / _totalMemory);
_refreshAfterMemory = now.Add(_memoryRefreshInterval);
}

Expand Down Expand Up @@ -168,7 +168,7 @@ private double CpuPercentage()
var timeTickDelta = (now.Ticks - _oldCpuTimeTicks) * _cpuUnits;
if (usageTickDelta > 0 && timeTickDelta > 0)
{
_cpuPercentage = Math.Min(Hundred, usageTickDelta / (double)timeTickDelta * Hundred); // Don't change calculation order, otherwise we loose some precision
_cpuPercentage = Math.Min(Hundred, usageTickDelta / (double)timeTickDelta);

Log.CpuUsageData(_logger, currentCpuTicks, _oldCpuUsageTicks, timeTickDelta, _cpuUnits, _cpuPercentage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,21 @@ public void SnapshotProvider_EmitsCpuMetrics(string instrumentName)
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

Assert.Equal(10, metricCollector.LastMeasurement.Value); // Consumed 10% of the CPU.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Consumed 10% of the CPU.

// Step #2 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

// CPU usage should be the same as before, as we didn't recalculate it:
Assert.Equal(10, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.

// Step #3 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

// CPU usage should be the same as before, as we're not simulating any CPU usage:
Assert.Equal(10, metricCollector.LastMeasurement.Value); // Consumed 10% of the CPU.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Consumed 10% of the CPU.
}

[Theory]
Expand Down Expand Up @@ -294,17 +294,17 @@ public void SnapshotProvider_EmitsMemoryMetrics(string instrumentName)
// Step #0 - state in the beginning:
metricCollector.RecordObservableInstruments();
Assert.NotNull(metricCollector.LastMeasurement?.Value);
Assert.Equal(10, metricCollector.LastMeasurement.Value); // Consuming 10% of the memory initially.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Consuming 10% of the memory initially.

// Step #1 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(options.MemoryConsumptionRefreshInterval - TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();
Assert.Equal(10, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.

// Step #2 - simulate 2 milliseconds passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();
Assert.Equal(30, metricCollector.LastMeasurement.Value); // Consuming 30% of the memory afterwards.
Assert.Equal(0.3, metricCollector.LastMeasurement.Value); // Consuming 30% of the memory afterwards.
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ public void SnapshotProvider_EmitsCpuMetrics()
// Step #1 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();
Assert.Equal(5, metricCollector.LastMeasurement?.Value); // Consuming 5% of the CPU (2 CPUs, 1000 ticks, 1ms).
Assert.Equal(0.05, metricCollector.LastMeasurement?.Value); // Consuming 5% of the CPU (2 CPUs, 1000 ticks, 1ms).

// Step #2 - simulate another 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

// CPU usage should be the same as before, as we're not simulating any CPU usage:
Assert.Equal(5, metricCollector.LastMeasurement?.Value); // Still consuming 5% of the CPU
Assert.Equal(0.05, metricCollector.LastMeasurement?.Value); // Still consuming 5% of the CPU
}

[ConditionalFact]
Expand All @@ -124,21 +124,21 @@ public void SnapshotProvider_EmitsMemoryMetrics()
// Step #0 - state in the beginning:
metricCollector.RecordObservableInstruments();
Assert.NotNull(metricCollector.LastMeasurement);
Assert.Equal(10, metricCollector.LastMeasurement.Value); // Consuming 5% of the memory initially
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Consuming 5% of the memory initially

memoryUsed = 900L; // Simulate 30% memory usage.

// Step #1 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

Assert.Equal(10, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.
Assert.Equal(0.1, metricCollector.LastMeasurement.Value); // Still consuming 10% as gauge wasn't updated.

// Step #2 - simulate 1 millisecond passing and collect metrics again:
fakeClock.Advance(TimeSpan.FromMilliseconds(1));
metricCollector.RecordObservableInstruments();

Assert.Equal(30, metricCollector.LastMeasurement.Value); // Consuming 30% of the memory afterwards
Assert.Equal(0.3, metricCollector.LastMeasurement.Value); // Consuming 30% of the memory afterwards

memoryUsed = 3_100L; // Simulate more than 100% memory usage

Expand All @@ -147,7 +147,7 @@ public void SnapshotProvider_EmitsMemoryMetrics()
metricCollector.RecordObservableInstruments();

// Memory usage should be the same as before, as we're not simulating any CPU usage:
Assert.Equal(100, metricCollector.LastMeasurement.Value); // Consuming 100% of the memory
Assert.Equal(1, metricCollector.LastMeasurement.Value); // Consuming 100% of the memory
}

[ConditionalFact]
Expand Down
Loading