Average Rating:

2.50 by 2 users.

47 Downloads, 151 Views

2 Comment(s)

B_Event_10.ps1

By: Eric Pankowski 22 Apr 2011 01:17 AM UTC in the category: Beginner Event 10

Description:

Run the Start-Sleep cmdlet a number of times for a set duration and then report the average amount of time it took to complete each run in milliseconds.
Beginner Event 10 - 2011
#ENP 4/21/2011
#Run the Start-Sleep cmdlet a number of times for a set duration and then 
#report the average amount of time it took to complete each time in milliseconds.

#As a function with parameters to accept a custom number of runs and a custom duration
#-RunSet will be how many iterations you want to run
#-RunLength will be how long you want each iteration to last
Function AverageElapsedTime
    {param([int]$RunSet="5", [int]$RunLength="5")
    $RunStep = 1
    while ($RunStep -lt $RunSet -Or $RunStep -eq $RunSet) 
        {Start-Sleep -s $RunLength; $RunStep++}}

#Execute the function and return the total number of milliseconds it took via Measure-Command.
#Calculate the average time each one took by taking the total number of milliseconds and dividing it by the number of runs
#Once the average has been calculated truncate it down to remove decimcal places and then diplay the results to the user.
$TotalTime = (Measure-Command {AverageElapsedTime}).TotalMilliseconds
$AvgTime = "{0:N0}" -f ($TotalTime/$RunSet)
Write-Host "The average time it took to execute $RunSet iterations with a duration of $RunLength seconds each is $AvgTime milliseconds."
Top

Comments:

4/22/2011 1:25 AM
Foiled by Copy/Paste when submitting this one. I didn't notice until after I hit Save that I somehow missed selecting the '#' at the very beginning of the script so the first line isn't commented out in the submission like it should be.
4/23/2011 10:59 PM
Ignoring the first line, the script works well. Another way you can tweak this is playing with INVOKE-EXPRESSION to REALLY make it more re-usable. Nice job.