2011-03-04 11 views
5

Yürütülebilir dosyaların performansını değerlendirmek için Windows PowerShell üzerinde basit bir komut dosyası yazıyorum.Yürütülebilir dosyaların performansını değerlendirmek için PowerShell 1.0 veya 2.0'ı kullanma PowerShell 1.0 veya 2.0'ı kullanma

Önemli hipotez, şudur: Yürütülebilir bir dosyam var, herhangi bir dilde yazılmış bir uygulama olabilir (.net ve değil, Viual-Prolog, C++, C, .exe dosyası olarak derlenebilir). Yürütme sürelerini almayı istiyorum.

Function Time-It { 
    Param ([string]$ProgramPath, [string]$Arguments) 
    $Watch = New-Object System.Diagnostics.Stopwatch 
    $NsecPerTick = (1000 * 1000 * 1000)/[System.Diagnostics.Stopwatch]::Frequency 
    Write-Output "Stopwatch created! NSecPerTick = $NsecPerTick" 
    $Watch.Start() # Starts the timer 
    [System.Diagnostics.Process]::Start($ProgramPath, $Arguments) 
    $Watch.Stop() # Stops the timer 
    # Collectiong timings 
    $Ticks = $Watch.ElapsedTicks 
    $NSecs = $Watch.ElapsedTicks * $NsecPerTick 
    Write-Output "Program executed: time is: $Nsecs ns ($Ticks ticks)" 
} 

Bu fonksiyon kronometre kullanır:

bunu yaptım. Peki, functoin bir program yolunu kabul eder, kronometre başlatılır, program çalıştırılır ve kronometre durdurulur. Sorun: System.Diagnostics.Process.Start eşzamansızdır ve uygulama bittiğinde bir sonraki talimat (durdurulmuş) yürütmez. Yeni bir süreç oluşturuldu ...

Program bittiğinde zamanlayıcıyı durdurmam gerekiyor.

Ben

Bu nasıl çözmek için ... o şanslı değildi yürütme sürelerini ... ilgili bazı bilgiler düzenlenen thicking, Process sınıfa düşündün mü?

cevap

6

Sen Burada Process.WaitForExit()

$proc = new-object "System.Diagnostics.Process" 
$proc.StartInfo.FileName = "notepad.exe" 
$proc.StartInfo.UseShellExecute = $false 
$proc.Start() 
$proc.WaitForExit() 
+0

Ama statik değildir ... Bir Süreç nesnesi oluşturmak gerekir ama Yürütülecek yolun nasıl ayarlanacağını gerçekten anlamayın ... – Andry

+0

'$ proc = yeni nesne" System.Diagnostics.Process "' ve ardından özellikleri ayarlayın. .NET belgelerini takip edebilirsiniz, sınıflar aynı şekilde çalışır. – kprobst

+0

@Andry - Bir örnek ekledim. – kprobst

2

komple bir çözüm için, Measure-Command cmdlet ile kombine kprobst's answer, var kullanabilirsiniz:

$proc = new-object "System.Diagnostics.Process" 
$proc.StartInfo.FileName = "notepad.exe" 
$proc.StartInfo.UseShellExecute = $false 
$timeSpan = (MeasureCommand { 
           $proc.Start() 
           $proc.WaitForExit() 
          } 
      ); 
"Program executed: Time is {0} seconds" -f $timeSpan.TotalSeconds; 
İlgili konular