PerformanceMonitor.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Log;
  8. using Aitex.Core.Util;
  9. namespace MECF.Framework.Common.Utilities
  10. {
  11. public class PerformanceMonitor
  12. {
  13. public static DeviceTimer _timer = new DeviceTimer();
  14. public static void TraceLog()
  15. {
  16. if (_timer.GetElapseTime() < 1000 && !_timer.IsIdle())
  17. return;
  18. _timer.Start(0);
  19. Action act = () =>
  20. {
  21. try
  22. {
  23. Process[] processes = Process.GetProcesses();
  24. StringBuilder sb = new StringBuilder();
  25. sb.Append("【System Performance Monitor Report】\r\n");
  26. sb.Append(string.Format("{0,30}", "ProcessName"));
  27. sb.Append(string.Format("{0,10}", "PID"));
  28. sb.Append(string.Format("{0,10}", "Priority"));
  29. sb.Append(string.Format("{0,10}", "Handle"));
  30. sb.Append(string.Format("{0,10}", "Thread"));
  31. sb.Append(string.Format("{0,15}", "PriMemory(MB)"));
  32. sb.Append(string.Format("{0,15}", " WorkingSet(MB)\r\n"));
  33. for (int i = 0; i < processes.Length - 1; i++)
  34. {
  35. var process = processes[i];
  36. sb.Append(string.Format("{0,30}", process.ProcessName));
  37. sb.Append(string.Format("{0,10}", Convert.ToString(process.Id.ToString("0000"))));
  38. sb.Append(string.Format("{0,10}", process.BasePriority));
  39. sb.Append(string.Format("{0,10}", process.HandleCount));
  40. sb.Append(string.Format("{0,10}", process.Threads.Count));
  41. sb.Append(string.Format("{0,15}", (process.PrivateMemorySize64 / 1000000.0).ToString("F1"))); ;
  42. sb.Append(string.Format("{0,15}", (process.WorkingSet64 / 1000000.0).ToString("F1"))); sb.Append("\r\n");
  43. }
  44. LOG.Write(sb.ToString());
  45. }
  46. catch (Exception ex)
  47. {
  48. LOG.Write(ex);
  49. }
  50. };
  51. act.BeginInvoke(null, null);
  52. }
  53. }
  54. }