Average Rating:

4.00 by 2 users.

50 Downloads, 193 Views

1 Comment(s)

Test-AvgCmdTime.psm1

By: Matt_G 22 Apr 2011 02:17 AM UTC in the category: Beginner Event 10

Description:

Test the average time it takes to execute a command
# ---------------------------------------------------------------------------------------------
#
#	Script Name:		Test-AvgCmdTime
#	File Name:			Test-AvgCmdTime.psm1
#	Author(s):			Matt Gohmann
#						Powershell Novice
#	Date Created:		4/21/11
#	Date Last Updated:	4/21/11
#	Requirements:		PowerShell v2
#	Instructions:		Import the module, execute commands.
#	Notes:				2011 Scripting Games 
#						Beginner Event 10
#
# ---------------------------------------------------------------------------------------------

function Test-AvgCmdTime
{
<#
.SYNOPSIS
	Used to test the average time it takes to execute a command. 
.Description
	Used to test the average time it takes to execute a command, well script block to be exact.
.EXAMPLE
	Test-AvgCmdTime
	
	Description
	---------------------
	By default it tests the average time it takes to sleep 5 seconds for cycles.
.EXAMPLE
	Test-AvgCmdTime -ScriptBlock {test-connection localhost -count 3} -Cycles 20
	
	Description
	---------------------
	In this example the test-connection command is being tested for 20 cycles.
#>
[CmdletBinding()]
PARAM
	(
	[Parameter(Mandatory=$false, HelpMessage="Enter a scriptblock. Ex: {ping yahoo.com}", ValueFromPipeline=$true)][scriptblock]$ScriptBlock={Start-Sleep 5},
	[Parameter(Mandatory=$false, HelpMessage="How many times do you want to run the scriptblock?", ValueFromPipeline=$false)][INT]$Cycles=5
	)
PROCESS
	{
	$total=0
	for($i=0;$i -lt $Cycles;$i++)
		{
		Write-Progress -activity "Please wait while your test is run" -status "Progress: " -PercentComplete (($i / $Cycles)  * 100)
		$total+=((Measure-Command -Expression $ScriptBlock) | Select-Object TotalMilliseconds).TotalMilliseconds
		}
	$Result = $total/$Cycles
	Write-host "Average time of $Cycles cycles is $Result milliseconds."
	}
}
Top

Comments:

4/23/2011 10:25 PM
Nice job on creating it as a function with a default value for the Script Block. The Comment based help is good, maybe (For fun, nothing to do with the Scripting Games) Add in some more examples.