public void WhenManagingMultipleProcesses() { const long oneProcessPrivateMemory = 1024; TimeSpan expectedTotalKernelTime = TimeSpan.FromSeconds(2); TimeSpan expectedTotalUserTime = TimeSpan.FromSeconds(2); var expectedCpuStats = new CpuStatistics { TotalKernelTime = expectedTotalKernelTime, TotalUserTime = expectedTotalUserTime, }; var firstProcess = Substitute.For <IProcess>(); firstProcess.Id.Returns(1); firstProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory); var secondProcess = Substitute.For <IProcess>(); secondProcess.Id.Returns(2); secondProcess.PrivateMemoryBytes.Returns(oneProcessPrivateMemory); JobObject.GetCpuStatistics().Returns(expectedCpuStats); JobObject.GetProcessIds().Returns(new int[] { 1, 2 }); ProcessHelper.GetProcesses(null).ReturnsForAnyArgs(new[] { firstProcess, secondProcess }); var metrics = Container.GetMetrics(); Assert.Equal(expectedTotalKernelTime + expectedTotalUserTime, metrics.CpuStat.TotalProcessorTime); Assert.Equal((ulong)firstProcess.PrivateMemoryBytes + (ulong)secondProcess.PrivateMemoryBytes, metrics.MemoryStat.PrivateBytes); }
public void WhenProcessHasExited() { var firstProcess = Substitute.For <IProcess>(); firstProcess.Id.Returns(1); firstProcess.PrivateMemoryBytes.Throws(new InvalidOperationException()); JobObject.GetProcessIds().Returns(new int[] { 1 }); ProcessHelper.GetProcesses(null).ReturnsForAnyArgs(new[] { firstProcess }); var metrics = Container.GetMetrics(); Assert.Equal(0ul, metrics.MemoryStat.PrivateBytes); }
public void WhenManagingNoProcess() { JobObject.GetCpuStatistics().Returns(new CpuStatistics { TotalKernelTime = TimeSpan.Zero, TotalUserTime = TimeSpan.Zero, }); JobObject.GetProcessIds().Returns(new int[0]); var metrics = Container.GetMetrics(); Assert.Equal(TimeSpan.Zero, metrics.CpuStat.TotalProcessorTime); Assert.Equal(0ul, metrics.MemoryStat.PrivateBytes); }