Saturday, 6 June 2009

Get-ProcessPerfCounter.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a process, then displays some performance counters. 
  4. .DESCRIPTION 
  5.     This script calls System.Diagnostics.Process's Start static 
  6.     method to create a process.  Then it displays perf stats till the
  7.     process is stopped. Then it prints final stats out.
  8. .NOTES 
  9.     File Name  : Get-ProcessPerfCounter 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. <# 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.diagnostics.process.peakworkingset64.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-ProcessPerfCounter.ps1 
  20.  
  21.     System.Diagnostics.Process (notepad) - 
  22.     ------------------------------------- 
  23.       physical memory usage     : 2,512 
  24.       base priority             : 8 
  25.       priority class            : Normal 
  26.       user processor time       : 00:00:00.0156001 
  27.       privileged processor time : 00:00:00.0156001 
  28.       total processor time      : 00:00:00.0312002 
  29.   
  30.     ** Loads more - snipped for brevity ** 
  31.      
  32.     System.Diagnostics.Process (notepad) - 
  33.     ------------------------------------- 
  34.       physical memory usage     : 12,424 
  35.       base priority             : 8 
  36.       priority class            : Normal 
  37.       user processor time       : 00:00:00.0156001 
  38.       privileged processor time : 00:00:00.0624004 
  39.       total processor time      : 00:00:00.0780005 
  40.   
  41.       Process has ended 
  42.       Process exit code: 0 
  43.       Peak physical memory usage of the process :  12,424 kb 
  44.       Peak paged memory usage of the process    :   2,740 kb 
  45.       Peak virtual memory usage of the process  :  88,832 kb 
  46. #> 
  47.   
  48. ## 
  49. # Start of script 
  50. ## 
  51.    
  52. # Start up Notepad, catching issues 
  53. try  { 
  54. $myproc =  [System.Diagnostics.Process]::Start("c:\windows\notepad.exe"
  55. catch { 
  56.   "Error starting process" 
  57.   return 
  58.   
  59. # Now print perf stats until Notepad.exe is closed 
  60. do { 
  61.   if ( ! $myproc.HasExited ) { 
  62.   $myproc.Refresh() 
  63.   "" 
  64.   "{0} -"        -f $myProc.ToString() 
  65.   "-------------------------------------" 
  66.   "  physical memory usage     : {0}" -f $($MyProc.WorkingSet64/1kb).tostring("###,###"
  67.   "  base priority             : {0}" -f $MyProc.BasePriority 
  68.   "  priority class            : {0}" -f $MyProc.PriorityClass 
  69.   "  user processor time       : {0}" -f $MyProc.UserProcessorTime 
  70.   "  privileged processor time : {0}" -f $MyProc.PrivilegedProcessorTime 
  71.   "  total processor time      : {0}" -f $MyProc.TotalProcessorTime 
  72.     
  73. # calculate overall peak 
  74. $peakPagedMem   = $MyProc.PeakPagedMemorySize64 
  75. $peakVirtualMem = $MyProc.PeakVirtualMemorySize64 
  76. $peakWorkingSet = $MyProc.PeakWorkingSet64 
  77.   
  78. } # end of if 
  79.      
  80. } # end of do 
  81. while (!$myproc.WaitForExit(1000)) # Wait a second and do it again 
  82.  
  83. # Here process has exited 
  84. # Print out final results 
  85. "" 
  86. "Process has ended" 
  87. "Process exit code: {0}" -f $MyProc.ExitCode 
  88.  
  89. # Display peak memory statistics for the process. 
  90. "Peak physical memory usage of the process : {0,7} kb" -f $($peakWorkingSet/1kb).ToString("###,###"
  91. "Peak paged memory usage of the process    : {0,7} kb" -f $($peakPagedMem/1kb).ToString("###,###"
  92. "Peak virtual memory usage of the process  : {0,7} kb" -f $($peakVirtualMem/1kb).ToString("###,###"
  93. # End of script 

No comments: