Average Rating:

4.50 by 2 users.

42 Downloads, 189 Views

1 Comment(s)

SG2011BE10V1.ps1

By: Winfried Schoierer 21 Apr 2011 10:43 PM UTC in the category: Beginner Event 10

Description:

This function calls the Start-Sleep Powershell cmdlet X times for a defined amount of seconds and measures the average execution time.
#Requires -Version 2.0
# -----------------------------------------------------------------------------
function Measure-SleepExecution
{
        <#
        .Synopsis
            This function calls the Start-Sleep Powershell cmdlet
            X times for a defined amount of seconds and measures
            the average execution time.
            
        .Parameter Seconds
            The time in seconds given as a parameter to the 
            Start-Sleep Powershell cmdlet.
            
        .Parameter Repetitions    
            How often the Start-Sleep Powershell cmdlet is called
            in a row.
            
        .Example
            Measure-SleepExecution
            
        .Example
            Measure-SleepExecution -seconds 2
            
         .Example
            Measure-SleepExecution -seconds 2 -repetitions 2
        #>

param (
        [int32]$seconds = 5,
        [int32]$repetitions = 5
      
      ) #end param

Set-Strictmode -Version Latest

# Run the start-sleep cmdlet for "$seconds" as many times as specified in $repetitions and get the average execution time in $run
$run = (1..$repetitions | Foreach-Object {(Measure-Command {Start-Sleep -s $seconds})}|
        Measure-Object -property totalmilliseconds -average)

# Build the output string and return it 
$result = "Average time of " + ($run.count) + " runs of " + $seconds + " seconds is " + $run.average + " milliseconds"
return $result
          
}

# To start the function:
Measure-SleepExecution
Top

Comments:

4/22/2011 12:23 AM
nice script. I like your use of strictmode. Consider using write-progress to provide feedback while your script is running.