Average Rating:

3.50 by 2 users.

41 Downloads, 163 Views

1 Comment(s)

Beg_10_TS_measure-tests.ps1

By: Thomas 21 Apr 2011 08:32 PM UTC in the category: Beginner Event 10

Description:

Submission for Last Event
- Function with reusable parameters: Different Tests & Runs can be specified
- Looping with: 1..$x |
- Verbose Option
#requires -version 2
<#
  .SYNOPSIS
     SG2011 - Event 10 - Beginner

  .DESCRIPTION
    Beginner Event 10: Use PowerShell to Measure Time to Complete a 
      
   .EXAMPLE
    measure-tests
	SG-Example taking defaultvalues inParameters: Command= Sleep 5 Runs:5
	
	.EXAMPLE
    measure-tests 5 'get-childitem'
	Measuring 5 runs of Get-Childitem execution time.
		
	.EXAMPLE
	measure-tests 5 'get-childitem' -verbose
	Display Verbose Messagges
			
   .PARAMETER $runs
    Specifys how many times the will Command will be executed and measured 
               
   .PARAMETER $command
	The comand / function which will be executed
	
   .NOTES
    NAME:  measure-tests.ps1
    AUTHOR:  TS, luesec@googlemail.com 
    HISTORY:      21.04.2011  V1.0 Initial Script  
#>


function measure-tests
{
[CmdletBinding()]
param 	(
		[System.Int32]$runs = 5,
		[string]$command = 'Start-Sleep 5'
		)



#Init $TotalTime
[System.TimeSpan]$TotalTime = 0

1..($runs) | ForEach-Object {

Write-Host "Performing Test Nr: $_ (Command: $command)" 
$TimeMeasured = Measure-Command -Expression {(Invoke-Expression ($command)) }
Write-Verbose "Time One Test: $($TimeMeasured.TotalMilliseconds)" 
#"Time One Test: {0:ms}" -f $TimeMeasured
$TotalTime = $TotalTime + $TimeMeasured
}


Write-Host "`n Tests Finished!"
Write-Verbose "Total Time All Tests: $($TotalTime.TotalMilliseconds)"
"Average time of `"$runs`" runs of `"$command`" is $($TotalTime.TotalMilliseconds / $runs) milliseconds"

}#End: function measure-tests


## Thank you for the great SG2011! Had much Fun, learned a lot and apprecieate the huge Timeinvest!
Top

Comments:

4/21/2011 8:54 PM
Good script.. I like that you have wrapped the script in a function.

Good usage description as well.