Average Rating:

5.00 by 2 users.

47 Downloads, 261 Views

2 Comment(s)

Measure-Average.ps1

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

Description:

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

[CmdletBinding()]

Param(

	[parameter(Mandatory=$true,
	ValueFromPipeline=$true)]
    [string]$Command,
 
    
    [parameter(Mandatory=$false)]
    [ValidateSet('Milliseconds','Seconds','Minutes','Hours')]
    [String]$Units = 'Milliseconds',
    
    
    [parameter(Mandatory=$false)]
    [ValidateRange(2,100)]
    [Int]$Count = 5
    
    )



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




    process
    {
        
        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) 
       
        }
        	
    }



    end 
    {
        
        # 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'

    }


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

.DESCRIPTION
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.

.PARAMETER Command
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.

.PARAMETER Count
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.

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

.EXAMPLE
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.

.EXAMPLE
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.

.NOTES
PowerShell Version 2.0
Measure-Average.ps1
Modified on: 4/21/2011
Author: Daniel Cruz

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

.INPUTS
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.

.OUTPUTS
Object[]
    Measure-Command returns an object representing the command supplied, number of runs and average run time.
	
#>

}
Top

Comments:

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!