Average Rating:

3.00 by 2 users.

43 Downloads, 155 Views

1 Comment(s)

Measure-Sleep

By: Bill Ehardt 22 Apr 2011 12:49 AM UTC in the category: Beginner Event 10

Description:

Function to verify how long Start-Sleep really sleeps. Will ask for parameters if not given and will round the output to the nearest tenth of a millisecond
Function Measure-Sleep {
<#
.SYNOPSIS
Function to test the accuracy of Start-Sleep
 
.DESCRIPTION
This function will test Start-Sleep a given number of times for a given number
of seconds and will report average time of those tests.
 
.PARAMETER passes
How many passes should be run

.PARAMETER sleepseconds
How long each pass should last
 
.NOTES
2011 Scripting Games Beginner event #10
by Bill Ehardt
4/21/2011
 
.EXAMPLE
Measure-Sleep -passes 5 -sleepseconds 2
 
Description
-----------
Will run Start-Sleep 5 times, each lasting for 2 seconds.

Output
------
Average time of 5 runs of 2 seconds is 2009.9 ms

#>
param
   (
   #if either parameter isn't given, ask for it.
   $passes=(read-host "How many passes?"),
   $sleepseconds=(read-host "How many seconds per pass?")
   )
 $output = @() #create array to get results
 for ($x=1;$x -le $passes;$x++) #for loop to run the given # of passes
  {
  $output += Measure-Command {Start-Sleep -Seconds $sleepseconds} | 
   select totalmilliseconds
  }
  $results = $output | Measure-Object totalmilliseconds -Average
  Write-Host "Average time of $($results.count) runs of $sleepseconds seconds is $([math]::Round($results.average,1)) ms"
 }
Top

Comments:

4/23/2011 11:02 PM
Good job. Pretty simple loop (Believe it or not you can rewrite the ($x=1 ....) statement to something like ($x in 1..$passes) .... Results are the same, just a different way of doing it. Play with INVOKE-EXPRESSION sometime which is one of the cooler Cmdlets...