private static readonly Dictionary <Enum, PerformanceCounter> _performanceCounters = new Dictionary <Enum, PerformanceCounter>(); // Save counters for fast reference /// <summary> /// Return memory usage for a specific counter /// </summary> /// <param name="performance"></param> /// <returns></returns> public static void Memory(enumPerformanceMeasureAttribute performance) { if (performance.Name_Category != "Memory" && performance.Name_Category != "Paging File") { throw new ArgumentException($"Error! '{performance.Name_Category}' is not a valid 'Memory' counter.", nameof(performance)); } switch (performance.Id) { case enCounter_Memory.activeProcessMemory: performance.Value_ = _lamed.lib.System.Runtime.activeProcessMemory(); break; case enCounter_Memory.PhysicalMemory_Available: performance.Value_ = VisualBasicMethods.Memory_PhysicalAvailable(); break; case enCounter_Memory.PhysicalMemory_Total: performance.Value_ = VisualBasicMethods.Memory_PhysicalTotal(); break; case enCounter_Memory.PhysicalMemory_InUse: performance.Value_ = VisualBasicMethods.Memory_PhysicalInUse(); break; case enCounter_Memory.VirtualMemory_Available: performance.Value_ = VisualBasicMethods.Memory_VirtualAvailable(); break; case enCounter_Memory.VirtualMemory_Total: performance.Value_ = VisualBasicMethods.Memory_VirtualTotal(); break; case enCounter_Memory.VirtualMemory_InUse: performance.Value_ = VisualBasicMethods.Memory_VirtualInUse(); break; default: WindowsPerformanceCounter(performance); break; } // Format the value FormatValue(performance); }
/// <summary> /// Return the IO performance measure /// </summary> /// <param name="performance"></param> public static void IO(enumPerformanceMeasureAttribute performance) { if (performance.Name_Category != "IO" && performance.Name_Category != "PhysicalDisk") { throw new ArgumentException($"Error! '{performance.Name_Category}' is not a valid IO counter.", nameof(performance)); } switch (performance.Id) { case enCounter_IO.Space_Free: performance.Value_ = _lamed.lib.IO.Drive.Space_Free(performance.Name_Instance); break; case enCounter_IO.Space_FreePercent: performance.Value_ = _lamed.lib.IO.Drive.Space_FreePercent(performance.Name_Instance); break; case enCounter_IO.Space_Total: performance.Value_ = _lamed.lib.IO.Drive.Space_Total(performance.Name_Instance); break; case enCounter_IO.Space_Used: performance.Value_ = _lamed.lib.IO.Drive.Space_InUse(performance.Name_Instance); break; case enCounter_IO.Speed: performance.Value_ = (float)_lamed.lib.IO.Drive.Drive_Speed(null, performance.Name_Instance); break; default: WindowsPerformanceCounter(performance); break; } // Format the value FormatValue(performance); }
public static enumPerformanceMeasureAttribute Memory(enCounter_Memory counterType) { //PerformanceCounter result; enumPerformanceMeasureAttribute result = counterType.zAttribute_AsPerformanceMeasure(); Memory(result); return(result); }
public static enumPerformanceMeasureAttribute IO(enCounter_IO counterType, string driveName = "") { //PerformanceCounter result; enumPerformanceMeasureAttribute result = counterType.zAttribute_AsPerformanceMeasure(); if (driveName != "" && result.Name_Instance.zIsNullOrEmpty()) { result.Name_Instance = driveName; } IO(result); return(result); }
public void Memory_Test() { _Debug.WriteLine("Memory:"); if (Environment.Is64BitProcess) { _Debug.WriteLine(" 64-bit process"); } else { _Debug.WriteLine(" 32-bit process"); } // activeProcessMemory var activeProcessMemory = SystemMeasurement.Memory(enCounter_Memory.activeProcessMemory); Assert.True(activeProcessMemory.Value_ > 0); _Debug.WriteLine($" {activeProcessMemory}"); _Debug.WriteLine("-----------------------------"); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.PhysicalMemory_Total).ToString()); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.PhysicalMemory_Available).ToString()); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.PhysicalMemory_InUse).ToString()); _Debug.WriteLine("-----------------------------"); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.VirtualMemory_Total).ToString()); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.VirtualMemory_Available).ToString()); _Debug.WriteLine(" " + SystemMeasurement.Memory(enCounter_Memory.VirtualMemory_InUse).ToString()); _Debug.WriteLine("-----------------------------"); // page file enumPerformanceMeasureAttribute pagingFileUsage = SystemMeasurement.Memory(enCounter_Memory.pagingFileUsage); _Debug.WriteLine($" {pagingFileUsage}"); // pagingFilePeak enumPerformanceMeasureAttribute pagingFilePeak = SystemMeasurement.Memory(enCounter_Memory.pagingFilePeak); _Debug.WriteLine($" {pagingFilePeak}"); _Debug.WriteLine("-----------------------------"); // PhysicalMemory_Available enumPerformanceMeasureAttribute availableMemory = SystemMeasurement.Memory(enCounter_Memory.pagingFileUsage); Assert.Equal("Paging file usage", availableMemory.Name_Caption); Assert.Equal("%", availableMemory.Value_Unit); Assert.Equal("Paging File", availableMemory.Name_Category); Assert.Equal("% Usage", availableMemory.Name_Counter); }
private static PerformanceCounter WindowsPerformanceCounter([NotNull] enumPerformanceMeasureAttribute performance) { PerformanceCounter performanceCounter; if (_performanceCounters.TryGetValue(performance.Id, out performanceCounter) == false) { if (performance.Name_Instance == "") { performanceCounter = new PerformanceCounter(performance.Name_Category, performance.Name_Counter); } else { performanceCounter = new PerformanceCounter(performance.Name_Category, performance.Name_Counter, performance.Name_Instance); } _performanceCounters.Add(performance.Id, performanceCounter); } // Get the counter value float valueMax = 0; float value = 0; if (performance.Format_NextValue2Use != 1) { for (int ii = 1; ii < performance.Format_NextValue2Use; ii++) { value = performanceCounter.NextValue(); if (value > valueMax) { valueMax = value; } Thread.Sleep(performance.Format_NextValueSleep); } } value = performanceCounter.NextValue(); if (value > valueMax) { valueMax = value; } performance.Value_ = valueMax; if (performance.Help == "") { performance.Help = performanceCounter.CounterHelp; } return(performanceCounter); }
private static void FormatValue(enumPerformanceMeasureAttribute performance) { var multiplier = (float)performance.Format_Multiplier; if (performance.Format_IsMultiplier) { performance.Value_ = performance.Value_ * multiplier; } else { performance.Value_ = performance.Value_ / multiplier; } performance.Value_AsStr = _lamed.Types.Convert.Str_FromDouble(performance.Value_, performance.Format_DecimalPlaces); // Merge the instance with the caption if (performance.Name_Caption.Contains("{0}")) { performance.Name_Caption = performance.Name_Caption.zFormat(performance.Name_Instance); } }