Average Rating:

5.00 by 2 users.

47 Downloads, 261 Views

2 Comment(s)


By: Daniel Cruz 22 Apr 2011 02:07 AM UTC in the category: Beginner Event 10


Measures the average amount of time it takes to run a specified command.
function Measure-Average {



    [String]$Units = 'Milliseconds',
    [Int]$Count = 5

        Set-StrictMode -Version Latest
        # Array for the run times
        $Runs = @()
        $r = 0

        for($i = 1; $i -lt ($Count + 1); $i++ )
            # Measures the command in milliseconds and rounds the value
            $Time = ("{0:N2}" -f ((Measure-Command {Invoke-Expression $Command}).('Total' + $Units)))
            # Adds the values to a new object which is added to the $Runs array	
        	$Runs += New-Object -TypeName PSObject -Property @{Run = $i; Runtime = $Time}
            Write-Verbose "Run $i : $Time"
            # Entertainment
            $r = ($r + 1)
        	Write-Progress -Activity "Measuring Run: $i" -Status "Progress:" -PercentComplete ($r/$Count*100) 

        # Measures the average time to complete the command
        $Average = $Runs | Measure-Object -Property Runtime -Average
        # Properties for the output object
        $Property = @{
            'Command' = $Command
            'Number of Runs'   = $Average.Count
            'Average Run Time' = ("{0:N2}" -f $Average.Average) + " $Units"
        # Creates, formats and exports the output object
        New-Object -TypeName PSObject -Property $Property | `
        Format-List -Property Command,'Number of Runs','Average Run Time'


Measures the average amount of time it takes to run a specified command.

Measure-Average uses the Invoke-Expression and Measure-Command cmdlets to measures the average time it takes 
to run a specified command.

The Count parameter can be used to specify the number of times to run the command. The output can be displayed 
in different time formats using the Units parameter. Use the Verbose switch to see the individual run times.

A string that specifies the command or expression to run. The command or expression can be piped into the 
function or entered as a variable that contains the command or expression. The Command parameter is required.

The Count parameter specifies the number of times the command will be run to calculate the average. The 
default value of 5 can be set to a value between 2 and 100.

The Units parameter specifies the unit of time used in the output. The default value Milliseconds can be set 
to Seconds, Minutes or Hours.

PS>. .\Measure-Average.ps1
PS>"Start-Sleep -Seconds 5" | Measure-Average

Command          : Start-Sleep -Seconds 5
Number of Runs   : 5
Average Run Time : 4,998.06 Milliseconds

This example shows the Measure-Average function being imported by dot-sourcing the .ps1 file. The function is 
called using the default parameters. The mandatory command is piped into the function.

PS>$Command = "Start-Sleep -Seconds 2"
PS>Measure-Average -Command $Command -Count 10 -Units Seconds -Verbose
VERBOSE: Run 1 : 1.99
VERBOSE: Run 2 : 1.99
VERBOSE: Run 3 : 2.01
VERBOSE: Run 4 : 1.98
VERBOSE: Run 5 : 1.99
VERBOSE: Run 6 : 1.98
VERBOSE: Run 7 : 1.98
VERBOSE: Run 8 : 2.00
VERBOSE: Run 9 : 1.98
VERBOSE: Run 10 : 1.99

Command          : Start-Sleep -Seconds 2
Number of Runs   : 10
Average Run Time : 1.99 Seconds

In this example the Count parameter is used to run the command 10 times. The output value is changed to seconds 
using the Units parameter and Verbose was specified to see the individual runs. The command was supplied using 
a the $Command variable.

PowerShell Version 2.0
Modified on: 4/21/2011
Author: Daniel Cruz

The 2011 Scripting Games Beginner Event 10: http://bit.ly/dKGDNO

System.String or PSObject
    You can pipe an object representing a command to Measure-Average or use the Command parameter with a string 
    or variable.

    Measure-Command returns an object representing the command supplied, number of runs and average run time.



4/23/2011 10:55 PM
*klunk* - Well defined Comment based help, the ability to re-use this script regardless of Cmdlet, Adjusting based upon different counts of tests.... SOMEBODY spent a lot of effort on this. :-=)

As we would say in Canada, "Beauty eh?"
4/24/2011 1:31 AM
:) <---PROUD SMILE! Wonderful comments, thank you so much. This script is going in my toolbox for sure. And yes, I worked on this baby for 3 nights in a row, well worth it!